Bug 1637593 [wpt PR 23564] - Fix superclass calls in MarionetteRefTestExecutor, a...
[gecko.git] / layout / style / nsHTMLStyleSheet.cpp
blob597ca45e6620ed348319e227b8dd43a12eade320
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 /*
8 * style sheet and style rule processor representing data from presentational
9 * HTML attributes
12 #include "nsHTMLStyleSheet.h"
13 #include "nsMappedAttributes.h"
14 #include "nsGkAtoms.h"
15 #include "nsPresContext.h"
16 #include "mozilla/EventStates.h"
17 #include "mozilla/PresShell.h"
18 #include "mozilla/dom/Document.h"
19 #include "mozilla/dom/DocumentInlines.h"
20 #include "nsStyleConsts.h"
21 #include "nsError.h"
22 #include "mozilla/MemoryReporting.h"
23 #include "mozilla/dom/Element.h"
24 #include "nsHashKeys.h"
25 #include "mozilla/OperatorNewExtensions.h"
26 #include "mozilla/RestyleManager.h"
27 #include "mozilla/ServoBindings.h"
28 #include "mozilla/ServoStyleSet.h"
30 using namespace mozilla;
31 using namespace mozilla::dom;
33 // -----------------------------------------------------------
35 struct MappedAttrTableEntry : public PLDHashEntryHdr {
36 nsMappedAttributes* mAttributes;
39 static PLDHashNumber MappedAttrTable_HashKey(const void* key) {
40 nsMappedAttributes* attributes =
41 static_cast<nsMappedAttributes*>(const_cast<void*>(key));
43 return attributes->HashValue();
46 static void MappedAttrTable_ClearEntry(PLDHashTable* table,
47 PLDHashEntryHdr* hdr) {
48 MappedAttrTableEntry* entry = static_cast<MappedAttrTableEntry*>(hdr);
50 entry->mAttributes->DropStyleSheetReference();
51 memset(entry, 0, sizeof(MappedAttrTableEntry));
54 static bool MappedAttrTable_MatchEntry(const PLDHashEntryHdr* hdr,
55 const void* key) {
56 nsMappedAttributes* attributes =
57 static_cast<nsMappedAttributes*>(const_cast<void*>(key));
58 const MappedAttrTableEntry* entry =
59 static_cast<const MappedAttrTableEntry*>(hdr);
61 return attributes->Equals(entry->mAttributes);
64 static const PLDHashTableOps MappedAttrTable_Ops = {
65 MappedAttrTable_HashKey, MappedAttrTable_MatchEntry,
66 PLDHashTable::MoveEntryStub, MappedAttrTable_ClearEntry, nullptr};
68 // -----------------------------------------------------------
70 // -----------------------------------------------------------
72 nsHTMLStyleSheet::nsHTMLStyleSheet(Document* aDocument)
73 : mDocument(aDocument),
74 mMappedAttrTable(&MappedAttrTable_Ops, sizeof(MappedAttrTableEntry)),
75 mMappedAttrsDirty(false) {
76 MOZ_ASSERT(aDocument);
79 void nsHTMLStyleSheet::SetOwningDocument(Document* aDocument) {
80 mDocument = aDocument; // not refcounted
83 void nsHTMLStyleSheet::Reset() {
84 mServoUnvisitedLinkDecl = nullptr;
85 mServoVisitedLinkDecl = nullptr;
86 mServoActiveLinkDecl = nullptr;
88 mMappedAttrTable.Clear();
89 mMappedAttrsDirty = false;
92 nsresult nsHTMLStyleSheet::ImplLinkColorSetter(
93 RefPtr<RawServoDeclarationBlock>& aDecl, nscolor aColor) {
94 if (!mDocument || !mDocument->GetPresShell()) {
95 return NS_OK;
98 MOZ_ASSERT(!ServoStyleSet::IsInServoTraversal());
99 aDecl = Servo_DeclarationBlock_CreateEmpty().Consume();
100 Servo_DeclarationBlock_SetColorValue(aDecl.get(), eCSSProperty_color, aColor);
102 // Now make sure we restyle any links that might need it. This
103 // shouldn't happen often, so just rebuilding everything is ok.
104 if (Element* root = mDocument->GetRootElement()) {
105 RestyleManager* rm = mDocument->GetPresContext()->RestyleManager();
106 rm->PostRestyleEvent(root, RestyleHint::RestyleSubtree(), nsChangeHint(0));
108 return NS_OK;
111 nsresult nsHTMLStyleSheet::SetLinkColor(nscolor aColor) {
112 return ImplLinkColorSetter(mServoUnvisitedLinkDecl, aColor);
115 nsresult nsHTMLStyleSheet::SetActiveLinkColor(nscolor aColor) {
116 return ImplLinkColorSetter(mServoActiveLinkDecl, aColor);
119 nsresult nsHTMLStyleSheet::SetVisitedLinkColor(nscolor aColor) {
120 return ImplLinkColorSetter(mServoVisitedLinkDecl, aColor);
123 already_AddRefed<nsMappedAttributes> nsHTMLStyleSheet::UniqueMappedAttributes(
124 nsMappedAttributes* aMapped) {
125 mMappedAttrsDirty = true;
126 auto entry = static_cast<MappedAttrTableEntry*>(
127 mMappedAttrTable.Add(aMapped, fallible));
128 if (!entry) return nullptr;
129 if (!entry->mAttributes) {
130 // We added a new entry to the hashtable, so we have a new unique set.
131 entry->mAttributes = aMapped;
133 RefPtr<nsMappedAttributes> ret = entry->mAttributes;
134 return ret.forget();
137 void nsHTMLStyleSheet::DropMappedAttributes(nsMappedAttributes* aMapped) {
138 NS_ENSURE_TRUE_VOID(aMapped);
139 #ifdef DEBUG
140 uint32_t entryCount = mMappedAttrTable.EntryCount() - 1;
141 #endif
143 mMappedAttrTable.Remove(aMapped);
145 NS_ASSERTION(entryCount == mMappedAttrTable.EntryCount(), "not removed");
148 void nsHTMLStyleSheet::CalculateMappedServoDeclarations() {
149 for (auto iter = mMappedAttrTable.Iter(); !iter.Done(); iter.Next()) {
150 MappedAttrTableEntry* attr = static_cast<MappedAttrTableEntry*>(iter.Get());
151 if (attr->mAttributes->GetServoStyle()) {
152 // Only handle cases which haven't been filled in already
153 continue;
155 attr->mAttributes->LazilyResolveServoDeclaration(mDocument);
159 size_t nsHTMLStyleSheet::DOMSizeOfIncludingThis(
160 MallocSizeOf aMallocSizeOf) const {
161 size_t n = aMallocSizeOf(this);
163 n += mMappedAttrTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
164 for (auto iter = mMappedAttrTable.ConstIter(); !iter.Done(); iter.Next()) {
165 auto entry = static_cast<MappedAttrTableEntry*>(iter.Get());
166 n += entry->mAttributes->SizeOfIncludingThis(aMallocSizeOf);
169 // Measurement of the following members may be added later if DMD finds it is
170 // worthwhile:
171 // - mURL
172 // - mLinkRule
173 // - mVisitedRule
174 // - mActiveRule
175 // - mTableQuirkColorRule
176 // - mTableTHRule
177 // - mLangRuleTable
179 // The following members are not measured:
180 // - mDocument, because it's non-owning
182 return n;