XPath compiler support for id() in predicates

This commit is contained in:
Yorick Peterse 2015-08-12 19:07:42 +02:00
parent 49196c285f
commit c026244f6e
2 changed files with 29 additions and 8 deletions

View File

@ -790,7 +790,7 @@ module Oga
node.is_a?(XML::Element).if_true do node.is_a?(XML::Element).if_true do
attr_var.assign(node.attribute(id_str_var)).followed_by do attr_var.assign(node.attribute(id_str_var)).followed_by do
attr_var.and(ids_var.include?(attr_var.value)) attr_var.and(ids_var.include?(attr_var.value))
.if_true { matched << node } .if_true { block_given? ? yield : matched << node }
end end
end end
end end

View File

@ -9,16 +9,37 @@ describe Oga::XPath::Compiler do
@a2 = @document.children[0].children[1] @a2 = @document.children[0].children[1]
end end
it 'returns a node set containing the nodes with ID "a1"' do describe 'at the top level' do
evaluate_xpath(@document, 'id("a1")').should == node_set(@a1) describe 'using a string' do
it 'returns a NodeSet' do
evaluate_xpath(@document, 'id("a1")').should == node_set(@a1)
end
end
describe 'using a space separated string' do
it 'returns a NodeSet' do
evaluate_xpath(@document, 'id("a1 a2")').should == node_set(@a1, @a2)
end
end
describe 'using a path' do
it 'returns a NodeSet' do
evaluate_xpath(@document, 'id(root/a[2])').should == node_set(@a1)
end
end
end end
it 'returns a node set containing the nodes with ID "a1" or "a2"' do describe 'in a predicate' do
evaluate_xpath(@document, 'id("a1 a2")').should == node_set(@a1, @a2) describe 'using a string' do
end it 'matches nodes when a non-empty NodeSet is returned' do
evaluate_xpath(@document, 'root/a[id("a1")]')
.should == node_set(@a1, @a2)
end
it 'returns a node set containing the nodes with an ID based on a path' do it 'does not match nodes when an empty NodeSet is returned' do
evaluate_xpath(@document, 'id(root/a[2])').should == node_set(@a1) evaluate_xpath(@document, 'root/a[id("foo")]').should == node_set
end
end
end end
end end
end end