Support for the xpath starts-with() function.

This commit is contained in:
Yorick Peterse 2014-08-25 09:43:06 +02:00
parent 276a5ab83b
commit 5b65d6c31a
2 changed files with 59 additions and 0 deletions

View File

@ -807,6 +807,27 @@ module Oga
return retval return retval
end end
##
# Processes the `starts-with()` function call.
#
# This function call returns `true` if the string in the 1st argument
# starts with the string in the 2nd argument.
#
# @example
# starts-with("hello world", "hello") # => true
#
# @param [Oga::XML::NodeSet] context
# @param [Oga::XPath::Node] haystack The string to search.
# @param [Oga::XPath::Node] needle The string to search for.
# @return [TrueClass|FalseClass]
#
def on_call_starts_with(context, haystack, needle)
haystack_str = on_call_string(context, haystack)
needle_str = on_call_string(context, needle)
return haystack_str.start_with?(needle_str)
end
## ##
# Processes an `(int)` node. # Processes an `(int)` node.
# #

View File

@ -0,0 +1,38 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'starts-with() function' do
before do
@document = parse('<root><a>foo</a><b>foobar</b></root>')
@evaluator = described_class.new(@document)
end
example 'return true if the 1st string starts with the 2nd string' do
@evaluator.evaluate('starts-with("foobar", "foo")').should == true
end
example "return false if the 1st string doesn't start with the 2nd string" do
@evaluator.evaluate('starts-with("foobar", "baz")').should == false
end
example 'return true if the 1st node set starts with the 2nd string' do
@evaluator.evaluate('starts-with(root/a, "foo")').should == true
end
example 'return true if the 1st node set starts with the 2nd node set' do
@evaluator.evaluate('starts-with(root/b, root/a)').should == true
end
example "return false if the 1st node set doesn't start with the 2nd string" do
@evaluator.evaluate('starts-with(root/a, "baz")').should == false
end
example 'return true if the 1st string starts with the 2nd node set' do
@evaluator.evaluate('starts-with("foobar", root/a)').should == true
end
example 'return true when using two empty strings' do
@evaluator.evaluate('starts-with("", "")').should == true
end
end
end