Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions lib/rexml/parsers/xpathparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def namespaces=( namespaces )
end

def parse path
path = path.dup
path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces
path.gsub!( /\s+([\]\)])/, '\1')
path = strip_ignorable_spaces(path)
parsed = []
rest = OrExpr(path, parsed)
if rest
Expand All @@ -33,6 +31,20 @@ def parse path
parsed
end

def strip_ignorable_spaces(path)
# Safely do `path.gsub(/([\(\[])\s+/, '\1').gsub( /\s+([\]\)])/, '\1')`
# without modifying spaces inside string literals.
quote = nil
path.gsub(/([\(\[])\s+|\s+([\]\)])|(['"])/) do
if quote
quote = nil if $3 == quote
$&
else
$1 || $2 || (quote = $3)
end
Comment on lines +38 to +44
end
end
Comment on lines +34 to +46

def predicate path
parsed = []
Predicate( "[#{path}]", parsed )
Expand Down Expand Up @@ -678,12 +690,20 @@ def get_group string
depth = 0
st = string[0,1]
en = (st == "(" ? ")" : "]")
quote = nil
begin
case string[ind,1]
when st
depth += 1
when en
depth -= 1
if quote
# ignore () [] inside quotes
quote = nil if string[ind] == quote
else
case string[ind]
when st
depth += 1
when en
depth -= 1
when '"', "'"
quote = string[ind]
end
Comment on lines +695 to +706
end
ind += 1
end while depth > 0 and ind < string.length
Expand Down
30 changes: 30 additions & 0 deletions test/xpath/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,36 @@ def test_spaces
match.call('/ a / child:: c [( @id )] /'))
end

def test_space_inside_xpath
parser = Parsers::XPathParser.new
assert_equal(
parser.parse('/a/b[string-length("1")<(2+3)]/c'),
parser.parse(' / a / b [ string-length( "1" ) < ( 2 + 3 ) ] / c '),
)
assert_equal(
parser.parse('//processing-instruction("a")'),
parser.parse('//processing-instruction( "a" )'),
)
end

def test_space_paren_brace_inside_xpath_string
doc = Document.new(<<~XML)
<a>
<b id=" [ ' 1 ) "/>
<b id=' ( " 2 ] '/>
</a>
XML

assert_equal(
[" [ ' 1 ) "],
REXML::XPath.match(doc, "/a/b[@id=\" [ ' 1 ) \"]").map { |e| e.attributes['id'] }
)
assert_equal(
[' ( " 2 ] '],
REXML::XPath.match(doc, "/a/b[@id=' ( \" 2 ] ']").map { |e| e.attributes['id'] }
)
end

def test_text_nodes
# source = "<root>
#<child/>
Expand Down
Loading