/src/mozilla-central/dom/base/nsChildContentList.h
Line | Count | Source (jump to first uncovered line) |
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 nsChildContentList_h__ |
8 | | #define nsChildContentList_h__ |
9 | | |
10 | | #include "nsISupportsImpl.h" |
11 | | #include "nsINodeList.h" // base class |
12 | | #include "js/TypeDecls.h" // for Handle, Value, JSObject, JSContext |
13 | | |
14 | | class nsIContent; |
15 | | class nsINode; |
16 | | |
17 | | /** |
18 | | * Class that implements the nsINodeList interface (a list of children of |
19 | | * the content), by holding a reference to the content and delegating Length |
20 | | * and Item to its existing child list. |
21 | | * @see nsINodeList |
22 | | */ |
23 | | class nsAttrChildContentList : public nsINodeList |
24 | | { |
25 | | public: |
26 | | explicit nsAttrChildContentList(nsINode* aNode) |
27 | | : mNode(aNode) |
28 | 0 | { |
29 | 0 | } |
30 | | |
31 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
32 | | NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsAttrChildContentList) |
33 | | |
34 | | // nsWrapperCache |
35 | | virtual JSObject* WrapObject(JSContext *cx, |
36 | | JS::Handle<JSObject*> aGivenProto) override; |
37 | | |
38 | | // nsINodeList interface |
39 | | virtual int32_t IndexOf(nsIContent* aContent) override; |
40 | | virtual nsIContent* Item(uint32_t aIndex) override; |
41 | | uint32_t Length() override; |
42 | | |
43 | | virtual void DropReference() |
44 | 0 | { |
45 | 0 | mNode = nullptr; |
46 | 0 | } |
47 | | |
48 | | virtual nsINode* GetParentObject() override |
49 | 0 | { |
50 | 0 | return mNode; |
51 | 0 | } |
52 | | |
53 | | protected: |
54 | 0 | virtual ~nsAttrChildContentList() {} |
55 | | |
56 | | private: |
57 | | // The node whose children make up the list. |
58 | | // This is a non-owning ref which is safe because it's set to nullptr by |
59 | | // DropReference() by the node slots get destroyed. |
60 | | nsINode* MOZ_NON_OWNING_REF mNode; |
61 | | }; |
62 | | |
63 | | class nsParentNodeChildContentList final : public nsAttrChildContentList |
64 | | { |
65 | | public: |
66 | | explicit nsParentNodeChildContentList(nsINode* aNode) |
67 | | : nsAttrChildContentList(aNode) |
68 | | , mIsCacheValid(false) |
69 | 0 | { |
70 | 0 | ValidateCache(); |
71 | 0 | } |
72 | | |
73 | | // nsINodeList interface |
74 | | virtual int32_t IndexOf(nsIContent* aContent) override; |
75 | | virtual nsIContent* Item(uint32_t aIndex) override; |
76 | | uint32_t Length() override; |
77 | | |
78 | | void DropReference() override |
79 | 0 | { |
80 | 0 | InvalidateCache(); |
81 | 0 | nsAttrChildContentList::DropReference(); |
82 | 0 | } |
83 | | |
84 | | void InvalidateCache() |
85 | 0 | { |
86 | 0 | mIsCacheValid = false; |
87 | 0 | mCachedChildArray.Clear(); |
88 | 0 | } |
89 | | |
90 | | private: |
91 | 0 | ~nsParentNodeChildContentList() {} |
92 | | |
93 | | // Return true if validation succeeds, false otherwise |
94 | | bool ValidateCache(); |
95 | | |
96 | | // Whether cached array of child nodes is valid |
97 | | bool mIsCacheValid; |
98 | | |
99 | | // Cached array of child nodes |
100 | | AutoTArray<nsIContent*, 8> mCachedChildArray; |
101 | | }; |
102 | | |
103 | | #endif /* nsChildContentList_h__ */ |