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
32 changes: 18 additions & 14 deletions lib/rexml/parsers/xpathparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ def namespaces=( namespaces )
end

def parse path
path = path.dup
path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces
path.gsub!( /\s+([\]\)])/, '\1')
parsed = []
rest = OrExpr(path, parsed)
if rest
Expand Down Expand Up @@ -359,17 +356,15 @@ def NodeTest path, parsed
path = $'
parsed << type.tr('-', '_').intern
when PI
path = $'
path = $'.lstrip
literal = nil
if path =~ /^\s*\)/
path = $'
else
unless path.start_with?(')')
path =~ LITERAL
literal = $1
path = $'
path = $'.lstrip
raise ParseException.new("Missing ')' after processing instruction") if path[0] != ?)
path = path[1..-1]
end
path = path[1..-1]
parsed << :processing_instruction
parsed << (literal || '')
when LOCAL_NAME_WILDCARD
Expand Down Expand Up @@ -400,6 +395,7 @@ def Predicate path, parsed
while path[0] == ?[
path, expr = get_group(path)
predicates << expr[1..-2] if expr
path = path.lstrip
end
predicates.each{ |pred|
preds = []
Expand Down Expand Up @@ -678,12 +674,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 thread
tompng marked this conversation as resolved.
end
ind += 1
end while depth > 0 and ind < string.length
Expand Down
36 changes: 36 additions & 0 deletions test/xpath/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,42 @@ def test_spaces
match.call('/ a / child:: c [( @id )] /'))
end

def test_space_inside_xpath
# REXML doesn't support space between function-name and opening paren.
# Spaces after `(` and spaces around `)`, `[`, `]` are tested here.
parser = Parsers::XPathParser.new
assert_equal(
parser.parse('//a/b[c][d]/e'),
parser.parse(' // a / b [ c ] [ d ] / e '),
)
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