Skip to content

Commit

Permalink
feat(parse): Ignore comments around selectors (#441)
Browse files Browse the repository at this point in the history
Fixes #391
  • Loading branch information
fb55 authored Mar 8, 2021
1 parent 4524985 commit fc91ba1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"build": "tsc",
"prepare": "npm run build"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.3",
"@types/node": "^14.0.5",
Expand Down
11 changes: 11 additions & 0 deletions src/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@ describe("Broken selectors", () => {
expect(() => parse(selector)).toThrow(Error);
});
}

it("should ignore comments", () => {
expect(parse("/* comment1 */ /**/ foo /*comment2*/")).toEqual([
[
{ type: "descendant" },
{ name: "foo", namespace: null, type: "tag" },
],
]);

expect(() => parse("/*/")).toThrowError("Comment was not terminated");
});
});
11 changes: 11 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ function parseSelector(
tokens = [];
sawWS = false;
stripWhitespace(1);
} else if (
firstChar === "/" &&
selector.charAt(selectorIndex + 1) === "*"
) {
const endIndex = selector.indexOf("*/", selectorIndex + 2);

if (endIndex < 0) {
throw new Error("Comment was not terminated");
}

selectorIndex = endIndex + 2;
} else {
if (sawWS) {
ensureNotTraversal();
Expand Down

0 comments on commit fc91ba1

Please sign in to comment.