Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parent and children for useId #237

Merged
merged 9 commits into from Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/constants.js
Expand Up @@ -7,6 +7,8 @@ export const SKIP_EFFECTS = '__s';

// VNode properties
export const COMPONENT = '__c';
export const CHILDREN = '__k';
export const PARENT = '__';

// Component properties
export const VNODE = '__v';
Expand Down
47 changes: 38 additions & 9 deletions src/index.js
Expand Up @@ -16,9 +16,11 @@ import {
DIFFED,
DIRTY,
NEXT_STATE,
PARENT,
RENDER,
SKIP_EFFECTS,
VNODE
VNODE,
CHILDREN
} from './constants';

/** @typedef {import('preact').VNode} VNode */
Expand Down Expand Up @@ -72,7 +74,9 @@ function renderToString(vnode, context, opts) {
) {
res = _renderToStringPretty(vnode, context, opts);
} else {
res = _renderToString(vnode, context, false, undefined);
res = _renderToString(vnode, context, false, undefined, {
[CHILDREN]: [vnode]
});
}

// options._commit, we don't schedule any effects in this library right now,
Expand Down Expand Up @@ -181,7 +185,7 @@ const isArray = Array.isArray;
const assign = Object.assign;

/** The default export is an alias of `render()`. */
function _renderToString(vnode, context, isSvgMode, selectValue) {
function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
// Ignore non-rendered VNodes/values
if (vnode == null || vnode === true || vnode === false || vnode === '') {
return '';
Expand All @@ -195,13 +199,17 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
// Recurse into children / Arrays
if (isArray(vnode)) {
let rendered = '';
parent[CHILDREN] = [];
for (let i = 0; i < vnode.length; i++) {
parent[CHILDREN].push(vnode[i]);
rendered =
rendered + _renderToString(vnode[i], context, isSvgMode, selectValue);
rendered +
_renderToString(vnode[i], context, isSvgMode, selectValue, parent);
}
return rendered;
}

vnode[PARENT] = parent;
if (options[DIFF]) options[DIFF](vnode);

let type = vnode.type,
Expand All @@ -215,7 +223,8 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
vnode.props.children,
context,
isSvgMode,
selectValue
selectValue,
vnode
);
}

Expand All @@ -232,7 +241,13 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
}

// Recurse into children before invoking the after-diff hook
const str = _renderToString(rendered, context, isSvgMode, selectValue);
const str = _renderToString(
rendered,
context,
isSvgMode,
selectValue,
vnode
);
if (options[DIFFED]) options[DIFFED](vnode);
return str;
}
Expand Down Expand Up @@ -314,13 +329,20 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
pieces = pieces + encodeEntities(children);
hasChildren = true;
} else if (isArray(children)) {
vnode[CHILDREN] = [];
for (let i = 0; i < children.length; i++) {
let child = children[i];

vnode[CHILDREN].push(child);
if (child != null && child !== false) {
let childSvgMode =
type === 'svg' || (type !== 'foreignObject' && isSvgMode);
let ret = _renderToString(child, context, childSvgMode, selectValue);
let ret = _renderToString(
child,
context,
childSvgMode,
selectValue,
vnode
);

// Skip if we received an empty string
if (ret) {
Expand All @@ -330,9 +352,16 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
}
}
} else if (children != null && children !== false && children !== true) {
vnode[CHILDREN] = [children];
let childSvgMode =
type === 'svg' || (type !== 'foreignObject' && isSvgMode);
let ret = _renderToString(children, context, childSvgMode, selectValue);
let ret = _renderToString(
children,
context,
childSvgMode,
selectValue,
vnode
);

// Skip if we received an empty string
if (ret) {
Expand Down