Skip to content

XPath: and/or/div/mod right after a predicate raise ParseException #384

Open

Description

@bardrop

A word operator (and, or, div, mod) right after a predicate fails to parse:

require "rexml/document"
doc = REXML::Document.new("<r><v>6</v><v>2</v></r>")
REXML::XPath.match(doc, "//v[1] and true()")
# REXML::ParseException: Garbage component exists at the end: <and true()>: <//v[1] and true()>
REXML::XPath.match(doc, "/r/v[1] div 3")
# REXML::ParseException: Garbage component exists at the end: <div 3>: </r/v[1] div 3>

Expected [true] and [2.0] (libxml2 gives those). Symbol operators work (//v[1] * 3, //v[1] = 6), and so do (//v[1]) div 3 and //v div 3.

Cause: Predicate in lib/rexml/parsers/xpathparser.rb calls path.lstrip after each ], so it returns "and true()". OrExpr, AndExpr and MultiplicativeExpr then look for /^\s*( and )/, /^\s*( or )/ and /^\s*(\*| div | mod )\s*/, which need the space before the word, so they don't match.

Possible fix: in Predicate, only strip the whitespace when another [ follows, and otherwise return the path with its leading space.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions