Repository navigation
fix(compiler): do not truncate script block at closing tag in string (fix #10628) - #13405
Open
Sudhakar7J wants to merge 1 commit into
Open
Sudhakar7J wants to merge 1 commit into
Sudhakar7J wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.What kind of change does this PR introduce?
Does this PR introduce a breaking change?
The PR fulfills these requirements:
mainbranch for v2.xfix #xxx[,#xxx], where "xxx" is the issue number)What
Language-aware plain-text scanning for
<script>and<style>blocks insrc/compiler/parser/html-parser.ts:${}interpolations, line/block comments, and regex literals (distinguishing regex vs division, handling JSX-style</...>closers)./, or>so</scriptx>or</stylesheet>does not close the block.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 —#10628shows 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 SFCparseComponentpath 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
packages/compiler-sfc/test/parseComponent.spec.ts(102 lines) andtest/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.Edge cases
)/identifieris treated as division.</scriptinside//comment does not close; in JSXa</b>inside script does not close.content: "</style>"no longer truncates.AI disclosure
This change was researched and implemented with AI assistance, then manually verified, tested, and reviewed.