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/. */
7 #ifndef mozilla_dom_HTMLFormControlsCollection_h
8 #define mozilla_dom_HTMLFormControlsCollection_h
10 #include "nsIHTMLCollection.h"
11 #include "nsInterfaceHashtable.h"
12 #include "mozilla/dom/TreeOrderedArray.h"
14 #include "nsWrapperCache.h"
16 class nsGenericHTMLFormElement
;
22 namespace mozilla::dom
{
24 class HTMLFormElement
;
25 class HTMLImageElement
;
26 class OwningRadioNodeListOrElement
;
30 class HTMLFormControlsCollection final
: public nsIHTMLCollection
,
31 public nsWrapperCache
{
33 explicit HTMLFormControlsCollection(HTMLFormElement
* aForm
);
35 void DropFormReference();
37 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
39 virtual uint32_t Length() override
;
40 virtual Element
* GetElementAt(uint32_t index
) override
;
41 virtual nsINode
* GetParentObject() override
;
43 virtual Element
* GetFirstNamedElement(const nsAString
& aName
,
44 bool& aFound
) override
;
46 void NamedGetter(const nsAString
& aName
, bool& aFound
,
47 Nullable
<OwningRadioNodeListOrElement
>& aResult
);
48 void NamedItem(const nsAString
& aName
,
49 Nullable
<OwningRadioNodeListOrElement
>& aResult
) {
51 NamedGetter(aName
, dummy
, aResult
);
53 virtual void GetSupportedNames(nsTArray
<nsString
>& aNames
) override
;
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
);
63 nsISupports
* NamedItemInternal(const nsAString
& aName
);
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
71 * @param aControls The list of sorted controls[out].
72 * @return NS_OK or NS_ERROR_OUT_OF_MEMORY.
74 nsresult
GetSortedControls(
75 nsTArray
<RefPtr
<nsGenericHTMLFormElement
>>& aControls
) const;
78 using nsWrapperCache::GetWrapper
;
79 using nsWrapperCache::GetWrapperPreserveColor
;
80 using nsWrapperCache::PreserveWrapper
;
81 virtual JSObject
* WrapObject(JSContext
* aCx
,
82 JS::Handle
<JSObject
*> aGivenProto
) override
;
85 virtual ~HTMLFormControlsCollection();
86 virtual JSObject
* GetWrapperPreserveColorInternal() override
{
87 return nsWrapperCache::GetWrapperPreserveColor();
89 virtual void PreserveWrapperInternal(
90 nsISupports
* aScriptObjectHolder
) override
{
91 nsWrapperCache::PreserveWrapper(aScriptObjectHolder
);
95 static bool ShouldBeInElements(nsIFormControl
* aFormControl
);
97 HTMLFormElement
* mForm
; // WEAK - the form owns me
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
;
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
;
110 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(HTMLFormControlsCollection
)
113 // Drop all our references to the form elements
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.
122 nsInterfaceHashtable
<nsStringHashKey
, nsISupports
> mNameLookupTable
;
125 } // namespace mozilla::dom
127 #endif // mozilla_dom_HTMLFormControlsCollection_h