-
Notifications
You must be signed in to change notification settings - Fork 98
Fix XPath variable handling and invalid value contamination #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,8 +244,10 @@ def expr( path_stack, nodeset, context=nil ) | |
| end | ||
| when :variable | ||
| var_name = path_stack.shift | ||
| return @variables[var_name] | ||
| value = coerce_variable(@variables[var_name]) | ||
| return value if path_stack.empty? | ||
|
|
||
| nodeset = apply_remaining_predicates(path_stack, value) | ||
| when :eq, :neq, :lt, :lteq, :gt, :gteq | ||
| left = expr( path_stack.shift, nodeset.dup, context ) | ||
| right = expr( path_stack.shift, nodeset.dup, context ) | ||
|
|
@@ -319,15 +321,9 @@ def expr( path_stack, nodeset, context=nil ) | |
| when :group | ||
| sub_expression = path_stack.shift | ||
| result = expr(sub_expression, nodeset, context) | ||
| if result.is_a?(Array) | ||
| # If result is a nodeset, apply following predicates | ||
| path_stack.unshift(:node) | ||
| nodeset = step(path_stack) do | ||
| [:iterate_nodesets, [XPathParser.sort(result)]] | ||
| end | ||
| else | ||
| return result | ||
| end | ||
| return result if path_stack.empty? | ||
|
|
||
| nodeset = apply_remaining_predicates(path_stack, result) | ||
| else | ||
| raise "[BUG] Unexpected path: <#{op.inspect}>: <#{path_stack.inspect}>" | ||
| end | ||
|
|
@@ -337,6 +333,31 @@ def expr( path_stack, nodeset, context=nil ) | |
| leave(:expr, path_stack, nodeset) if @debug | ||
| end | ||
|
|
||
| def apply_remaining_predicates(path_stack, value) | ||
| # If evaluated value is not a nodeset, treat it as an empty nodeset. | ||
| # TODO: Decide whether REXML should raise type error or keep this behavior. | ||
| value = [] unless value.is_a?(Array) | ||
| path_stack.unshift(:node) | ||
| step(path_stack) do | ||
| [:iterate_nodesets, [XPathParser.sort(value)]] | ||
| end | ||
| end | ||
|
|
||
| # Coerces a variable value to a type that can be used in XPath expressions. | ||
| # TODO: Decide whether REXML should warn, raise, or ignore when a variable value is invalid. | ||
|
Comment on lines
+346
to
+347
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
According to the XPath specification, if a variable name is not bound to any value, an error occurs. https://www.w3.org/TR/1999/REC-xpath-19991116/#section-Basics
I think it would be better to raise an error, as that would make the processing clearer.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think referencing unbound variable should raise error. Concern: when to do that, and what kind of error should it raise. when :variable
var_name = path_stack.shift
# Handle unbound variables
raise unless @variables.key?(var_name)
# Handle conversion and validation of variable value
# @variables[var_name]==nil case is handled here
value = coerce_variable(@variables[var_name])
...Even if we reject unbound variables, this TODO comment for bound variables (reject bound invalid-value variable or convert it to a fallback value "") still remains. |
||
| def coerce_variable(value) | ||
| case value | ||
| when REXML::Node | ||
| [value] | ||
| when Array | ||
| value.grep(REXML::Node).uniq | ||
| when Numeric, String, true, false | ||
| value | ||
| else | ||
| "" | ||
| end | ||
| end | ||
|
|
||
| # Determines if a predicate expression is dependent on the position of nodes. | ||
| # Returns false if the expression is guaranteed to be position-independent. | ||
| # Returns true if the expression might be position-dependent. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the XPath specification, anything other than a node set results in an error.
https://www.w3.org/TR/1999/REC-xpath-19991116/#node-sets
I think it would be better to raise an error, as that would make the processing clearer.
What do you think? @kou @tompng