diff --git a/src/Parser.spec.ts b/src/Parser.spec.ts index f187521e..b485156f 100644 --- a/src/Parser.spec.ts +++ b/src/Parser.spec.ts @@ -177,6 +177,41 @@ 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 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 0d6557b3..67db7537 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}`); } diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 6f983e1b..64bd1ae6 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 70f7e69c..38e3a3f1 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 3012e435..5470725e 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 fe2473e8..d2efaafd 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",