Skip to content

Commit 8030f67

Browse files
committed
feat: Support parsing column combinators
1 parent a812a1c commit 8030f67

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

src/__fixtures__/tests.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1016,4 +1016,25 @@ export const tests: [
10161016
],
10171017
"case-sensitive attribute selector",
10181018
],
1019+
[
1020+
"foo || bar",
1021+
[
1022+
[
1023+
{
1024+
name: "foo",
1025+
namespace: null,
1026+
type: SelectorType.Tag,
1027+
},
1028+
{
1029+
type: SelectorType.ColumnCombinator,
1030+
},
1031+
{
1032+
name: "bar",
1033+
namespace: null,
1034+
type: SelectorType.Tag,
1035+
},
1036+
],
1037+
],
1038+
"column combinator",
1039+
],
10191040
];

src/parse.ts

+9
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export function isTraversal(selector: Selector): selector is Traversal {
137137
case SelectorType.Descendant:
138138
case SelectorType.Parent:
139139
case SelectorType.Sibling:
140+
case SelectorType.ColumnCombinator:
140141
return true;
141142
default:
142143
return false;
@@ -616,6 +617,14 @@ function parseSelector(
616617
name = "*";
617618
} else if (firstChar === CharCode.Pipe) {
618619
name = "";
620+
621+
if (
622+
selector.charCodeAt(selectorIndex + 1) === CharCode.Pipe
623+
) {
624+
addTraversal(SelectorType.ColumnCombinator);
625+
stripWhitespace(2);
626+
break;
627+
}
619628
} else if (reName.test(selector.slice(selectorIndex))) {
620629
name = getName(0);
621630
} else {

src/stringify.ts

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ function stringifyToken(token: Selector): string {
3232
return " + ";
3333
case SelectorType.Descendant:
3434
return " ";
35+
case SelectorType.ColumnCombinator:
36+
return " || ";
3537
case SelectorType.Universal:
3638
return `${getNamespace(token.namespace)}*`;
3739

src/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export enum SelectorType {
4242
Descendant = "descendant",
4343
Parent = "parent",
4444
Sibling = "sibling",
45+
ColumnCombinator = "column-combinator",
4546
}
4647

4748
export interface AttributeSelector {
@@ -97,4 +98,5 @@ export type TraversalType =
9798
| SelectorType.Child
9899
| SelectorType.Descendant
99100
| SelectorType.Parent
100-
| SelectorType.Sibling;
101+
| SelectorType.Sibling
102+
| SelectorType.ColumnCombinator;

0 commit comments

Comments
 (0)