diff --git a/src/Parser.spec.ts b/src/Parser.spec.ts index f187521e..29685d14 100644 --- a/src/Parser.spec.ts +++ b/src/Parser.spec.ts @@ -193,6 +193,42 @@ 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 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 0d6557b3..ecd80c2c 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,7 +766,13 @@ 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(reTrailingQuestionMark, ""), + ); 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", diff --git a/src/index.spec.ts b/src/index.spec.ts index 14bed480..ce07694b 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;