Skip to content

Reject underscored XPath function names - #366

Open
sjh9714 wants to merge 2 commits into
ruby:masterfrom
sjh9714:codex/20260830-352-reject-underscored-xpath-function
Open

Reject underscored XPath function names#366
sjh9714 wants to merge 2 commits into
ruby:masterfrom
sjh9714:codex/20260830-352-reject-underscored-xpath-function

Conversation

@sjh9714

@sjh9714 sjh9714 commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #352.

Reject XPath function names containing underscores instead of treating them as
aliases for hyphenated standard functions. Standard names such as
local-name() continue to parse normally.

Changes

  • Stop underscored names from being parsed as XPath function calls.
  • Update XPath fixtures to use standard hyphenated names and add regression
    coverage.

Testing

  • ruby -Itest -Ilib test/parser/test_xpath.rb --name=test_function_with_underscore
  • rake test
@kou

kou commented Aug 30, 2026

Copy link
Copy Markdown
Member

I want to keep backward compatibility as much as possible.

How about rejecting _ only when a user specify an option explicitly?

@sjh9714

sjh9714 commented Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

Updated this so underscored function names remain accepted by default and are rejected only when the existing strict: true option is passed. The low-level XPath parser now receives that option, and the tests cover both default compatibility and strict rejection.

Ran:

  • ruby -Ilib:test test/parser/test_xpath.rb
  • ruby -Ilib:test test/functions/test_base.rb
  • ruby -Ilib:test test/run.rb
#arry << @variables[ varname ]
when /^(\w[-\w]*)(?:\()/
fname = $1
return path if @strict && fname.include?("_")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mechanism that this return path rejects the xpath, is complicated. (See LocationPath → RelativeLocationPath → NodeTest called when 'local_name()' is passed)

I think underscored function names are invalid not because it has underscore, but because it's not registered as a function in XPath 1.0 spec.
Both examples in the code below is in the same situation: using unknown function, so the result (return value or raised error) should be the same.

REXML::XPath.match(REXML::Document.new('<root/>'), 'local_name()')
REXML::XPath.match(REXML::Document.new('<root/>'), 'localname()')

From a viewpoint of module's responsibility, I personally think this check shouldn't be done in REXML::Parsers:XPathParser, but in one of:

  • REXML::XPathParser: layer that converts '-' to '_', function name to a method name
  • REXML::FunctionsClass: layer that knows all available functions, checks if a method name is registered as a function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants