Skip to content

fix: warn on document-structure tags inside LWC template (#3681)#5835

Open
Berestly wants to merge 1 commit into
salesforce:masterfrom
Berestly:fix-3681
Open

fix: warn on document-structure tags inside LWC template (#3681)#5835
Berestly wants to merge 1 commit into
salesforce:masterfrom
Berestly:fix-3681

Conversation

@Berestly

@Berestly Berestly commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Hi Team,

Fixes #3681

Details

When <!doctype>, <html>, <head>, or <body> appear inside an LWC template, parse5 collapses them into a text node. LWC's parseTextNode re-reads the raw source (to avoid HTML entity decoding), which accidentally causes the literal tag markup to leak through and render as visible text on the screen.

This PR updates parseTextNode to intercept these document-structure tags, strip them from the output so they don't render, and emit a specific warning diagnostic (LWC1214) per tag to notify the developer.

Summary of Changes:

  • @lwc/errors: Added DOCUMENT_STRUCTURE_TAG_NOT_ALLOWED_IN_TEMPLATE (Warning code 1214) to template-transform.ts and bumped the next error code marker to 1215 in index.ts.
  • @lwc/template-compiler: Added matching regex rules in constants.ts and wired them into parseTextNode inside parser/index.ts.
  • Tests: Created a new test fixture (disallowed-document-structure-tags) verifying that the compiler cleanly strips out the invalid markup and emits four individual LWC1214 warnings.

Does this pull request introduce a breaking change?

🗿 No, it does not introduce a breaking change.


Does this pull request introduce an observable change?

✅ Yes, it does include an observable change.

Anticipated Observable Changes:

  • Developers will now see an explicit LWC1214 compiler warning in the console if they include document-level tags inside an LWC template.
  • The literal tag strings (e.g., <html>) will no longer render onto the viewport at runtime.

GUS work item

W-18040860

Thanks!

@@ -0,0 +1,6 @@
<template>
<!doctype html>
<html></html>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tags with props? Also, what happens if there's more than one of a kind, since these are usually unique per document? What happens if there's an unmatched opening tag, or an unmatched closing tag?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LWC renders <!doctype>, <html>, <body>, <head> as strings

2 participants