From af6f4151d336303d5be38c188befc76d101f7de6 Mon Sep 17 00:00:00 2001 From: Fadhlan Date: Sat, 7 Nov 2026 08:24:49 +0700 Subject: [PATCH 1/3] fix(tokenizer): keep the trailing `?` of XML processing instructions Since #2382 the XML branch ended the instruction at `this.index - 1`, which excluded the `?` of the `?>` closer from the section slice. `Parser` then reports `?xml version="1.0"` instead of `?xml version="1.0"?`, so `dom-serializer` writes `` on a parse/serialize round trip, which is not well-formed XML. Pass the `>` index, like the HTML-mode branch does, so the slice includes the `?` again. The following node also starts one character later, so the reported end offsets now cover the closer. --- src/Parser.spec.ts | 20 +++++++++++++++++++ src/Tokenizer.ts | 2 +- src/__snapshots__/Tokenizer.spec.ts.snap | 2 +- .../WebWritableStream.spec.ts.snap | 18 ++++++++--------- src/__snapshots__/WritableStream.spec.ts.snap | 18 ++++++++--------- src/index.spec.ts | 10 ++++++++++ 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/Parser.spec.ts b/src/Parser.spec.ts index f187521eb..ee9d4da82 100644 --- a/src/Parser.spec.ts +++ b/src/Parser.spec.ts @@ -193,6 +193,26 @@ describe("API", () => { ); }); + it("should keep the trailing question mark of XML processing instructions", () => { + const onprocessinginstruction = vi.fn(); + + const parser = new Parser( + { + onprocessinginstruction, + }, + { xmlMode: true }, + ); + + // Split the instruction, so the slice has to span two chunks. + parser.write('"); + + expect(onprocessinginstruction).toHaveBeenCalledWith( + "?xml", + '?xml version="1.0"?', + ); + }); + it("should update the position when a single tag is spread across multiple chunks", () => { let isCalled = false; const p = new Parser({ 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", diff --git a/src/index.spec.ts b/src/index.spec.ts index 14bed480a..ce07694b2 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -32,6 +32,16 @@ describe("Index", () => { expect(dom).toMatchSnapshot(); }); + it("parseDocument keeps the `?>` of an XML processing instruction", () => { + const dom = parseDocument('', { + xmlMode: true, + }); + + // `dom-serializer` wraps the data in `<…>`, so the data has to keep the `?`. + const [instruction] = dom.children; + expect(instruction).toHaveProperty("data", '?xml version="1.0"?'); + }); + it("createDocumentStream", () => { let documentStream!: Parser; From 31d0e0023854b1f96b51371dbf1e676b5821cac4 Mon Sep 17 00:00:00 2001 From: Fadhlan Date: Sat, 7 Nov 2026 08:30:11 +0700 Subject: [PATCH 2/3] fix(parser): keep the closing `?` out of XML processing instruction names The instruction slice now ends with the `?` of the closer, so a no-whitespace instruction like `` reported the name `?target?`. Strip the trailing `?` before deriving the name; the data keeps it. --- src/Parser.spec.ts | 16 ++++++++++++++++ src/Parser.ts | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Parser.spec.ts b/src/Parser.spec.ts index ee9d4da82..29685d14a 100644 --- a/src/Parser.spec.ts +++ b/src/Parser.spec.ts @@ -213,6 +213,22 @@ describe("API", () => { ); }); + it("should not include the closing question mark in instruction names", () => { + const onprocessinginstruction = vi.fn(); + + new Parser( + { + onprocessinginstruction, + }, + { xmlMode: true }, + ).end(""); + + expect(onprocessinginstruction).toHaveBeenCalledWith( + "?target", + "?target?", + ); + }); + it("should update the position when a single tag is spread across multiple chunks", () => { let isCalled = false; const p = new Parser({ diff --git a/src/Parser.ts b/src/Parser.ts index 0d6557b32..b6de38af4 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -765,7 +765,9 @@ export class Parser implements Callbacks { const value = this.getSlice(start, endIndex); if (this.cbs.onprocessinginstruction) { - const name = this.getInstructionName(value); + // In XML mode the slice ends with the `?` of the closer, which is + // part of the instruction but not of its name. + const name = this.getInstructionName(value.replace(/\?$/, "")); this.cbs.onprocessinginstruction(`?${name}`, `?${value}`); } From 235700a62cc209b05e6b89fee5872d36375141b0 Mon Sep 17 00:00:00 2001 From: Fadhlan Date: Sat, 7 Nov 2026 14:45:13 +0700 Subject: [PATCH 3/3] style(parser): hoist the PI trailing-question-mark regex to module scope --- src/Parser.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Parser.ts b/src/Parser.ts index b6de38af4..ecd80c2cc 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -266,6 +266,7 @@ export interface Handler { } const reNameEnd = /\s|\//; +const reTrailingQuestionMark = /\?$/; /** * Incremental parser implementation. @@ -765,9 +766,13 @@ export class Parser implements Callbacks { const value = this.getSlice(start, endIndex); if (this.cbs.onprocessinginstruction) { - // In XML mode the slice ends with the `?` of the closer, which is - // part of the instruction but not of its name. - const name = this.getInstructionName(value.replace(/\?$/, "")); + /* + * In XML mode the slice ends with the `?` of the closer, which is + * part of the instruction but not of its name. + */ + const name = this.getInstructionName( + value.replace(reTrailingQuestionMark, ""), + ); this.cbs.onprocessinginstruction(`?${name}`, `?${value}`); }