Skip to content

Commit

Permalink
fix(parse): Throw errors when encountering multiple traversals in a row
Browse files Browse the repository at this point in the history
Eg. `<>` is no longer a valid selector
  • Loading branch information
fb55 committed Oct 12, 2020
1 parent 8f64671 commit f07d582
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,26 @@ describe("Collected selectors", () => {
}
});
});

const broken = [
"[",
"(",
"{",
"()",
"<>",
"{}",
",",
",a",
"a,",
"[id=012345678901234567890123456789",
"input[name=foo.baz]",
"input[name=foo[baz]]",
];

describe("Broken selectors", () => {
for (const selector of broken) {
it(`should not parse — ${selector}`, () => {
expect(() => parse(selector)).toThrow(Error);
});
}
});
8 changes: 8 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ function parseSelector(
return (slashCount & 1) === 1;
}

function ensureNotTraversal() {
if (tokens.length > 0 && isTraversal(tokens[tokens.length - 1])) {
throw new Error("Did not expect successive traversals.");
}
}

stripWhitespace(0);

while (selector !== "") {
Expand All @@ -220,6 +226,7 @@ function parseSelector(
sawWS = true;
stripWhitespace(1);
} else if (firstChar in Traversals) {
ensureNotTraversal();
tokens.push({ type: Traversals[firstChar] });
sawWS = false;

Expand All @@ -235,6 +242,7 @@ function parseSelector(
} else {
if (sawWS) {
if (tokens.length > 0) {
ensureNotTraversal();
tokens.push({ type: "descendant" });
}
sawWS = false;
Expand Down

0 comments on commit f07d582

Please sign in to comment.