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/. */
|
|
|
|
#ifndef mozilla_dom_HTMLOptionsCollection_h
|
|
|
|
#define mozilla_dom_HTMLOptionsCollection_h
|
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-03-17 11:55:17 +04:00
|
|
|
#include "nsIHTMLCollection.h"
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/HTMLOptionElement.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-11-27 11:10:27 +03:00
|
|
|
class DocGroup;
|
2014-10-01 22:43:26 +04:00
|
|
|
class HTMLElementOrLong;
|
|
|
|
class HTMLOptionElementOrHTMLOptGroupElement;
|
2013-04-04 11:03:33 +04:00
|
|
|
class HTMLSelectElement;
|
|
|
|
|
2013-03-17 11:55:17 +04:00
|
|
|
/**
|
|
|
|
* The collection of options in the select (what you get back when you do
|
|
|
|
* select.options in DOM)
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class HTMLOptionsCollection final : public nsIHTMLCollection,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsWrapperCache {
|
2013-03-17 11:55:17 +04:00
|
|
|
typedef HTMLOptionElementOrHTMLOptGroupElement HTMLOptionOrOptGroupElement;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-03-17 11:55:17 +04:00
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit HTMLOptionsCollection(HTMLSelectElement* aSelect);
|
2013-03-17 11:55:17 +04:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
|
2013-04-13 11:01:59 +04:00
|
|
|
// nsWrapperCache
|
2015-07-31 20:30:55 +03:00
|
|
|
using nsWrapperCache::GetWrapper;
|
2013-04-13 11:01:59 +04:00
|
|
|
using nsWrapperCache::GetWrapperPreserveColor;
|
2017-05-17 07:52:53 +03:00
|
|
|
using nsWrapperCache::PreserveWrapper;
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-12 19:22:22 +04:00
|
|
|
protected:
|
2018-11-07 16:38:42 +03:00
|
|
|
virtual ~HTMLOptionsCollection() = default;
|
2014-06-25 06:09:15 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* GetWrapperPreserveColorInternal() override {
|
2013-11-12 19:22:22 +04:00
|
|
|
return nsWrapperCache::GetWrapperPreserveColor();
|
|
|
|
}
|
2017-05-17 07:52:53 +03:00
|
|
|
virtual void PreserveWrapperInternal(
|
|
|
|
nsISupports* aScriptObjectHolder) override {
|
|
|
|
nsWrapperCache::PreserveWrapper(aScriptObjectHolder);
|
|
|
|
}
|
2013-03-17 11:55:17 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2017-11-13 18:39:06 +03:00
|
|
|
virtual uint32_t Length() override;
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual Element* GetElementAt(uint32_t aIndex) override;
|
|
|
|
virtual nsINode* GetParentObject() override;
|
2017-11-27 11:10:27 +03:00
|
|
|
DocGroup* GetDocGroup() const;
|
2013-03-17 11:55:17 +04:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(HTMLOptionsCollection,
|
|
|
|
nsIHTMLCollection)
|
|
|
|
|
2013-04-04 11:03:33 +04:00
|
|
|
// Helpers for HTMLSelectElement
|
2013-03-17 11:55:17 +04:00
|
|
|
/**
|
|
|
|
* Insert an option
|
|
|
|
* @param aOption the option to insert
|
|
|
|
* @param aIndex the index to insert at
|
|
|
|
*/
|
|
|
|
void InsertOptionAt(mozilla::dom::HTMLOptionElement* aOption,
|
|
|
|
uint32_t aIndex) {
|
|
|
|
mElements.InsertElementAt(aIndex, aOption);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an option
|
|
|
|
* @param aIndex the index of the option to remove
|
|
|
|
*/
|
|
|
|
void RemoveOptionAt(uint32_t aIndex) { mElements.RemoveElementAt(aIndex); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the option at the index
|
|
|
|
* @param aIndex the index
|
|
|
|
* @param aReturn the option returned [OUT]
|
|
|
|
*/
|
|
|
|
mozilla::dom::HTMLOptionElement* ItemAsOption(uint32_t aIndex) {
|
|
|
|
return mElements.SafeElementAt(aIndex, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears out all options
|
|
|
|
*/
|
|
|
|
void Clear() { mElements.Clear(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append an option to end of array
|
|
|
|
*/
|
|
|
|
void AppendOption(mozilla::dom::HTMLOptionElement* aOption) {
|
|
|
|
mElements.AppendElement(aOption);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the index of a given option element.
|
|
|
|
* If the option isn't part of the collection, return NS_ERROR_FAILURE
|
|
|
|
* without setting aIndex.
|
|
|
|
*
|
|
|
|
* @param aOption the option to get the index of
|
|
|
|
* @param aStartIndex the index to start looking at
|
|
|
|
* @param aForward TRUE to look forward, FALSE to look backward
|
|
|
|
* @return the option index
|
|
|
|
*/
|
|
|
|
nsresult GetOptionIndex(Element* aOption, int32_t aStartIndex, bool aForward,
|
|
|
|
int32_t* aIndex);
|
|
|
|
|
2013-11-11 11:55:41 +04:00
|
|
|
HTMLOptionElement* GetNamedItem(const nsAString& aName) {
|
|
|
|
bool dummy;
|
|
|
|
return NamedGetter(aName, dummy);
|
|
|
|
}
|
|
|
|
HTMLOptionElement* NamedGetter(const nsAString& aName, bool& aFound);
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual Element* GetFirstNamedElement(const nsAString& aName,
|
|
|
|
bool& aFound) override {
|
2013-11-11 11:55:41 +04:00
|
|
|
return NamedGetter(aName, aFound);
|
|
|
|
}
|
2013-03-17 11:55:17 +04:00
|
|
|
void Add(const HTMLOptionOrOptGroupElement& aElement,
|
|
|
|
const Nullable<HTMLElementOrLong>& aBefore, ErrorResult& aError);
|
2019-01-22 22:03:07 +03:00
|
|
|
void Remove(int32_t aIndex);
|
|
|
|
int32_t SelectedIndex();
|
|
|
|
void SetSelectedIndex(int32_t aSelectedIndex);
|
2016-11-19 00:38:29 +03:00
|
|
|
void IndexedSetter(uint32_t aIndex, HTMLOptionElement* aOption,
|
|
|
|
ErrorResult& aError);
|
2016-05-10 05:25:40 +03:00
|
|
|
virtual void GetSupportedNames(nsTArray<nsString>& aNames) override;
|
2017-10-13 02:32:25 +03:00
|
|
|
void SetLength(uint32_t aLength, ErrorResult& aError);
|
2013-03-17 11:55:17 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
/** The list of options (holds strong references). This is infallible, so
|
|
|
|
* various members such as InsertOptionAt are also infallible. */
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<mozilla::dom::HTMLOptionElement> > mElements;
|
2013-03-17 11:55:17 +04:00
|
|
|
/** The select element that contains this array */
|
2018-11-07 16:38:42 +03:00
|
|
|
RefPtr<HTMLSelectElement> mSelect;
|
2013-03-17 11:55:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_HTMLOptionsCollection_h
|