Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 36 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ function markAsDirty() {
this.__d = true;
}

const MANGLED_PARENT = '__';
const MANGLED_CHILDREN = '__k';

/** Render Preact JSX + Components to an HTML string.
* @name render
* @function
Expand Down Expand Up @@ -58,7 +61,15 @@ function renderToString(vnode, context, opts) {
const previousSkipEffects = options.__s;
options.__s = true;

const res = _renderToString(vnode, context, opts);
const res = _renderToString(
vnode,
context,
opts,
undefined,
undefined,
undefined,
vnode
);

// options._commit, we don't schedule any effects in this library right now,
// so we can pass an empty queue to this hook.
Expand All @@ -69,7 +80,15 @@ function renderToString(vnode, context, opts) {
}

/** The default export is an alias of `render()`. */
function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
function _renderToString(
vnode,
context,
opts,
inner,
isSvgMode,
selectValue,
parent
) {
if (vnode == null || typeof vnode === 'boolean') {
return '';
}
Expand All @@ -85,14 +104,16 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
if (Array.isArray(vnode)) {
let rendered = '';
for (let i = 0; i < vnode.length; i++) {
if (typeof vnode[i] === 'object') vnode[i][MANGLED_PARENT] = parent;
if (pretty && i > 0) rendered += '\n';
rendered += _renderToString(
vnode[i],
context,
opts,
inner,
isSvgMode,
selectValue
selectValue,
parent
);
}
return rendered;
Expand All @@ -110,13 +131,15 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
} else if (nodeName === Fragment) {
const children = [];
getChildren(children, vnode.props.children);
parent[MANGLED_CHILDREN] = children;
return _renderToString(
children,
context,
opts,
opts.shallowHighOrder !== false,
isSvgMode,
selectValue
selectValue,
parent
);
} else {
let rendered;
Expand Down Expand Up @@ -220,13 +243,16 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
}

if (options.diffed) options.diffed(vnode);

vnode[MANGLED_CHILDREN] = rendered;
return _renderToString(
rendered,
context,
opts,
opts.shallowHighOrder !== false,
isSvgMode,
selectValue
selectValue,
vnode
);
}
}
Expand Down Expand Up @@ -355,13 +381,16 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
propChildren != null &&
getChildren((children = []), propChildren).length
) {
vnode[MANGLED_CHILDREN] = children;
let hasLarge = pretty && ~s.indexOf('\n');
let lastWasText = false;

for (let i = 0; i < children.length; i++) {
let child = children[i];

if (child != null && child !== false) {
if (typeof child === 'object') child[MANGLED_PARENT] = parent;

let childSvgMode =
nodeName === 'svg'
? true
Expand All @@ -374,7 +403,8 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
opts,
true,
childSvgMode,
selectValue
selectValue,
vnode
);

if (pretty && !hasLarge && isLargeString(ret)) hasLarge = true;
Expand Down