1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
# frozen_string_literal: true
require "helper"
module Nokogiri
module XML
describe XPathContext do
it "can register and deregister namespaces" do
doc = Document.parse(<<~XML)
<root xmlns="http://nokogiri.org/default" xmlns:ns1="http://nokogiri.org/ns1">
<child>default</child>
<ns1:child>ns1</ns1:child>
</root>
XML
xc = XPathContext.new(doc)
assert_raises(XPath::SyntaxError) do
xc.evaluate("//foo:child")
end
xc.register_namespaces({ "foo" => "http://nokogiri.org/default" })
assert_pattern do
xc.evaluate("//foo:child") => [
{ name: "child", namespace: { href: "http://nokogiri.org/default" } }
]
end
xc.register_namespaces({ "foo" => nil })
assert_raises(XPath::SyntaxError) do
xc.evaluate("//foo:child")
end
end
it "can register and deregister variables" do
doc = Nokogiri::XML.parse(File.read(TestBase::XML_FILE), TestBase::XML_FILE)
xc = XPathContext.new(doc)
assert_raises(XPath::SyntaxError) do
xc.evaluate("//address[@domestic=$value]")
end
xc.register_variables({ "value" => "Yes" })
nodes = xc.evaluate("//address[@domestic=$value]")
assert_equal(4, nodes.length)
xc.register_variables({ "value" => "Qwerty" })
nodes = xc.evaluate("//address[@domestic=$value]")
assert_empty(nodes)
xc.register_variables({ "value" => nil })
assert_raises(XPath::SyntaxError) do
xc.evaluate("//address[@domestic=$value]")
end
end
it "#node=" do
doc = Nokogiri::XML::Document.parse(<<~XML)
<root>
<child><foo>one</foo></child>
<child><foo>two</foo></child>
<child><foo>three</foo></child>
</root>
XML
xc = XPathContext.new(doc)
results = xc.evaluate(".//foo")
assert_equal(3, results.length)
xc.node = doc.root.elements[0]
assert_pattern { xc.evaluate(".//foo") => [{ name: "foo", inner_html: "one" }] }
xc.node = doc.root.elements[1]
assert_pattern { xc.evaluate(".//foo") => [{ name: "foo", inner_html: "two" }] }
end
end
end
end
|