From 40022bdf63c3eeb39151288b0f078ebe627a3dd3 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Mon, 20 Nov 2023 17:07:45 +0000 Subject: [PATCH] refactor: simplify --- packages/integrations/react/vnode-children.js | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/packages/integrations/react/vnode-children.js b/packages/integrations/react/vnode-children.js index 0b5738c2cce64..40e031cc336bd 100644 --- a/packages/integrations/react/vnode-children.js +++ b/packages/integrations/react/vnode-children.js @@ -1,9 +1,6 @@ import { parse, DOCUMENT_NODE, ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'; import { createElement, Fragment } from 'react'; -// https://github.com/facebook/react/blob/bbb9cb116dbf7b6247721aa0c4bcb6ec249aa8af/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js#L3593-L3617 -const selfClosingTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'keygen', 'meta', 'param', 'source', 'track', 'wbr'] - let ids = 0; export default function convert(children) { let doc = parse(children.toString().trim()); @@ -11,28 +8,21 @@ export default function convert(children) { let key = 0; function createReactElementFromNode(node) { - if (node.isSelfClosingTag === true && Array.isArray(node.children) && node.children.length === 0) { - node.children = undefined; - } - const childVnodes = Array.isArray(node.children) ? node.children - .map((child) => { - if (child.type === ELEMENT_NODE) { - return createReactElementFromNode(child); - } else if (child.type === TEXT_NODE) { - // 0-length text gets omitted in JSX - return child.value.trim() ? child.value : undefined; - } - }) - .filter((n) => !!n) + .map((child) => createReactElementFromNode(child)) + .filter(Boolean) : undefined; if (node.type === DOCUMENT_NODE) { return createElement(Fragment, {}, childVnodes); } else if (node.type === ELEMENT_NODE) { + const children = Array.isArray(node.children) && node.children.length === 0 ? undefined : childVnodes; const { class: className, ...props } = node.attributes; - return createElement(node.name, { ...props, className, key: `${id}-${key++}` }, childVnodes); + return createElement(node.name, { ...props, className, key: `${id}-${key++}` }, children); + } else if (child.type === TEXT_NODE) { + // 0-length text gets omitted in JSX + return child.value.trim() ? child.value : undefined; } }