Bug 1318479 part 3. Remove uses of nsIDOMNode::AppendChild in HTMLOptionsCollection. r=ehsan

This commit is contained in:
Boris Zbarsky 2016-11-18 16:38:29 -05:00
Родитель 6cee715d54
Коммит afbcfe1a92
4 изменённых файлов: 35 добавлений и 41 удалений

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

@ -131,12 +131,13 @@ HTMLOptionsCollection::SetLength(uint32_t aLength)
return mSelect->SetLength(aLength); return mSelect->SetLength(aLength);
} }
NS_IMETHODIMP void
HTMLOptionsCollection::SetOption(uint32_t aIndex, HTMLOptionsCollection::IndexedSetter(uint32_t aIndex,
nsIDOMHTMLOptionElement* aOption) HTMLOptionElement* aOption,
ErrorResult& aError)
{ {
if (!mSelect) { if (!mSelect) {
return NS_OK; return;
} }
// if the new option is null, just remove this option. Note that it's safe // if the new option is null, just remove this option. Note that it's safe
@ -145,43 +146,41 @@ HTMLOptionsCollection::SetOption(uint32_t aIndex,
mSelect->Remove(aIndex); mSelect->Remove(aIndex);
// We're done. // We're done.
return NS_OK; return;
} }
nsresult rv = NS_OK;
uint32_t index = uint32_t(aIndex);
// Now we're going to be setting an option in our collection // Now we're going to be setting an option in our collection
if (index > mElements.Length()) { if (aIndex > mElements.Length()) {
// Fill our array with blank options up to (but not including, since we're // Fill our array with blank options up to (but not including, since we're
// about to change it) aIndex, for compat with other browsers. // about to change it) aIndex, for compat with other browsers.
rv = SetLength(index); nsresult rv = SetLength(aIndex);
NS_ENSURE_SUCCESS(rv, rv); if (NS_WARN_IF(NS_FAILED(rv))) {
} aError.Throw(rv);
return;
NS_ASSERTION(index <= mElements.Length(), "SetLength lied");
nsCOMPtr<nsIDOMNode> ret;
if (index == mElements.Length()) {
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aOption);
rv = mSelect->AppendChild(node, getter_AddRefs(ret));
} else {
// Find the option they're talking about and replace it
// hold a strong reference to follow COM rules.
RefPtr<HTMLOptionElement> refChild = ItemAsOption(index);
NS_ENSURE_TRUE(refChild, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsINode> parent = refChild->GetParent();
if (parent) {
nsCOMPtr<nsINode> node = do_QueryInterface(aOption);
ErrorResult res;
parent->ReplaceChild(*node, *refChild, res);
rv = res.StealNSResult();
} }
} }
return rv; NS_ASSERTION(aIndex <= mElements.Length(), "SetLength lied");
if (aIndex == mElements.Length()) {
mSelect->AppendChild(*aOption, aError);
return;
}
// Find the option they're talking about and replace it
// hold a strong reference to follow COM rules.
RefPtr<HTMLOptionElement> refChild = ItemAsOption(aIndex);
if (!refChild) {
aError.Throw(NS_ERROR_UNEXPECTED);
return;
}
nsCOMPtr<nsINode> parent = refChild->GetParent();
if (!parent) {
return;
}
parent->ReplaceChild(*aOption, *refChild, aError);
} }
int32_t int32_t

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

@ -149,11 +149,8 @@ public:
void Remove(int32_t aIndex, ErrorResult& aError); void Remove(int32_t aIndex, ErrorResult& aError);
int32_t GetSelectedIndex(ErrorResult& aError); int32_t GetSelectedIndex(ErrorResult& aError);
void SetSelectedIndex(int32_t aSelectedIndex, ErrorResult& aError); void SetSelectedIndex(int32_t aSelectedIndex, ErrorResult& aError);
void IndexedSetter(uint32_t aIndex, nsIDOMHTMLOptionElement* aOption, void IndexedSetter(uint32_t aIndex, HTMLOptionElement* aOption,
ErrorResult& aError) ErrorResult& aError);
{
aError = SetOption(aIndex, aOption);
}
virtual void GetSupportedNames(nsTArray<nsString>& aNames) override; virtual void GetSupportedNames(nsTArray<nsString>& aNames) override;
private: private:

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

@ -272,6 +272,7 @@ public:
// nsIConstraintValidation::SetCustomValidity() is fine. // nsIConstraintValidation::SetCustomValidity() is fine.
using nsINode::Remove; using nsINode::Remove;
using nsINode::AppendChild;
// nsINode // nsINode

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

@ -36,9 +36,6 @@ interface nsIDOMHTMLOptionsCollection : nsISupports
attribute long selectedIndex; attribute long selectedIndex;
[noscript] void setOption(in unsigned long index,
in nsIDOMHTMLOptionElement option);
[noscript] readonly attribute nsIDOMHTMLSelectElement select; [noscript] readonly attribute nsIDOMHTMLSelectElement select;
// This add method implementation means the following // This add method implementation means the following