nsChildContentList.h (2524B)
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 "js/TypeDecls.h" // for Handle, Value, JSObject, JSContext 11 #include "mozilla/RefPtr.h" 12 #include "nsINodeList.h" // base class 13 #include "nsISupportsImpl.h" 14 15 class nsIContent; 16 class nsINode; 17 18 /** 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. 22 * @see nsINodeList 23 */ 24 class nsAttrChildContentList : public nsINodeList { 25 public: 26 explicit nsAttrChildContentList(nsINode* aNode) : mNode(aNode) {} 27 28 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 29 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(nsAttrChildContentList) 30 31 // nsWrapperCache 32 JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 33 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; } 39 40 virtual void InvalidateCacheIfAvailable() {} 41 42 protected: 43 virtual ~nsAttrChildContentList() = default; 44 45 // The node whose children make up the list. 46 RefPtr<nsINode> mNode; 47 }; 48 49 class nsParentNodeChildContentList final : public nsAttrChildContentList { 50 public: 51 explicit nsParentNodeChildContentList(nsINode* aNode) 52 : nsAttrChildContentList(aNode) { 53 ValidateCache(); 54 } 55 56 // nsINodeList interface 57 int32_t IndexOf(nsIContent* aContent) override; 58 nsIContent* Item(uint32_t aIndex) override; 59 uint32_t Length() override; 60 61 void InvalidateCacheIfAvailable() final { InvalidateCache(); } 62 63 void InvalidateCache() { 64 mIsCacheValid = false; 65 mCachedChildArray.Clear(); 66 } 67 68 private: 69 ~nsParentNodeChildContentList() = default; 70 71 // Return true if validation succeeds, false otherwise 72 void ValidateCache(); 73 void EnsureCacheValid() { 74 if (!mIsCacheValid) { 75 ValidateCache(); 76 } 77 MOZ_ASSERT(mIsCacheValid); 78 } 79 80 // Whether cached array of child nodes is valid 81 bool mIsCacheValid = false; 82 83 // Cached array of child nodes 84 AutoTArray<nsIContent*, 8> mCachedChildArray; 85 }; 86 87 #endif /* nsChildContentList_h__ */