Repository navigation
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already report this problem, without success.
Stencil Version
4.45.6. The main branch at 176b027 has the same code, and the v5 branch has the same lines in packages/mock-doc/src/serialize-node.ts.
Current Behavior
serializeNodeToHtml() in src/mock-doc/serialize-node.ts cleans up the whitespace of a text node with String.prototype.trim(), trimLeft(), trimRight() and the regex \s. In JavaScript these also match the no-break space (U+00A0) and other Unicode spaces such as U+3000 and U+2003. In HTML they are not whitespace and they do not collapse.
So a no-break space at the start or the end of a text node becomes a plain space, and a text node that holds only a no-break space is treated as empty and written as nothing. A no-break space in the middle of a text node is kept. The final renderedNode.trim() also removes a Unicode space such as U+3000 from the start or end of the whole output.
The serializer is used by renderToString() and the hydrate output, by stencil build --prerender, by innerHTML and outerHTML in mock-doc, and by toEqualHtml.
A published component hits it. ion-skeleton-text in Ionic Framework renders <span> </span> (core/src/components/skeleton-text/skeleton-text.tsx, line 56, and @ionic/core 9.0.14 ships a hydrate build). With require('@ionic/core/hydrate').renderToString('<ion-skeleton-text animated></ion-skeleton-text>') the text of the span is a plain space (char code 0x20), not . The same happens to Price: <b>5</b> in a component of my own.
Expected Behavior
The no-break space is kept as , as it already is in the middle of a text node. Only the five HTML whitespace characters (space, tab, line feed, form feed, carriage return) are trimmed or collapsed.
System Info
@stencil/core 4.45.6 from npm, and main at 176b0272
Node 24.19.0, npm 11.17.0, Windows 11Steps to Reproduce
npm i @stencil/[email protected]- Run this script:
const { MockDocument, serializeNodeToHtml } = require('@stencil/core/mock-doc');
const doc = new MockDocument();
const div = doc.createElement('div');
div.innerHTML = '<p>Price: <b>5</b></p><p> Hi </p><span> </span>';
console.log(JSON.stringify(serializeNodeToHtml(div)));
console.log(JSON.stringify(div.innerHTML));It prints this line twice. There is a plain space (0x20) where the input had , and the span is empty:
"<p>Price: <b>5</b></p><p> Hi </p><span></span>"
Expected:
"<p>Price: <b>5</b></p><p> Hi </p><span> </span>"
To see it with a real component, build a project with the dist-hydrate-script output target and this component:
@Component({ tag: 'skel-text', shadow: false })
export class SkelText {
render() {
return (
<Host>
<span> </span>
<p>Price: <b>5</b></p>
</Host>
);
}
}Then run renderToString('<html><body><skel-text></skel-text></body></html>') from the generated hydrate folder. With 4.45.6 the text in the span and the text after Price: is a plain space (0x20). With the change below both are .
Code Reproduction URL
No repository. The steps are inline in Steps to Reproduce above. It is a short script that runs against the published package with nothing else installed.
Additional Information
I looked for an existing report. #6434 says that is stripped during hydration of slotted CMS content, and it was closed by #6477. That change is in client-hydrate.ts and does not touch serialize-node.ts, so the serializer still trims the no-break space on main. I found no open issue or pull request about the serializer and whitespace characters.
I did not run a browser, so I do not know what the client shows after hydration. The report is about the server rendered HTML only.
I have a fix with tests and I am sending it as a pull request. It swaps the trim and \s calls for a small helper that only knows the five HTML whitespace characters. One behavior change comes with it: markup that has a no-break space or another Unicode space at the edge of a text node now keeps it, so renderToString(), prerender, innerHTML and toEqualHtml output changes for such markup.
Validated with Claude Opus 5.5.
Activity