preact-render-to-string
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -1,2 +0,2 @@ | ||
(function(global,factory){if(typeof define === 'function' && define.amd){define('preactRenderToString',['exports','module'],factory);}else if(typeof exports !== 'undefined' && typeof module !== 'undefined'){factory(exports,module);}else {var mod={exports:{}};factory(mod.exports,mod);global.preactRenderToString = mod.exports;}})(this,function(exports,module){'use strict';var _extends=Object.assign || function(target){for(var i=1;i < arguments.length;i++) {var source=arguments[i];for(var key in source) {if(Object.prototype.hasOwnProperty.call(source,key)){target[key] = source[key];}}}return target;};module.exports = renderToString;var SHALLOW={shallow:true};var ESC={'<':'<','>':'>','"':'"','&':'&'};var EMPTY={};var HOP=Object.prototype.hasOwnProperty;var encodeEntities=function encodeEntities(s){return String(s).replace(/[<>"&]/g,escapeChar);};var escapeChar=function escapeChar(a){return ESC[a] || a;};renderToString.render = renderToString;renderToString.shallowRender = function(vnode,context){return renderToString(vnode,context,SHALLOW);};function renderToString(_x,_x2,_x3,_x4){var _again=true;_function: while(_again) {var vnode=_x,context=_x2,opts=_x3,inner=_x4;_ref = nodeName = attributes = children = props = rendered = c = s = html = _name = v = len = i = undefined;_again = false;var _ref=vnode || EMPTY;var nodeName=_ref.nodeName;var attributes=_ref.attributes;var children=_ref.children;context = context || {};if(!nodeName){return encodeEntities(vnode);}if(typeof nodeName === 'function'){if(opts && opts.shallow && inner){nodeName = getComponentName(nodeName);}else {var props=_extends({children:children},attributes),rendered=undefined;if(typeof nodeName.prototype.render !== 'function'){rendered = nodeName(props,context);}else {var c=new nodeName(props,context);c.props = props;c.context = context;rendered = c.render(c.props,c.state,c.context);if(c.getChildContext){context = c.getChildContext();}}_x = rendered;_x2 = context;_x3 = opts;_x4 = !opts || opts.shallowHighOrder !== false;_again = true;continue _function;}}var s='<' + nodeName,html=undefined;for(var _name in attributes) {if(HOP.call(attributes,_name)){var v=attributes[_name];if(_name === 'className'){if(attributes['class'])continue;_name = 'class';}if(_name === 'dangerouslySetInnerHTML'){html = v && v.__html;}else if(v !== null && v !== undefined && typeof v !== 'function'){s += ' ' + _name + '="' + encodeEntities(v) + '"';}}}s += '>';if(html){s += html;}else {var len=children && children.length;if(len){for(var i=0;i < len;i++) {s += renderToString(children[i],context,opts,true);}}else if(opts && opts.xml){return s.substring(0,s.length - 1) + ' />';}}s += '</' + nodeName + '>';return s;}};function getComponentName(component){return component.displayName || component.name || component.prototype.displayName || component.prototype.name || (Function.prototype.toString.call(component).match(/\s([^\(]+)/) || EMPTY)[1] || 'Component';}}); | ||
(function(global,factory){if(typeof define === 'function' && define.amd){define('preactRenderToString',['exports','module'],factory);}else if(typeof exports !== 'undefined' && typeof module !== 'undefined'){factory(exports,module);}else {var mod={exports:{}};factory(mod.exports,mod);global.preactRenderToString = mod.exports;}})(this,function(exports,module){'use strict';var _extends=Object.assign || function(target){for(var i=1;i < arguments.length;i++) {var source=arguments[i];for(var key in source) {if(Object.prototype.hasOwnProperty.call(source,key)){target[key] = source[key];}}}return target;};module.exports = renderToString;var SHALLOW={shallow:true};var ESC={'<':'<','>':'>','"':'"','&':'&'};var EMPTY={};var HOP=Object.prototype.hasOwnProperty;var encodeEntities=function encodeEntities(s){return String(s).replace(/[<>"&]/g,escapeChar);};var escapeChar=function escapeChar(a){return ESC[a] || a;};var falsey=function falsey(v){return v == null || v === false;};renderToString.render = renderToString;renderToString.shallowRender = function(vnode,context){return renderToString(vnode,context,SHALLOW);};function renderToString(_x,_x2,_x3,_x4){var _again=true;_function: while(_again) {var vnode=_x,context=_x2,opts=_x3,inner=_x4;_again = false;var _ref=vnode || EMPTY;var nodeName=_ref.nodeName;var attributes=_ref.attributes;var children=_ref.children;context = context || {};if(!nodeName){return encodeEntities(vnode);}if(typeof nodeName === 'function'){if(opts && opts.shallow && inner){nodeName = getComponentName(nodeName);}else {var props=_extends({children:children},attributes),rendered=undefined;if(typeof nodeName.prototype.render !== 'function'){rendered = nodeName(props,context);}else {var c=new nodeName(props,context);c.props = props;c.context = context;rendered = c.render(c.props,c.state,c.context);if(c.getChildContext){context = c.getChildContext();}}_x = rendered;_x2 = context;_x3 = opts;_x4 = !opts || opts.shallowHighOrder !== false;_again = true;_ref = nodeName = attributes = children = props = rendered = c = undefined;continue _function;}}var s='<' + nodeName,html=undefined;for(var _name in attributes) {if(HOP.call(attributes,_name)){var v=attributes[_name];if(_name === 'className'){if(attributes['class'])continue;_name = 'class';}if(_name === 'dangerouslySetInnerHTML'){html = v && v.__html;}else if(!falsey(v) && typeof v !== 'function'){s += ' ' + _name + '="' + encodeEntities(v) + '"';}}}s += '>';if(html){s += html;}else {var len=children && children.length;if(len){for(var i=0;i < len;i++) {var child=children[i];if(!falsey(child)){s += renderToString(child,context,opts,true);}}}else if(opts && opts.xml){return s.substring(0,s.length - 1) + ' />';}}s += '</' + nodeName + '>';return s;}};function getComponentName(component){return component.displayName || component.name || component.prototype.displayName || component.prototype.name || (Function.prototype.toString.call(component).match(/\s([^\(]+)/) || EMPTY)[1] || 'Component';}}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "preact-render-to-string", | ||
"amdName": "preactRenderToString", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Render JSX to an HTML string, with support for Preact components.", | ||
@@ -38,3 +38,3 @@ "main": "dist/index.js", | ||
"mocha": "^2.3.3", | ||
"preact": "^3.3.0", | ||
"preact": "^4.1.3", | ||
"sinon": "^1.17.1", | ||
@@ -41,0 +41,0 @@ "sinon-chai": "^2.8.0" |
@@ -19,3 +19,5 @@ | ||
let falsey = v => v==null || v===false; | ||
/** Render Preact JSX + Components to an HTML string. | ||
@@ -104,3 +106,3 @@ * @name render | ||
} | ||
else if (v!==null && v!==undefined && typeof v!=='function') { | ||
else if (!falsey(v) && typeof v!=='function') { | ||
s += ` ${name}="${encodeEntities(v)}"`; | ||
@@ -119,3 +121,6 @@ } | ||
for (let i=0; i<len; i++) { | ||
s += renderToString(children[i], context, opts, true); | ||
let child = children[i]; | ||
if (!falsey(child)) { | ||
s += renderToString(child, context, opts, true); | ||
} | ||
} | ||
@@ -122,0 +127,0 @@ } |
@@ -17,4 +17,4 @@ import { render, shallowRender } from '../src'; | ||
it('should omit null and undefined attributes', () => { | ||
let rendered = render(<div a={null} b={undefined} />), | ||
it('should omit falsey attributes', () => { | ||
let rendered = render(<div a={null} b={undefined} c={false} />), | ||
expected = `<div></div>`; | ||
@@ -38,2 +38,9 @@ | ||
}); | ||
it('should omit falsey children', () => { | ||
let rendered = render(<div>{null}|{undefined}|{false}</div>), | ||
expected = `<div>||</div>`; | ||
expect(rendered).to.equal(expected); | ||
}); | ||
}); | ||
@@ -40,0 +47,0 @@ |
Sorry, the diff of this file is not supported yet
26540
355