2013-03-17 11:55:17 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "nsIDOMHTMLOptionsCollection.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"
|
|
|
|
|
|
|
|
class nsIDOMHTMLOptionElement;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
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)
|
|
|
|
*/
|
2014-08-05 21:33:55 +04:00
|
|
|
class HTMLOptionsCollection MOZ_FINAL : public nsIHTMLCollection
|
|
|
|
, public nsIDOMHTMLOptionsCollection
|
|
|
|
, public nsWrapperCache
|
2013-03-17 11:55:17 +04:00
|
|
|
{
|
|
|
|
typedef HTMLOptionElementOrHTMLOptGroupElement HTMLOptionOrOptGroupElement;
|
|
|
|
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
|
|
|
|
using nsWrapperCache::GetWrapperPreserveColor;
|
2014-04-09 02:27:18 +04:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2013-11-12 19:22:22 +04:00
|
|
|
protected:
|
2014-06-25 06:09:15 +04:00
|
|
|
virtual ~HTMLOptionsCollection();
|
|
|
|
|
2013-11-12 19:22:22 +04:00
|
|
|
virtual JSObject* GetWrapperPreserveColorInternal() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return nsWrapperCache::GetWrapperPreserveColor();
|
|
|
|
}
|
|
|
|
public:
|
2013-03-17 11:55:17 +04:00
|
|
|
|
|
|
|
// nsIDOMHTMLOptionsCollection interface
|
|
|
|
NS_DECL_NSIDOMHTMLOPTIONSCOLLECTION
|
|
|
|
|
|
|
|
// nsIDOMHTMLCollection interface, all its methods are defined in
|
|
|
|
// nsIDOMHTMLOptionsCollection
|
|
|
|
|
2015-01-06 23:13:36 +03:00
|
|
|
virtual Element* GetElementAt(uint32_t aIndex) MOZ_OVERRIDE;
|
2013-05-30 00:43:41 +04:00
|
|
|
virtual nsINode* GetParentObject() MOZ_OVERRIDE;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drop the reference to the select. Called during select destruction.
|
|
|
|
*/
|
|
|
|
void DropReference();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
virtual Element*
|
|
|
|
GetFirstNamedElement(const nsAString& aName, bool& aFound) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return NamedGetter(aName, aFound);
|
|
|
|
}
|
2013-03-17 11:55:17 +04:00
|
|
|
|
|
|
|
void Add(const HTMLOptionOrOptGroupElement& aElement,
|
|
|
|
const Nullable<HTMLElementOrLong>& aBefore,
|
|
|
|
ErrorResult& aError);
|
|
|
|
void Remove(int32_t aIndex, ErrorResult& aError);
|
|
|
|
int32_t GetSelectedIndex(ErrorResult& aError);
|
|
|
|
void SetSelectedIndex(int32_t aSelectedIndex, ErrorResult& aError);
|
|
|
|
void IndexedSetter(uint32_t aIndex, nsIDOMHTMLOptionElement* aOption,
|
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
|
|
|
aError = SetOption(aIndex, aOption);
|
|
|
|
}
|
2014-04-16 06:58:44 +04:00
|
|
|
virtual void GetSupportedNames(unsigned aFlags,
|
|
|
|
nsTArray<nsString>& aNames) MOZ_OVERRIDE;
|
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. */
|
|
|
|
nsTArray<nsRefPtr<mozilla::dom::HTMLOptionElement> > mElements;
|
|
|
|
/** The select element that contains this array */
|
2013-04-04 11:03:33 +04:00
|
|
|
HTMLSelectElement* mSelect;
|
2013-03-17 11:55:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_HTMLOptionsCollection_h
|