Skip to content

fix(parser): read escaped-string templates like Angular's cursor (line continuations, NULs, bad escapes) - #759

Merged
Brooooooklyn merged 47 commits into
mainfrom
fix/escaped-string-older-gaps
Dec 4, 2026
Merged

Brooooooklyn merged 47 commits into
mainfrom
fix/escaped-string-older-gaps

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Follow-up to #755. Its description listed older differences from Angular 22.3.0 in escaped-string templates (inline template: strings, HtmlLexer::with_escaped_string(true)). This PR fixes them, plus what the same root causes touched.

What changes

Area Angular 22.3.0 Before
Line continuation (\ + LF / CR LF / CR) The cursor skips it when it lands on it; the next char is read as is (no escape decoding, a NUL there is EOF) We only skipped it when moving from it, and ate the next char
Escaped NUL Peeked as EOF, like a raw NUL: scans that stop at EOF stop there, the others keep the NUL as a char Read as U+FFFD, then the token list was cut at the first NUL
<?"\0"?>z PI "\0", text z text after the PI lost
Bad escape right after a token end (<?x?>\u{, </a>\xzz, comment, ICU }) Cursor throws while moving onto it: that token is dropped Token kept, escape lexed as text
<?x?\0>z Unexpected character "EOF" @4, EOF token 4-4 "�", EOF 0-0
Escape digits Read like parseInt (\x0g = 0), counted in UTF-16 units; CR/LF normalized after decoding Read strictly, counted in chars, normalized before decoding
Entities with a bad escape, unterminated attribute values, tag/attr prefix : splits, ICU case value trim as Angular several small differences (see commits)

Some plain-mode (not escaped) gaps share these code paths and are fixed too: </ab=x, <a-b:c>, <a b="{{x}}abc, a&#x\r\nb, &#x {{c}}.

How it was checked

Differential fuzzing against Angular 22.3.0's own lexer (tokenize), token types + parts, spans and errors compared one by one, both escaped and plain mode, ICU on/off:

inputs   mismatches main -> here   fixed    worse than main
  31k        13581 ->  4056          9525     0 real, 2 luck
 2.6k         1291 ->   307           984     0
  88k        39669 -> 17261         22408     0 real, 82 luck
 259k       151860 -> 59928         91932     0 real, 15 luck
 147k        71705 -> 45635         26070     0 real, 46 luck
 196k        95102 -> 61136         33966     2 real (plain, errors only), 47 luck

"luck" = main's tokens were already wrong there and only its spans or errors happened to match (it misread the line continuation earlier in the input). Every behaviour change was checked against Angular, and each new test fails without its source change. cargo test --all-features, NAPI tests (623), cargo fmt --check pass; conformance snapshot unchanged.

Left for later

  • A bad escape inside a token (not right after it): Angular drops the token in progress (ab\xzz gives text \ 2-6). Needs a larger redesign of how bad escapes are kept in tokens.
  • The 2 plain-mode cases above come from open tag names not stopping at = or a quote. Matching Angular there also means <a href=x?a=b> gets an extra attribute with an empty name, so it is a separate decision.
  • &#😀: Angular keeps a lone surrogate, which a Rust String cannot hold; we keep the whole emoji (spans match, the message shows �).
  • \x-5: Angular throws a RangeError; we read U+0005.
  • <a x=> has no empty attribute value token; an entity error inside an attribute value is reported where Angular reports none.

🤖 Generated with Claude Code

Brooooooklyn and others added 30 commits December 4, 2026 16:47
Angular's EscapedCharacterCursor skips a line continuation (a backslash
and one CR or LF, so a CRLF leaves its LF) when it lands on one, and then
reads the next character as is: an escape right after it is not decoded,
and a NUL there ends the input. The skipped characters are part of the
previous token's span.

We read a continuation only when advancing from it: peek() returned the
newline, advance() also moved past the next character, and the text
decoder dropped the LF of a CRLF and decoded an escape after it.

Now the cursor skips a continuation when it lands on one (and at the
start), the character after it is read as is by peek, peek_at, advance
and the text decoder, and errors after an escaped NUL that ends the text
are dropped, as Angular reads nothing after it.

Example (escaped, one backslash): `<?x\<LF>\0y?>z`
  Angular: PI 'x\0y' 0-10, Text 'z'
  before:  PI 'x' 0-10, 'z' lost
  `<?>\<LF>{`  Angular: PI 0-5, Text '{' 5-6; before: PI 0-3, Text 3-6

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ction

Angular's cursor peeks a NUL as EOF, but its processing instruction scan
does not check for EOF inside quotes: it moves past the NUL, keeps it in
the content as U+0000, and lexing goes on after the instruction.

In escaped-string mode our post-pass decoded escapes in every token part
and cut the token list at the first escaped NUL, so all text after the
instruction was lost. A NUL escape in a ProcessingInstruction part now
decodes to '\0' and does not cut the tokens; other token kinds are
unchanged.

