From b32affccaf4168fc2b9c0d9c78e373602de4dfb8 Mon Sep 17 00:00:00 2001 From: Matt Pulver Date: Thu, 25 May 2023 08:51:38 -0400 Subject: [PATCH 1/3] Add XPathParser#QuoteLiteral. --- lib/rexml/parsers/xpathparser.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb index afff85ce..0d0b67a2 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 << QuoteLiteral(path.shift) string << " " else string << " " @@ -363,6 +363,18 @@ def Predicate path, parsed path end + def QuoteLiteral literal + case literal + when String + # Xpath 1.0 does not support escape characters. + # Assumes literal does not contain both single and double quotes. + pattern = literal.include?('"') ? "'%s'" : '"%s"' + pattern % literal + else + literal.inspect + end + end + # The following return arrays of true/false, a 1-1 mapping of the # supplied nodeset, except for axe(), which returns a filtered # nodeset From d50d7f3f648b32c3f3b4f5f58a75dfc05b9c1958 Mon Sep 17 00:00:00 2001 From: Matt Pulver Date: Fri, 26 May 2023 06:30:28 -0400 Subject: [PATCH 2/3] PR Feedback. * Rename QuoteLiteral -> quote_literal and move to after predicate_to_string. * Xpath -> XPath. * Replace %-substitution w/ String "#{...}". --- lib/rexml/parsers/xpathparser.rb | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb index 0d0b67a2..1c73ade3 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 << QuoteLiteral(path.shift) + string << quote_literal(path.shift) string << " " else string << " " @@ -188,6 +188,21 @@ def predicate_to_string( path, &block ) return string.squeeze(" ") end + 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 + private #LocationPath # | RelativeLocationPath @@ -363,18 +378,6 @@ def Predicate path, parsed path end - def QuoteLiteral literal - case literal - when String - # Xpath 1.0 does not support escape characters. - # Assumes literal does not contain both single and double quotes. - pattern = literal.include?('"') ? "'%s'" : '"%s"' - pattern % literal - else - literal.inspect - end - end - # The following return arrays of true/false, a 1-1 mapping of the # supplied nodeset, except for axe(), which returns a filtered # nodeset From d6a02b91a244b8c0805a780525568ad4bc2b74de Mon Sep 17 00:00:00 2001 From: Matt Pulver Date: Fri, 26 May 2023 09:48:33 -0400 Subject: [PATCH 3/3] Make quote_literal private. --- lib/rexml/parsers/xpathparser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb index 1c73ade3..7961e32f 100644 --- a/lib/rexml/parsers/xpathparser.rb +++ b/lib/rexml/parsers/xpathparser.rb @@ -188,6 +188,7 @@ def predicate_to_string( path, &block ) return string.squeeze(" ") end + private def quote_literal( literal ) case literal when String @@ -203,7 +204,6 @@ def quote_literal( literal ) end end - private #LocationPath # | RelativeLocationPath # | '/' RelativeLocationPath?