Skip to content

Commit 5b4571f

Browse files
committed
Parse arguments for pseudo-class functions
so that the AST doesn't just contain tokens. Previously the arguments were something like ``` [(ident-token "div"), (comma-token), (whitespace-token " "), (ident-token "span"), (delim-token "."), (ident-token "wide"), (comma-token), (whitespace-token " "), (delim-token "."), (ident-token "hidden")] ``` Now those tokens will be represented in the AST as ``` [(type-selector (wqname (ident-token "div"))), (compound-selector (type (type-selector (wqname (ident-token "span")))) (subclasses (class-selector (ident-token "wide")) ) (pseudo-elements) ), (class-selector (ident-token "hidden"))] ```
1 parent c152737 commit 5b4571f

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/syntax_tree/css/selectors.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ def pseudo_class_selector
509509
PseudoClassSelector.new(value: consume(IdentToken))
510510
in Function
511511
node = consume(Function)
512-
function = PseudoClassFunction.new(name: node.name, arguments: node.value)
512+
arguments = Selectors.new(node.value).parse
513+
function = PseudoClassFunction.new(name: node.name, arguments: arguments)
513514
PseudoClassSelector.new(value: function)
514515
else
515516
raise MissingTokenError, "Expected pseudo class selector to produce something"

test/selectors_test.rb

+28-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class SelectorsTest < Minitest::Spec
8282
end
8383
end
8484

85-
it "parses a compound selector with a pseudo-class" do
85+
it "parses a compound selector with a pseudo-class selector" do
8686
actual = parse_selectors("div.flex:hover")
8787

8888
assert_pattern do
@@ -98,6 +98,33 @@ class SelectorsTest < Minitest::Spec
9898
end
9999
end
100100

101+
it "parses a compound selector with a pseudo-class function" do
102+
actual = parse_selectors(".flex:not(div, span.wide, .hidden)")
103+
104+
assert_pattern do
105+
actual => [
106+
Selectors::CompoundSelector[
107+
type: nil,
108+
subclasses: [
109+
Selectors::ClassSelector[value: { value: "flex" }],
110+
Selectors::PseudoClassSelector[
111+
value: Selectors::PseudoClassFunction[
112+
name: "not",
113+
arguments: [
114+
Selectors::TypeSelector[value: { name: { value: "div" } }],
115+
Selectors::CompoundSelector[
116+
Selectors::TypeSelector[value: { name: { value: "span" } }],
117+
Selectors::ClassSelector[value: { value: "wide" }],
118+
],
119+
],
120+
],
121+
],
122+
],
123+
]
124+
]
125+
end
126+
end
127+
101128
it "parses a compound selector with pseudo-elements and pseudo-classes" do
102129
actual = parse_selectors("div.flex:hover::first-line:last-child:active::first-letter")
103130

@@ -208,7 +235,6 @@ class SelectorsTest < Minitest::Spec
208235
]
209236
end
210237
end
211-
212238
end
213239

214240
describe "formatting" do

0 commit comments

Comments
 (0)