Skip to content

Commit

Permalink
fix(tokenizer): Don't process data after error (#923)
Browse files Browse the repository at this point in the history
Also add a test for multi-byte entities, and change the Jest coverage provider to V8.
  • Loading branch information
fb55 committed Aug 24, 2021
1 parent 58ee36c commit 08b2040
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -67,7 +67,8 @@
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
"testEnvironment": "node",
"coverageProvider": "v8"
},
"prettier": {
"tabWidth": 4
Expand Down
4 changes: 2 additions & 2 deletions src/Tokenizer.ts
Expand Up @@ -315,14 +315,14 @@ export default class Tokenizer {
}

public write(chunk: string): void {
if (this.ended) this.cbs.onerror(Error(".write() after done!"));
if (this.ended) return this.cbs.onerror(Error(".write() after done!"));
if (this.buffer.length) this.buffer += chunk;
else this.buffer = chunk;
this.parse();
}

public end(chunk?: string): void {
if (this.ended) this.cbs.onerror(Error(".end() after done!"));
if (this.ended) return this.cbs.onerror(Error(".end() after done!"));
if (chunk) this.write(chunk);
this.ended = true;
if (this.running) this.finish();
Expand Down
10 changes: 10 additions & 0 deletions src/__fixtures__/Events/43-multibyte-entity.json
@@ -0,0 +1,10 @@
{
"name": "Multi-byte entity",
"html": "≧̸",
"expected": [
{
"data": ["≧̸"],
"event": "text"
}
]
}
17 changes: 17 additions & 0 deletions src/__tests__/events.ts
@@ -1,3 +1,4 @@
import { Parser } from "..";
import * as helper from "../__fixtures__/test-helper";

helper.createSuite("Events", (test, cb) =>
Expand All @@ -7,3 +8,19 @@ helper.createSuite("Events", (test, cb) =>
test.html
)
);

describe("Helper", () => {
it("should handle errors", () => {
const eventCb = jest.fn();
const parser = new Parser(helper.getEventCollector(eventCb));

parser.end();
parser.write("foo");

expect(eventCb).toHaveBeenCalledTimes(2);
expect(eventCb).toHaveBeenNthCalledWith(1, null, []);
expect(eventCb).toHaveBeenLastCalledWith(
new Error(".write() after done!")
);
});
});

0 comments on commit 08b2040

Please sign in to comment.