From 3287ef2aa42c19d11823964d6ab702b5bd29602f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20B=C3=B6hm?= <188768+fb55@users.noreply.github.com> Date: Sat, 11 Sep 2021 19:49:28 +0100 Subject: [PATCH] fix(parser): Fix indices of self-closing tags in XML (#949) --- src/Parser.ts | 3 + .../Events/13-long-cdata-end.json | 4 +- .../Events/45-self-closing-indices.json | 65 +++++++++++++++++++ src/__snapshots__/WritableStream.spec.ts.snap | 24 +++---- 4 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 src/__fixtures__/Events/45-self-closing-indices.json diff --git a/src/Parser.ts b/src/Parser.ts index 5d0bda411..cccd33f9e 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -356,6 +356,9 @@ export class Parser { this.foreignContext[this.foreignContext.length - 1] ) { this.closeCurrentTag(false); + + // Set `startIndex` for next node + this.startIndex = this.endIndex + 1; } else { // Ignore the fact that the tag is self-closing. this.onopentagend(); diff --git a/src/__fixtures__/Events/13-long-cdata-end.json b/src/__fixtures__/Events/13-long-cdata-end.json index e353f57e4..d44d2b95b 100644 --- a/src/__fixtures__/Events/13-long-cdata-end.json +++ b/src/__fixtures__/Events/13-long-cdata-end.json @@ -27,13 +27,13 @@ }, { "event": "opentagname", - "startIndex": 0, + "startIndex": 10, "endIndex": 14, "data": ["tag"] }, { "event": "opentag", - "startIndex": 0, + "startIndex": 10, "endIndex": 14, "data": ["tag", {}, false] }, diff --git a/src/__fixtures__/Events/45-self-closing-indices.json b/src/__fixtures__/Events/45-self-closing-indices.json new file mode 100644 index 000000000..c4985681e --- /dev/null +++ b/src/__fixtures__/Events/45-self-closing-indices.json @@ -0,0 +1,65 @@ +{ + "name": "Self-closing indices (#941)", + "options": { + "parser": { + "xmlMode": true + } + }, + "input": "", + "expected": [ + { + "event": "opentagname", + "data": ["xml"], + "startIndex": 0, + "endIndex": 4 + }, + { + "event": "opentag", + "data": ["xml", {}, false], + "startIndex": 0, + "endIndex": 4 + }, + { + "event": "opentagname", + "data": ["a"], + "startIndex": 5, + "endIndex": 7 + }, + { + "event": "opentag", + "data": ["a", {}, false], + "startIndex": 5, + "endIndex": 8 + }, + { + "event": "closetag", + "data": ["a", true], + "startIndex": 5, + "endIndex": 8 + }, + { + "event": "opentagname", + "data": ["b"], + "startIndex": 9, + "endIndex": 11 + }, + { + "event": "opentag", + "data": ["b", {}, false], + "startIndex": 9, + "endIndex": 12 + }, + { + "event": "closetag", + "data": ["b", true], + "startIndex": 9, + "endIndex": 12 + }, + { + "event": "closetag", + "data": ["xml", false], + "startIndex": 13, + "endIndex": 18 + } + ] +} diff --git a/src/__snapshots__/WritableStream.spec.ts.snap b/src/__snapshots__/WritableStream.spec.ts.snap index 901aa5197..6891c1b9f 100644 --- a/src/__snapshots__/WritableStream.spec.ts.snap +++ b/src/__snapshots__/WritableStream.spec.ts.snap @@ -227,7 +227,7 @@ Array [ ], "endIndex": 256, "event": "text", - "startIndex": 204, + "startIndex": 255, }, Object { "data": Array [ @@ -275,7 +275,7 @@ Array [ ], "endIndex": 293, "event": "text", - "startIndex": 257, + "startIndex": 292, }, Object { "data": Array [ @@ -616,7 +616,7 @@ Array [ ], "endIndex": 584, "event": "text", - "startIndex": 530, + "startIndex": 582, }, Object { "data": Array [ @@ -686,7 +686,7 @@ Array [ ], "endIndex": 676, "event": "text", - "startIndex": 585, + "startIndex": 674, }, Object { "data": Array [ @@ -745,7 +745,7 @@ Array [ ], "endIndex": 746, "event": "text", - "startIndex": 677, + "startIndex": 744, }, Object { "data": Array [ @@ -972,7 +972,7 @@ Array [ ], "endIndex": 919, "event": "text", - "startIndex": 910, + "startIndex": 918, }, Object { "data": Array [ @@ -1911,7 +1911,7 @@ Array [ ], "endIndex": 657, "event": "text", - "startIndex": 641, + "startIndex": 655, }, Object { "data": Array [ @@ -2453,7 +2453,7 @@ Array [ ], "endIndex": 1257, "event": "text", - "startIndex": 1187, + "startIndex": 1254, }, Object { "data": Array [ @@ -5191,7 +5191,7 @@ Array [ ], "endIndex": 2669, "event": "text", - "startIndex": 2575, + "startIndex": 2663, }, Object { "data": Array [ @@ -5543,7 +5543,7 @@ Array [ ], "endIndex": 206, "event": "text", - "startIndex": 193, + "startIndex": 204, }, Object { "data": Array [ @@ -5579,7 +5579,7 @@ Array [ ], "endIndex": 220, "event": "text", - "startIndex": 207, + "startIndex": 218, }, Object { "data": Array [ @@ -5713,7 +5713,7 @@ Array [ ], "endIndex": 274, "event": "text", - "startIndex": 260, + "startIndex": 271, }, Object { "data": Array [