Skip to content

Commit

Permalink
Simplify convert funtions
Browse files Browse the repository at this point in the history
  • Loading branch information
StandardGage committed Feb 1, 2024
1 parent 41aa1f1 commit 1bf0315
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
3 changes: 1 addition & 2 deletions packages/integrations/react/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ function convert(children) {
}, {});
const className = innerProps.class;
delete innerProps.class;
const isVoidElement = ['img', 'input', 'br', 'hr', 'meta', 'area', 'base', 'col', 'command', 'embed', 'keygen', 'link', 'param', 'source', 'track', 'wbr'].includes(node.nodeName.toLowerCase());
const elementProps = isVoidElement ? { ...innerProps, className, key:`${id}-${key++}` } : { ...innerProps, className, children: childVnodes, key:`${id}-${key++}` };
const elementProps = { ...innerProps, className, children: childVnodes, key:`${id}-${key++}` };
return createElement(node.nodeName.toLowerCase(), elementProps);
} else if (node.nodeType === Node.TEXT_NODE) {
// 0-length text gets omitted in JSX
Expand Down
13 changes: 7 additions & 6 deletions packages/integrations/react/test/parsed-react-children.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import convert from '../vnode-children.js';

describe('experimental react children', () => {
it('has no children property for direct children', () => {
const [imgVNode] = convert('<img src="abc" alt="test"></img><img src="def"></img><img src="ghi"></img>');
expect(imgVNode.props).to.not.have.property('children');
it('has undefined as children for direct children', () => {
const [imgVNode] = convert('<img src="abc"></img>');
assert.deepStrictEqual(imgVNode.props.children, undefined);
});

it('does not use dangerouslySetInnerHTML', () => {
const [imgVNode] = convert('<img></img><img></img><img></img>');
expect(imgVNode.props).to.not.have.property('dangerouslySetInnerHTML');
assert.ok(!imgVNode.props.hasOwnProperty('dangerouslySetInnerHTML'));
});

it('has no children property for nested children', () => {
const [divVNode] = convert('<div><img src="xyz"></img></div>');
const [imgVNode] = divVNode.props.children;
expect(imgVNode.props).to.not.have.property('children');
assert.deepStrictEqual(imgVNode.props.children, undefined);
});
});
3 changes: 1 addition & 2 deletions packages/integrations/react/vnode-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export default function convert(children) {
return createElement(Fragment, {}, childVnodes);
} else if (node.type === ELEMENT_NODE) {
const { class: className, ...props } = node.attributes;
const isVoidElement = ['img', 'input', 'br', 'hr', 'meta', 'area', 'base', 'col', 'command', 'embed', 'keygen', 'link', 'param', 'source', 'track', 'wbr'].includes(node.name);
const elementProps = isVoidElement ? { ...props, className, key:`${id}-${key++}` } : { ...props, className, children: childVnodes };
const elementProps = { ...props, className, children: childVnodes };
return createElement(node.name, elementProps);
} else if (node.type === TEXT_NODE) {
// 0-length text gets omitted in JSX
Expand Down

0 comments on commit 1bf0315

Please sign in to comment.