From 37a410a0128ecc9367de704428a8b665d7e074b1 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 18 Aug 2015 14:40:56 +0200 Subject: [PATCH] XPath compiler support for starts-with() --- lib/oga/xpath/compiler.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index 7460cf7..f4d6eab 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -944,6 +944,31 @@ module Oga end end + # @param [Oga::Ruby::Node] input + # @param [AST::Node] haystack + # @param [AST::Node] needle + # @return [Oga::Ruby::Node] + def on_call_starts_with(input, haystack, needle) + haystack_var = unique_literal(:haystack) + needle_var = unique_literal(:needle) + conversion = literal(Conversion) + + haystack_var.assign(try_match_first_node(haystack, input)) + .followed_by do + needle_var.assign(try_match_first_node(needle, input)) + end + .followed_by do + converted = conversion.to_string(haystack_var) + .start_with?(conversion.to_string(needle_var)) + + if block_given? + converted.if_true { yield } + else + converted + end + end + end + ## # Delegates type tests to specific handlers. #