Support for the XPath contains() function.

This commit is contained in:
Yorick Peterse 2014-08-25 21:52:33 +02:00
parent 5b65d6c31a
commit 83b873e3c1
2 changed files with 55 additions and 1 deletions

View File

@ -811,7 +811,7 @@ module Oga
# 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.
# starts with the string in the 2nd argument. Node sets can also be used.
#
# @example
# starts-with("hello world", "hello") # => true
@ -828,6 +828,22 @@ module Oga
return haystack_str.start_with?(needle_str)
end
##
# Processes the `contains()` function call.
#
# This function call returns `true` if the string in the 1st argument
# contains the string in the 2nd argument. Node sets can also be used.
#
# @example
# contains("hello world", "o w") # => true
#
def on_call_contains(context, haystack, needle)
haystack_str = on_call_string(context, haystack)
needle_str = on_call_string(context, needle)
return haystack_str.include?(needle_str)
end
##
# Processes an `(int)` node.
#

View File

@ -0,0 +1,38 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'contains() 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 contains the 2nd string' do
@evaluator.evaluate('contains("foobar", "oo")').should == true
end
example "return false if the 1st string doesn't contain the 2nd string" do
@evaluator.evaluate('contains("foobar", "baz")').should == false
end
example 'return true if the 1st node set contains the 2nd string' do
@evaluator.evaluate('contains(root/a, "oo")').should == true
end
example 'return true if the 1st node set contains the 2nd node set' do
@evaluator.evaluate('contains(root/b, root/a)').should == true
end
example "return false if the 1st node doesn't contain the 2nd node set" do
@evaluator.evaluate('contains(root/a, root/b)').should == false
end
example 'return true if the 1st string contains the 2nd node set' do
@evaluator.evaluate('contains("foobar", root/a)').should == true
end
example 'return true when using two empty strings' do
@evaluator.evaluate('contains("", "")').should == true
end
end
end