Support for the XPath "and" operator.

This commit is contained in:
Yorick Peterse 2014-08-28 09:42:55 +02:00
parent 809ed9bfa6
commit 4f189d9218
2 changed files with 49 additions and 0 deletions

View File

@ -592,6 +592,23 @@ module Oga
return process(left, context) + process(right, context)
end
##
# Processes the `and` operator.
#
# This operator returns true if both the left and right expression
# evaluate to `true`. If the first expression evaluates to `false` the
# right expression is ignored.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context
# @return [TrueClass|FalseClass]
#
def on_and(ast_node, context)
left, right = *ast_node
return on_call_boolean(context, left) && on_call_boolean(context, right)
end
##
# Delegates function calls to specific handlers.
#

View File

@ -0,0 +1,32 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'and operator' do
before do
@document = parse('<root><a>1</a><b>1</b></root>')
@evaluator = described_class.new(@document)
end
example 'return true if both boolean literals are true' do
@evaluator.evaluate('true() and true()').should == true
end
example 'return false if one of the boolean literals is false' do
@evaluator.evaluate('true() and false()').should == false
end
example 'return true if both node sets are non empty' do
@evaluator.evaluate('root/a and root/b').should == true
end
example 'false false if one of the node sets is empty' do
@evaluator.evaluate('root/a and root/c').should == false
end
example 'skip the right expression if the left one evaluates to false' do
@evaluator.should_not receive(:on_call_true)
@evaluator.evaluate('false() and true()').should == false
end
end
end