diff --git a/lib/rexml/namespace.rb b/lib/rexml/namespace.rb index 232b7ca4..b5681152 100644 --- a/lib/rexml/namespace.rb +++ b/lib/rexml/namespace.rb @@ -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 ) diff --git a/lib/rexml/parsers/xpathparser.rb b/lib/rexml/parsers/xpathparser.rb index ccdc1639..9ffcb8a8 100644 --- a/lib/rexml/parsers/xpathparser.rb +++ b/lib/rexml/parsers/xpathparser.rb @@ -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 @@ -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 @@ -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 @@ -364,7 +364,7 @@ def NodeTest path, parsed parsed << :qname parsed << prefix parsed << name - when /^\*/ + when /\A\*/ path = $' parsed << :any when NODE_TYPE @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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" ) @@ -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 @@ -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 @@ -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 @@ -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 = [] diff --git a/test/parser/test_xpath.rb b/test/parser/test_xpath.rb index ffef7bb6..9d8ee3fb 100644 --- a/test/parser/test_xpath.rb +++ b/test/parser/test_xpath.rb @@ -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(