diff --git a/test/css/test_css_integration.rb b/test/css/test_css_integration.rb index 115b320023e..129b769ed9d 100644 --- a/test/css/test_css_integration.rb +++ b/test/css/test_css_integration.rb @@ -67,6 +67,43 @@ def setup EOF @parser = Nokogiri.HTML doc + + @nested = Nokogiri.HTML(<<~EOF) + +
+ bold +

para

+
+ +
+ bold + ... + ... +

para

+
+ +
+
+ bold +

para

+
+ +
+ bold + ... + ... +

para

+
+
+ +
+ bold +
+ +
+

para

+
+ EOF end def test_even @@ -216,6 +253,83 @@ def test_siblings assert_equal 0, parser.search("#5 + p").size end + def test_has_a + result = @nested.css("div:has(b)") + expected = [ + @nested.at_css(".unnested.direct"), + @nested.at_css(".unnested.indirect"), + @nested.at_css(".nested-parent"), + @nested.at_css(".nested-child.direct"), + @nested.at_css(".nested-child.indirect"), + @nested.at_css(".has-bold"), + ] + assert_equal expected, result.to_a + end + + def test_has_a_gt_b + result = @nested.css("body *:has(div > b)") + expected = [ + @nested.at_css(".nested-parent"), + ] + assert_equal expected, result.to_a + end + + def test_has_gt_b + result = @nested.css("body *:has(> b)") + expected = [ + @nested.at_css(".unnested.direct"), + @nested.at_css(".unnested.indirect"), + @nested.at_css(".nested-child.direct"), + @nested.at_css(".nested-child.indirect"), + @nested.at_css(".has-bold"), + ] + assert_equal expected, result.to_a + end + + def test_has_a_plus_b + result = @nested.css("div:has(b + p)") + expected = [ + @nested.at_css(".unnested.direct"), + @nested.at_css(".nested-parent"), + @nested.at_css(".nested-child.direct"), + ] + assert_equal expected, result.to_a + end + + def test_has_plus_b + result = @nested.css("b:has(+ p)") + expected = [ + @nested.at_css(".unnested.direct b"), + @nested.at_css(".nested-child.direct b"), + ] + assert_equal expected, result.to_a + end + + def test_has_a_tilde_b + result = @nested.css("div:has(b ~ p)") + expected = [ + @nested.at_css(".unnested.direct"), + @nested.at_css(".unnested.indirect"), + @nested.at_css(".nested-parent"), + @nested.at_css(".nested-child.direct"), + @nested.at_css(".nested-child.indirect"), + ] + assert_equal expected, result.to_a + end + + def test_has_tilde_b + result = @nested.css("b:has(~ p)") + expected = [ + @nested.at_css(".unnested.direct b"), + @nested.at_css(".unnested.indirect b"), + @nested.at_css(".nested-child.direct b"), + @nested.at_css(".nested-child.indirect b"), + ].flatten + assert_equal expected, result.to_a + end + + private + def assert_result_rows(intarray, result, word = "row") assert_equal intarray.size, result.size, "unexpected number of rows returned: '#{result.inner_text}'"