Skip to content

fix(compiler): do not truncate script block at closing tag in string (fix #10628) - #13405

Open
Sudhakar7J wants to merge 1 commit into
vuejs:mainfrom
Sudhakar7J:fix/10628-sfc-script-in-string
Open

Sudhakar7J wants to merge 1 commit into
vuejs:mainfrom
Sudhakar7J:fix/10628-sfc-script-in-string

Conversation

@Sudhakar7J

Copy link
Copy Markdown

Summary

Fixes #10628 — the SFC / template HTML parser no longer truncates a <script> (or <style>) block at a </script>/</style> sequence that appears inside a JS string, template literal, comment, or regex literal.

Supersedes #13404, which GitHub auto-closed when the branch was force-pushed to correct commit authorship. The code is unchanged; the branch is rebuilt on current main.

What kind of change does this PR introduce?

  • Bugfix

Does this PR introduce a breaking change?

  • Yes
  • No

The PR fulfills these requirements:

  • It's submitted to the main branch for v2.x
  • When resolving a specific issue, it's referenced in the PR's title (e.g. fix #xxx[,#xxx], where "xxx" is the issue number)
  • All tests are passing
  • New/updated tests are included

What

Language-aware plain-text scanning for <script> and <style> blocks in src/compiler/parser/html-parser.ts:

  • script: tracks single/double-quoted strings, template literals with nested ${} interpolations, line/block comments, and regex literals (distinguishing regex vs division, handling JSX-style </...> closers).
  • style: tracks single/double-quoted strings and block comments.
  • Only a closing tag in code context terminates the block; the tag name must be followed by whitespace, /, or > so </scriptx> or </stylesheet> does not close the block.
  • On unterminated string/comment/regex, falls back to historical first-closing-tag behavior to preserve error recovery.

Why

The parser treated script/style as raw text and closed at the first </script> sequence even inside a long concatenated string (e.g. ad markup with many + on many lines per #10628), producing truncated invalid script content and a compilation error. Browsers and HTML spec treat script as raw text delimited only by the matching end tag in code context, but for SFC authoring the intent is JS-aware — #10628 shows real-world markup strings break the naive scan.

How

Replaced single-regex plain-text search with a small state-machine scanner (findPlainTextBlockEnd) used by both the SFC parseComponent path and the template compiler parser. State machine handles escapes, template-stack for nested backticks/interpolations, regex heuristics (last significant token), and uppercase/spaced closing tags (</SCRIPT>, </script >).

This is additive — well-formed files without closing tags in strings parse identically to before.

Testing

  • Full unit suite: 1451 passed. New regression tests in packages/compiler-sfc/test/parseComponent.spec.ts (102 lines) and test/unit/modules/compiler/parser.spec.ts (24 lines) cover: the original long concatenated string repro, template literals, line/block comments, regex vs division, escaped quotes, JSX closers, uppercase/spaced closing tags, longer tag names not closing blocks, subsequent blocks after script, style strings/comments, and unterminated-string fallback.
  • Verified no duplicate open PR for Error compiling long string litteral (many + on many lines) #10628.

Edge cases

  • Unterminated JS string/comment/regex falls back to old behavior (error recovery preserved).
  • Closing tag inside regex literal is ignored; division by regex-like pattern after )/identifier is treated as division.
  • </script inside // comment does not close; in JSX a</b> inside script does not close.
  • Style blocks: content: "</style>" no longer truncates.

AI disclosure

This change was researched and implemented with AI assistance, then manually verified, tested, and reviewed.

…vuejs#10628)The HTML parser treated script/style as plain text and closed theblock at the first closing tag sequence, even when it occurredinside a JS string literal (e.g. a long string built with many +concatenations containing ad markup). This produced truncated,invalid script content and a compilation error.Scan plain-text blocks in a language-aware way: for script, tracksingle/double quoted strings, template literals with nestedinterpolations, line/block comments and regex literals(distinguishing regex vs division, and JSX-style closing tags);for style, track strings and block comments. Only a closing tagin code context terminates the block. Require the tag name to befollowed by whitespace, / or > so a longer tag name does notclose a block. On unterminated string/comment/regex, fall backto the historical first-closing-tag behaviour to preserve errorrecovery.Add regression tests for SFC parseComponent and templatecompiler parser covering the reproduction, long concatenatedstrings, template literals, comments, regex vs division,escaped quotes, JSX closers, uppercase/spaced closing tags,longer tag names, blocks after script, style strings/comments,and unterminated-string fallback.
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.

Error compiling long string litteral (many + on many lines)

1 participant