preact-render-to-string
Advanced tools
Comparing version 2.4.0 to 2.5.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 VOID_ELEMENTS=['area','base','br','col','embed','hr','img','input','link','meta','param','source','track','wbr'];var UNNAMED=[];var objectKeys=Object.keys || function(obj){var keys=[];for(var i in obj) {if(obj.hasOwnProperty(i))keys.push(i);}return keys;};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 || opts && opts.renderRootComponent === false)){nodeName = getComponentName(nodeName);}else {var props=_extends({children:children},attributes),rendered=undefined;if(!nodeName.prototype || 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;if(attributes){var attrs=objectKeys(attributes);if(opts && opts.sortAttributes === true)attrs.sort();for(var i=0;i < attrs.length;i++) {var _name=attrs[i],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) + ' />';}}if(VOID_ELEMENTS.indexOf(nodeName) === -1){s += '</' + nodeName + '>';}return s;}}function getComponentName(component){var proto=component.prototype,ctor=proto && proto.constructor;return component.displayName || component.name || proto && (proto.displayName || proto.name) || getFallbackComponentName(component);}function getFallbackComponentName(component){var str=Function.prototype.toString.call(component),name=(str.match(/^\s*function\s+([^\( ]+)/) || EMPTY)[1];if(!name){var index=-1;for(var i=UNNAMED.length;i--;) {if(UNNAMED[i] === component){index = i;break;}}if(index < 0){index = UNNAMED.push(component) - 1;}name = 'UnnamedComponent' + index;}return name;}}); | ||
(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 VOID_ELEMENTS=['area','base','br','col','embed','hr','img','input','link','meta','param','source','track','wbr'];var UNNAMED=[];var objectKeys=Object.keys || function(obj){var keys=[];for(var i in obj) {if(obj.hasOwnProperty(i))keys.push(i);}return keys;};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;};var indent=function indent(s,char){return String(s).replace(/(\n+)/g,'$1' + (char || '\t'));};var isLargeString=function isLargeString(s){return String(s).length > 40 || String(s).indexOf('\n') !== -1 || String(s).indexOf('<') !== -1;};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 || {};opts = opts || {};var pretty=opts.pretty,indentChar=typeof pretty === 'string'?pretty:'\t';if(!nodeName){return encodeEntities(vnode);}if(typeof nodeName === 'function'){if(opts.shallow && (inner || opts.renderRootComponent === false)){nodeName = getComponentName(nodeName);}else {var props=_extends({children:children},attributes),rendered=undefined;if(!nodeName.prototype || 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.shallowHighOrder !== false;_again = true;_ref = nodeName = attributes = children = pretty = indentChar = props = rendered = c = undefined;continue _function;}}var s='<' + nodeName,html=undefined;if(attributes){var attrs=objectKeys(attributes);if(opts && opts.sortAttributes === true)attrs.sort();for(var i=0;i < attrs.length;i++) {var _name=attrs[i],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){if(pretty && isLargeString(html)){html = '\n' + indentChar + indent(html,indentChar);}s += html;}else {var len=children && children.length;if(len){var pieces=[],hasLarge=false;for(var i=0;i < len;i++) {var child=children[i];if(!falsey(child)){var ret=renderToString(child,context,opts,true);if(!hasLarge && pretty && isLargeString(ret))hasLarge = true;pieces.push(ret);}}if(hasLarge){for(var i=pieces.length;i--;) {pieces[i] = '\n' + indentChar + indent(pieces[i],indentChar);}}s += pieces.join('');}else if(opts && opts.xml){return s.substring(0,s.length - 1) + ' />';}}if(VOID_ELEMENTS.indexOf(nodeName) === -1){if(pretty && ~s.indexOf('\n'))s += '\n';s += '</' + nodeName + '>';}return s;}}function getComponentName(component){var proto=component.prototype,ctor=proto && proto.constructor;return component.displayName || component.name || proto && (proto.displayName || proto.name) || getFallbackComponentName(component);}function getFallbackComponentName(component){var str=Function.prototype.toString.call(component),name=(str.match(/^\s*function\s+([^\( ]+)/) || EMPTY)[1];if(!name){var index=-1;for(var i=UNNAMED.length;i--;) {if(UNNAMED[i] === component){index = i;break;}}if(index < 0){index = UNNAMED.push(component) - 1;}name = 'UnnamedComponent' + index;}return name;}}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "preact-render-to-string", | ||
"amdName": "preactRenderToString", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "Render JSX to an HTML string, with support for Preact components.", | ||
@@ -6,0 +6,0 @@ "main": "dist/index.js", |
@@ -45,3 +45,6 @@ | ||
let indent = (s, char) => String(s).replace(/(\n+)/g, '$1' + (char || '\t')); | ||
let isLargeString = s => (String(s).length>40 || String(s).indexOf('\n')!==-1 || String(s).indexOf('<')!==-1); | ||
/** Render Preact JSX + Components to an HTML string. | ||
@@ -55,2 +58,3 @@ * @name render | ||
* @param {Boolean} [options.xml=false] If `true`, uses self-closing tags for elements without children. | ||
* @param {Boolean} [options.pretty=false] If `true`, adds whitespace for readability | ||
*/ | ||
@@ -82,3 +86,7 @@ renderToString.render = renderToString; | ||
context = context || {}; | ||
opts = opts || {}; | ||
let pretty = opts.pretty, | ||
indentChar = typeof pretty==='string' ? pretty : '\t'; | ||
// #text nodes | ||
@@ -91,3 +99,3 @@ if (!nodeName) { | ||
if (typeof nodeName==='function') { | ||
if (opts && opts.shallow && (inner || (opts && opts.renderRootComponent===false))) { | ||
if (opts.shallow && (inner || opts.renderRootComponent===false)) { | ||
nodeName = getComponentName(nodeName); | ||
@@ -115,3 +123,3 @@ } | ||
return renderToString(rendered, context, opts, !opts || opts.shallowHighOrder!==false); | ||
return renderToString(rendered, context, opts, opts.shallowHighOrder!==false); | ||
} | ||
@@ -148,3 +156,9 @@ } | ||
// if (pretty) s += '\n' + indentChar; | ||
if (html) { | ||
// if multiline, indent. | ||
if (pretty && isLargeString(html)) { | ||
html = '\n' + indentChar + indent(html, indentChar); | ||
} | ||
s += html; | ||
@@ -155,8 +169,23 @@ } | ||
if (len) { | ||
let pieces = [], | ||
hasLarge = false; | ||
for (let i=0; i<len; i++) { | ||
let child = children[i]; | ||
if (!falsey(child)) { | ||
s += renderToString(child, context, opts, true); | ||
let ret = renderToString(child, context, opts, true); | ||
// if (pretty && isLargeString(ret)) { | ||
// if (pretty && isLargeString(ret)) { | ||
// ret = '\n' + indentChar + indent(ret); | ||
// } | ||
//s += ret; | ||
if (!hasLarge && pretty && isLargeString(ret)) hasLarge = true; | ||
pieces.push(ret); | ||
} | ||
} | ||
if (hasLarge) { | ||
for (let i=pieces.length; i--; ) { | ||
pieces[i] = '\n' + indentChar + indent(pieces[i], indentChar); | ||
} | ||
} | ||
s += pieces.join(''); | ||
} | ||
@@ -169,2 +198,3 @@ else if (opts && opts.xml) { | ||
if (VOID_ELEMENTS.indexOf(nodeName) === -1) { | ||
if (pretty && ~s.indexOf('\n')) s += '\n'; | ||
s += `</${nodeName}>`; | ||
@@ -171,0 +201,0 @@ } |
Sorry, the diff of this file is not supported yet
37653
14
515