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_SCRIPT_HOLDER_CLASS(nsAttrChildContentList
)
32 virtual JSObject
* WrapObject(JSContext
* cx
,
33 JS::Handle
<JSObject
*> aGivenProto
) override
;
35 // nsINodeList interface
36 virtual int32_t IndexOf(nsIContent
* aContent
) override
;
37 virtual nsIContent
* Item(uint32_t aIndex
) override
;
38 uint32_t Length() override
;
39 nsINode
* GetParentObject() final
{ return mNode
; }
41 virtual void InvalidateCacheIfAvailable() {}
44 virtual ~nsAttrChildContentList() = default;
47 // The node whose children make up the list.
48 RefPtr
<nsINode
> mNode
;
51 class nsParentNodeChildContentList final
: public nsAttrChildContentList
{
53 explicit nsParentNodeChildContentList(nsINode
* aNode
)
54 : nsAttrChildContentList(aNode
), mIsCacheValid(false) {
58 // nsINodeList interface
59 virtual int32_t IndexOf(nsIContent
* aContent
) override
;
60 virtual nsIContent
* Item(uint32_t aIndex
) override
;
61 uint32_t Length() override
;
63 void InvalidateCacheIfAvailable() final
{ InvalidateCache(); }
65 void InvalidateCache() {
66 mIsCacheValid
= false;
67 mCachedChildArray
.Clear();
71 ~nsParentNodeChildContentList() = default;
73 // Return true if validation succeeds, false otherwise
76 // Whether cached array of child nodes is valid
79 // Cached array of child nodes
80 AutoTArray
<nsIContent
*, 8> mCachedChildArray
;
83 #endif /* nsChildContentList_h__ */