Example (escaped string '<?"\0"?>z'):
  Angular: PI '"\u0000"' 0-8, Text 'z' 8-9, EOF
  before:  PI '"' 0-8, EOF
  after:   same as Angular (also \000, \x00, \u0000, \u{0})

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ike Angular

Angular's EscapedCharacterCursor decodes the next escape as soon as it
moves onto it. So when _consumeProcessingInstruction moves past the end
of the instruction (`>`, the `?` of `?>`, or a NUL) onto a bad escape, the
cursor throws before the token is emitted: the instruction is dropped and
tokenize() records the error. For invalid digits the cursor still peeks the
backslash, which is read as text before moving on from the escape's end;
an escape cut short by the end of the input ends it.

We kept the instruction and lexed the escape as text. Now, in escaped-string
mode, the lexer checks for such an escape after each of those moves and
recovers like Angular (not after an escaped NUL in text, where Angular's
lexing has already ended).

  '<?x\0\u{y'     Angular [EOF 9] err EOF @9; was [PI 'x', Text, EOF] err Invalid hex @8
  '<?x\0\xZZ'     Angular [Text '\' 5-9, EOF] err Invalid hex @7
  '<?x?>\u{<?y?>' Angular [EOF 13] err EOF @13

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ke Angular

Angular's EscapedCharacterCursor peeks an escaped NUL (\0, \x00, \u0000,
\u{0}) as $EOF. So an "Unexpected character" error at one says "EOF", and
the tokenize loop stops at it, putting the EOF token at the NUL.

Our peek() reads an escaped NUL as U+FFFD, so the error said
"Unexpected character \"\u{fffd}\"", and the loop went on lexing the rest
as text, which the NUL post-pass then cut, leaving the EOF token at 0-0.
Now the tokenize loop also stops at at_nul(), and
unexpected_character_message (also used for `<!-` and `<![`) says EOF
at a NUL.

Example, escaped '<?x?\0>z':
  Angular: [EOF 4-4], 'Unexpected character "EOF"' @4
  before:  [EOF 0-0], 'Unexpected character "\u{fffd}"' @4
