Home | History | Annotate | Download | only in resolver
      1 // Copyright 2014 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 #ifndef StyleResolverParentScope_h
      6 #define StyleResolverParentScope_h
      7 
      8 #include "core/css/resolver/StyleResolver.h"
      9 #include "core/dom/Element.h"
     10 #include "core/dom/shadow/ShadowRoot.h"
     11 
     12 namespace WebCore {
     13 
     14 // Maintains the parent element stack (and bloom filter) inside recalcStyle.
     15 class StyleResolverParentScope FINAL {
     16 public:
     17     explicit StyleResolverParentScope(Node& parent);
     18     ~StyleResolverParentScope();
     19 
     20     static void ensureParentStackIsPushed();
     21 
     22 private:
     23     void pushParentIfNeeded();
     24 
     25     Node& m_parent;
     26     bool m_pushed;
     27     StyleResolverParentScope* m_previous;
     28     StyleResolver& m_resolver;
     29 
     30     static StyleResolverParentScope* s_currentScope;
     31 };
     32 
     33 inline StyleResolverParentScope::StyleResolverParentScope(Node& parent)
     34     : m_parent(parent)
     35     , m_pushed(false)
     36     , m_previous(s_currentScope)
     37     , m_resolver(*m_parent.document().styleResolver())
     38 {
     39     ASSERT(m_parent.document().inStyleRecalc());
     40     ASSERT(parent.isElementNode() || parent.isShadowRoot());
     41     s_currentScope = this;
     42     m_resolver.increaseStyleSharingDepth();
     43 }
     44 
     45 inline StyleResolverParentScope::~StyleResolverParentScope()
     46 {
     47     s_currentScope = m_previous;
     48     m_resolver.decreaseStyleSharingDepth();
     49     if (!m_pushed)
     50         return;
     51     if (m_parent.isElementNode())
     52         m_resolver.popParentElement(toElement(m_parent));
     53     else
     54         m_resolver.popParentShadowRoot(toShadowRoot(m_parent));
     55 }
     56 
     57 inline void StyleResolverParentScope::ensureParentStackIsPushed()
     58 {
     59     if (s_currentScope)
     60         s_currentScope->pushParentIfNeeded();
     61 }
     62 
     63 inline void StyleResolverParentScope::pushParentIfNeeded()
     64 {
     65     if (m_pushed)
     66         return;
     67     if (m_previous)
     68         m_previous->pushParentIfNeeded();
     69     if (m_parent.isElementNode())
     70         m_resolver.pushParentElement(toElement(m_parent));
     71     else
     72         m_resolver.pushParentShadowRoot(toShadowRoot(m_parent));
     73     m_pushed = true;
     74 }
     75 
     76 } // namespace WebCore
     77 
     78 #endif // StyleResolverParentScope_h
     79