Commit 86941786 authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

XPath: Fix context node after evaluating an expression in a predicate

The new behavior matches to Edge and Firefox.

Bug: 625710
Change-Id: I27de8b769712062f548eb9d2274544d1ec0ff073
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1973435
Commit-Queue: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#726221}
parent 2bf5c9d3
......@@ -263,7 +263,11 @@ void Predicate::Trace(blink::Visitor* visitor) {
bool Predicate::Evaluate(EvaluationContext& context) const {
DCHECK(expr_);
Value result(expr_->Evaluate(context));
// Apply a cloned context because position() requires the current
// context node.
EvaluationContext cloned_context = context;
Value result(expr_->Evaluate(cloned_context));
context.had_type_conversion_error |= cloned_context.had_type_conversion_error;
// foo[3] means foo[position()=3]
if (result.IsNumber())
......
<!DOCTYPE html>
<link rel="help" href="https://www.w3.org/TR/1999/REC-xpath-19991116/#predicates">
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
function nodesetToSet(result) {
const set = new Set();
for (let node = result.iterateNext(); node; node = result.iterateNext()) {
set.add(node);
}
return set;
}
test(() => {
const doc = document.implementation.createHTMLDocument();
doc.body.innerHTML = '<table></table>' +
'<table><tr><th><th><th><th></table>' +
'<table></table>';
const result = nodesetToSet(doc.evaluate('(//table)[count((//table)[2]/descendant::th)-1]', doc.documentElement));
assert_equals(result.size, 1);
assert_true(result.has(doc.body.lastChild));
}, 'An expression in a predicate should not change the context node');
</script>
</body>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment