HTMLFormControlsCollection.h (4623B)
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 7 #ifndef mozilla_dom_HTMLFormControlsCollection_h 8 #define mozilla_dom_HTMLFormControlsCollection_h 9 10 #include "mozilla/dom/TreeOrderedArray.h" 11 #include "nsIHTMLCollection.h" 12 #include "nsInterfaceHashtable.h" 13 #include "nsTArray.h" 14 #include "nsWrapperCache.h" 15 16 class nsGenericHTMLFormElement; 17 class nsIContent; 18 class nsIFormControl; 19 template <class T> 20 class RefPtr; 21 22 namespace mozilla::dom { 23 class Element; 24 class HTMLFormElement; 25 class HTMLImageElement; 26 class OwningRadioNodeListOrElement; 27 template <typename> 28 struct Nullable; 29 30 class HTMLFormControlsCollection final : public nsIHTMLCollection, 31 public nsWrapperCache { 32 public: 33 explicit HTMLFormControlsCollection(HTMLFormElement* aForm); 34 35 void DropFormReference(); 36 37 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 38 39 virtual uint32_t Length() override; 40 virtual Element* GetElementAt(uint32_t index) override; 41 virtual nsINode* GetParentObject() override; 42 43 virtual Element* GetFirstNamedElement(const nsAString& aName, 44 bool& aFound) override; 45 46 void NamedGetter(const nsAString& aName, bool& aFound, 47 Nullable<OwningRadioNodeListOrElement>& aResult); 48 void NamedItem(const nsAString& aName, 49 Nullable<OwningRadioNodeListOrElement>& aResult) { 50 bool dummy; 51 NamedGetter(aName, dummy, aResult); 52 } 53 virtual void GetSupportedNames(nsTArray<nsString>& aNames) override; 54 55 nsresult AddElementToTable(nsGenericHTMLFormElement* aChild, 56 const nsAString& aName); 57 nsresult AddImageElementToTable(HTMLImageElement* aChild, 58 const nsAString& aName); 59 nsresult RemoveElementFromTable(nsGenericHTMLFormElement* aChild, 60 const nsAString& aName); 61 nsresult IndexOfContent(nsIContent* aContent, int32_t* aIndex); 62 63 nsISupports* NamedItemInternal(const nsAString& aName); 64 65 /** 66 * Create a sorted list of form control elements. This list is sorted 67 * in document order and contains the controls in the mElements and 68 * mNotInElements list. This function does not add references to the 69 * elements. 70 * 71 * @param aControls The list of sorted controls[out]. 72 * @return NS_OK or NS_ERROR_OUT_OF_MEMORY. 73 */ 74 nsresult GetSortedControls( 75 nsTArray<RefPtr<nsGenericHTMLFormElement>>& aControls) const; 76 77 // nsWrapperCache 78 using nsWrapperCache::GetWrapper; 79 using nsWrapperCache::GetWrapperPreserveColor; 80 using nsWrapperCache::PreserveWrapper; 81 virtual JSObject* WrapObject(JSContext* aCx, 82 JS::Handle<JSObject*> aGivenProto) override; 83 84 protected: 85 virtual ~HTMLFormControlsCollection(); 86 virtual JSObject* GetWrapperPreserveColorInternal() override { 87 return nsWrapperCache::GetWrapperPreserveColor(); 88 } 89 virtual void PreserveWrapperInternal( 90 nsISupports* aScriptObjectHolder) override { 91 nsWrapperCache::PreserveWrapper(aScriptObjectHolder); 92 } 93 94 public: 95 static bool ShouldBeInElements(const nsIFormControl* aFormControl); 96 97 HTMLFormElement* mForm; // WEAK - the form owns me 98 99 // Holds WEAK references - bug 36639 100 // NOTE(emilio): These are not guaranteed to be descendants of mForm, because 101 // of the form attribute, though that's likely. 102 TreeOrderedArray<nsGenericHTMLFormElement*> mElements; 103 104 // This array holds on to all form controls that are not contained 105 // in mElements (form.elements in JS, see ShouldBeInFormControl()). 106 // This is needed to properly clean up the bi-directional references 107 // (both weak and strong) between the form and its form controls. 108 TreeOrderedArray<nsGenericHTMLFormElement*> mNotInElements; 109 110 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(HTMLFormControlsCollection) 111 112 protected: 113 // Drop all our references to the form elements 114 void Clear(); 115 116 // A map from an ID or NAME attribute to the form control(s), this 117 // hash holds strong references either to the named form control, or 118 // to a list of named form controls, in the case where this hash 119 // holds on to a list of named form controls the list has weak 120 // references to the form control. 121 122 nsInterfaceHashtable<nsStringHashKey, nsISupports> mNameLookupTable; 123 }; 124 125 } // namespace mozilla::dom 126 127 #endif // mozilla_dom_HTMLFormControlsCollection_h