Skip to content

Commit a2eae51

Browse files
authored
refactor(tokenizer): Remove unused branches, improve test coverage (#914)
`Tokenizer`'s `option` argument can no longer be `null`, and we no longer check for unknown states.
1 parent 04c411c commit a2eae51

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

src/Tokenizer.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,15 @@ export default class Tokenizer {
291291
private readonly entityTrie: Uint16Array;
292292

293293
constructor(
294-
options: { xmlMode?: boolean; decodeEntities?: boolean } | null,
294+
{
295+
xmlMode = false,
296+
decodeEntities = true,
297+
}: { xmlMode?: boolean; decodeEntities?: boolean },
295298
private readonly cbs: Callbacks
296299
) {
297-
this.cbs = cbs;
298-
this.xmlMode = !!options?.xmlMode;
299-
this.decodeEntities = options?.decodeEntities ?? true;
300-
this.entityTrie = this.xmlMode ? xmlDecodeTree : htmlDecodeTree;
300+
this.xmlMode = xmlMode;
301+
this.decodeEntities = decodeEntities;
302+
this.entityTrie = xmlMode ? xmlDecodeTree : htmlDecodeTree;
301303
}
302304

303305
public reset(): void {
@@ -314,7 +316,8 @@ export default class Tokenizer {
314316

315317
public write(chunk: string): void {
316318
if (this.ended) this.cbs.onerror(Error(".write() after done!"));
317-
this.buffer += chunk;
319+
if (this.buffer.length) this.buffer += chunk;
320+
else this.buffer = chunk;
318321
this.parse();
319322
}
320323

@@ -777,7 +780,7 @@ export default class Tokenizer {
777780
const entity = this.buffer.substring(sectionStart, this._index);
778781
const parsed = parseInt(entity, base);
779782
this.emitPartial(decodeCodePoint(parsed));
780-
this.sectionStart = strict ? this._index + 1 : this._index;
783+
this.sectionStart = this._index + Number(strict);
781784
}
782785
this._state = this.baseState;
783786
}
@@ -980,11 +983,9 @@ export default class Tokenizer {
980983
this.stateInHexEntity(c);
981984
} else if (this._state === State.InNumericEntity) {
982985
this.stateInNumericEntity(c);
983-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
984-
} else if (this._state === State.BeforeNumericEntity) {
985-
stateBeforeNumericEntity(this, c);
986986
} else {
987-
this.cbs.onerror(Error("unknown _state"), this._state);
987+
// `this._state === State.BeforeNumericEntity`
988+
stateBeforeNumericEntity(this, c);
988989
}
989990
this._index++;
990991
}
@@ -1023,16 +1024,10 @@ export default class Tokenizer {
10231024
}
10241025
} else if (this._state === State.InNumericEntity && !this.xmlMode) {
10251026
this.decodeNumericEntity(10, false);
1026-
if (this.sectionStart < this._index) {
1027-
this._state = this.baseState;
1028-
this.handleTrailingData();
1029-
}
1027+
// All trailing data will have been consumed
10301028
} else if (this._state === State.InHexEntity && !this.xmlMode) {
10311029
this.decodeNumericEntity(16, false);
1032-
if (this.sectionStart < this._index) {
1033-
this._state = this.baseState;
1034-
this.handleTrailingData();
1035-
}
1030+
// All trailing data will have been consumed
10361031
} else if (
10371032
this._state !== State.InTagName &&
10381033
this._state !== State.BeforeAttributeName &&
@@ -1047,7 +1042,6 @@ export default class Tokenizer {
10471042
this.cbs.ontext(data);
10481043
}
10491044
/*
1050-
* Else, ignore remaining data
10511045
* TODO add a way to remove current tag
10521046
*/
10531047
}
@@ -1057,7 +1051,7 @@ export default class Tokenizer {
10571051
}
10581052
private emitPartial(value: string) {
10591053
if (this.baseState !== State.Text) {
1060-
this.cbs.onattribdata(value); // TODO implement the new event
1054+
this.cbs.onattribdata(value);
10611055
} else {
10621056
this.cbs.ontext(value);
10631057
}

src/__fixtures__/Events/37-entity-in-title.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "entity in title (#592)",
3-
"html": "<title>the &quot;title&quot;",
3+
"html": "<title>the &quot;title&quot",
44
"expected": [
55
{
66
"event": "opentagname",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Trailing legacy entity",
3+
"html": "&timesbar;&timesbar",
4+
"expected": [
5+
{
6+
"data": ["⨱×bar"],
7+
"event": "text"
8+
}
9+
]
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Trailing numeric entity",
3+
"html": "&#53&#53",
4+
"expected": [
5+
{
6+
"data": ["55"],
7+
"event": "text"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)