From b9cdb4a72bffc7303b53519c7293011cecf056c1 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 25 Aug 2014 22:11:05 +0200 Subject: [PATCH] Support for the XPath substring-before() function. --- lib/oga/xpath/evaluator.rb | 29 ++++++++++++++++++ .../evaluator/calls/substring_before_spec.rb | 30 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 spec/oga/xpath/evaluator/calls/substring_before_spec.rb diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 681c64a..de603e3 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -837,6 +837,11 @@ module Oga # @example # contains("hello world", "o w") # => 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 [String] + # def on_call_contains(context, haystack, needle) haystack_str = on_call_string(context, haystack) needle_str = on_call_string(context, needle) @@ -844,6 +849,30 @@ module Oga return haystack_str.include?(needle_str) end + ## + # Processes the `substring-before()` function call. + # + # This function call returns the substring of the 1st argument that occurs + # before the string given in the 2nd argument. For example: + # + # substring-before("2014-08-25", "-") + # + # This would return "2014" as its the first string that occurs before "-". + # + # @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_before(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 : before + end + ## # Processes an `(int)` node. # diff --git a/spec/oga/xpath/evaluator/calls/substring_before_spec.rb b/spec/oga/xpath/evaluator/calls/substring_before_spec.rb new file mode 100644 index 0000000..a12fb45 --- /dev/null +++ b/spec/oga/xpath/evaluator/calls/substring_before_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'substring-before() function' do + before do + @document = parse('-a-b-c') + @evaluator = described_class.new(@document) + end + + example 'return the substring of the 1st string before the 2nd string' do + @evaluator.evaluate('substring-before("a-b-c", "-")').should == 'a' + end + + example 'return an empty string if the 2nd string is not present' do + @evaluator.evaluate('substring-before("a-b-c", "x")').should == '' + end + + example 'return the substring of the 1st node set before the 2nd string' do + @evaluator.evaluate('substring-before(root/b, "-")').should == 'a' + end + + example 'return the substring of the 1st node set before the 2nd node set' do + @evaluator.evaluate('substring-before(root/b, root/a)').should == 'a' + end + + example 'return an empty string when using two empty strings' do + @evaluator.evaluate('substring-before("", "")').should == '' + end + end +end