Support for the XPath comment() test.

This commit is contained in:
Yorick Peterse 2014-08-15 20:49:13 +02:00
parent 4e8cca258c
commit ccd95d69d8
2 changed files with 59 additions and 0 deletions

View File

@ -474,6 +474,23 @@ module Oga
return nodes return nodes
end end
##
# Processes the `comment()` type test. This matches only comment nodes.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context
# @return [Oga::XML::NodeSet]
#
def on_type_test_comment(ast_node, context)
nodes = XML::NodeSet.new
context.each do |node|
nodes << node if node.is_a?(XML::Comment)
end
return nodes
end
## ##
# Returns a node set containing all the child nodes of the given set of # Returns a node set containing all the child nodes of the given set of
# nodes. # nodes.

View File

@ -0,0 +1,42 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'comment() tests' do
before do
@document = parse('<a><!--foo--><b><!--bar--></b></a>')
@evaluator = described_class.new(@document)
end
context 'matching text nodes' do
before do
@set = @evaluator.evaluate('a/comment()')
end
it_behaves_like :node_set, :length => 1
example 'return a Comment instance' do
@set[0].is_a?(Oga::XML::Comment).should == true
end
example 'return the "foo" comment node' do
@set[0].text.should == 'foo'
end
end
context 'matching nested text nodes' do
before do
@set = @evaluator.evaluate('a/b/comment()')
end
it_behaves_like :node_set, :length => 1
example 'return a Comment instance' do
@set[0].is_a?(Oga::XML::Comment).should == true
end
example 'return the "bar" comment node' do
@set[0].text.should == 'bar'
end
end
end
end