From 8f01e8f12a82cb75c6e50f70c1bfa81dbd265972 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Mon, 22 Apr 2019 16:37:32 -0400 Subject: [PATCH] test: add ad-hoc tests for CSS `:has` pseudoselector --- test/css/test_css_integration.rb | 122 +++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/test/css/test_css_integration.rb b/test/css/test_css_integration.rb index 115b320023..948f29e153 100644 --- a/test/css/test_css_integration.rb +++ b/test/css/test_css_integration.rb @@ -67,6 +67,34 @@ def setup EOF @parser = Nokogiri.HTML doc + + @nested = Nokogiri.HTML(<<~EOF) + +
+ bold1 + italic1 + bold2 + emphasis1 + italic2 +

para1

+
+ +
+
+ bold1 + italic1 + bold2 + emphasis1 + italic2 +
+
+ +
+ nerrrrp + nerp + also nerp +
+ EOF end def test_even @@ -216,6 +244,100 @@ def test_siblings assert_equal 0, parser.search("#5 + p").size end + def test_has_a + result = @nested.css("div:has(em)") + expected = [ + @nested.at_css(".unnested"), + @nested.at_css(".nested-parent"), + @nested.at_css(".nested-child"), + ] + assert_equal expected, result.to_a + end + + def test_has_a_gt_b + result = @nested.css("body *:has(div > em)") + expected = [ + @nested.at_css(".nested-parent"), + ] + assert_equal expected, result.to_a + end + + def test_has_gt_b + result = @nested.css("body *:has(> em)") + expected = [ + @nested.at_css(".unnested"), + @nested.at_css(".nested-child"), + ] + assert_equal expected, result.to_a + end + + def test_has_a_plus_b + result = @nested.css("div:has(b + em)") + expected = [ + @nested.at_css(".unnested"), + @nested.at_css(".nested-parent"), + @nested.at_css(".nested-child"), + ] + assert_equal expected, result.to_a + end + + def test_has_plus_b + result = @nested.css("i:has(+ b)") + expected = [ + @nested.css(".unnested i")[0], + @nested.css(".nested-parent i")[0], + ] + assert_equal expected, result.to_a + + result = @nested.css("a:has(+ b)") + expected = [ + @nested.at_css(".nerp a"), + ] + 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"), + ] + assert_equal expected, result.to_a + + result = @nested.css("div:has(b ~ i)") + expected = [ + @nested.at_css(".unnested"), + @nested.at_css(".nested-parent"), + @nested.at_css(".nested-child"), + ] + assert_equal expected, result.to_a + + result = @nested.css("div:has(i ~ b)") + expected = [ + @nested.at_css(".unnested"), + @nested.at_css(".nested-parent"), + @nested.at_css(".nested-child"), + @nested.at_css(".nerp"), + ] + assert_equal expected, result.to_a + end + + def test_has_tilde_b + result = @nested.css("b:has(~ p)") + expected = [ + @nested.css(".unnested b").to_a, + ].flatten + assert_equal expected, result.to_a + + result = @nested.css("i:has(~ i)") + expected = [ + @nested.css(".unnested i")[0], + @nested.css(".nested-child i")[0], + ] + 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}'"