Bug 1819335 Part 2 - Add tests for page name change and break-after/break-before...
[gecko.git] / layout / style / AttributeStyles.cpp
blob5bf74d2697898d5317482d7f2598c6e2a2216439
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 /* Data from presentational HTML attributes and style attributes */
9 #include "mozilla/AttributeStyles.h"
11 #include "nsGkAtoms.h"
12 #include "nsPresContext.h"
13 #include "mozilla/dom/Document.h"
14 #include "mozilla/dom/DocumentInlines.h"
15 #include "mozilla/dom/Element.h"
16 #include "nsStyleConsts.h"
17 #include "nsError.h"
18 #include "nsHashKeys.h"
19 #include "mozilla/DeclarationBlock.h"
20 #include "mozilla/MemoryReporting.h"
21 #include "mozilla/OperatorNewExtensions.h"
22 #include "mozilla/PresShell.h"
23 #include "mozilla/RestyleManager.h"
24 #include "mozilla/ServoBindings.h"
25 #include "mozilla/ServoStyleSet.h"
27 using namespace mozilla::dom;
29 namespace mozilla {
31 // -----------------------------------------------------------
33 AttributeStyles::AttributeStyles(Document* aDocument) : mDocument(aDocument) {
34 MOZ_ASSERT(aDocument);
37 void AttributeStyles::SetOwningDocument(Document* aDocument) {
38 mDocument = aDocument; // not refcounted
41 void AttributeStyles::Reset() {
42 mServoUnvisitedLinkDecl = nullptr;
43 mServoVisitedLinkDecl = nullptr;
44 mServoActiveLinkDecl = nullptr;
47 nsresult AttributeStyles::ImplLinkColorSetter(
48 RefPtr<StyleLockedDeclarationBlock>& aDecl, nscolor aColor) {
49 if (!mDocument || !mDocument->GetPresShell()) {
50 return NS_OK;
53 MOZ_ASSERT(!ServoStyleSet::IsInServoTraversal());
54 aDecl = Servo_DeclarationBlock_CreateEmpty().Consume();
55 Servo_DeclarationBlock_SetColorValue(aDecl.get(), eCSSProperty_color, aColor);
57 // Now make sure we restyle any links that might need it. This
58 // shouldn't happen often, so just rebuilding everything is ok.
59 if (Element* root = mDocument->GetRootElement()) {
60 RestyleManager* rm = mDocument->GetPresContext()->RestyleManager();
61 rm->PostRestyleEvent(root, RestyleHint::RestyleSubtree(), nsChangeHint(0));
63 return NS_OK;
66 nsresult AttributeStyles::SetLinkColor(nscolor aColor) {
67 return ImplLinkColorSetter(mServoUnvisitedLinkDecl, aColor);
70 nsresult AttributeStyles::SetActiveLinkColor(nscolor aColor) {
71 return ImplLinkColorSetter(mServoActiveLinkDecl, aColor);
74 nsresult AttributeStyles::SetVisitedLinkColor(nscolor aColor) {
75 return ImplLinkColorSetter(mServoVisitedLinkDecl, aColor);
78 size_t AttributeStyles::DOMSizeOfIncludingThis(
79 MallocSizeOf aMallocSizeOf) const {
80 size_t n = aMallocSizeOf(this);
81 // Measurement of the following members may be added later if DMD finds it is
82 // worthwhile:
83 // - mServoUnvisitedLinkDecl;
84 // - mServoVisitedLinkDecl;
85 // - mServoActiveLinkDecl;
87 // The following members are not measured:
88 // - mDocument, because it's non-owning
89 return n;
92 AttributeStyles::~AttributeStyles() {
93 // We may go away before all of our cached style attributes do, so clean up
94 // any that are left.
95 for (auto iter = mCachedStyleAttrs.Iter(); !iter.Done(); iter.Next()) {
96 MiscContainer*& value = iter.Data();
98 // Ideally we'd just call MiscContainer::Evict, but we can't do that since
99 // we're iterating the hashtable.
100 if (value->mType == nsAttrValue::eCSSDeclaration) {
101 DeclarationBlock* declaration = value->mValue.mCSSDeclaration;
102 declaration->SetAttributeStyles(nullptr);
103 } else {
104 MOZ_ASSERT_UNREACHABLE("unexpected cached nsAttrValue type");
107 value->mValue.mCached = 0;
108 iter.Remove();
112 } // namespace mozilla