Skip to content

Commit bb5fcc0

Browse files
committed
fix: handle selector combinators surrounded by whitespace
Partial fix for #25
1 parent 6a62cd4 commit bb5fcc0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lib/syntax_tree/css/selectors.rb

+4
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ def relative_selector_list
337337

338338
# <complex-selector> = <compound-selector> [ <combinator>? <compound-selector> ]*
339339
def complex_selector
340+
consume_whitespace
341+
340342
left = compound_selector
341343

342344
if (c = maybe { combinator })
@@ -396,6 +398,8 @@ def simple_selector
396398

397399
# <combinator> = '>' | '+' | '~' | [ '|' '|' ]
398400
def combinator
401+
consume_whitespace
402+
399403
value =
400404
options do
401405
maybe { consume(">") } ||

test/selectors_test.rb

+46
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,52 @@ class SelectorsTest < Minitest::Spec
9797
end
9898
end
9999

100+
it "parses a complex selector with whitespace" do
101+
actual = parse_selectors("section > table")
102+
103+
assert_pattern do
104+
actual => [
105+
Selectors::ComplexSelector[
106+
left: Selectors::TypeSelector[value: { name: { value: "section" } }],
107+
combinator: { value: { value: ">" } },
108+
right: Selectors::TypeSelector[value: { name: { value: "table" } }],
109+
]
110+
]
111+
end
112+
end
113+
114+
it "parses a complex selector with implicit descendant combinator" do
115+
actual = parse_selectors("section table")
116+
117+
assert_pattern do
118+
actual => [
119+
Selectors::ComplexSelector[
120+
left: Selectors::TypeSelector[value: { name: { value: "section" } }],
121+
combinator: nil,
122+
right: Selectors::TypeSelector[value: { name: { value: "table" } }],
123+
]
124+
]
125+
end
126+
end
127+
128+
it "parses a complex complex selector" do
129+
actual = parse_selectors("section > table tr")
130+
131+
assert_pattern do
132+
actual => [
133+
Selectors::ComplexSelector[
134+
left: Selectors::TypeSelector[value: { name: { value: "section" } }],
135+
combinator: { value: { value: ">" } },
136+
right: Selectors::ComplexSelector[
137+
left: Selectors::TypeSelector[value: { name: { value: "table" } }],
138+
combinator: nil,
139+
right: Selectors::TypeSelector[value: { name: { value: "tr" } }]
140+
]
141+
]
142+
]
143+
end
144+
end
145+
100146
private
101147

102148
def parse_selectors(selectors)

0 commit comments

Comments
 (0)