Support for the XPath substring-after() function.
This commit is contained in:
parent
b9cdb4a72b
commit
b2ca18e127
|
@ -857,7 +857,7 @@ module Oga
|
||||||
#
|
#
|
||||||
# substring-before("2014-08-25", "-")
|
# substring-before("2014-08-25", "-")
|
||||||
#
|
#
|
||||||
# This would return "2014" as its the first string that occurs before "-".
|
# This would return "2014" as it occurs before the first "-".
|
||||||
#
|
#
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @param [Oga::XPath::Node] haystack The string to search.
|
# @param [Oga::XPath::Node] haystack The string to search.
|
||||||
|
@ -873,6 +873,30 @@ module Oga
|
||||||
return sep.empty? ? sep : before
|
return sep.empty? ? sep : before
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Processes the `substring-after()` function call.
|
||||||
|
#
|
||||||
|
# This function call returns the substring of the 1st argument that occurs
|
||||||
|
# after the string given in the 2nd argument. For example:
|
||||||
|
#
|
||||||
|
# substring-before("2014-08-25", "-")
|
||||||
|
#
|
||||||
|
# This would return "08-25" as it occurs after the first "-".
|
||||||
|
#
|
||||||
|
# @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 [String]
|
||||||
|
#
|
||||||
|
def on_call_substring_after(context, haystack, needle)
|
||||||
|
haystack_str = on_call_string(context, haystack)
|
||||||
|
needle_str = on_call_string(context, needle)
|
||||||
|
|
||||||
|
before, sep, after = haystack_str.partition(needle_str)
|
||||||
|
|
||||||
|
return sep.empty? ? sep : after
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Processes an `(int)` node.
|
# Processes an `(int)` node.
|
||||||
#
|
#
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Oga::XPath::Evaluator do
|
||||||
|
context 'substring-after() function' do
|
||||||
|
before do
|
||||||
|
@document = parse('<root><a>-</a><b>a-b-c</b></root>')
|
||||||
|
@evaluator = described_class.new(@document)
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return the substring of the 1st string after the 2nd string' do
|
||||||
|
@evaluator.evaluate('substring-after("a-b-c", "-")').should == 'b-c'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return an empty string if the 2nd string is not present' do
|
||||||
|
@evaluator.evaluate('substring-after("a-b-c", "x")').should == ''
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return the substring of the 1st node set after the 2nd string' do
|
||||||
|
@evaluator.evaluate('substring-after(root/b, "-")').should == 'b-c'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return the substring of the 1st node set after the 2nd node set' do
|
||||||
|
@evaluator.evaluate('substring-after(root/b, root/a)').should == 'b-c'
|
||||||
|
end
|
||||||
|
|
||||||
|
example 'return an empty string when using two empty strings' do
|
||||||
|
@evaluator.evaluate('substring-after("", "")').should == ''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue