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