diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 53dc2ad..43231f9 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -474,6 +474,23 @@ module Oga return nodes 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 # nodes. diff --git a/spec/oga/xpath/evaluator/type_tests/comment_spec.rb b/spec/oga/xpath/evaluator/type_tests/comment_spec.rb new file mode 100644 index 0000000..adef384 --- /dev/null +++ b/spec/oga/xpath/evaluator/type_tests/comment_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'comment() tests' do + before do + @document = parse('') + @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