ContainStyleScopeManager.h (4597B)
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 ContainStyleScopeManager_h_ 8 #define ContainStyleScopeManager_h_ 9 10 #include "nsClassHashtable.h" 11 #include "nsCounterManager.h" 12 #include "nsQuoteList.h" 13 #include "nsTHashSet.h" 14 15 class nsIContent; 16 class nsAtom; 17 18 namespace mozilla { 19 20 namespace dom { 21 class Element; 22 } 23 24 class ContainStyleScopeManager; 25 26 /* Implementation of a self-contained `contain: style` scope which manages its 27 * own counters and quotes. Since the `counters()` function has read access to 28 * other `contain: style` scopes, USE counter nodes may link across `contain: 29 * style` scopes. */ 30 class ContainStyleScope final { 31 public: 32 ContainStyleScope(ContainStyleScopeManager* aManager, 33 ContainStyleScope* aParent, nsIContent* aContent) 34 : mQuoteList(this), 35 mCounterManager(this), 36 mScopeManager(aManager), 37 mParent(aParent), 38 mContent(aContent) { 39 MOZ_ASSERT(aManager); 40 if (mParent) { 41 mParent->AddChild(this); 42 } 43 } 44 45 ~ContainStyleScope() { 46 if (mParent) { 47 mParent->RemoveChild(this); 48 } 49 } 50 51 nsQuoteList& GetQuoteList() { return mQuoteList; } 52 nsCounterManager& GetCounterManager() { return mCounterManager; } 53 ContainStyleScopeManager& GetScopeManager() { return *mScopeManager; } 54 ContainStyleScope* GetParent() { return mParent; } 55 nsIContent* GetContent() { return mContent; } 56 57 void AddChild(ContainStyleScope* aScope) { mChildren.AppendElement(aScope); } 58 void RemoveChild(ContainStyleScope* aScope) { 59 mChildren.RemoveElement(aScope); 60 } 61 const nsTArray<ContainStyleScope*>& GetChildren() const { return mChildren; } 62 63 void RecalcAllCounters(); 64 void RecalcAllQuotes(); 65 66 // Find the element in the given nsGenConList that directly precedes 67 // the mContent node of this ContainStyleScope in the flat tree. Can 68 // return null if no element in the list precedes the content. 69 nsGenConNode* GetPrecedingElementInGenConList(nsGenConList*); 70 71 private: 72 nsQuoteList mQuoteList; 73 nsCounterManager mCounterManager; 74 75 // We are owned by the |mScopeManager|, so this is guaranteed to be a live 76 // pointer as long as we are alive as well. 77 ContainStyleScopeManager* mScopeManager; 78 79 // Although parent and child relationships are represented as raw pointers 80 // here, |mScopeManager| is responsible for managing creation and deletion of 81 // all these data structures and also that it happens in the correct order. 82 ContainStyleScope* mParent; 83 nsTArray<ContainStyleScope*> mChildren; 84 85 // |mContent| is guaranteed to outlive this scope because mScopeManager will 86 // delete the scope when the corresponding frame for |mContent| is destroyed. 87 nsIContent* mContent; 88 }; 89 90 /* Management of the tree `contain: style` scopes. This class ensures that 91 * recalculation is done top-down, so that nodes that rely on other nodes in 92 * ancestor `contain: style` scopes are calculated properly. */ 93 class ContainStyleScopeManager { 94 public: 95 ContainStyleScopeManager() : mRootScope(this, nullptr, nullptr) {} 96 ContainStyleScope& GetRootScope() { return mRootScope; } 97 ContainStyleScope& GetOrCreateScopeForContent(nsIContent*); 98 ContainStyleScope& GetScopeForContent(nsIContent*); 99 100 void Clear(); 101 102 // If this frame creates a `contain: style` scope, destroy that scope and 103 // all of its child scopes. 104 void DestroyScopesFor(nsIFrame*); 105 106 // Destroy this scope and all its children starting from the leaf nodes. 107 void DestroyScope(ContainStyleScope*); 108 109 bool DestroyCounterNodesFor(nsIFrame*); 110 bool AddCounterChanges(nsIFrame* aNewFrame); 111 nsCounterList* GetOrCreateCounterList(dom::Element&, nsAtom* aCounterName); 112 113 bool CounterDirty(nsAtom* aCounterName); 114 void SetCounterDirty(nsAtom* aCounterName); 115 void RecalcAllCounters(); 116 void SetAllCountersDirty(); 117 118 bool DestroyQuoteNodesFor(nsIFrame*); 119 nsQuoteList* QuoteListFor(dom::Element&); 120 void RecalcAllQuotes(); 121 122 #if defined(DEBUG) || defined(MOZ_LAYOUT_DEBUGGER) 123 void DumpCounters(); 124 #endif 125 126 #ifdef ACCESSIBILITY 127 void GetSpokenCounterText(nsIFrame* aFrame, nsAString& aText); 128 #endif 129 130 private: 131 ContainStyleScope mRootScope; 132 nsClassHashtable<nsPtrHashKey<nsIContent>, ContainStyleScope> mScopes; 133 nsTHashSet<RefPtr<nsAtom>> mDirtyCounters; 134 }; 135 136 } // namespace mozilla 137 138 #endif /* ContainStyleScopeManager_h_ */