From 9501ba032222ee8529413d8d034b144708ea47a1 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sat, 2 Apr 2022 16:20:40 -0400 Subject: [PATCH] test: add coverage for some css pseudo-classes - :gt() - :contains() --- test/css/test_css_integration.rb | 12 ++++++++++-- test/css/test_xpath_visitor.rb | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/test/css/test_css_integration.rb b/test/css/test_css_integration.rb index c8a324206f1..a7e04663e78 100644 --- a/test/css/test_css_integration.rb +++ b/test/css/test_css_integration.rb @@ -6,7 +6,7 @@ class TestNokogiriCssIntegration < Nokogiri::TestCase describe "CSS integration tests" do let(:subject) do subject_class.parse(<<~HTML) - + @@ -64,7 +64,7 @@ class TestNokogiriCssIntegration < Nokogiri::TestCase

- + HTML end @@ -343,6 +343,14 @@ def assert_result_rows(intarray, result, word = "row") assert_equal(expected, result.to_a) end + it "selects using contains" do + assert_equal(14, subject.css("td:contains('row')").length) + assert_equal(6, subject.css("td:contains('row1')").length) + assert_equal(4, subject.css("h1:contains('header')").length) + assert_equal(4, subject.css("div :contains('header')").length) + assert_equal(9, subject.css(":contains('header')").length) # 9 = 4xh1 + 3xdiv + body + html + end + it "selects class_attr_selector" do doc = subject_class.parse(<<~HTML) diff --git a/test/css/test_xpath_visitor.rb b/test/css/test_xpath_visitor.rb index d4cb4b1923b..f8c377243fb 100644 --- a/test/css/test_xpath_visitor.rb +++ b/test/css/test_xpath_visitor.rb @@ -383,6 +383,21 @@ def assert_xpath(expecteds, asts) assert_xpath("//script//comment()", parser.parse("script comment()")) end + it "handles contains() (non-standard)" do + # https://api.jquery.com/contains-selector/ + assert_xpath(%{//div[contains(.,"youtube")]}, parser.parse(%{div:contains("youtube")})) + end + + it "handles gt() (non-standard)" do + # https://api.jquery.com/gt-selector/ + assert_xpath("//td[position()>3]", parser.parse("td:gt(3)")) + end + + it "handles self()" do + # TODO: it's unclear how this is useful and we should consider deprecating it + assert_xpath("//self::div", parser.parse("self(div)")) + end + it "supports custom functions" do visitor = Class.new(Nokogiri::CSS::XPathVisitor) do attr_accessor :awesome
row1
row2