Bug 536891 - Negative maxlength should be treated the same as unspecified maxlength, r=smaug, sr=jst

--HG--
extra : rebase_source : eef5330e6de87f9b9a5189bd26f13418c404d659
This commit is contained in:
mounir.lamouri@gmail.com 2010-03-05 21:38:26 +02:00
parent 5f9a4782f7
commit b243710610
7 changed files with 89 additions and 3 deletions

View File

@ -1058,6 +1058,25 @@ nsAttrValue::ParseIntWithBounds(const nsAString& aString,
return PR_TRUE;
}
PRBool
nsAttrValue::ParseNonNegativeIntValue(const nsAString& aString)
{
ResetIfSet();
PRInt32 ec;
PRBool strict;
PRInt32 originalVal = StringToInteger(aString, &strict, &ec);
if (NS_FAILED(ec)) {
return PR_FALSE;
}
PRInt32 val = PR_MAX(originalVal, -1);
strict = strict && (originalVal == val);
SetIntValueAndType(val, eInteger, strict ? nsnull : &aString);
return PR_TRUE;
}
void
nsAttrValue::SetColorValue(nscolor aColor, const nsAString& aString)
{

View File

@ -240,6 +240,17 @@ public:
PRBool ParseIntWithBounds(const nsAString& aString, PRInt32 aMin,
PRInt32 aMax = PR_INT32_MAX);
/**
* Parse a string value into a non-negative integer.
* This method follows the rules for parsing non-negative integer from:
* http://dev.w3.org/html5/spec/infrastructure.html#rules-for-parsing-non-negative-integers
* If the parsed value is negative, the value will be set to -1.
*
* @param aString the string to parse
* @return whether the value could be parsed
*/
PRBool ParseNonNegativeIntValue(const nsAString& aString);
/**
* Parse a string into a color.
*

View File

@ -2206,7 +2206,7 @@ nsHTMLInputElement::ParseAttribute(PRInt32 aNamespaceID,
return aResult.ParseSpecialIntValue(aValue, PR_TRUE);
}
if (aAttribute == nsGkAtoms::maxlength) {
return aResult.ParseIntWithBounds(aValue, 0);
return aResult.ParseNonNegativeIntValue(aValue);
}
if (aAttribute == nsGkAtoms::size) {
return aResult.ParseIntWithBounds(aValue, 0);

View File

@ -527,7 +527,7 @@ nsHTMLTextAreaElement::ParseAttribute(PRInt32 aNamespaceID,
{
if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::maxlength) {
return aResult.ParseIntWithBounds(aValue, 0);
return aResult.ParseNonNegativeIntValue(aValue);
}
if (aAttribute == nsGkAtoms::cols) {
return aResult.ParseIntWithBounds(aValue, 0);

View File

@ -155,6 +155,7 @@ _TEST_FILES = test_bug589.html \
test_bug535043.html \
test_bug547850.html \
test_bug457800.html \
test_bug536891.html \
$(NULL)
libs:: $(_TEST_FILES)

View File

@ -38,7 +38,7 @@ function checkTextArea(textArea) {
} else if (htmlMaxLength < 0) {
// Per the HTML5 spec, out-of-range values are supposed to translate to -1,
// not 0, but they don't?
todo_is(domMaxLength, -1,
is(domMaxLength, -1,
'maxlength is out of range but maxLength DOM attribute is not -1');
} else {
is(domMaxLength, parseInt(htmlMaxLength),

View File

@ -0,0 +1,55 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=536891
-->
<head>
<title>Test for Bug 536891</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/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=536891">Mozilla Bug 536891</a>
<p id="display"></p>
<div id="content" style="display: none">
<textarea id="t" maxlength="-2"></textarea>
<input id="i" type="text" maxlength="-2">
<input id="p" type="password" maxlength="-2">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 536891 **/
function checkNegativeMaxLength(element)
{
/* maxLength is set to -2 initially in the document, see above */
is(element.maxLength, -1, "negative maxLength should be considered invalid and represented as -1");
element.setAttribute('maxLength', -15);
is(element.maxLength, -1, "negative maxLength is not processed correctly when set dynamically");
is(element.getAttribute('maxLength'), -15, "maxLength attribute doesn't return the correct value");
element.setAttribute('maxLength', 0);
is(element.maxLength, 0, "negative maxLength is not processed correctly");
element.setAttribute('maxLength', 2147483647); /* PR_INT32_MAX */
is(element.maxLength, 2147483647, "negative maxLength is not processed correctly");
element.setAttribute('maxLength', -2147483648); /* PR_INT32_MIN */
is(element.maxLength, -1, "negative maxLength is not processed correctly");
element.setAttribute('maxLength', 'non-numerical-value');
is(element.maxLength, -1, "non-numerical value should be considered invalid and represented as -1");
element.maxLength = -10;
is(element.maxLength, -1, "negative maxLength is not processed correctly when set dynamically from the DOM");
}
/* TODO: correct behavior may be checked for email, telephone, url and search input types */
checkNegativeMaxLength(document.getElementById('t'));
checkNegativeMaxLength(document.getElementById('i'));
checkNegativeMaxLength(document.getElementById('p'));
</script>
</pre>
</body>
</html>