Bug 673820 - Rewriting StringToInteger function to match behavior of Rules for parsing Integers specified in spec. r=jonas f=mounir,Ms2ger,dbaron

This commit is contained in:
Atul Aggarwal 2011-10-01 19:30:27 +05:30
Родитель aa7afa50d1
Коммит 68c6dc4c1f
4 изменённых файлов: 57 добавлений и 86 удалений

Просмотреть файл

@ -1356,70 +1356,70 @@ nsAttrValue::StringToInteger(const nsAString& aValue, bool* aStrict,
bool aCanBePercent,
bool* aIsPercent) const
{
*aStrict = PR_FALSE;
*aStrict = true;
*aErrorCode = NS_ERROR_ILLEGAL_VALUE;
if (aCanBePercent) {
*aIsPercent = PR_FALSE;
*aIsPercent = false;
}
nsAString::const_iterator iter, end;
aValue.BeginReading(iter);
aValue.EndReading(end);
while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) {
*aStrict = false;
++iter;
}
if (iter == end) {
return 0;
}
bool negate = false;
if (*iter == PRUnichar('-')) {
negate = true;
++iter;
} else if (*iter == PRUnichar('+')) {
*aStrict = false;
++iter;
}
PRInt32 value = 0;
if (iter != end) {
if (*iter == PRUnichar('-')) {
negate = PR_TRUE;
PRInt32 pValue = 0; // Previous value, used to check integer overflow
while (iter != end) {
if (*iter >= PRUnichar('0') && *iter <= PRUnichar('9')) {
value = (value * 10) + (*iter - PRUnichar('0'));
++iter;
}
if (iter != end) {
if ((*iter >= PRUnichar('1') || (*iter == PRUnichar('0') && !negate)) &&
*iter <= PRUnichar('9')) {
value = *iter - PRUnichar('0');
++iter;
*aStrict = (value != 0 || iter == end ||
(aCanBePercent && *iter == PRUnichar('%')));
while (iter != end && *aStrict) {
if (*iter >= PRUnichar('0') && *iter <= PRUnichar('9')) {
value = (value * 10) + (*iter - PRUnichar('0'));
++iter;
if (iter != end && value > ((PR_INT32_MAX / 10) - 9)) {
*aStrict = PR_FALSE;
}
} else if (aCanBePercent && *iter == PRUnichar('%')) {
++iter;
if (iter == end) {
*aIsPercent = PR_TRUE;
} else {
*aStrict = PR_FALSE;
}
} else {
*aStrict = PR_FALSE;
}
}
if (*aStrict) {
if (negate) {
value = -value;
}
if (!aCanBePercent || !*aIsPercent) {
*aErrorCode = NS_OK;
#ifdef DEBUG
nsAutoString stringValue;
stringValue.AppendInt(value);
if (aCanBePercent && *aIsPercent) {
stringValue.AppendLiteral("%");
}
NS_ASSERTION(stringValue.Equals(aValue), "Wrong conversion!");
#endif
return value;
}
}
// Checking for integer overflow.
if (pValue > value) {
*aStrict = false;
*aErrorCode = NS_ERROR_ILLEGAL_VALUE;
break;
} else {
pValue = value;
*aErrorCode = NS_OK;
}
} else if (aCanBePercent && *iter == PRUnichar('%')) {
++iter;
*aIsPercent = true;
if (iter != end) {
*aStrict = false;
break;
}
} else {
*aStrict = false;
break;
}
}
if (negate) {
value = -value;
// Checking the special case of -0.
if (!value) {
*aStrict = false;
}
}
nsAutoString tmp(aValue);
return tmp.ToInteger(aErrorCode);
return value;
}
PRInt64

Просмотреть файл

@ -20190,7 +20190,7 @@ function test_size_attributes_parse_badsuffix() {
var canvas = document.getElementById('c637');
var ctx = canvas.getContext('2d');
todo(canvas.width == 100, "canvas.width == 100");
is(canvas.width, 100, "canvas.width == 100");
}
@ -20400,7 +20400,7 @@ var canvas = document.getElementById('c648');
var ctx = canvas.getContext('2d');
canvas.setAttribute('width', '100foo');
todo(canvas.width == 100, "canvas.width == 100");
is(canvas.width, 100, "canvas.width == 100");
}

Просмотреть файл

@ -462,11 +462,6 @@ function reflectBoolean(aParameters)
*/
function reflectInt(aParameters)
{
//TBD: Bug 673820: .setAttribute(exponential) -> incorrect reflection for element[attr]
function testExponential(value) {
return !!/^[ \t\n\f\r]*[\+\-]?[0-9]+e[0-9]+/.exec(value);
}
// Expected value returned by .getAttribute() when |value| has been previously passed to .setAttribute().
function expectedGetAttributeResult(value) {
return (value !== null) ? String(value) : "";
@ -556,34 +551,10 @@ function reflectInt(aParameters)
//TBD: Bug 586761: .setAttribute(attr, -2147483648) --> element[attr] == defaultValue instead of -2147483648
todo_is(element[attr], intValue, "Bug 586761: " + element.localName +
".setAttribute(value, " + v + "), " + element.localName + "[" + attr + "] ");
} else if (testExponential(v)) {
//TBD: Bug 673820: .setAttribute(exponential) -> incorrect reflection for element[attr]
todo_is(element[attr], intValue, "Bug 673820: " + element.localName +
".setAttribute(" + attr + ", " + v + "), " + element.localName + "[" + attr + "] ");
} else if (v == "why 567 what") {
//TBD: Bug 679672: .setAttribute() is somehow able to parse "why 567 what" into "567"
todo_is(element[attr], intValue, "Bug 679672: " + element.localName +
".setAttribute(" + attr + ", " + v + "), " + element.localName + "[" + attr + "] ");
} else if (v === "-0" && nonNegative) {
} else if ((v === "-0" || v == "-0xABCDEF") && nonNegative) {
//TBD: Bug 688093: Non-negative integers should return defaultValue when attempting to reflect "-0"
todo_is(element[attr], intValue, "Bug 688093: " + element.localName +
".setAttribute(" + attr + ", " + v + "), " + element.localName + "[" + attr + "] ");
} else if (v == "+42foo") {
//TBD: Bug: Unable to correctly parse "+" character in front of string
todo_is(element[attr], intValue, "Bug: " + element.localName +
".setAttribute(" + attr + ", " + v + "), " + element.localName + "[" + attr + "] ");
} else if (v == "0x10FFFF" && defaultValue != 0) {
//TBD: Bug: Integer attributes should parse "0x10FFFF" as 0, but instead incorrectly return defaultValue
todo_is(element[attr], intValue, "Bug: " + element.localName +
".setAttribute(" + attr + ", " + v + "), " + element.localName + "[" + attr + "] ");
} else if (v == "-0xABCDEF" && !nonNegative && defaultValue != 0) {
//TBD: Bug: Signed integer attributes should parse "-0xABCDEF" as -0, but instead incorrectly return defaultValue
todo_is(element[attr], intValue, "Bug: " + element.localName +
".setAttribute(" + attr + ", " + v + "), " + element.localName + "[" + attr + "] ");
} else if ((v == "++2" || v == "+-2" || v == "--2" || v == "-+2") && element[attr] != defaultValue) {
//TBD: Bug: Should not be able to parse strings with multiple sign characters, should return defaultValue
todo_is(element[attr], intValue, "Bug: " + element.localName +
".setAttribute(" + attr + ", " + v + "), " + element.localName + "[" + attr + "] ");
} else {
is(element[attr], intValue, element.localName +
".setAttribute(" + attr + ", " + v + "), " + element.localName + "[" + attr + "] ");

Просмотреть файл

@ -6,7 +6,7 @@
<th>0</th>
<td><table cellpadding="4" border="0"><tr><td>border="0"</td></tr></table></td>
<td><table cellpadding="4" border="0"><tr><td>border="0px"</td></tr></table></td>
<td><table cellpadding="4" border="1"><tr><td>border="0em"</td></tr></table></td>
<td><table cellpadding="4" border="0"><tr><td>border="0em"</td></tr></table></td>
</tr>
<tr>
<th>1</th>
@ -18,19 +18,19 @@
<th>2</th>
<td><table cellpadding="4" border="2"><tr><td>border="2"</td></tr></table></td>
<td><table cellpadding="4" border="2"><tr><td>border="2px"</td></tr></table></td>
<td><table cellpadding="4" border="1"><tr><td>border="2em"</td></tr></table></td>
<td><table cellpadding="4" border="2"><tr><td>border="2em"</td></tr></table></td>
</tr>
<tr>
<th>3</th>
<td><table cellpadding="4" border="3"><tr><td>border="3"</td></tr></table></td>
<td><table cellpadding="4" border="3"><tr><td>border="3px"</td></tr></table></td>
<td><table cellpadding="4" border="1"><tr><td>border="3em"</td></tr></table></td>
<td><table cellpadding="4" border="3"><tr><td>border="3em"</td></tr></table></td>
</tr>
<tr>
<th>10</th>
<td><table cellpadding="4" border="10"><tr><td>border="10"</td></tr></table></td>
<td><table cellpadding="4" border="10"><tr><td>border="10px"</td></tr></table></td>
<td><table cellpadding="4" border="1"><tr><td>border="10em"</td></tr></table></td>
<td><table cellpadding="4" border="10"><tr><td>border="10em"</td></tr></table></td>
</tr>
</table>