From e08c52fac812799a8f6433fe92eb41a2e224e0cd Mon Sep 17 00:00:00 2001 From: pulver <39707+pulver@users.noreply.github.com> Date: Fri, 26 May 2023 11:06:49 -0400 Subject: [PATCH] xpath abbreviate: add support for string literal that contains double-quote (#96) This adds support for a string literal that contains a double-quote to `XPathParser#abbreviate`. Basically any literal that contains a double-quote `"` must be quoted by single-quotes `'` since XPath 1.0 does not support any escape characters. The change improves the following test script ```ruby require 'rexml' parsed = REXML::Parsers::XPathParser.new.parse('/a[b/text()=concat("c\'",\'"d\')]') puts "#{parsed}" puts "" appreviated = REXML::Parsers::XPathParser.new.abbreviate parsed puts "#{appreviated}" ``` ### Output Before Change ``` [:document, :child, :qname, "", "a", :predicate, [:eq, [:child, :qname, "", "b", :child, :text], [:function, "concat", [[:literal, "c'"], [:literal, "\"d"]]]]] /a[ b/text() = concat( "c'" , "\"d" ) ] ``` ### Output After Change ``` [:document, :child, :qname, "", "a", :predicate, [:eq, [:child, :qname, "", "b", :child, :text], [:function, "concat", [[:literal, "c'"], [:literal, "\"d"]]]]] /a[ b/text() = concat( "c'" , '"d' ) ] ``` --------- Co-authored-by: Matt Pulver --- lib/rexml/parsers/xpathparser.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb index afff85ce..7961e32f 100644 --- a/lib/rexml/parsers/xpathparser.rb +++ b/lib/rexml/parsers/xpathparser.rb @@ -178,7 +178,7 @@ def predicate_to_string( path, &block ) when :literal path.shift string << " " - string << path.shift.inspect + string << quote_literal(path.shift) string << " " else string << " " @@ -189,6 +189,21 @@ def predicate_to_string( path, &block ) end private + def quote_literal( literal ) + case literal + when String + # XPath 1.0 does not support escape characters. + # Assumes literal does not contain both single and double quotes. + if literal.include?("'") + "\"#{literal}\"" + else + "'#{literal}'" + end + else + literal.inspect + end + end + #LocationPath # | RelativeLocationPath # | '/' RelativeLocationPath?