1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/core/css/style_invalidation_root.h"
6 #include "third_party/blink/renderer/core/dom/document.h"
7 #include "third_party/blink/renderer/core/dom/element.h"
8 #include "third_party/blink/renderer/core/dom/shadow_root.h"
9 
10 namespace blink {
11 
RootElement() const12 Element* StyleInvalidationRoot::RootElement() const {
13   Node* root_node = GetRootNode();
14   DCHECK(root_node);
15   if (auto* shadow_root = DynamicTo<ShadowRoot>(root_node))
16     return &shadow_root->host();
17   if (root_node->IsDocumentNode())
18     return root_node->GetDocument().documentElement();
19   return To<Element>(root_node);
20 }
21 
22 #if DCHECK_IS_ON()
Parent(const Node & node) const23 ContainerNode* StyleInvalidationRoot::Parent(const Node& node) const {
24   return node.ParentOrShadowHostNode();
25 }
26 #endif  // DCHECK_IS_ON()
27 
IsDirty(const Node & node) const28 bool StyleInvalidationRoot::IsDirty(const Node& node) const {
29   return node.NeedsStyleInvalidation();
30 }
31 
RootRemoved(ContainerNode & parent)32 void StyleInvalidationRoot::RootRemoved(ContainerNode& parent) {
33   for (Node* ancestor = &parent; ancestor;
34        ancestor = ancestor->ParentOrShadowHostNode()) {
35     DCHECK(ancestor->ChildNeedsStyleInvalidation());
36     DCHECK(!ancestor->NeedsStyleInvalidation());
37     ancestor->ClearChildNeedsStyleInvalidation();
38   }
39   Clear();
40 }
41 
42 }  // namespace blink
43