Skip to content

Commit e08c52f

Browse files
authored
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 <[email protected]>
1 parent 54b7109 commit e08c52f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/rexml/parsers/xpathparser.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def predicate_to_string( path, &block )
178178
when :literal
179179
path.shift
180180
string << " "
181-
string << path.shift.inspect
181+
string << quote_literal(path.shift)
182182
string << " "
183183
else
184184
string << " "
@@ -189,6 +189,21 @@ def predicate_to_string( path, &block )
189189
end
190190

191191
private
192+
def quote_literal( literal )
193+
case literal
194+
when String
195+
# XPath 1.0 does not support escape characters.
196+
# Assumes literal does not contain both single and double quotes.
197+
if literal.include?("'")
198+
"\"#{literal}\""
199+
else
200+
"'#{literal}'"
201+
end
202+
else
203+
literal.inspect
204+
end
205+
end
206+
192207
#LocationPath
193208
# | RelativeLocationPath
194209
# | '/' RelativeLocationPath?

0 commit comments

Comments
 (0)