Skip to content

Commit

Permalink
fix(decode): Handle chunks for numeric entities (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Apr 13, 2023
1 parent 9cad46b commit d8b550f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/decode.spec.ts
Expand Up @@ -144,6 +144,19 @@ describe("EntityDecoder", () => {
expect(cb).toHaveBeenCalledWith(":".charCodeAt(0), 6);
});

it("should decode hex entities across several chunks", () => {
const cb = jest.fn();
const decoder = new entities.EntityDecoder(entities.htmlDecodeTree, cb);

for (const chunk of ["#x", "cf", "ff", "d"]) {
expect(decoder.write(chunk, 0)).toBe(-1);
}

expect(decoder.write(";", 0)).toBe(9);
expect(cb).toHaveBeenCalledTimes(1);
expect(cb).toHaveBeenCalledWith(0xcfffd, 9);
});

it("should not fail if nothing is written", () => {
const cb = jest.fn();
const decoder = new entities.EntityDecoder(entities.htmlDecodeTree, cb);
Expand Down
6 changes: 4 additions & 2 deletions src/decode.ts
Expand Up @@ -212,9 +212,11 @@ export class EntityDecoder {
base: number
): void {
if (start !== end) {
const digitCount = end - start;
this.result =
this.result * base + parseInt(str.slice(start, end), base);
this.consumed += end - start;
this.result * Math.pow(base, digitCount) +
parseInt(str.substr(start, digitCount), base);
this.consumed += digitCount;
}
}

Expand Down

0 comments on commit d8b550f

Please sign in to comment.