2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-03-17 11:55:17 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/HTMLOptionsCollection.h"
|
|
|
|
|
|
|
|
#include "HTMLOptGroupElement.h"
|
|
|
|
#include "mozAutoDocUpdate.h"
|
2013-09-10 11:03:38 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2018-06-22 19:48:42 +03:00
|
|
|
#include "mozilla/MappedDeclarations.h"
|
2016-06-16 10:24:16 +03:00
|
|
|
#include "mozilla/dom/HTMLFormSubmission.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "mozilla/dom/HTMLOptionElement.h"
|
|
|
|
#include "mozilla/dom/HTMLOptionsCollectionBinding.h"
|
2013-04-04 11:03:33 +04:00
|
|
|
#include "mozilla/dom/HTMLSelectElement.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "nsContentCreatorFunctions.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "nsIFormControlFrame.h"
|
|
|
|
#include "nsIForm.h"
|
|
|
|
#include "nsIFormProcessor.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
|
|
|
#include "nsMappedAttributes.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStyleConsts.h"
|
2014-04-16 06:58:44 +04:00
|
|
|
#include "jsfriendapi.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLOptionsCollection::HTMLOptionsCollection(HTMLSelectElement* aSelect)
|
2018-11-07 16:38:42 +03:00
|
|
|
: mSelect(aSelect) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-03-17 11:55:17 +04:00
|
|
|
nsresult HTMLOptionsCollection::GetOptionIndex(Element* aOption,
|
|
|
|
int32_t aStartIndex,
|
|
|
|
bool aForward, int32_t* aIndex) {
|
|
|
|
// NOTE: aIndex shouldn't be set if the returned value isn't NS_OK.
|
|
|
|
|
|
|
|
int32_t index;
|
|
|
|
|
|
|
|
// Make the common case fast
|
|
|
|
if (aStartIndex == 0 && aForward) {
|
|
|
|
index = mElements.IndexOf(aOption);
|
|
|
|
if (index == -1) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2013-03-17 11:55:17 +04:00
|
|
|
*aIndex = index;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t high = mElements.Length();
|
|
|
|
int32_t step = aForward ? 1 : -1;
|
|
|
|
|
|
|
|
for (index = aStartIndex; index < high && index > -1; index += step) {
|
|
|
|
if (mElements[index] == aOption) {
|
|
|
|
*aIndex = index;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-11-07 16:38:42 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLOptionsCollection, mElements, mSelect)
|
2013-03-17 11:55:17 +04:00
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
|
|
|
|
// QueryInterface implementation for HTMLOptionsCollection
|
|
|
|
NS_INTERFACE_TABLE_HEAD(HTMLOptionsCollection)
|
2014-08-26 03:21:35 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_INTERFACE_TABLE(HTMLOptionsCollection, nsIHTMLCollection)
|
2013-03-17 11:55:17 +04:00
|
|
|
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(HTMLOptionsCollection)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(HTMLOptionsCollection)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(HTMLOptionsCollection)
|
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* HTMLOptionsCollection::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return HTMLOptionsCollection_Binding::Wrap(aCx, this, aGivenProto);
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
2017-11-13 18:39:06 +03:00
|
|
|
uint32_t HTMLOptionsCollection::Length() { return mElements.Length(); }
|
2013-03-17 11:55:17 +04:00
|
|
|
|
2017-10-13 02:32:25 +03:00
|
|
|
void HTMLOptionsCollection::SetLength(uint32_t aLength, ErrorResult& aError) {
|
|
|
|
mSelect->SetLength(aLength, aError);
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:38:29 +03:00
|
|
|
void HTMLOptionsCollection::IndexedSetter(uint32_t aIndex,
|
|
|
|
HTMLOptionElement* aOption,
|
|
|
|
ErrorResult& aError) {
|
2013-03-17 11:55:17 +04:00
|
|
|
// if the new option is null, just remove this option. Note that it's safe
|
|
|
|
// to pass a too-large aIndex in here.
|
|
|
|
if (!aOption) {
|
|
|
|
mSelect->Remove(aIndex);
|
|
|
|
|
|
|
|
// We're done.
|
2016-11-19 00:38:29 +03:00
|
|
|
return;
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we're going to be setting an option in our collection
|
2016-11-19 00:38:29 +03:00
|
|
|
if (aIndex > mElements.Length()) {
|
2013-03-17 11:55:17 +04:00
|
|
|
// Fill our array with blank options up to (but not including, since we're
|
|
|
|
// about to change it) aIndex, for compat with other browsers.
|
2017-10-13 02:32:25 +03:00
|
|
|
SetLength(aIndex, aError);
|
|
|
|
ENSURE_SUCCESS_VOID(aError);
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:38:29 +03:00
|
|
|
NS_ASSERTION(aIndex <= mElements.Length(), "SetLength lied");
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2016-11-19 00:38:29 +03:00
|
|
|
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;
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:38:29 +03:00
|
|
|
parent->ReplaceChild(*aOption, *refChild, aError);
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
2019-01-22 22:03:07 +03:00
|
|
|
int32_t HTMLOptionsCollection::SelectedIndex() {
|
2017-10-13 02:32:25 +03:00
|
|
|
return mSelect->SelectedIndex();
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
2019-01-22 22:03:07 +03:00
|
|
|
void HTMLOptionsCollection::SetSelectedIndex(int32_t aSelectedIndex) {
|
|
|
|
mSelect->SetSelectedIndex(aSelectedIndex);
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Element* HTMLOptionsCollection::GetElementAt(uint32_t aIndex) {
|
|
|
|
return ItemAsOption(aIndex);
|
|
|
|
}
|
|
|
|
|
2013-11-11 11:55:41 +04:00
|
|
|
HTMLOptionElement* HTMLOptionsCollection::NamedGetter(const nsAString& aName,
|
|
|
|
bool& aFound) {
|
2013-06-01 10:56:34 +04:00
|
|
|
uint32_t count = mElements.Length();
|
2013-03-17 11:55:17 +04:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2013-06-01 10:56:34 +04:00
|
|
|
HTMLOptionElement* content = mElements.ElementAt(i);
|
2013-03-17 11:55:17 +04:00
|
|
|
if (content && (content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name,
|
|
|
|
aName, eCaseMatters) ||
|
|
|
|
content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::id,
|
|
|
|
aName, eCaseMatters))) {
|
2013-11-11 11:55:41 +04:00
|
|
|
aFound = true;
|
2013-03-17 11:55:17 +04:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 11:55:41 +04:00
|
|
|
aFound = false;
|
2013-03-17 11:55:17 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* HTMLOptionsCollection::GetParentObject() { return mSelect; }
|
|
|
|
|
2017-11-27 11:10:27 +03:00
|
|
|
DocGroup* HTMLOptionsCollection::GetDocGroup() const {
|
|
|
|
return mSelect ? mSelect->GetDocGroup() : nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-10 05:25:40 +03:00
|
|
|
void HTMLOptionsCollection::GetSupportedNames(nsTArray<nsString>& aNames) {
|
2017-10-03 01:05:19 +03:00
|
|
|
AutoTArray<nsAtom*, 8> atoms;
|
2013-03-17 11:55:17 +04:00
|
|
|
for (uint32_t i = 0; i < mElements.Length(); ++i) {
|
|
|
|
HTMLOptionElement* content = mElements.ElementAt(i);
|
|
|
|
if (content) {
|
|
|
|
// Note: HasName means the names is exposed on the document,
|
|
|
|
// which is false for options, so we don't check it here.
|
|
|
|
const nsAttrValue* val = content->GetParsedAttr(nsGkAtoms::name);
|
|
|
|
if (val && val->Type() == nsAttrValue::eAtom) {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* name = val->GetAtomValue();
|
2013-03-17 11:55:17 +04:00
|
|
|
if (!atoms.Contains(name)) {
|
|
|
|
atoms.AppendElement(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (content->HasID()) {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* id = content->GetID();
|
2013-03-17 11:55:17 +04:00
|
|
|
if (!atoms.Contains(id)) {
|
|
|
|
atoms.AppendElement(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 20:38:34 +03:00
|
|
|
uint32_t atomsLen = atoms.Length();
|
|
|
|
nsString* names = aNames.AppendElements(atomsLen);
|
|
|
|
for (uint32_t i = 0; i < atomsLen; ++i) {
|
|
|
|
atoms[i]->ToString(names[i]);
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLOptionsCollection::Add(const HTMLOptionOrOptGroupElement& aElement,
|
|
|
|
const Nullable<HTMLElementOrLong>& aBefore,
|
|
|
|
ErrorResult& aError) {
|
2013-04-13 11:01:04 +04:00
|
|
|
mSelect->Add(aElement, aBefore, aError);
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
|
|
|
|
2019-01-22 22:03:07 +03:00
|
|
|
void HTMLOptionsCollection::Remove(int32_t aIndex) { mSelect->Remove(aIndex); }
|
2013-03-17 11:55:17 +04:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|