Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?xml version="1.0"');
parser.end("?>");

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("<?target?>");

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({
Expand Down
9 changes: 8 additions & 1 deletion src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export interface Handler {
}

const reNameEnd = /\s|\//;
const reTrailingQuestionMark = /\?$/;

/**
* Incremental parser implementation.
Expand Down Expand Up @@ -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}`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude the terminator from no-data PI names

For a valid no-data instruction such as <?target?> in XML mode, this end index makes getSlice return target?; since getInstructionName only stops at whitespace or /, onprocessinginstruction now receives the name ?target? instead of ?target. Preserve the added question mark in the instruction data, but strip it before deriving the instruction name.

Useful? React with 👍 / 👎.

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
this.sequenceIndex = 0;
this.state = State.Text;
this.sectionStart = this.index + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/__snapshots__/Tokenizer.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ exports[`Tokenizer > should terminate XML processing instructions on ?> 1`] = `
[
"onprocessinginstruction",
2,
25,
26,
],
[
"onend",
Expand Down
18 changes: 9 additions & 9 deletions src/__snapshots__/WebWritableStream.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand All @@ -18,7 +18,7 @@ exports[`WebWritableStream > Atom feed 1`] = `
",
],
"endIndex": 38,
"startIndex": 37,
"startIndex": 38,
},
{
"$event": "comment",
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -1621,7 +1621,7 @@ exports[`WebWritableStream > RDF feed 1`] = `
",
],
"endIndex": 38,
"startIndex": 37,
"startIndex": 38,
},
{
"$event": "opentagname",
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -3621,7 +3621,7 @@ exports[`WebWritableStream > RSS feed 1`] = `
",
],
"endIndex": 21,
"startIndex": 20,
"startIndex": 21,
},
{
"$event": "comment",
Expand Down
18 changes: 9 additions & 9 deletions src/__snapshots__/WritableStream.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand All @@ -18,7 +18,7 @@ exports[`WritableStream > Atom feed 1`] = `
",
],
"endIndex": 38,
"startIndex": 37,
"startIndex": 38,
},
{
"$event": "comment",
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -1621,7 +1621,7 @@ exports[`WritableStream > RDF feed 1`] = `
",
],
"endIndex": 38,
"startIndex": 37,
"startIndex": 38,
},
{
"$event": "opentagname",
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -3621,7 +3621,7 @@ exports[`WritableStream > RSS feed 1`] = `
",
],
"endIndex": 21,
"startIndex": 20,
"startIndex": 21,
},
{
"$event": "comment",
Expand Down
10 changes: 10 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ describe("Index", () => {
expect(dom).toMatchSnapshot();
});

it("parseDocument keeps the `?>` of an XML processing instruction", () => {
const dom = parseDocument('<?xml version="1.0"?><a/>', {
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;

Expand Down