Skip to content

bug: serializeNodeToHtml turns a no-break space at the edge of a text node into a plain space #7066

Backlog

Description

@goranai

Prerequisites

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>&nbsp;</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 &nbsp;. The same happens to Price:&nbsp;<b>5</b> in a component of my own.

Expected Behavior

The no-break space is kept as &nbsp;, 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 11

Steps to Reproduce

  1. npm i @stencil/[email protected]
  2. Run this script:
const { MockDocument, serializeNodeToHtml } = require('@stencil/core/mock-doc');
const doc = new MockDocument();
const div = doc.createElement('div');
div.innerHTML = '<p>Price:&nbsp;<b>5</b></p><p>&nbsp;Hi&nbsp;</p><span>&nbsp;</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 &nbsp;, and the span is empty:

"<p>Price: <b>5</b></p><p> Hi </p><span></span>"

Expected:

"<p>Price:&nbsp;<b>5</b></p><p>&nbsp;Hi&nbsp;</p><span>&nbsp;</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>&nbsp;</span>
        <p>Price:&nbsp;<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 &nbsp;.

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 &nbsp; 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions