Skip to content

Commit 620ffd0

Browse files
committed
fix(compiler): treat normalized empty JSXText as non-rendering in rendersChild
After normalizeAuthoredJsxLiterals collapses whitespace-only JSXText containing linebreaks to '', the rendersChild function incorrectly treated the empty string as a rendered child. This caused multiline empty hosts like <div ref={r}>\n</div> to be marked as owning children, producing false OCTANE_STRONG_MANAGED_DOM_WRITE diagnostics for textContent and child-list writes that the docs allow. The fix adds an explicit empty-string check since '' is the normalized form of whitespace-with-linebreak text and never renders content.
1 parent ee3e9f1 commit 620ffd0

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎packages/octane/src/compiler/strong-dom.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,12 @@ function styleKeys(value) {
154154
return keys;
155155
}
156156

157-
// JSX drops whitespace-only text that contains a line break.
157+
// JSX drops whitespace-only text that contains a line break. After
158+
// normalizeAuthoredJsxLiterals those nodes are collapsed to '' but left in
159+
// children, so an empty value is never rendered content.
158160
function rendersChild(child) {
159-
if (child.type === 'JSXText') return !/^\s*$/.test(child.value) || !/[\r\n]/.test(child.value);
161+
if (child.type === 'JSXText')
162+
return child.value !== '' && (!/^\s*$/.test(child.value) || !/[\r\n]/.test(child.value));
160163
if (child.type === 'JSXExpressionContainer')
161164
return child.expression?.type !== 'JSXEmptyExpression';
162165
return true;

‎packages/octane/tests/compiler/strong-dom.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,16 @@ describe('Strong managed DOM writes through refs', () => {
312312
'r.current.textContent = label;',
313313
'<span ref={r} />',
314314
],
315+
[
316+
'textContent on a multiline empty host',
317+
'r.current.textContent = label;',
318+
'<span ref={r}>\n</span>',
319+
],
320+
[
321+
'mounting into a multiline empty container',
322+
"r.current.appendChild(document.createElement('canvas'));",
323+
'<div ref={r}>\n</div>',
324+
],
315325
[
316326
'mounting into an empty container',
317327
"r.current.appendChild(document.createElement('canvas'));",

0 commit comments

Comments
 (0)