| 1 | /* |
| 2 | * Copyright (C) 2013-2016 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "SelectorChecker.h" |
| 29 | #include "SelectorFilter.h" |
| 30 | #include "StyleChange.h" |
| 31 | #include "StyleSharingResolver.h" |
| 32 | #include "StyleUpdate.h" |
| 33 | #include <wtf/Function.h> |
| 34 | #include <wtf/Ref.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class Document; |
| 39 | class Element; |
| 40 | class Node; |
| 41 | class RenderStyle; |
| 42 | class ShadowRoot; |
| 43 | class StyleResolver; |
| 44 | |
| 45 | namespace Style { |
| 46 | |
| 47 | class TreeResolver { |
| 48 | public: |
| 49 | TreeResolver(Document&); |
| 50 | ~TreeResolver(); |
| 51 | |
| 52 | std::unique_ptr<Update> resolve(); |
| 53 | |
| 54 | private: |
| 55 | std::unique_ptr<RenderStyle> styleForElement(Element&, const RenderStyle& inheritedStyle); |
| 56 | |
| 57 | void resolveComposedTree(); |
| 58 | |
| 59 | ElementUpdates resolveElement(Element&); |
| 60 | |
| 61 | ElementUpdate createAnimatedElementUpdate(std::unique_ptr<RenderStyle>, Element&, Change); |
| 62 | ElementUpdate resolvePseudoStyle(Element&, const ElementUpdate&, PseudoId); |
| 63 | |
| 64 | struct Scope : RefCounted<Scope> { |
| 65 | StyleResolver& styleResolver; |
| 66 | SelectorFilter selectorFilter; |
| 67 | SharingResolver sharingResolver; |
| 68 | ShadowRoot* shadowRoot { nullptr }; |
| 69 | Scope* enclosingScope { nullptr }; |
| 70 | |
| 71 | Scope(Document&); |
| 72 | Scope(ShadowRoot&, Scope& enclosingScope); |
| 73 | ~Scope(); |
| 74 | }; |
| 75 | |
| 76 | struct Parent { |
| 77 | Element* element; |
| 78 | const RenderStyle& style; |
| 79 | Change change { NoChange }; |
| 80 | DescendantsToResolve descendantsToResolve { DescendantsToResolve::None }; |
| 81 | bool didPushScope { false }; |
| 82 | |
| 83 | Parent(Document&); |
| 84 | Parent(Element&, const RenderStyle&, Change, DescendantsToResolve); |
| 85 | }; |
| 86 | |
| 87 | Scope& scope() { return m_scopeStack.last(); } |
| 88 | Parent& parent() { return m_parentStack.last(); } |
| 89 | |
| 90 | void pushScope(ShadowRoot&); |
| 91 | void pushEnclosingScope(); |
| 92 | void popScope(); |
| 93 | |
| 94 | void pushParent(Element&, const RenderStyle&, Change, DescendantsToResolve); |
| 95 | void popParent(); |
| 96 | void popParentsToDepth(unsigned depth); |
| 97 | |
| 98 | const RenderStyle* parentBoxStyle() const; |
| 99 | |
| 100 | Document& m_document; |
| 101 | std::unique_ptr<RenderStyle> m_documentElementStyle; |
| 102 | |
| 103 | Vector<Ref<Scope>, 4> m_scopeStack; |
| 104 | Vector<Parent, 32> m_parentStack; |
| 105 | bool m_didSeePendingStylesheet { false }; |
| 106 | |
| 107 | std::unique_ptr<Update> m_update; |
| 108 | }; |
| 109 | |
| 110 | void queuePostResolutionCallback(Function<void ()>&&); |
| 111 | bool postResolutionCallbacksAreSuspended(); |
| 112 | |
| 113 | class PostResolutionCallbackDisabler { |
| 114 | public: |
| 115 | enum class DrainCallbacks { Yes, No }; |
| 116 | explicit PostResolutionCallbackDisabler(Document&, DrainCallbacks = DrainCallbacks::Yes); |
| 117 | ~PostResolutionCallbackDisabler(); |
| 118 | private: |
| 119 | DrainCallbacks m_drainCallbacks; |
| 120 | }; |
| 121 | |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |