/src/mozilla-central/layout/style/nsHTMLStyleSheet.cpp
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 | | /* |
8 | | * style sheet and style rule processor representing data from presentational |
9 | | * HTML attributes |
10 | | */ |
11 | | |
12 | | #include "nsHTMLStyleSheet.h" |
13 | | #include "nsMappedAttributes.h" |
14 | | #include "nsGkAtoms.h" |
15 | | #include "nsPresContext.h" |
16 | | #include "mozilla/EventStates.h" |
17 | | #include "nsIDocument.h" |
18 | | #include "nsIPresShell.h" |
19 | | #include "nsStyleConsts.h" |
20 | | #include "nsError.h" |
21 | | #include "mozilla/MemoryReporting.h" |
22 | | #include "mozilla/dom/Element.h" |
23 | | #include "nsHashKeys.h" |
24 | | #include "mozilla/OperatorNewExtensions.h" |
25 | | #include "mozilla/RestyleManager.h" |
26 | | #include "mozilla/ServoBindings.h" |
27 | | #include "mozilla/ServoStyleSet.h" |
28 | | |
29 | | using namespace mozilla; |
30 | | using namespace mozilla::dom; |
31 | | |
32 | | |
33 | | // ----------------------------------------------------------- |
34 | | |
35 | | struct MappedAttrTableEntry : public PLDHashEntryHdr { |
36 | | nsMappedAttributes *mAttributes; |
37 | | }; |
38 | | |
39 | | static PLDHashNumber |
40 | | MappedAttrTable_HashKey(const void *key) |
41 | 0 | { |
42 | 0 | nsMappedAttributes *attributes = |
43 | 0 | static_cast<nsMappedAttributes*>(const_cast<void*>(key)); |
44 | 0 |
|
45 | 0 | return attributes->HashValue(); |
46 | 0 | } |
47 | | |
48 | | static void |
49 | | MappedAttrTable_ClearEntry(PLDHashTable *table, PLDHashEntryHdr *hdr) |
50 | 0 | { |
51 | 0 | MappedAttrTableEntry *entry = static_cast<MappedAttrTableEntry*>(hdr); |
52 | 0 |
|
53 | 0 | entry->mAttributes->DropStyleSheetReference(); |
54 | 0 | memset(entry, 0, sizeof(MappedAttrTableEntry)); |
55 | 0 | } |
56 | | |
57 | | static bool |
58 | | MappedAttrTable_MatchEntry(const PLDHashEntryHdr *hdr, const void *key) |
59 | 0 | { |
60 | 0 | nsMappedAttributes *attributes = |
61 | 0 | static_cast<nsMappedAttributes*>(const_cast<void*>(key)); |
62 | 0 | const MappedAttrTableEntry *entry = |
63 | 0 | static_cast<const MappedAttrTableEntry*>(hdr); |
64 | 0 |
|
65 | 0 | return attributes->Equals(entry->mAttributes); |
66 | 0 | } |
67 | | |
68 | | static const PLDHashTableOps MappedAttrTable_Ops = { |
69 | | MappedAttrTable_HashKey, |
70 | | MappedAttrTable_MatchEntry, |
71 | | PLDHashTable::MoveEntryStub, |
72 | | MappedAttrTable_ClearEntry, |
73 | | nullptr |
74 | | }; |
75 | | |
76 | | // ----------------------------------------------------------- |
77 | | |
78 | | |
79 | | // ----------------------------------------------------------- |
80 | | |
81 | | nsHTMLStyleSheet::nsHTMLStyleSheet(nsIDocument* aDocument) |
82 | | : mDocument(aDocument) |
83 | | , mMappedAttrTable(&MappedAttrTable_Ops, sizeof(MappedAttrTableEntry)) |
84 | | , mMappedAttrsDirty(false) |
85 | 0 | { |
86 | 0 | MOZ_ASSERT(aDocument); |
87 | 0 | } |
88 | | |
89 | | |
90 | | void |
91 | | nsHTMLStyleSheet::SetOwningDocument(nsIDocument* aDocument) |
92 | 0 | { |
93 | 0 | mDocument = aDocument; // not refcounted |
94 | 0 | } |
95 | | |
96 | | void |
97 | | nsHTMLStyleSheet::Reset() |
98 | 0 | { |
99 | 0 |
|
100 | 0 | mServoUnvisitedLinkDecl = nullptr; |
101 | 0 | mServoVisitedLinkDecl = nullptr; |
102 | 0 | mServoActiveLinkDecl = nullptr; |
103 | 0 |
|
104 | 0 | mMappedAttrTable.Clear(); |
105 | 0 | mMappedAttrsDirty = false; |
106 | 0 | } |
107 | | |
108 | | nsresult |
109 | | nsHTMLStyleSheet::ImplLinkColorSetter( |
110 | | RefPtr<RawServoDeclarationBlock>& aDecl, |
111 | | nscolor aColor) |
112 | 0 | { |
113 | 0 | if (!mDocument || !mDocument->GetShell()) { |
114 | 0 | return NS_OK; |
115 | 0 | } |
116 | 0 | |
117 | 0 | RestyleManager* restyle = mDocument->GetPresContext()->RestyleManager(); |
118 | 0 |
|
119 | 0 | MOZ_ASSERT(!ServoStyleSet::IsInServoTraversal()); |
120 | 0 | aDecl = Servo_DeclarationBlock_CreateEmpty().Consume(); |
121 | 0 | Servo_DeclarationBlock_SetColorValue(aDecl.get(), eCSSProperty_color, |
122 | 0 | aColor); |
123 | 0 |
|
124 | 0 | // Now make sure we restyle any links that might need it. This |
125 | 0 | // shouldn't happen often, so just rebuilding everything is ok. |
126 | 0 | Element* root = mDocument->GetRootElement(); |
127 | 0 | if (root) { |
128 | 0 | restyle->PostRestyleEvent(root, eRestyle_Subtree, nsChangeHint(0)); |
129 | 0 | } |
130 | 0 | return NS_OK; |
131 | 0 | } |
132 | | |
133 | | nsresult |
134 | | nsHTMLStyleSheet::SetLinkColor(nscolor aColor) |
135 | 0 | { |
136 | 0 | return ImplLinkColorSetter( |
137 | 0 | mServoUnvisitedLinkDecl, |
138 | 0 | aColor); |
139 | 0 | } |
140 | | |
141 | | nsresult |
142 | | nsHTMLStyleSheet::SetActiveLinkColor(nscolor aColor) |
143 | 0 | { |
144 | 0 | return ImplLinkColorSetter( |
145 | 0 | mServoActiveLinkDecl, |
146 | 0 | aColor); |
147 | 0 | } |
148 | | |
149 | | nsresult |
150 | | nsHTMLStyleSheet::SetVisitedLinkColor(nscolor aColor) |
151 | 0 | { |
152 | 0 | return ImplLinkColorSetter( |
153 | 0 | mServoVisitedLinkDecl, |
154 | 0 | aColor); |
155 | 0 | } |
156 | | |
157 | | already_AddRefed<nsMappedAttributes> |
158 | | nsHTMLStyleSheet::UniqueMappedAttributes(nsMappedAttributes* aMapped) |
159 | 0 | { |
160 | 0 | mMappedAttrsDirty = true; |
161 | 0 | auto entry = static_cast<MappedAttrTableEntry*> |
162 | 0 | (mMappedAttrTable.Add(aMapped, fallible)); |
163 | 0 | if (!entry) |
164 | 0 | return nullptr; |
165 | 0 | if (!entry->mAttributes) { |
166 | 0 | // We added a new entry to the hashtable, so we have a new unique set. |
167 | 0 | entry->mAttributes = aMapped; |
168 | 0 | } |
169 | 0 | RefPtr<nsMappedAttributes> ret = entry->mAttributes; |
170 | 0 | return ret.forget(); |
171 | 0 | } |
172 | | |
173 | | void |
174 | | nsHTMLStyleSheet::DropMappedAttributes(nsMappedAttributes* aMapped) |
175 | 0 | { |
176 | 0 | NS_ENSURE_TRUE_VOID(aMapped); |
177 | | #ifdef DEBUG |
178 | | uint32_t entryCount = mMappedAttrTable.EntryCount() - 1; |
179 | | #endif |
180 | |
|
181 | 0 | mMappedAttrTable.Remove(aMapped); |
182 | 0 |
|
183 | 0 | NS_ASSERTION(entryCount == mMappedAttrTable.EntryCount(), "not removed"); |
184 | 0 | } |
185 | | |
186 | | void |
187 | | nsHTMLStyleSheet::CalculateMappedServoDeclarations() |
188 | 0 | { |
189 | 0 | for (auto iter = mMappedAttrTable.Iter(); !iter.Done(); iter.Next()) { |
190 | 0 | MappedAttrTableEntry* attr = static_cast<MappedAttrTableEntry*>(iter.Get()); |
191 | 0 | if (attr->mAttributes->GetServoStyle()) { |
192 | 0 | // Only handle cases which haven't been filled in already |
193 | 0 | continue; |
194 | 0 | } |
195 | 0 | attr->mAttributes->LazilyResolveServoDeclaration(mDocument); |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | | |
200 | | size_t |
201 | | nsHTMLStyleSheet::DOMSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const |
202 | 0 | { |
203 | 0 | size_t n = aMallocSizeOf(this); |
204 | 0 |
|
205 | 0 | n += mMappedAttrTable.ShallowSizeOfExcludingThis(aMallocSizeOf); |
206 | 0 | for (auto iter = mMappedAttrTable.ConstIter(); !iter.Done(); iter.Next()) { |
207 | 0 | auto entry = static_cast<MappedAttrTableEntry*>(iter.Get()); |
208 | 0 | n += entry->mAttributes->SizeOfIncludingThis(aMallocSizeOf); |
209 | 0 | } |
210 | 0 |
|
211 | 0 | // Measurement of the following members may be added later if DMD finds it is |
212 | 0 | // worthwhile: |
213 | 0 | // - mURL |
214 | 0 | // - mLinkRule |
215 | 0 | // - mVisitedRule |
216 | 0 | // - mActiveRule |
217 | 0 | // - mTableQuirkColorRule |
218 | 0 | // - mTableTHRule |
219 | 0 | // - mLangRuleTable |
220 | 0 | // |
221 | 0 | // The following members are not measured: |
222 | 0 | // - mDocument, because it's non-owning |
223 | 0 |
|
224 | 0 | return n; |
225 | 0 | } |