Bug 616774: Make XPath expressions like "--expr" coerce into a number. r=sicking

This commit is contained in:
Timothy Zhu 2011-11-07 22:27:23 -08:00
parent ebb14423fe
commit 55b50797ab
3 changed files with 43 additions and 10 deletions

View File

@ -313,9 +313,9 @@ txExprParser::createExpr(txExprLexer& lexer, txIParseContext* aContext,
while (!done) {
MBool unary = MB_FALSE;
PRUint16 negations = 0;
while (lexer.peek()->mType == Token::SUBTRACTION_OP) {
unary = !unary;
negations++;
lexer.nextToken();
}
@ -324,15 +324,19 @@ txExprParser::createExpr(txExprLexer& lexer, txIParseContext* aContext,
break;
}
if (unary) {
Expr* unaryExpr = new UnaryExpr(expr);
if (!unaryExpr) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
if (negations > 0) {
if (negations % 2 == 0) {
FunctionCall* fcExpr = new txCoreFunctionCall(txCoreFunctionCall::NUMBER);
rv = fcExpr->addParam(expr);
if (NS_FAILED(rv))
return rv;
expr.forget();
expr = fcExpr;
}
else {
expr = new UnaryExpr(expr.forget());
}
expr.forget();
expr = unaryExpr;
}
Token* tok = lexer.nextToken();

View File

@ -55,6 +55,7 @@ _TEST_FILES = test_bug319374.xhtml \
test_bug566629.html \
test_bug566629.xhtml \
test_bug603159.html \
test_bug616774.html \
test_bug667315.html \
test_exslt_regex.html \
$(NULL)

View File

@ -0,0 +1,28 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=616774-->
<head>
<title>Test for Bug 616774</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=616774">Mozilla Bug 616774</a>
<p id="display"></p>
<div id="content" style="display: none">
42
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 616774 **/
is(document.evaluate('- "8"', document, null, XPathResult.ANY_TYPE, null).numberValue, -8, "Negated string literal should evaluate to itself negated");
is(document.evaluate('- - "999"', document, null, XPathResult.ANY_TYPE, null).numberValue, 999, "String literal should evaluate to itself");
is(document.evaluate('- - id("content")', document, null, XPathResult.ANY_TYPE, null).numberValue, 42, "DOM element should evaluate to itself coerced to a number");
</script>
</pre>
</body>
</html>