Skip to content

Commit

Permalink
prefactor: restructure css-to-xpath tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Jan 19, 2025
1 parent 3b28b49 commit b112e18
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions test/css/test_xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,53 +143,61 @@ def visit_pseudo_class_aaron(node)
)
end

it "# id" do
assert_xpath("//*[@id='foo']", "#foo")
assert_xpath("//*[@id='escape:needed,']", "#escape\\:needed\\,")
assert_xpath("//*[@id='escape:needed,']", '#escape\3Aneeded\,')
assert_xpath("//*[@id='escape:needed,']", '#escape\3A needed\2C')
assert_xpath("//*[@id='escape:needed']", '#escape\00003Aneeded')
end

describe "attribute" do
it "basic mechanics" do
assert_xpath("//h1[@a='Tender Lovemaking']", "h1[a='Tender Lovemaking']")
assert_xpath("//h1[@a]", "h1[a]")
assert_xpath(%q{//h1[@a='gnewline\n']}, "h1[a='\\gnew\\\nline\\\\n']")
assert_xpath("//h1[@a='test']", %q{h1[a=\te\st]})
end

it "parses leading @ (extended-syntax)" do
assert_xpath("//a[@id='Boing']", "a[@id='Boing']")
assert_xpath("//a[@id='Boing']", "a[@id = 'Boing']")
assert_xpath("//a[@id='Boing']//div", "a[@id='Boing'] div")
describe "namespaces" do
let(:ns) do
{
"xmlns" => "http://default.example.com/",
"hoge" => "http://hoge.example.com/",
}
end

it "namespacing" do
it "basic mechanics" do
assert_xpath("//a[@flavorjones:href]", "a[flavorjones|href]")
assert_xpath("//a[@href]", "a[|href]")
assert_xpath("//*[@flavorjones:href]", "*[flavorjones|href]")
end

ns = {
"xmlns" => "http://default.example.com/",
"hoge" => "http://hoge.example.com/",
}

# An intentionally-empty namespace means "don't use the default xmlns"
assert_equal(["//a"], Nokogiri::CSS.xpath_for("|a", ns: ns, cache: false))

# The default namespace is not applied to attributes (just elements)
it "default namespace is applied to elements but not attributes" do
assert_equal(
["//xmlns:a[@class='bar']"],
Nokogiri::CSS.xpath_for("a[class='bar']", ns: ns, cache: false),
)
end

it "intentionally-empty namespace omits the default xmlns" do
# An intentionally-empty namespace
assert_equal(["//a"], Nokogiri::CSS.xpath_for("|a", ns: ns, cache: false))
end

# We can explicitly apply a namespace to an attribue
it "explicit namespaces are applied to attributes" do
assert_equal(
["//xmlns:a[@hoge:class='bar']"],
Nokogiri::CSS.xpath_for("a[hoge|class='bar']", ns: ns, cache: false),
)
end
end

describe "attribute" do
it "basic mechanics" do
assert_xpath("//h1[@a='Tender Lovemaking']", "h1[a='Tender Lovemaking']")
assert_xpath("//h1[@a]", "h1[a]")
assert_xpath(%q{//h1[@a='gnewline\n']}, "h1[a='\\gnew\\\nline\\\\n']")
assert_xpath("//h1[@a='test']", %q{h1[a=\te\st]})
end

it "#id escaping" do
assert_xpath("//*[@id='foo']", "#foo")
assert_xpath("//*[@id='escape:needed,']", "#escape\\:needed\\,")
assert_xpath("//*[@id='escape:needed,']", '#escape\3Aneeded\,')
assert_xpath("//*[@id='escape:needed,']", '#escape\3A needed\2C')
assert_xpath("//*[@id='escape:needed']", '#escape\00003Aneeded')
end

it "parses leading @ (extended-syntax)" do
assert_xpath("//a[@id='Boing']", "a[@id='Boing']")
assert_xpath("//a[@id='Boing']", "a[@id = 'Boing']")
assert_xpath("//a[@id='Boing']//div", "a[@id='Boing'] div")
end

it "rhs with quotes" do
assert_xpath(%q{//h1[@a="'"]}, %q{h1[a="'"]})
Expand Down

0 comments on commit b112e18

Please sign in to comment.