From 61c5c545bdbb336ab40eacf81d4d64fe2720e739 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Tue, 5 Jul 2022 15:39:34 +0200 Subject: [PATCH 1/3] set parent and children --- package-lock.json | 4 ++-- src/index.js | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 96391807..bb00cba3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "preact-render-to-string", - "version": "5.1.19", + "version": "5.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "preact-render-to-string", - "version": "5.1.19", + "version": "5.2.0", "license": "MIT", "dependencies": { "pretty-format": "^3.8.0" diff --git a/src/index.js b/src/index.js index 6c2b24aa..f991a3b8 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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, + parent + ); // options._commit, we don't schedule any effects in this library right now, // so we can pass an empty queue to this hook. @@ -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 ''; } @@ -85,6 +104,7 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) { if (Array.isArray(vnode)) { let rendered = ''; for (let i = 0; i < vnode.length; i++) { + vnode[i]._parent = parent; if (pretty && i > 0) rendered += '\n'; rendered += _renderToString( vnode[i], @@ -92,7 +112,8 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) { opts, inner, isSvgMode, - selectValue + selectValue, + parent ); } return rendered; @@ -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; @@ -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 ); } } @@ -355,6 +381,7 @@ 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; @@ -362,6 +389,8 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) { let child = children[i]; if (child != null && child !== false) { + child[MANGLED_PARENT] = parent; + let childSvgMode = nodeName === 'svg' ? true @@ -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; From b60c96ef98424c61f0ab91d2eb35f1288eb97e4f Mon Sep 17 00:00:00 2001 From: jdecroock Date: Tue, 5 Jul 2022 18:05:47 +0200 Subject: [PATCH 2/3] guard --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index f991a3b8..2c9fff65 100644 --- a/src/index.js +++ b/src/index.js @@ -104,7 +104,7 @@ function _renderToString( if (Array.isArray(vnode)) { let rendered = ''; for (let i = 0; i < vnode.length; i++) { - vnode[i]._parent = parent; + if (typeof vnode === 'object') vnode[i][MANGLED_PARENT] = parent; if (pretty && i > 0) rendered += '\n'; rendered += _renderToString( vnode[i], @@ -389,7 +389,7 @@ function _renderToString( let child = children[i]; if (child != null && child !== false) { - child[MANGLED_PARENT] = parent; + if (typeof child === 'object') child[MANGLED_PARENT] = parent; let childSvgMode = nodeName === 'svg' From 031c422e676150728a7181bcca3b02b180e1ef14 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Tue, 5 Jul 2022 18:07:18 +0200 Subject: [PATCH 3/3] fix --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 2c9fff65..6e92ca30 100644 --- a/src/index.js +++ b/src/index.js @@ -68,7 +68,7 @@ function renderToString(vnode, context, opts) { undefined, undefined, undefined, - parent + vnode ); // options._commit, we don't schedule any effects in this library right now, @@ -104,7 +104,7 @@ function _renderToString( if (Array.isArray(vnode)) { let rendered = ''; for (let i = 0; i < vnode.length; i++) { - if (typeof vnode === 'object') vnode[i][MANGLED_PARENT] = parent; + if (typeof vnode[i] === 'object') vnode[i][MANGLED_PARENT] = parent; if (pretty && i > 0) rendered += '\n'; rendered += _renderToString( vnode[i],