Bug 536895 - Setting a negative maxLength should throw an exception, r=smaug, sr=jst

--HG--
extra : rebase_source : d54814499d4114f239678941da3368d1a4aaff5f
This commit is contained in:
mounir.lamouri@gmail.com 2010-03-05 21:42:46 +02:00
parent 11338cfacc
commit bd5b2d6dc1
7 changed files with 84 additions and 5 deletions

View File

@ -1039,6 +1039,31 @@ NS_NewHTML##_elementName##Element(nsINodeInfo *aNodeInfo, PRBool aFromParser)\
return SetAttrHelper(nsGkAtoms::_atom, aValue); \
}
/**
* A macro to implement the getter and setter for a given content
* property that needs to set a non-negative integer. The method
* uses the generic GetAttr and SetAttr methods. This macro is much
* like the NS_IMPL_INT_ATTR macro except we throw an exception if
* the set value is negative.
*/
#define NS_IMPL_NON_NEGATIVE_INT_ATTR(_class, _method, _atom) \
NS_IMPL_NON_NEGATIVE_INT_ATTR_DEFAULT_VALUE(_class, _method, _atom, -1)
#define NS_IMPL_NON_NEGATIVE_INT_ATTR_DEFAULT_VALUE(_class, _method, _atom, _default) \
NS_IMETHODIMP \
_class::Get##_method(PRInt32* aValue) \
{ \
return GetIntAttr(nsGkAtoms::_atom, _default, aValue); \
} \
NS_IMETHODIMP \
_class::Set##_method(PRInt32 aValue) \
{ \
if (aValue < 0) { \
return NS_ERROR_DOM_INDEX_SIZE_ERR; \
} \
return SetIntAttr(nsGkAtoms::_atom, aValue); \
}
/**
* QueryInterface() implementation helper macros
*/

View File

@ -755,7 +755,7 @@ NS_IMPL_STRING_ATTR(nsHTMLInputElement, Alt, alt)
//NS_IMPL_BOOL_ATTR(nsHTMLInputElement, Checked, checked)
NS_IMPL_BOOL_ATTR(nsHTMLInputElement, Disabled, disabled)
NS_IMPL_BOOL_ATTR(nsHTMLInputElement, Multiple, multiple)
NS_IMPL_INT_ATTR(nsHTMLInputElement, MaxLength, maxlength)
NS_IMPL_NON_NEGATIVE_INT_ATTR(nsHTMLInputElement, MaxLength, maxlength)
NS_IMPL_STRING_ATTR(nsHTMLInputElement, Name, name)
NS_IMPL_BOOL_ATTR(nsHTMLInputElement, ReadOnly, readonly)
NS_IMPL_URI_ATTR(nsHTMLInputElement, Src, src)

View File

@ -373,7 +373,7 @@ nsHTMLTextAreaElement::IsHTMLFocusable(PRBool *aIsFocusable, PRInt32 *aTabIndex)
NS_IMPL_STRING_ATTR(nsHTMLTextAreaElement, AccessKey, accesskey)
NS_IMPL_INT_ATTR(nsHTMLTextAreaElement, Cols, cols)
NS_IMPL_BOOL_ATTR(nsHTMLTextAreaElement, Disabled, disabled)
NS_IMPL_INT_ATTR(nsHTMLTextAreaElement, MaxLength, maxlength)
NS_IMPL_NON_NEGATIVE_INT_ATTR(nsHTMLTextAreaElement, MaxLength, maxlength)
NS_IMPL_STRING_ATTR(nsHTMLTextAreaElement, Name, name)
NS_IMPL_BOOL_ATTR(nsHTMLTextAreaElement, ReadOnly, readonly)
NS_IMPL_INT_ATTR(nsHTMLTextAreaElement, Rows, rows)

View File

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

View File

@ -71,7 +71,7 @@ SimpleTest.waitForFocus(function() {
caught = true;
}
if (testNums[i] < 0) {
todo(caught, 'Setting negative maxLength should throw exception');
ok(caught, 'Setting negative maxLength should throw exception');
} else {
ok(!caught, 'Setting nonnegative maxLength should not throw exception');
}

View File

@ -40,8 +40,7 @@ function checkNegativeMaxLength(element)
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");
/* we do not check when changing the value from the DOM because it is throwing an exception, see bug 536895 */
}
/* TODO: correct behavior may be checked for email, telephone, url and search input types */

View File

@ -0,0 +1,54 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=536895
-->
<head>
<title>Test for Bug 536895</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=536895">Mozilla Bug 536895</a>
<p id="display"></p>
<div id="content" style="display: none">
<textarea id="t"></textarea>
<input id="i" type="text">
<input id="p" type="password">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 536895 **/
function checkNegativeMaxLengthException(element)
{
caught = false;
try {
element.setAttribute('maxLength', -10);
} catch(e) {
caught = true;
}
ok(!caught, "Setting maxLength attribute to a negative value shouldn't throw an exception");
caught = false;
try {
element.maxLength = -20;
} catch(e) {
caught = true;
}
ok(caught, "Setting negative maxLength from the DOM should throw an INDEX_SIZE_ERR exception");
is(element.getAttribute('maxLength'), -10, "When the exception is raised, the maxLength attribute shouldn't change");
}
/* TODO: correct behavior may be checked for email, telephone, url and search input types */
checkNegativeMaxLengthException(document.getElementById('t'));
checkNegativeMaxLengthException(document.getElementById('i'));
checkNegativeMaxLengthException(document.getElementById('p'));
</script>
</pre>
</body>
</html>