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 nsChildContentList_h__
8 #define nsChildContentList_h__
10 #include "mozilla/RefPtr.h"
11 #include "nsISupportsImpl.h"
12 #include "nsINodeList.h" // base class
13 #include "js/TypeDecls.h" // for Handle, Value, JSObject, JSContext
19 * Class that implements the nsINodeList interface (a list of children of
20 * the content), by holding a reference to the content and delegating Length
21 * and Item to its existing child list.
24 class nsAttrChildContentList
: public nsINodeList
{
26 explicit nsAttrChildContentList(nsINode
* aNode
) : mNode(aNode
) {}
28 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
29 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(nsAttrChildContentList
)
32 JSObject
* WrapObject(JSContext
*, JS::Handle
<JSObject
*> aGivenProto
) override
;
34 // nsINodeList interface
35 int32_t IndexOf(nsIContent
* aContent
) override
;
36 nsIContent
* Item(uint32_t aIndex
) override
;
37 uint32_t Length() override
;
38 nsINode
* GetParentObject() final
{ return mNode
; }
40 virtual void InvalidateCacheIfAvailable() {}
43 virtual ~nsAttrChildContentList() = default;
45 // The node whose children make up the list.
46 RefPtr
<nsINode
> mNode
;
49 class nsParentNodeChildContentList final
: public nsAttrChildContentList
{
51 explicit nsParentNodeChildContentList(nsINode
* aNode
)
52 : nsAttrChildContentList(aNode
) {
56 // nsINodeList interface
57 int32_t IndexOf(nsIContent
* aContent
) override
;
58 nsIContent
* Item(uint32_t aIndex
) override
;
59 uint32_t Length() override
;
61 void InvalidateCacheIfAvailable() final
{ InvalidateCache(); }
63 void InvalidateCache() {
64 mIsCacheValid
= false;
65 mCachedChildArray
.Clear();
69 ~nsParentNodeChildContentList() = default;
71 // Return true if validation succeeds, false otherwise
73 void EnsureCacheValid() {
77 MOZ_ASSERT(mIsCacheValid
);
80 // Whether cached array of child nodes is valid
81 bool mIsCacheValid
= false;
83 // Cached array of child nodes
84 AutoTArray
<nsIContent
*, 8> mCachedChildArray
;
87 #endif /* nsChildContentList_h__ */