Skip to content
Open
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
35 changes: 35 additions & 0 deletions src/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?xml version="1.0"?>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(
"<?foo?><?bar baz?>",
);

expect(onprocessinginstruction.mock.calls).toEqual([
["?foo", "?foo?"],
["?bar", "?bar baz?"],
]);
});

it("should preserve declaration casing when lowerCaseTags is disabled", () => {
const onprocessinginstruction = vi.fn();

Expand Down
5 changes: 4 additions & 1 deletion src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

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

When parsing a valid no-data instruction such as <?foo?> in xmlMode, this makes Parser#onprocessinginstruction slice foo?; getInstructionName() only terminates on whitespace or /, so callbacks now receive the name ?foo? instead of ?foo. The added test uses an XML declaration with whitespace after xml, which masks this case; preserve the trailing ? in the data and corrected offsets without letting it become part of the target 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