preact-render-to-string
Advanced tools
Comparing version 2.2.0 to 2.3.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 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;_ref = nodeName = attributes = children = props = rendered = c = s = html = _name = v = len = i = child = 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(!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){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 VOID_ELEMENTS=['area','base','br','col','embed','hr','img','input','link','meta','param','source','track','wbr'];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){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;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){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.2.0", | ||
"version": "2.3.0", | ||
"description": "Render JSX to an HTML string, with support for Preact components.", | ||
@@ -6,0 +6,0 @@ "main": "dist/index.js", |
@@ -30,3 +30,7 @@ | ||
const HOP = Object.prototype.hasOwnProperty; | ||
const objectKeys = Object.keys || (obj => { | ||
let keys = []; | ||
for (let i in obj) if (obj.hasOwnProperty(i)) keys.push(i); | ||
return keys; | ||
}); | ||
@@ -113,5 +117,11 @@ let encodeEntities = s => String(s).replace(/[<>"&]/g, escapeChar); | ||
for (let name in attributes) { | ||
if (HOP.call(attributes, name)) { | ||
let v = attributes[name]; | ||
if (attributes) { | ||
let attrs = objectKeys(attributes); | ||
// allow sorting lexicographically for more determinism (useful for tests, such as via preact-jsx-chai) | ||
if (opts && opts.sortAttributes===true) attrs.sort(); | ||
for (let i=0; i<attrs.length; i++) { | ||
let name = attrs[i], | ||
v = attributes[name]; | ||
if (name==='className') { | ||
@@ -129,2 +139,3 @@ if (attributes['class']) continue; | ||
} | ||
s += '>'; | ||
@@ -155,3 +166,3 @@ | ||
return s; | ||
}; | ||
} | ||
@@ -158,0 +169,0 @@ function getComponentName(component) { |
@@ -245,2 +245,14 @@ import { render, shallowRender } from '../src'; | ||
}); | ||
describe('sortAttributes', () => { | ||
it('should leave attributes unsorted by default', () => { | ||
let rendered = render(<div b1="b1" c="c" a="a" b="b" />); | ||
expect(rendered).to.equal('<div b1="b1" c="c" a="a" b="b"></div>'); | ||
}); | ||
it('should sort attributes lexicographically if enabled', () => { | ||
let rendered = render(<div b1="b1" c="c" a="a" b="b" />, null, { sortAttributes:true }); | ||
expect(rendered).to.equal('<div a="a" b="b" b1="b1" c="c"></div>'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
29405
402