Support for the xpath starts-with() function.
This commit is contained in:
parent
276a5ab83b
commit
5b65d6c31a
|
@ -807,6 +807,27 @@ module Oga
|
|||
return retval
|
||||
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.
|
||||
#
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue