From f90a8788e25b2b091231c8a6b9021cd010ad1f22 Mon Sep 17 00:00:00 2001 From: Ryan Chou <88779759+ryanchou1994@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:12:47 +0800 Subject: [PATCH 1/2] fix(tokenizer): keep the trailing `?` of XML processing instructions Since #2382, the tokenizer ends an XML processing instruction at `this.index - 1`, so the data loses the `?` of `?>`, `endIndex` points at the `?`, and the next node starts at the `>`. Pass `this.index` again, as v10 did and as `ondeclaration` does. Fixes #2562 --- src/Parser.spec.ts | 22 +++++++++++++++++++ src/Tokenizer.ts | 2 +- src/__snapshots__/Tokenizer.spec.ts.snap | 2 +- .../WebWritableStream.spec.ts.snap | 18 +++++++-------- src/__snapshots__/WritableStream.spec.ts.snap | 18 +++++++-------- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/Parser.spec.ts b/src/Parser.spec.ts index f187521eb..ffa91f398 100644 --- a/src/Parser.spec.ts +++ b/src/Parser.spec.ts @@ -177,6 +177,28 @@ describe("API", () => { ); }); + it("should keep the trailing `?` of processing instructions (#2562)", () => { + const events: unknown[] = []; + const p = new Parser( + { + onprocessinginstruction(name, data) { + events.push([name, data, p.startIndex, p.endIndex]); + }, + ontext(data) { + events.push([data, p.startIndex, p.endIndex]); + }, + }, + { xmlMode: true }, + ); + + p.end('a'); + + expect(events).toEqual([ + ["?xml", '?xml version="1.0"?', 0, 20], + ["a", 21, 21], + ]); + }); + it("should preserve declaration casing when lowerCaseTags is disabled", () => { const onprocessinginstruction = vi.fn(); diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 6f983e1be..64bd1ae63 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -782,7 +782,7 @@ export default class Tokenizer { // Remember that we just consumed `?`, so the next `>` closes the PI. this.sequenceIndex = 1; } else if (c === CharCodes.Gt && this.sequenceIndex === 1) { - this.cbs.onprocessinginstruction(this.sectionStart, this.index - 1); + this.cbs.onprocessinginstruction(this.sectionStart, this.index); this.sequenceIndex = 0; this.state = State.Text; this.sectionStart = this.index + 1; diff --git a/src/__snapshots__/Tokenizer.spec.ts.snap b/src/__snapshots__/Tokenizer.spec.ts.snap index 70f7e69c0..38e3a3f1c 100644 --- a/src/__snapshots__/Tokenizer.spec.ts.snap +++ b/src/__snapshots__/Tokenizer.spec.ts.snap @@ -1063,7 +1063,7 @@ exports[`Tokenizer > should terminate XML processing instructions on ?> 1`] = ` [ "onprocessinginstruction", 2, - 25, + 26, ], [ "onend", diff --git a/src/__snapshots__/WebWritableStream.spec.ts.snap b/src/__snapshots__/WebWritableStream.spec.ts.snap index 3012e4352..5470725e6 100644 --- a/src/__snapshots__/WebWritableStream.spec.ts.snap +++ b/src/__snapshots__/WebWritableStream.spec.ts.snap @@ -6,9 +6,9 @@ exports[`WebWritableStream > Atom feed 1`] = ` "$event": "processinginstruction", "data": [ "?xml", - "?xml version="1.0" encoding="utf-8"", + "?xml version="1.0" encoding="utf-8"?", ], - "endIndex": 36, + "endIndex": 37, "startIndex": 0, }, { @@ -18,7 +18,7 @@ exports[`WebWritableStream > Atom feed 1`] = ` ", ], "endIndex": 38, - "startIndex": 37, + "startIndex": 38, }, { "$event": "comment", @@ -1609,9 +1609,9 @@ exports[`WebWritableStream > RDF feed 1`] = ` "$event": "processinginstruction", "data": [ "?xml", - "?xml version="1.0" encoding="UTF-8"", + "?xml version="1.0" encoding="UTF-8"?", ], - "endIndex": 36, + "endIndex": 37, "startIndex": 0, }, { @@ -1621,7 +1621,7 @@ exports[`WebWritableStream > RDF feed 1`] = ` ", ], "endIndex": 38, - "startIndex": 37, + "startIndex": 38, }, { "$event": "opentagname", @@ -3609,9 +3609,9 @@ exports[`WebWritableStream > RSS feed 1`] = ` "$event": "processinginstruction", "data": [ "?xml", - "?xml version="1.0"", + "?xml version="1.0"?", ], - "endIndex": 19, + "endIndex": 20, "startIndex": 0, }, { @@ -3621,7 +3621,7 @@ exports[`WebWritableStream > RSS feed 1`] = ` ", ], "endIndex": 21, - "startIndex": 20, + "startIndex": 21, }, { "$event": "comment", diff --git a/src/__snapshots__/WritableStream.spec.ts.snap b/src/__snapshots__/WritableStream.spec.ts.snap index fe2473e89..d2efaafd6 100644 --- a/src/__snapshots__/WritableStream.spec.ts.snap +++ b/src/__snapshots__/WritableStream.spec.ts.snap @@ -6,9 +6,9 @@ exports[`WritableStream > Atom feed 1`] = ` "$event": "processinginstruction", "data": [ "?xml", - "?xml version="1.0" encoding="utf-8"", + "?xml version="1.0" encoding="utf-8"?", ], - "endIndex": 36, + "endIndex": 37, "startIndex": 0, }, { @@ -18,7 +18,7 @@ exports[`WritableStream > Atom feed 1`] = ` ", ], "endIndex": 38, - "startIndex": 37, + "startIndex": 38, }, { "$event": "comment", @@ -1609,9 +1609,9 @@ exports[`WritableStream > RDF feed 1`] = ` "$event": "processinginstruction", "data": [ "?xml", - "?xml version="1.0" encoding="UTF-8"", + "?xml version="1.0" encoding="UTF-8"?", ], - "endIndex": 36, + "endIndex": 37, "startIndex": 0, }, { @@ -1621,7 +1621,7 @@ exports[`WritableStream > RDF feed 1`] = ` ", ], "endIndex": 38, - "startIndex": 37, + "startIndex": 38, }, { "$event": "opentagname", @@ -3609,9 +3609,9 @@ exports[`WritableStream > RSS feed 1`] = ` "$event": "processinginstruction", "data": [ "?xml", - "?xml version="1.0"", + "?xml version="1.0"?", ], - "endIndex": 19, + "endIndex": 20, "startIndex": 0, }, { @@ -3621,7 +3621,7 @@ exports[`WritableStream > RSS feed 1`] = ` ", ], "endIndex": 21, - "startIndex": 20, + "startIndex": 21, }, { "$event": "comment", From f3020c365c5816eb708a3c7aedcaf4887016397e Mon Sep 17 00:00:00 2001 From: Ryan Chou <88779759+ryanchou1994@users.noreply.github.com> Date: Fri, 6 Nov 2026 16:15:02 +0800 Subject: [PATCH 2/2] fix(parser): leave the closing `?` out of processing instruction names Now that the data of an XML processing instruction keeps the `?` of `?>`, `getInstructionName` (which stops only at whitespace or `/`) returned `?foo?` as the name of a name-only instruction like ``. Drop the trailing `?` before extracting the name, so names are the same as before and only the data gains its closing `?`. --- src/Parser.spec.ts | 13 +++++++++++++ src/Parser.ts | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Parser.spec.ts b/src/Parser.spec.ts index ffa91f398..b485156f7 100644 --- a/src/Parser.spec.ts +++ b/src/Parser.spec.ts @@ -199,6 +199,19 @@ describe("API", () => { ]); }); + it("should not include the closing `?` in processing instruction names", () => { + const onprocessinginstruction = vi.fn(); + + new Parser({ onprocessinginstruction }, { xmlMode: true }).end( + "", + ); + + expect(onprocessinginstruction.mock.calls).toEqual([ + ["?foo", "?foo?"], + ["?bar", "?bar baz?"], + ]); + }); + it("should preserve declaration casing when lowerCaseTags is disabled", () => { const onprocessinginstruction = vi.fn(); diff --git a/src/Parser.ts b/src/Parser.ts index 0d6557b32..67db75378 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -765,7 +765,10 @@ export class Parser implements Callbacks { const value = this.getSlice(start, endIndex); if (this.cbs.onprocessinginstruction) { - const name = this.getInstructionName(value); + // The closing `?` of `?>` belongs to the data, not to the name. + const name = this.getInstructionName( + value.endsWith("?") ? value.slice(0, -1) : value, + ); this.cbs.onprocessinginstruction(`?${name}`, `?${value}`); }