Skip to content

Commit

Permalink
fix(parser): Fix indices for end, CDATA, add indices to tests (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Aug 27, 2021
1 parent 9c30fe6 commit 4e25252
Show file tree
Hide file tree
Showing 56 changed files with 6,222 additions and 3,069 deletions.
4 changes: 3 additions & 1 deletion src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ export class Parser {
}

oncdata(value: string): void {
this.updatePosition(1);
this.updatePosition(9);
if (this.options.xmlMode || this.options.recognizeCDATA) {
this.cbs.oncdatastart?.();
this.cbs.ontext?.(value);
Expand All @@ -419,6 +419,8 @@ export class Parser {

onend(): void {
if (this.cbs.onclosetag) {
// Set start- and end indices for remaining tags
this.startIndex = this.endIndex = this.tokenizer.getAbsoluteIndex();
for (
let i = this.stack.length;
i > 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ export default class Tokenizer {

if (this.trieIndex < 0) {
this.emitNamedEntity();
this._index--;
return;
}

Expand Down Expand Up @@ -770,7 +771,6 @@ export default class Tokenizer {

this.sectionStart = this._index - this.trieExcess + 1;
this._state = this.baseState;
this._index--;
}

private decodeNumericEntity(base: 10 | 16, strict: boolean) {
Expand Down
7 changes: 3 additions & 4 deletions src/WritableStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ helper.createSuite("Stream", (test, cb) => {
const handler = helper.getEventCollector(cb);
const stream = new WritableStream(handler, test.options);

fs.readFile(filePath, (err, data) => {
if (err) throw err;
stream.end(data);
});
fs.readFile(filePath, (err, data) =>
err ? cb(err) : stream.end(data)
);
}),
test.options
)
Expand Down
8 changes: 7 additions & 1 deletion src/__fixtures__/Events/01-simple.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "simple",
"html": "<h1 class=test>adsf</h1>",
"input": "<h1 class=test>adsf</h1>",
"expected": [
{
"event": "opentagname",
"startIndex": 0,
"endIndex": 3,
"data": ["h1"]
},
{
Expand All @@ -12,6 +14,8 @@
},
{
"event": "opentag",
"startIndex": 0,
"endIndex": 14,
"data": [
"h1",
{
Expand All @@ -25,6 +29,8 @@
},
{
"event": "closetag",
"startIndex": 19,
"endIndex": 23,
"data": ["h1"]
}
]
Expand Down
14 changes: 13 additions & 1 deletion src/__fixtures__/Events/02-template.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
{
"name": "Template script tags",
"html": "<p><script type=\"text/template\"><h1>Heading1</h1></script></p>",
"input": "<p><script type=\"text/template\"><h1>Heading1</h1></script></p>",
"expected": [
{
"event": "opentagname",
"startIndex": 0,
"endIndex": 2,
"data": ["p"]
},
{
"event": "opentag",
"startIndex": 0,
"endIndex": 2,
"data": ["p", {}]
},
{
"event": "opentagname",
"startIndex": 3,
"endIndex": 10,
"data": ["script"]
},
{
Expand All @@ -20,6 +26,8 @@
},
{
"event": "opentag",
"startIndex": 3,
"endIndex": 31,
"data": [
"script",
{
Expand All @@ -33,10 +41,14 @@
},
{
"event": "closetag",
"startIndex": 49,
"endIndex": 57,
"data": ["script"]
},
{
"event": "closetag",
"startIndex": 58,
"endIndex": 61,
"data": ["p"]
}
]
Expand Down
8 changes: 7 additions & 1 deletion src/__fixtures__/Events/03-lowercase_tags.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "Lowercase tags",
"input": "<H1 class=test>adsf</H1>",
"options": {
"parser": {
"lowerCaseTags": true
}
},
"html": "<H1 class=test>adsf</H1>",
"expected": [
{
"event": "opentagname",
"startIndex": 0,
"endIndex": 3,
"data": ["h1"]
},
{
Expand All @@ -17,6 +19,8 @@
},
{
"event": "opentag",
"startIndex": 0,
"endIndex": 14,
"data": [
"h1",
{
Expand All @@ -30,6 +34,8 @@
},
{
"event": "closetag",
"startIndex": 19,
"endIndex": 23,
"data": ["h1"]
}
]
Expand Down
18 changes: 16 additions & 2 deletions src/__fixtures__/Events/04-cdata.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
{
"name": "CDATA",
"input": "<tag><![CDATA[ asdf ><asdf></adsf><> fo]]></tag><![CD>",
"options": {
"parser": { "xmlMode": true }
"parser": {
"xmlMode": true
}
},
"html": "<tag><![CDATA[ asdf ><asdf></adsf><> fo]]></tag><![CD>",
"expected": [
{
"event": "opentagname",
"startIndex": 0,
"endIndex": 4,
"data": ["tag"]
},
{
"event": "opentag",
"startIndex": 0,
"endIndex": 4,
"data": ["tag", {}]
},
{
"event": "cdatastart",
"startIndex": 5,
"endIndex": 41,
"data": []
},
{
Expand All @@ -23,14 +31,20 @@
},
{
"event": "cdataend",
"startIndex": 5,
"endIndex": 41,
"data": []
},
{
"event": "closetag",
"startIndex": 42,
"endIndex": 47,
"data": ["tag"]
},
{
"event": "processinginstruction",
"startIndex": 48,
"endIndex": 53,
"data": ["![CD", "![CD"]
}
]
Expand Down
8 changes: 7 additions & 1 deletion src/__fixtures__/Events/05-cdata-special.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"name": "CDATA (inside special)",
"html": "<script>/*<![CDATA[*/ asdf ><asdf></adsf><> fo/*]]>*/</script>",
"input": "<script>/*<![CDATA[*/ asdf ><asdf></adsf><> fo/*]]>*/</script>",
"expected": [
{
"event": "opentagname",
"startIndex": 0,
"endIndex": 7,
"data": ["script"]
},
{
"event": "opentag",
"startIndex": 0,
"endIndex": 7,
"data": ["script", {}]
},
{
Expand All @@ -16,6 +20,8 @@
},
{
"event": "closetag",
"startIndex": 53,
"endIndex": 61,
"data": ["script"]
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/__fixtures__/Events/06-leading-lt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leading lt",
"html": ">a>",
"input": ">a>",
"expected": [
{
"event": "text",
Expand Down
14 changes: 13 additions & 1 deletion src/__fixtures__/Events/07-self-closing.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "Self-closing tags",
"html": "<a href=http://test.com/>Foo</a><hr / >",
"input": "<a href=http://test.com/>Foo</a><hr / >",
"expected": [
{
"event": "opentagname",
"startIndex": 0,
"endIndex": 2,
"data": ["a"]
},
{
Expand All @@ -12,6 +14,8 @@
},
{
"event": "opentag",
"startIndex": 0,
"endIndex": 24,
"data": [
"a",
{
Expand All @@ -25,18 +29,26 @@
},
{
"event": "closetag",
"startIndex": 28,
"endIndex": 31,
"data": ["a"]
},
{
"event": "opentagname",
"startIndex": 32,
"endIndex": 35,
"data": ["hr"]
},
{
"event": "opentag",
"startIndex": 32,
"endIndex": 38,
"data": ["hr", {}]
},
{
"event": "closetag",
"startIndex": 32,
"endIndex": 38,
"data": ["hr"]
}
]
Expand Down
Loading

0 comments on commit 4e25252

Please sign in to comment.