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
21 changes: 21 additions & 0 deletions src/Tokenizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ describe("Tokenizer", () => {

it("for multi-byte entities", () =>
expect(tokenize("≧̸")).toMatchSnapshot());

it("for text and entities split across chunks", () =>
expect(
tokenize((tokenizer) => {
tokenizer.write("one & two & three");
tokenizer.write(" four<b>five</b>six &lt");
tokenizer.end();
}),
).toEqual([
["ontext", 0, 10],
["ontextentity", 38, 15],
["ontext", 15, 21],
["ontext", 21, 26],
["onopentagname", 27, 28],
["onopentagend", 28],
["ontext", 29, 33],
["onclosetag", 35, 36],
["ontext", 37, 41],
["ontextentity", 60, 44],
["onend"],
]));
});

it("should close comments on --!>", () => {
Expand Down
16 changes: 14 additions & 2 deletions src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,20 @@ export default class Tokenizer {
}
this.state = State.BeforeTagName;
this.sectionStart = this.index;
} else if (this.decodeEntities && c === CharCodes.Amp) {
this.startEntity();
} else if (this.decodeEntities) {
if (c === CharCodes.Amp) {
this.startEntity();
return;
}
// Skip to the next `<` or `&`, or the end of the buffer.
const { buffer, offset } = this;
let index = this.index - offset + 1;
while (index < buffer.length) {
const next = buffer.charCodeAt(index);
if (next === CharCodes.Lt || next === CharCodes.Amp) break;
index++;
}
this.index = index + offset - 1;
}
}

Expand Down