preact-render-to-string
Advanced tools
Comparing version 3.0.7 to 3.1.0
@@ -110,3 +110,3 @@ (function (global, factory) { | ||
var children = _ref.children; | ||
var isComponent = false; | ||
context = context || {}; | ||
@@ -127,2 +127,3 @@ opts = opts || {}; | ||
if (typeof nodeName === 'function') { | ||
isComponent = true; | ||
if (opts.shallow && (inner || opts.renderRootComponent === false)) { | ||
@@ -166,3 +167,3 @@ nodeName = getComponentName(nodeName); | ||
var hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts); | ||
var hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts, isComponent); | ||
if (hooked || hooked === '') { | ||
@@ -169,0 +170,0 @@ s += hooked; |
@@ -122,3 +122,3 @@ (function (global, factory) { | ||
var children = _ref.children; | ||
var isComponent = false; | ||
context = context || {}; | ||
@@ -139,2 +139,3 @@ opts = opts || {}; | ||
if (typeof nodeName === 'function') { | ||
isComponent = true; | ||
if (opts.shallow && (inner || opts.renderRootComponent === false)) { | ||
@@ -178,3 +179,3 @@ nodeName = getComponentName(nodeName); | ||
var hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts); | ||
var hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts, isComponent); | ||
if (hooked || hooked === '') { | ||
@@ -636,12 +637,18 @@ s += hooked; | ||
function attributeHook(name, value, context, opts) { | ||
if (value == null) return ''; | ||
function attributeHook(name, value, context, opts, isComponent) { | ||
var type = typeof value; | ||
if (value == null || !isComponent && opts.skipFalseAttributes && value === false || type === 'function' && !opts.functions) return ''; | ||
var indentChar = typeof opts.pretty === 'string' ? opts.pretty : '\t'; | ||
if (typeof value !== 'string') { | ||
preactPlugin.context = context; | ||
preactPlugin.opts = opts; | ||
value = prettyFormat(value, prettyFormatOpts); | ||
if (~value.indexOf('\n')) { | ||
value = indent('\n' + value, indentChar) + '\n'; | ||
if (type !== 'string') { | ||
if (type === 'function' && !opts.functionNames) { | ||
value = 'Function'; | ||
} else { | ||
preactPlugin.context = context; | ||
preactPlugin.opts = opts; | ||
value = prettyFormat(value, prettyFormatOpts); | ||
if (~value.indexOf('\n')) { | ||
value = indent('\n' + value, indentChar) + '\n'; | ||
} | ||
} | ||
@@ -657,2 +664,5 @@ return indent('\n' + name + '={' + value + '}', indentChar); | ||
xml: false, | ||
functions: true, | ||
functionNames: true, | ||
skipFalseAttributes: true, | ||
pretty: ' ' | ||
@@ -659,0 +669,0 @@ }; |
{ | ||
"name": "preact-render-to-string", | ||
"amdName": "preactRenderToString", | ||
"version": "3.0.7", | ||
"version": "3.1.0", | ||
"description": "Render JSX to an HTML string, with support for Preact components.", | ||
@@ -6,0 +6,0 @@ "main": "dist/index.js", |
@@ -53,3 +53,4 @@ import { objectKeys, encodeEntities, falsey, memoize, indent, isLargeString, styleObjToCss, assign, getNodeProps } from './util'; | ||
export default function renderToString(vnode, context, opts, inner) { | ||
let { nodeName, attributes, children } = vnode || EMPTY; | ||
let { nodeName, attributes, children } = vnode || EMPTY, | ||
isComponent = false; | ||
context = context || {}; | ||
@@ -72,2 +73,3 @@ opts = opts || {}; | ||
if (typeof nodeName==='function') { | ||
isComponent = true; | ||
if (opts.shallow && (inner || opts.renderRootComponent===false)) { | ||
@@ -116,3 +118,3 @@ nodeName = getComponentName(nodeName); | ||
let hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts); | ||
let hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts, isComponent); | ||
if (hooked || hooked==='') { | ||
@@ -119,0 +121,0 @@ s += hooked; |
@@ -23,13 +23,21 @@ import './polyfills'; | ||
function attributeHook(name, value, context, opts) { | ||
if (value==null) return ''; | ||
function attributeHook(name, value, context, opts, isComponent) { | ||
let type = typeof value; | ||
// always skip null & undefined values, skip false DOM attributes, skip functions if told to | ||
if (value==null || (!isComponent && opts.skipFalseAttributes && value===false) || (type==='function' && !opts.functions)) return ''; | ||
let indentChar = typeof opts.pretty==='string' ? opts.pretty : '\t'; | ||
if (typeof value!=='string') { | ||
preactPlugin.context = context; | ||
preactPlugin.opts = opts; | ||
value = prettyFormat(value, prettyFormatOpts); | ||
if (~value.indexOf('\n')) { | ||
value = `${indent('\n'+value, indentChar)}\n`; | ||
if (type!=='string') { | ||
if (type==='function' && !opts.functionNames) { | ||
value = 'Function'; | ||
} | ||
else { | ||
preactPlugin.context = context; | ||
preactPlugin.opts = opts; | ||
value = prettyFormat(value, prettyFormatOpts); | ||
if (~value.indexOf('\n')) { | ||
value = `${indent('\n'+value, indentChar)}\n`; | ||
} | ||
} | ||
return indent(`\n${name}={${value}}`, indentChar); | ||
@@ -45,2 +53,5 @@ } | ||
xml: false, | ||
functions: true, | ||
functionNames: true, | ||
skipFalseAttributes: true, | ||
pretty: ' ' | ||
@@ -47,0 +58,0 @@ }; |
@@ -14,3 +14,3 @@ import render from '../src/jsx'; | ||
describe('jsx', () => { | ||
let renderJsx = jsx => render(jsx).replace(/ {2}/g, '\t'); | ||
let renderJsx = (jsx, opts) => render(jsx, null, opts).replace(/ {2}/g, '\t'); | ||
@@ -51,4 +51,12 @@ it('should render as JSX', () => { | ||
)).to.equal(dedent` | ||
<a b={false}>bar</a> | ||
<a>bar</a> | ||
`); | ||
function F(){} | ||
expect(renderJsx( | ||
<F b={false}>bar</F>, | ||
{ shallow:true, renderRootComponent:false } | ||
)).to.equal(dedent` | ||
<F b={false}>bar</F> | ||
`); | ||
}); | ||
@@ -89,3 +97,3 @@ | ||
)).to.equal(dedent` | ||
<a b={<c />}>bar</a> | ||
<a b={<c></c>}>bar</a> | ||
`); | ||
@@ -102,4 +110,4 @@ | ||
Array [ | ||
<c />, | ||
<d f="g" /> | ||
<c></c>, | ||
<d f="g"></d> | ||
] | ||
@@ -123,4 +131,4 @@ } | ||
<div> | ||
<a /> | ||
<b /> | ||
<a></a> | ||
<b></b> | ||
<c> | ||
@@ -132,2 +140,21 @@ <!----> | ||
}); | ||
it('should skip functions if functions=false', () => { | ||
expect(renderJsx( | ||
<div onClick={() => {}} />, | ||
{ functions:false } | ||
)).to.equal('<div></div>'); | ||
}); | ||
it('should skip function names if functionNames=false', () => { | ||
expect(renderJsx( | ||
<div onClick={() => {}} />, | ||
{ functionNames:false } | ||
)).to.equal('<div onClick={Function}></div>'); | ||
expect(renderJsx( | ||
<div onClick={function foo(){}} />, | ||
{ functionNames:false } | ||
)).to.equal('<div onClick={Function}></div>'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
120415
1683