Bug 1856663 - Add more chunks for Android mochitest-plain. r=jmaher,taskgraph-reviewe...
[gecko.git] / layout / base / nsGenConList.h
blob271c426080bc6805baf6391d71eda7e21d44368c
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 /* base class for nsCounterList and nsQuoteList */
9 #ifndef nsGenConList_h___
10 #define nsGenConList_h___
12 #include "mozilla/FunctionRef.h"
13 #include "mozilla/LinkedList.h"
14 #include "nsStyleStruct.h"
15 #include "nsCSSPseudoElements.h"
16 #include "nsTextNode.h"
17 #include <functional>
19 class nsGenConList;
20 class nsIFrame;
22 struct nsGenConNode : public mozilla::LinkedListElement<nsGenConNode> {
23 using StyleContentType = mozilla::StyleContentItem::Tag;
25 // The wrapper frame for all of the pseudo-element's content. This
26 // frame generally has useful style data and has the
27 // NS_FRAME_GENERATED_CONTENT bit set (so we use it to track removal),
28 // but does not necessarily for |nsCounterChangeNode|s.
29 nsIFrame* mPseudoFrame;
31 // Index within the list of things specified by the 'content' property,
32 // which is needed to do 'content: open-quote open-quote' correctly,
33 // and needed for similar cases for counters.
34 const int32_t mContentIndex;
36 // null for:
37 // * content: no-open-quote / content: no-close-quote
38 // * counter nodes for increments and resets
39 RefPtr<nsTextNode> mText;
41 explicit nsGenConNode(int32_t aContentIndex)
42 : mPseudoFrame(nullptr), mContentIndex(aContentIndex) {}
44 /**
45 * Finish initializing the generated content node once we know the
46 * relevant text frame. This must be called just after
47 * the textframe has been initialized. This need not be called at all
48 * for nodes that don't generate text. This will generally set the
49 * mPseudoFrame, insert the node into aList, and set aTextFrame up
50 * with the correct text.
51 * @param aList the list the node belongs to
52 * @param aPseudoFrame the :before or :after frame
53 * @param aTextFrame the textframe where the node contents will render
54 * @return true iff this marked the list dirty
56 virtual bool InitTextFrame(nsGenConList* aList, nsIFrame* aPseudoFrame,
57 nsIFrame* aTextFrame) {
58 mPseudoFrame = aPseudoFrame;
59 CheckFrameAssertions();
60 return false;
63 virtual ~nsGenConNode() = default; // XXX Avoid, perhaps?
65 protected:
66 void CheckFrameAssertions();
69 class nsGenConList {
70 protected:
71 mozilla::LinkedList<nsGenConNode> mList;
72 uint32_t mSize;
74 public:
75 nsGenConList() : mSize(0), mLastInserted(nullptr) {}
76 ~nsGenConList() { Clear(); }
77 void Clear();
78 static nsGenConNode* Next(nsGenConNode* aNode) {
79 MOZ_ASSERT(aNode, "aNode cannot be nullptr!");
80 return aNode->getNext();
82 static nsGenConNode* Prev(nsGenConNode* aNode) {
83 MOZ_ASSERT(aNode, "aNode cannot be nullptr!");
84 return aNode->getPrevious();
86 void Insert(nsGenConNode* aNode);
88 // Destroy all nodes with aFrame as parent. Returns true if some nodes
89 // have been destroyed; otherwise false.
90 bool DestroyNodesFor(nsIFrame* aFrame);
92 // Return the first node for aFrame on this list, or nullptr.
93 nsGenConNode* GetFirstNodeFor(nsIFrame* aFrame) const {
94 return mNodes.Get(aFrame);
97 // Return true if |aNode1| is after |aNode2|.
98 static bool NodeAfter(const nsGenConNode* aNode1, const nsGenConNode* aNode2);
100 // Find the first element in the list for which the given comparator returns
101 // true. This does a binary search on the list contents.
102 nsGenConNode* BinarySearch(
103 const mozilla::FunctionRef<bool(nsGenConNode*)>& aIsAfter);
105 nsGenConNode* GetLast() { return mList.getLast(); }
107 bool IsFirst(nsGenConNode* aNode) {
108 MOZ_ASSERT(aNode, "aNode cannot be nullptr!");
109 return aNode == mList.getFirst();
112 bool IsLast(nsGenConNode* aNode) {
113 MOZ_ASSERT(aNode, "aNode cannot be nullptr!");
114 return aNode == mList.getLast();
117 private:
118 void Destroy(nsGenConNode* aNode) {
119 MOZ_ASSERT(aNode, "aNode cannot be nullptr!");
120 delete aNode;
121 mSize--;
124 // Map from frame to the first nsGenConNode of it in the list.
125 nsTHashMap<nsPtrHashKey<nsIFrame>, nsGenConNode*> mNodes;
127 // A weak pointer to the node most recently inserted, used to avoid repeated
128 // list traversals in Insert().
129 nsGenConNode* mLastInserted;
132 #endif /* nsGenConList_h___ */