Same for '<??\0' (@3) and '</a \0>' (@4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ocessing instruction ends

Angular's EscapedCharacterCursor skips a line continuation it lands on and
then reads the next character raw, without decoding it. So when the advance
past a processing instruction's end lands on a continuation followed by a
bad escape, nothing throws: the instruction is kept and the escape is text.

With the line continuation and bad-escape fixes together, the bad-escape
check looked at the raw backslash after the continuation and dropped the
instruction. It now skips positions the cursor reads raw (reads_raw_at).

Example, escaped '<?x?>\<LF>\xZZ':
  Angular: [PI 'x' 0-7, Text '\xZZ' 7-11, EOF]
  before:  [Text '\' 7-11, EOF], 'Invalid hexadecimal escape sequence' @9

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…token text

Token text was normalized before the escape pass, so a backslash and CRLF
looked like a backslash and LF: the LF was lost and the escape after it was
read as is. Keep the CR after a backslash so the escape pass skips only the
backslash and CR, like Angular's cursor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's cursor reads the digits of \x and \u by UTF-16 units, so an
emoji is both digits of \x. Read every code-point escape in one helper,
code_point_escape, which the cursor checks and the bad-escape check share,
and drop hex_is_nan for parse_int_prefix, which now skips a sign too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The pass that decodes escapes in token text after lexing now reads them
as Angular's cursor does:
- A token part that starts right after a line continuation reads its
  first character as is, so its escape is not decoded.
- Code-point escapes use code_point_escape: parseInt digits (\x0\ is
  NUL), and invalid digits read as one backslash of text.
- An escape cut short by the end of the input drops its token and
  reports one EOF error at the end, not a second one next to the lexer's.
- Error offsets count from the part, so an interpolation's are right.

The old decoder had a hidden nul! macro for its many NUL cases; the shared
helper leaves one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The cursor read an escaped NUL as U+FFFD, so every scan went on past it
and the escape pass cut the tokens later, leaving the lexer's errors from
after the NUL. Angular's cursor peeks it as EOF: read it as chars::EOF, as
a raw NUL is, so each scan stops there where Angular's does and the lexer
stops after it. A bad escape after {{\0}}, @if (\0), @let a = \0, or a
tag name with \0 is then not read, and text ends at the NUL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Now that the lexer stops at an escaped NUL like Angular, nothing after it
is read: the guard in recover_at_bad_escape and the pruning of errors
after a NUL in text never change a result (same on the whole corpus).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… input

Angular's cursor throws at the end of the input there, so a bad escape
after it is never reported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The lexer already ends a token where Angular's cursor peeks a NUL as EOF,
so the escape pass now keeps any NUL left in a token: quoted attribute
values, comments, CDATA, raw text, doctypes and ICU raw text go on past it.
A NUL escape cut short by the end of the input (`\x0`) peeks as EOF too,
and a processing instruction ending there is dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's _processCarriageReturns runs on the characters its cursor reads,
so a CR before a line continuation and the LF after it are one line
ending, and an escaped CR is a CR. Doctypes, processing instructions and
an ICU's switch value and type keep their CRs, as in Angular.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The check for a backslash read as is after a line continuation used the
token start for every part. A name after a prefix starts after its `:`,
and the name of a closing tag after the `</` and whitespace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's cursor throws at the end of the input before it ends the text
token it is in, so that token is lost, with the empty one after an
interpolation or entity. This holds in plain templates too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's _consumeEntity throws out of the text: the text before the
entity is a token, the characters the entity read are lost, and lexing
goes on where its cursor stopped, past an escaped NUL too. The message
has the characters the cursor read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cape

Angular's cursor decodes an escape as soon as it moves onto it and throws
for invalid digits: a processing instruction or comment text it is in is
lost, and so is an end tag whose `>` it moves past onto one. The
backslash it still peeks starts a text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The bad escapes found while lexing and those found when the escapes of
token text are decoded afterwards now come in the order Angular's cursor
meets them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's cursor reports the invalid digits, then fails to move past
them: the token is lost, and the input ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The text an unterminated attribute value ends in is lost, but Angular's
cursor still reports the bad escapes it read in it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An escape cut short by the end of the input loses only the text it ends,
as in Angular: the text before an instruction dropped at a bad escape
stays, while an empty text after an entity and the backslash read after
an earlier bad escape go with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ibute value

Angular emits an `&` that starts no entity as a text token of its own, so
only the text after the last `&`, entity or interpolation is lost when the
quote is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The escape pass took any part that ended inside `\x` or `\u` for the end of
the input. But the raw `:` split of a tag name and the trim of an ICU case
value can cut an escape. Now the digits are read from the input, so only a
real end of the input cuts an escape short. Names split at the first `:`
the cursor reads, so the `:` in `\x4:` is a digit, and an ICU case value is
trimmed after its escapes are decoded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's cursor throws out of `_consumeEntity` when it moves onto a bad
escape, so the entity is lost and the escape is read as a `\` of text, with
its own error. The lexer read such an escape as U+FFFD and reported an
"Unable to parse entity" error with it instead. An escape the end of the
input cuts short is an EOF error there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r its end

Angular's cursor throws when it moves past the closing `}` onto a bad
escape, before the end token and before it leaves the case or form. Between
the cases of a form, the escape then starts a case value, read from the
backslash. An escape error found twice, once with the expansion form hint,
is reported once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It shared its name with the free `normalize_line_endings`, and both are
used in `tokenize`. It only normalizes outside an escaped string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It had one caller, and its doc spoke of a use without the line
continuation skip that nothing had.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…an enum

`code_point_escape` took two bools in a row, one of which implied the
other not set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brooooooklyn and others added 17 commits December 4, 2026 21:54
…one pass

The escape pass looked through all errors for each one it found, and the
unterminated attribute value walked the input from the start for each
error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`lex`, `text`, `eof` and `eof_error` were copied in four test modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`parse_int_prefix` reads `\x-5` as U+0005, where Angular throws a
`RangeError`. The test pins this behaviour, it is not Angular's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tag name splits at the first `:` the cursor reads, and `cursor_text`
is never given a bad escape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Use the shared lex/eof/eof_error helpers in the attribute and processing
instruction tests, add shared token and lex_icu helpers for the tests that
built the expansion lexer inline, and reuse the lexer's ICU_HINT instead of
a copy of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular throws a RangeError for `\x-5`, so the lexer's U+0005 is not an
Angular value to hold; the doc of parse_int_prefix keeps the note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's `_consumePrefixAndName` uses `isPrefixEnd` for tag names too, so
`<a-b:c>` and `<a;\x3ab>` have no prefix: a `:` after any other character
is part of the name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular trims the case value with `String.prototype.trim`, which keeps
U+0085 and removes U+FEFF; `parseInt` skips the same whitespace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The content of an interpolation, a processing instruction or a doctype starts
where the cursor moves to past the marker's characters, any of which may be
an escape, and a line continuation after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's value text ends at the NUL, raw or escaped, which its cursor
peeks as EOF, and the empty token stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's cursor throws when it moves onto a bad escape anywhere in the end
tag, in its name or the whitespace around it, not only after its `>`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's `_consumeTagClose` reads the name up to `isNameEnd`, so the
missing `>` is an error at the quote or `=`, and the rest is text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The whitespace Angular requires after `@let` is the character its cursor
reads, so an escaped `@` or whitespace counts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's `isExpansionFormStart` tries `{{`, so its cursor moves onto the
escape after the `{` and throws there, as it does in a text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular starts an empty text after the entity error, which ends at the
`{{` and is merged into the text before the entity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's cursor moves past the CR of a CRLF only. In a character outside
the BMP it moves into it; no str starts at its second half, so the lexer
keeps the whole character there and reads the first half as U+FFFD in the
message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Brooooooklyn

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T15:10:01.147095Z 4cfec8e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 4cfec8ef90

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Brooooooklyn
Brooooooklyn merged commit e059d63 into main Dec 4, 2026
10 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/escaped-string-older-gaps branch December 4, 2026 15:44
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.

1 participant