Skip to content

Commit

Permalink
fix: Update end index for text events (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Aug 29, 2021
1 parent be1e9ba commit d88e5b0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 63 deletions.
126 changes: 82 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@typescript-eslint/parser": "^4.29.3",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.1.0",
"htmlparser2": "^7.0.0",
"htmlparser2": "^7.1.1",
"jest": "^27.1.0",
"prettier": "^2.0.5",
"ts-jest": "^27.0.5",
Expand Down
1 change: 0 additions & 1 deletion src/__fixtures__/24-with-start-indices.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "withStartIndices adds correct startIndex properties",
"options": { "withStartIndices": true },
"streaming": false,
"html": "<!DOCTYPE html> <html> <title>The Title</title> <body class='foo'>Hello world <p></p></body> <!-- the comment --> </html> ",
"expected": [
{
Expand Down
1 change: 0 additions & 1 deletion src/__fixtures__/25-with-end-indices.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "withEndIndices adds correct endIndex properties",
"options": { "withEndIndices": true },
"streaming": false,
"html": "<!DOCTYPE html> <html> <title>The Title</title> <body class='foo'>Hello world <p></p></body> <!-- the comment --> </html> ",
"expected": [
{
Expand Down
23 changes: 9 additions & 14 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ describe("DomHandler", () => {
.filter((name) => name.endsWith(".json")) // Only allow .json files
.map((name) => resolve(basePath, name))
.map(require)
.forEach(({ name, html, options = {}, streaming = true, expected }) =>
.forEach(({ name, html, options = {}, expected }) =>
test(name, () => {
const result = parse(html, options, streaming);
const result = parse(html, options);

compare(result, expected);
})
Expand All @@ -21,8 +21,7 @@ describe("DomHandler", () => {

function parse(
data: string,
options: DomHandlerOptions & ParserOptions,
streaming: boolean
options: DomHandlerOptions & ParserOptions
): Node[] {
const results: Node[][] = [];

Expand All @@ -34,21 +33,17 @@ function parse(
const parser = new Parser(handler, options);

// First, try to run the fixture via chunks
if (streaming) {
for (let i = 0; i < data.length; i++) {
parser.write(data.charAt(i));
}

parser.done();
for (let i = 0; i < data.length; i++) {
parser.write(data.charAt(i));
}

parser.done();

// Then parse everything
parser.parseComplete(data);

if (streaming) {
// Ensure streaming doesn't change anything.
expect(results[0]).toEqual(results[1]);
}
// Ensure streaming doesn't change anything.
expect(results[0]).toEqual(results[1]);

return results[0];
}
Expand Down

0 comments on commit d88e5b0

Please sign in to comment.