Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Nov 20, 2023
1 parent b8bc059 commit fc44ec2
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions packages/integrations/react/vnode-children.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
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());
let id = ids++;
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)
const childVnodes = Array.isArray(node.children) && node.children.length
? 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 { class: className, ...props } = node.attributes;
return createElement(node.name, { ...props, className, key: `${id}-${key++}` }, childVnodes);
} else if (node.type === TEXT_NODE) {
// 0-length text gets omitted in JSX
return node.value.trim() ? node.value : undefined;
}
}

Expand Down

0 comments on commit fc44ec2

Please sign in to comment.