Bumping gaia.json for 8 gaia revision(s) a=gaia-bump
[gecko.git] / dom / html / HTMLPropertiesCollection.h
blobd8d8f39a6ce8c9d04e97ffd86898688412210a8d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set tw=80 expandtab softtabstop=2 ts=2 sw=2: */
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 HTMLPropertiesCollection_h_
8 #define HTMLPropertiesCollection_h_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/DOMStringList.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "nsAutoPtr.h"
14 #include "nsCOMArray.h"
15 #include "nsIMutationObserver.h"
16 #include "nsStubMutationObserver.h"
17 #include "nsBaseHashtable.h"
18 #include "nsINodeList.h"
19 #include "nsIHTMLCollection.h"
20 #include "nsHashKeys.h"
21 #include "nsRefPtrHashtable.h"
22 #include "nsGenericHTMLElement.h"
24 class nsIDocument;
25 class nsINode;
27 namespace mozilla {
28 namespace dom {
30 class HTMLPropertiesCollection;
31 class PropertyNodeList;
32 class Element;
34 class PropertyStringList : public DOMStringList
36 public:
37 explicit PropertyStringList(HTMLPropertiesCollection* aCollection);
38 NS_DECL_ISUPPORTS_INHERITED
39 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PropertyStringList, DOMStringList)
41 bool ContainsInternal(const nsAString& aString);
43 protected:
44 virtual ~PropertyStringList();
46 virtual void EnsureFresh() MOZ_OVERRIDE;
48 nsRefPtr<HTMLPropertiesCollection> mCollection;
51 class HTMLPropertiesCollection MOZ_FINAL : public nsIHTMLCollection,
52 public nsStubMutationObserver,
53 public nsWrapperCache
55 friend class PropertyNodeList;
56 friend class PropertyStringList;
57 public:
58 explicit HTMLPropertiesCollection(nsGenericHTMLElement* aRoot);
60 // nsWrapperCache
61 using nsWrapperCache::GetWrapperPreserveColor;
62 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
63 protected:
64 virtual ~HTMLPropertiesCollection();
66 virtual JSObject* GetWrapperPreserveColorInternal() MOZ_OVERRIDE
68 return nsWrapperCache::GetWrapperPreserveColor();
70 public:
72 virtual Element* GetElementAt(uint32_t aIndex) MOZ_OVERRIDE;
74 void SetDocument(nsIDocument* aDocument);
75 nsINode* GetParentObject() MOZ_OVERRIDE;
77 virtual Element*
78 GetFirstNamedElement(const nsAString& aName, bool& aFound) MOZ_OVERRIDE
80 // HTMLPropertiesCollection.namedItem and the named getter call the
81 // NamedItem that returns a PropertyNodeList, calling
82 // HTMLCollection.namedItem doesn't make sense so this returns null.
83 aFound = false;
84 return nullptr;
86 PropertyNodeList* NamedItem(const nsAString& aName);
87 PropertyNodeList* NamedGetter(const nsAString& aName, bool& aFound)
89 aFound = IsSupportedNamedProperty(aName);
90 return aFound ? NamedItem(aName) : nullptr;
92 bool NameIsEnumerable(const nsAString& aName)
94 return true;
96 DOMStringList* Names()
98 EnsureFresh();
99 return mNames;
101 virtual void GetSupportedNames(unsigned,
102 nsTArray<nsString>& aNames) MOZ_OVERRIDE;
104 NS_DECL_NSIDOMHTMLCOLLECTION
106 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
108 NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
109 NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
110 NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
111 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
113 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(HTMLPropertiesCollection,
114 nsIHTMLCollection)
116 protected:
117 // Make sure this collection is up to date, in case the DOM has been mutated.
118 void EnsureFresh();
120 // Crawl the properties of mRoot, following any itemRefs it may have
121 void CrawlProperties();
123 // Crawl startNode and its descendants, looking for items
124 void CrawlSubtree(Element* startNode);
126 bool IsSupportedNamedProperty(const nsAString& aName)
128 EnsureFresh();
129 return mNames->ContainsInternal(aName);
132 // the items that make up this collection
133 nsTArray<nsRefPtr<nsGenericHTMLElement> > mProperties;
135 // the itemprop attribute of the properties
136 nsRefPtr<PropertyStringList> mNames;
138 // The cached PropertyNodeLists that are NamedItems of this collection
139 nsRefPtrHashtable<nsStringHashKey, PropertyNodeList> mNamedItemEntries;
141 // The element this collection is rooted at
142 nsRefPtr<nsGenericHTMLElement> mRoot;
144 // The document mRoot is in, if any
145 nsCOMPtr<nsIDocument> mDoc;
147 // True if there have been DOM modifications since the last EnsureFresh call.
148 bool mIsDirty;
151 class PropertyNodeList MOZ_FINAL : public nsINodeList,
152 public nsStubMutationObserver
154 public:
155 PropertyNodeList(HTMLPropertiesCollection* aCollection,
156 nsIContent* aRoot, const nsAString& aName);
158 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
160 void SetDocument(nsIDocument* aDocument);
162 void GetValues(JSContext* aCx, nsTArray<JS::Value >& aResult,
163 ErrorResult& aError);
165 virtual nsIContent* Item(uint32_t aIndex) MOZ_OVERRIDE;
167 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
169 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(PropertyNodeList,
170 nsINodeList)
171 NS_DECL_NSIDOMNODELIST
173 NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
174 NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
175 NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
176 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
178 // nsINodeList interface
179 virtual int32_t IndexOf(nsIContent* aContent) MOZ_OVERRIDE;
180 virtual nsINode* GetParentObject() MOZ_OVERRIDE;
182 void AppendElement(nsGenericHTMLElement* aElement)
184 mElements.AppendElement(aElement);
187 void Clear()
189 mElements.Clear();
192 void SetDirty() { mIsDirty = true; }
194 protected:
195 virtual ~PropertyNodeList();
197 // Make sure this list is up to date, in case the DOM has been mutated.
198 void EnsureFresh();
200 // the the name that this list corresponds to
201 nsString mName;
203 // the document mParent is in, if any
204 nsCOMPtr<nsIDocument> mDoc;
206 // the collection that this list is a named item of
207 nsRefPtr<HTMLPropertiesCollection> mCollection;
209 // the node this list is rooted at
210 nsCOMPtr<nsINode> mParent;
212 // the properties that make up this list
213 nsTArray<nsRefPtr<nsGenericHTMLElement> > mElements;
215 // True if there have been DOM modifications since the last EnsureFresh call.
216 bool mIsDirty;
219 } // namespace dom
220 } // namespace mozilla
221 #endif // HTMLPropertiesCollection_h_