Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/rexml/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Namespace
attr_accessor :prefix
include XMLTokens
NAME_WITHOUT_NAMESPACE = /\A#{NCNAME_STR}\z/
NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u
NAMESPLIT = /\A(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u

# Sets the name and the expanded name
def name=( name )
Expand Down
36 changes: 18 additions & 18 deletions lib/rexml/parsers/xpathparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Parsers
# back while you still can!
class XPathParser # :nodoc:
include XMLTokens
LITERAL = /^'([^']*)'|^"([^"]*)"/u
LITERAL = /\A'([^']*)'|\A"([^"]*)"/u

def namespaces=( namespaces )
@namespaces = namespaces
Expand Down Expand Up @@ -276,7 +276,7 @@ def LocationPath path, parsed
# | '.' | '..' AbbreviatedStep
# | RelativeLocationPath '/' Step
# | RelativeLocationPath '//' Step
AXIS = /^(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/
AXIS = /\A(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/
def RelativeLocationPath path, parsed
loop do
original_path = path
Expand Down Expand Up @@ -348,11 +348,11 @@ def RelativeLocationPath path, parsed
# | NODE_TYPE '(' ')' NodeType
# | PI '(' LITERAL ')' PI
# | '[' expr ']' Predicate
PREFIX_WILDCARD = /^\*:(#{NCNAME_STR})/u
LOCAL_NAME_WILDCARD = /^(#{NCNAME_STR}):\*/u
PREFIX_WILDCARD = /\A\*:(#{NCNAME_STR})/u
LOCAL_NAME_WILDCARD = /\A(#{NCNAME_STR}):\*/u
QNAME = Namespace::NAMESPLIT
NODE_TYPE = /^(comment|text|node)\(\s*\)/m
PI = /^processing-instruction\(/
NODE_TYPE = /\A(comment|text|node)\(\s*\)/m
PI = /\Aprocessing-instruction\(/
def NodeTest path, parsed
original_path = path
path = path.lstrip
Expand All @@ -364,7 +364,7 @@ def NodeTest path, parsed
parsed << :qname
parsed << prefix
parsed << name
when /^\*/
when /\A\*/
path = $'
parsed << :any
when NODE_TYPE
Expand Down Expand Up @@ -433,7 +433,7 @@ def OrExpr path, parsed
n = []
rest = AndExpr( path, n )
if rest != path
while rest =~ /^\s*( or )/
while rest =~ /\A\s*( or )/
n = [ :or, n, [] ]
rest = AndExpr( $', n[-1] )
end
Expand All @@ -452,7 +452,7 @@ def AndExpr path, parsed
n = []
rest = EqualityExpr( path, n )
if rest != path
while rest =~ /^\s*( and )/
while rest =~ /\A\s*( and )/
n = [ :and, n, [] ]
rest = EqualityExpr( $', n[-1] )
end
Expand All @@ -471,7 +471,7 @@ def EqualityExpr path, parsed
n = []
rest = RelationalExpr( path, n )
if rest != path
while rest =~ /^\s*(!?=)\s*/
while rest =~ /\A\s*(!?=)\s*/
if $1[0] == ?!
n = [ :neq, n, [] ]
else
Expand All @@ -494,7 +494,7 @@ def RelationalExpr path, parsed
n = []
rest = AdditiveExpr( path, n )
if rest != path
while rest =~ /^\s*([<>]=?)\s*/
while rest =~ /\A\s*([<>]=?)\s*/
if $1[0] == ?<
sym = "lt"
else
Expand All @@ -519,7 +519,7 @@ def AdditiveExpr path, parsed
n = []
rest = MultiplicativeExpr( path, n )
if rest != path
while rest =~ /^\s*(\+|-)\s*/
while rest =~ /\A\s*(\+|-)\s*/
if $1[0] == ?+
n = [ :plus, n, [] ]
else
Expand All @@ -542,7 +542,7 @@ def MultiplicativeExpr path, parsed
n = []
rest = UnaryExpr( path, n )
if rest != path
while rest =~ /^\s*(\*| div | mod )\s*/
while rest =~ /\A\s*(\*| div | mod )\s*/
if $1[0] == ?*
n = [ :mult, n, [] ]
elsif $1.include?( "div" )
Expand Down Expand Up @@ -584,7 +584,7 @@ def UnionExpr path, parsed
n = []
rest = PathExpr( path, n )
if rest != path
while rest =~ /^\s*(\|)\s*/
while rest =~ /\A\s*(\|)\s*/
n = [ :union, n, [] ]
rest = PathExpr( $', n[-1] )
end
Expand Down Expand Up @@ -641,8 +641,8 @@ def FilterExpr path, parsed
#| LITERAL
#| NUMBER
#| FunctionCall
VARIABLE_REFERENCE = /^\$(#{NAME_STR})/u
NUMBER = /^(\d*\.?\d+)/
VARIABLE_REFERENCE = /\A\$(#{NAME_STR})/u
NUMBER = /\A(\d*\.?\d+)/
NT = /^comment|text|processing-instruction|node$/
def PrimaryExpr path, parsed
case path
Expand All @@ -652,7 +652,7 @@ def PrimaryExpr path, parsed
parsed << :variable
parsed << varname
#arry << @variables[ varname ]
when /^(\w[-\w]*)(?:\()/
when /\A(\w[-\w]*)(?:\()/
fname = $1
tmp = $'
return path if fname =~ NT
Expand All @@ -670,7 +670,7 @@ def PrimaryExpr path, parsed
path = $'
parsed << :literal
parsed << varname
when /^\(/ #/
when /\A\(/ #/
path, contents = get_group(path)
contents = contents[1..-2]
n = []
Expand Down
57 changes: 57 additions & 0 deletions test/parser/test_xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,63 @@ def test_spaces_between_tokens
)
end

def test_line_breaks_between_tokens
parser = REXML::Parsers::XPathParser.new
assert_equal(
parser.parse('//a[1]'),
parser.parse("//a[\n1\n]"),
)
assert_equal(
parser.parse('//a[count(b) > 1]'),
parser.parse("//a[\ncount(b) > 1\n]"),
)
assert_equal(
parser.parse('//a[@b = "c"]'),
parser.parse("//a[@b =\n\"c\"]"),
)
assert_equal(
parser.parse('//a[@b = $c]'),
parser.parse("//a[@b =\n$c]"),
)
assert_equal(
parser.parse('//a[ancestor::b]/*'),
parser.parse("//a[\nancestor::b]/\n*"),
)
assert_equal(
parser.parse('//a = (1 + 2)'),
parser.parse("//a =\n(1 + 2)"),
)
end

def test_line_breaks_in_literal
parser = REXML::Parsers::XPathParser.new
assert_equal([:literal, "a\n1"],
parser.parse("\"a\n1\""))
assert_equal([:literal, "a\nb(c)"],
parser.parse("'a\nb(c)'"))
assert_equal([:function, "concat", [[:literal, "a"], [:literal, "b\n$c"]]],
parser.parse("concat('a', 'b\n$c')"))
assert_equal([:eq,
[:child, :qname, "", "a"],
[:literal, "b\n* c"]],
parser.parse("a = 'b\n* c'"))
assert_equal([:eq,
[:child, :qname, "", "a"],
[:literal, "b\n| c"]],
parser.parse("a = 'b\n| c'"))
end

def test_qname_must_start_at_the_beginning_of_the_expression
parser = REXML::Parsers::XPathParser.new
assert_raise(REXML::ParseException) do
parser.parse("//a/1\nb")
end
assert_raise(REXML::ParseException) do
parser.parse("child::1\nb")
end
assert_equal([:child, :qname, "a", "b"], parser.parse("a:b"))
end

def test_unary_minus_with_spaces
parser = REXML::Parsers::XPathParser.new
assert_equal(
Expand Down
Loading