Skip to content

fix(compiler): include template contents when validating markup - #5126

Merged
ryansolid merged 1 commit into
solidjs:mainfrom
Marve10s:fix-compiler-validate-template-contents
Nov 20, 2026
Merged

ryansolid merged 1 commit into
solidjs:mainfrom
Marve10s:fix-compiler-validate-template-contents

Conversation

@Marve10s

Copy link
Copy Markdown
Contributor

Summary

@solidjs/compiler rejects every <template> element that has content, in DOM and SSR, while @solidjs/babel-plugin compiles it:

<div><template id="row"><li class="item">x</li></template></div>

fails with "The HTML provided is malformed and will yield unexpected output when evaluated by a browser", and the full message shows why: User HTML: <template><div>#text</div></template> Browser HTML: <template></template>.

The native validator (shared/validate.rs) parses the markup with html5ever and serializes it back with markup5ever_rcdom's SerializableHandle to compare with the input. html5ever stores a template's nodes in template_contents, not children, and that serializer only walks children, so the "browser" side of every template is empty. Babel's validator serializes through parse5, which includes template content, and the two validators are meant to agree.

A shared serialize_tree helper, used by both the fragment and the document-shell validation paths, now copies each template's template_contents children into children before serializing. Valid templates compile again, and malformed markup inside a template still fails with the same diagnostic Babel reports.

This fixes validation only. A template with dynamic parts (<template><div>{x()}</div></template>, a custom element or a ref inside it) still fails at render time, because both compilers' DOM codegen walks template.firstChild instead of .content. That output is identical in babel-plugin 2.0.2 and this branch, so it is a separate codegen change for both compilers.

Public API changes

None.

How did you test this change?

  • Added four cases to packages/compiler/__tests__/validate-parity.test.js, run in both the DOM and SSR tables: template text, a nested template, a template in <head> (the document-shell path), and <template><p><div>x</div></p></template>, which must still throw with Babel's diff. With main's binary 7 of the 34 tests fail; with the fix all 34 pass.
  • cargo test, cargo clippy -- -D warnings and rustfmt on validate.rs pass. The compiler vitest suite (6945 passed, 2 skipped), babel-plugin (432) and pnpm test from the repo root (39 of 39 turbo tasks) pass.

Verification

A script that compiles each case with both compilers, renders the SSR output and the DOM template's content. Published @solidjs/compiler 2.0.2:

element          oxc   dom COMPILE ERROR: The HTML provided is malformed and will yield unexpected output when evaluated by a browser. (1:22)
element          oxc   ssr COMPILE ERROR: The HTML provided is malformed and will yield unexpected output when evaluated by a browser. (1:22)
text             oxc   dom COMPILE ERROR: The HTML provided is malformed and will yield unexpected output when evaluated by a browser. (1:22)
element          babel ssr html: <div><template id="row"><li class="item">x</li></template></div>
element          babel dom template.content: "<li class=\"item\">x</li>"

This branch:

element          oxc   ssr html: <div><template id="row"><li class="item">x</li></template></div>
text             oxc   ssr html: <template>hi</template>
element          oxc   dom template.content: "<li class=\"item\">x</li>"
text             oxc   dom template.content: "hi"
empty (control)  oxc   dom template.content: ""

@changeset-bot

changeset-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2b42d41

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@solidjs/compiler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codspeed

codspeed Bot commented Nov 20, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 31.59%

3 warnings

⚠️ Upgrade your CodSpeed plugin for Node.js 24

@codspeed/* 5.4.0 is only tested on Node.js 18, 20, and 22, so your Node.js 24 measurements may be inaccurate. Upgrade your @codspeed/* packages to 6.0.0 or later.

See affected benchmarks

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 200 untouched benchmarks
⏩ 5 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ projection derive: write one NESTED field (reference) 1,245.7 µs 946.7 µs +31.59%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Marve10s:fix-compiler-validate-template-contents (2b42d41) with main (99db9bc)

Open in CodSpeed

Footnotes

  1. 5 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@ryansolid
ryansolid merged commit 507b1ea into solidjs:main Nov 20, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants