Bug 666200 - support select.add(element, long before). r=smaug

This commit is contained in:
Makoto Kato 2011-07-21 19:16:28 +09:00
parent 1445046528
commit e40f86cea7
6 changed files with 102 additions and 26 deletions

View File

@ -643,7 +643,7 @@ nsHTMLSelectElement::GetSelectFrame()
return select_frame;
}
NS_IMETHODIMP
nsresult
nsHTMLSelectElement::Add(nsIDOMHTMLElement* aElement,
nsIDOMHTMLElement* aBefore)
{
@ -679,6 +679,44 @@ nsHTMLSelectElement::Add(nsIDOMHTMLElement* aElement,
return parent->InsertBefore(aElement, aBefore, getter_AddRefs(added));
}
NS_IMETHODIMP
nsHTMLSelectElement::Add(nsIDOMHTMLElement* aElement,
nsIVariant* aBefore)
{
PRUint16 dataType;
nsresult rv = aBefore->GetDataType(&dataType);
NS_ENSURE_SUCCESS(rv, rv);
// aBefore is omitted or null
if (dataType == nsIDataType::VTYPE_EMPTY) {
return Add(aElement);
}
nsCOMPtr<nsISupports> supports;
nsCOMPtr<nsIDOMHTMLElement> beforeElement;
// whether aBefore is nsIDOMHTMLElement...
if (NS_SUCCEEDED(aBefore->GetAsISupports(getter_AddRefs(supports)))) {
beforeElement = do_QueryInterface(supports);
NS_ENSURE_TRUE(beforeElement, NS_ERROR_DOM_SYNTAX_ERR);
return Add(aElement, beforeElement);
}
// otherwise, whether aBefore is long
PRInt32 index;
NS_ENSURE_SUCCESS(aBefore->GetAsInt32(&index), NS_ERROR_DOM_SYNTAX_ERR);
// If item index is out of range, insert to last.
// (since beforeElement becomes null, it is inserted to last)
nsCOMPtr<nsIDOMNode> beforeNode;
if NS_SUCCEEDED(Item(index, getter_AddRefs(beforeNode))) {
beforeElement = do_QueryInterface(beforeNode);
}
return Add(aElement, beforeElement);
}
NS_IMETHODIMP
nsHTMLSelectElement::Remove(PRInt32 aIndex)
{
@ -2194,35 +2232,17 @@ nsHTMLOptionCollection::GetSelect(nsIDOMHTMLSelectElement **aReturn)
NS_IMETHODIMP
nsHTMLOptionCollection::Add(nsIDOMHTMLOptionElement *aOption,
PRInt32 aIndex, PRUint8 optional_argc)
nsIVariant *aBefore)
{
if (!aOption) {
return NS_ERROR_INVALID_ARG;
}
if (aIndex < -1) {
return NS_ERROR_DOM_INDEX_SIZE_ERR;
}
if (!mSelect) {
return NS_ERROR_NOT_INITIALIZED;
}
PRUint32 length;
GetLength(&length);
if (optional_argc == 0 || aIndex == -1 || aIndex > (PRInt32)length) {
// IE appends in these cases
aIndex = length;
}
nsCOMPtr<nsIDOMNode> beforeNode;
Item(aIndex, getter_AddRefs(beforeNode));
nsCOMPtr<nsIDOMHTMLOptionElement> beforeElement =
do_QueryInterface(beforeNode);
return mSelect->Add(aOption, beforeElement);
return mSelect->Add(aOption, aBefore);
}
NS_IMETHODIMP

View File

@ -612,6 +612,11 @@ protected:
return mSelectionHasChanged;
}
/**
* Insert aElement before the node given by aBefore
*/
nsresult Add(nsIDOMHTMLElement* aElement, nsIDOMHTMLElement* aBefore = nsnull);
/** The options[] array */
nsRefPtr<nsHTMLOptionCollection> mOptions;
/** false if the parser is in the middle of adding children. */

View File

@ -276,6 +276,7 @@ _TEST_FILES = \
test_bug659743.xml \
test_bug660663.html \
test_bug664299.html \
test_bug666200.html \
test_bug666666.html \
test_restore_from_parser_fragment.html \
$(NULL)

View File

@ -0,0 +1,40 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=666200
-->
<head>
<title>Test for Bug 666200</title>
<script type="text/javascript" src="/MochiKit/packed.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=666200">Mozilla Bug 666200</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 666200 **/
var sel = document.createElement("select");
var opt1 = new Option();
var opt2 = new Option();
var opt3 = new Option();
var opt4 = new Option();
opt1.value = 1;
opt2.value = 2;
opt3.value = 3;
opt4.value = 4;
sel.add(opt1);
sel.add(opt2, 0);
sel.add(opt3, 1000);
sel.options.add(opt4, opt3);
is(sel[0], opt2, "1st item should be 2");
is(sel[1], opt1, "2nd item should be 1");
is(sel[2], opt4, "3rd item should be 4");
is(sel[3], opt3, "4th item should be 3");
</script>
</pre>
</body>
</html>

View File

@ -53,7 +53,7 @@
interface nsIDOMValidityState;
[scriptable, uuid(5aeb2480-cb21-4483-b9b3-0c914502eb81)]
[scriptable, uuid(069bc0d8-d16d-406a-8555-2f84384c9b3b)]
interface nsIDOMHTMLSelectElement : nsIDOMHTMLElement
{
attribute boolean autofocus;
@ -69,8 +69,12 @@ interface nsIDOMHTMLSelectElement : nsIDOMHTMLElement
attribute unsigned long length;
nsIDOMNode item(in unsigned long index);
nsIDOMNode namedItem(in DOMString name);
// This add method implementation means the following
// since IDL doesn't support overfload.
// void add(in nsIDOMHTMLElement, [optional] in nsIDOMHTMLElement)
// void add(in nsIDOMHTMLElement, in long)
void add(in nsIDOMHTMLElement element,
[optional] in nsIDOMHTMLElement before)
[optional] in nsIVariant before)
raises(DOMException);
void remove(in long index);

View File

@ -41,8 +41,9 @@
interface nsIDOMHTMLOptionElement;
interface nsIDOMHTMLSelectElement;
interface nsIVariant;
[scriptable, uuid(a4f2b279-8096-48ea-8a95-640c5a55b697)]
[scriptable, uuid(03bd61d6-b851-465a-ab3f-99945f77cfa5)]
interface nsIDOMNSHTMLOptionCollection : nsISupports
{
attribute long selectedIndex;
@ -52,7 +53,12 @@ interface nsIDOMNSHTMLOptionCollection : nsISupports
[noscript] readonly attribute nsIDOMHTMLSelectElement select;
[optional_argc] void add(in nsIDOMHTMLOptionElement option,
[optional] in long index);
// This add method implementation means the following
// since IDL doesn't support overfload.
// void add(in nsIDOMHTMLOptionElement,
// [optional] in nsIDOMHTMLOptionElement)
// void add(in nsIDOMHTMLOptionElement, in long)
void add(in nsIDOMHTMLOptionElement option,
[optional] in nsIVariant before);
void remove(in long index);
};