HTMLOptionsCollection.h (4849B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #ifndef mozilla_dom_HTMLOptionsCollection_h 7 #define mozilla_dom_HTMLOptionsCollection_h 8 9 #include "mozilla/dom/HTMLOptionElement.h" 10 #include "nsCOMPtr.h" 11 #include "nsError.h" 12 #include "nsGenericHTMLElement.h" 13 #include "nsIHTMLCollection.h" 14 #include "nsTArray.h" 15 #include "nsWrapperCache.h" 16 17 namespace mozilla { 18 class ErrorResult; 19 20 namespace dom { 21 22 class DocGroup; 23 class HTMLElementOrLong; 24 class HTMLOptionElementOrHTMLOptGroupElement; 25 class HTMLSelectElement; 26 27 /** 28 * The collection of options in the select (what you get back when you do 29 * select.options in DOM) 30 */ 31 class HTMLOptionsCollection final : public nsIHTMLCollection, 32 public nsWrapperCache { 33 typedef HTMLOptionElementOrHTMLOptGroupElement HTMLOptionOrOptGroupElement; 34 35 public: 36 explicit HTMLOptionsCollection(HTMLSelectElement* aSelect); 37 38 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 39 40 // nsWrapperCache 41 using nsWrapperCache::GetWrapper; 42 using nsWrapperCache::GetWrapperPreserveColor; 43 using nsWrapperCache::PreserveWrapper; 44 virtual JSObject* WrapObject(JSContext* aCx, 45 JS::Handle<JSObject*> aGivenProto) override; 46 47 protected: 48 virtual ~HTMLOptionsCollection() = default; 49 50 virtual JSObject* GetWrapperPreserveColorInternal() override { 51 return nsWrapperCache::GetWrapperPreserveColor(); 52 } 53 virtual void PreserveWrapperInternal( 54 nsISupports* aScriptObjectHolder) override { 55 nsWrapperCache::PreserveWrapper(aScriptObjectHolder); 56 } 57 58 public: 59 virtual uint32_t Length() override; 60 virtual Element* GetElementAt(uint32_t aIndex) override; 61 virtual nsINode* GetParentObject() override; 62 DocGroup* GetDocGroup() const; 63 64 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(HTMLOptionsCollection, 65 nsIHTMLCollection) 66 67 // Helpers for HTMLSelectElement 68 /** 69 * Insert an option 70 * @param aOption the option to insert 71 * @param aIndex the index to insert at 72 */ 73 void InsertOptionAt(mozilla::dom::HTMLOptionElement* aOption, 74 uint32_t aIndex) { 75 mElements.InsertElementAt(aIndex, aOption); 76 } 77 78 /** 79 * Remove an option 80 * @param aIndex the index of the option to remove 81 */ 82 void RemoveOptionAt(uint32_t aIndex) { mElements.RemoveElementAt(aIndex); } 83 84 /** 85 * Get the option at the index 86 * @param aIndex the index 87 * @param aReturn the option returned [OUT] 88 */ 89 mozilla::dom::HTMLOptionElement* ItemAsOption(uint32_t aIndex) { 90 return mElements.SafeElementAt(aIndex, nullptr); 91 } 92 93 /** 94 * Clears out all options 95 */ 96 void Clear() { mElements.Clear(); } 97 98 /** 99 * Append an option to end of array 100 */ 101 void AppendOption(mozilla::dom::HTMLOptionElement* aOption) { 102 mElements.AppendElement(aOption); 103 } 104 105 /** 106 * Finds the index of a given option element. 107 * If the option isn't part of the collection, return NS_ERROR_FAILURE 108 * without setting aIndex. 109 * 110 * @param aOption the option to get the index of 111 * @param aStartIndex the index to start looking at 112 * @param aForward TRUE to look forward, FALSE to look backward 113 * @return the option index 114 */ 115 nsresult GetOptionIndex(Element* aOption, int32_t aStartIndex, bool aForward, 116 int32_t* aIndex); 117 118 HTMLOptionElement* GetNamedItem(const nsAString& aName) { 119 bool dummy; 120 return NamedGetter(aName, dummy); 121 } 122 HTMLOptionElement* NamedGetter(const nsAString& aName, bool& aFound); 123 virtual Element* GetFirstNamedElement(const nsAString& aName, 124 bool& aFound) override { 125 return NamedGetter(aName, aFound); 126 } 127 void Add(const HTMLOptionOrOptGroupElement& aElement, 128 const Nullable<HTMLElementOrLong>& aBefore, ErrorResult& aError); 129 void Remove(int32_t aIndex); 130 int32_t SelectedIndex(); 131 void SetSelectedIndex(int32_t aSelectedIndex); 132 void IndexedSetter(uint32_t aIndex, HTMLOptionElement* aOption, 133 ErrorResult& aError); 134 virtual void GetSupportedNames(nsTArray<nsString>& aNames) override; 135 void SetLength(uint32_t aLength, ErrorResult& aError); 136 137 private: 138 /** The list of options (holds strong references). This is infallible, so 139 * various members such as InsertOptionAt are also infallible. */ 140 nsTArray<RefPtr<mozilla::dom::HTMLOptionElement> > mElements; 141 /** The select element that contains this array */ 142 RefPtr<HTMLSelectElement> mSelect; 143 }; 144 145 } // namespace dom 146 } // namespace mozilla 147 148 #endif // mozilla_dom_HTMLOptionsCollection_h