Skip to content

Commit 6a62cd4

Browse files
committed
fix: parse Selectors::ComplexSelectors properly
- avoid clobbering the combinator method with a local variable - return a recursive tree of complex selectors Partial fix for #25
1 parent 1a50f58 commit 6a62cd4

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

lib/syntax_tree/css/selectors.rb

+8-14
Original file line numberDiff line numberDiff line change
@@ -339,25 +339,19 @@ def relative_selector_list
339339
def complex_selector
340340
left = compound_selector
341341

342-
loop do
343-
if (combinator = maybe { combinator })
344-
ComplexSelector.new(left: left, combinator: combinator, right: compound_selector)
345-
elsif (right = maybe { compound_selector })
346-
ComplexSelector.new(left: left, combinator: nil, right: right)
347-
else
348-
break
349-
end
342+
if (c = maybe { combinator })
343+
ComplexSelector.new(left: left, combinator: c, right: complex_selector)
344+
elsif (right = maybe { complex_selector })
345+
ComplexSelector.new(left: left, combinator: nil, right: right)
346+
else
347+
left
350348
end
351-
352-
left
353349
end
354350

355351
# <relative-selector> = <combinator>? <complex-selector>
356352
def relative_selector
357-
combinator = maybe { combinator }
358-
359-
if combinator
360-
RelativeSelector.new(combinator: combinator, complex_selector: complex_selector)
353+
if (c = maybe { combinator })
354+
RelativeSelector.new(combinator: c, complex_selector: complex_selector)
361355
else
362356
complex_selector
363357
end

test/selectors_test.rb

+32
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,38 @@ class SelectorsTest < Minitest::Spec
6565
end
6666
end
6767

68+
it "parses a complex selector" do
69+
actual = parse_selectors("section>table")
70+
71+
assert_pattern do
72+
actual => [
73+
Selectors::ComplexSelector[
74+
left: Selectors::TypeSelector[value: { name: { value: "section" } }],
75+
combinator: { value: { value: ">" } },
76+
right: Selectors::TypeSelector[value: { name: { value: "table" } }]
77+
]
78+
]
79+
end
80+
end
81+
82+
it "parses a complex selector with many selectors" do
83+
actual = parse_selectors("section>table>tr")
84+
85+
assert_pattern do
86+
actual => [
87+
Selectors::ComplexSelector[
88+
left: Selectors::TypeSelector[value: { name: { value: "section" } }],
89+
combinator: { value: { value: ">" } },
90+
right: Selectors::ComplexSelector[
91+
left: Selectors::TypeSelector[value: { name: { value: "table" } }],
92+
combinator: { value: { value: ">" } },
93+
right: Selectors::TypeSelector[value: { name: { value: "tr" } }]
94+
]
95+
]
96+
]
97+
end
98+
end
99+
68100
private
69101

70102
def parse_selectors(selectors)

0 commit comments

Comments
 (0)