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

Open Beginner friendly
#384 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ruby
Domain
backend

Research direction

Start with Predicate in lib/rexml/parsers/xpathparser.rb and trace how it handles whitespace after a closing ], then check the operator parsing in OrExpr, AndExpr, and MultiplicativeExpr. Run the issue’s Ruby examples before and after the change; done means the and and div expressions return [true] and [2.0], while the existing symbol operator and parenthesized cases continue to work.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Ruby
Stars
181
Forks
105
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ruby/rexml

All issues in ruby/rexml

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.