Skip to content

Commit

Permalink
feat: Support parsing column combinators
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Dec 25, 2021
1 parent a812a1c commit 8030f67
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/__fixtures__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,4 +1016,25 @@ export const tests: [
],
"case-sensitive attribute selector",
],
[
"foo || bar",
[
[
{
name: "foo",
namespace: null,
type: SelectorType.Tag,
},
{
type: SelectorType.ColumnCombinator,
},
{
name: "bar",
namespace: null,
type: SelectorType.Tag,
},
],
],
"column combinator",
],
];
9 changes: 9 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export function isTraversal(selector: Selector): selector is Traversal {
case SelectorType.Descendant:
case SelectorType.Parent:
case SelectorType.Sibling:
case SelectorType.ColumnCombinator:
return true;
default:
return false;
Expand Down Expand Up @@ -616,6 +617,14 @@ function parseSelector(
name = "*";
} else if (firstChar === CharCode.Pipe) {
name = "";

if (
selector.charCodeAt(selectorIndex + 1) === CharCode.Pipe
) {
addTraversal(SelectorType.ColumnCombinator);
stripWhitespace(2);
break;
}
} else if (reName.test(selector.slice(selectorIndex))) {
name = getName(0);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function stringifyToken(token: Selector): string {
return " + ";
case SelectorType.Descendant:
return " ";
case SelectorType.ColumnCombinator:
return " || ";
case SelectorType.Universal:
return `${getNamespace(token.namespace)}*`;

Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export enum SelectorType {
Descendant = "descendant",
Parent = "parent",
Sibling = "sibling",
ColumnCombinator = "column-combinator",
}

export interface AttributeSelector {
Expand Down Expand Up @@ -97,4 +98,5 @@ export type TraversalType =
| SelectorType.Child
| SelectorType.Descendant
| SelectorType.Parent
| SelectorType.Sibling;
| SelectorType.Sibling
| SelectorType.ColumnCombinator;

0 comments on commit 8030f67

Please sign in to comment.