[Shadow DOM] Implement HTMLShadowElement::olderShadowRoot
[WebKit-https.git] / Source / WebCore / dom / ElementShadow.h
1 /*
2  * Copyright (C) 2012 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Neither the name of Google Inc. nor the names of its
11  * contributors may be used to endorse or promote products derived from
12  * this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef ElementShadow_h
28 #define ElementShadow_h
29
30 #include "ContentDistributor.h"
31 #include "ExceptionCode.h"
32 #include "SelectRuleFeatureSet.h"
33 #include "ShadowRoot.h"
34 #include <wtf/DoublyLinkedList.h>
35 #include <wtf/Noncopyable.h>
36 #include <wtf/OwnPtr.h>
37 #include <wtf/PassRefPtr.h>
38 #include <wtf/Vector.h>
39
40 namespace WebCore {
41
42 class Node;
43 class Element;
44 class TreeScope;
45
46 class ElementShadow {
47    WTF_MAKE_NONCOPYABLE(ElementShadow); WTF_MAKE_FAST_ALLOCATED;
48 public:
49     ElementShadow();
50     ~ElementShadow();
51
52     Element* host() const;
53     ShadowRoot* youngestShadowRoot() const;
54     ShadowRoot* oldestShadowRoot() const;
55
56     void removeAllShadowRoots();
57     void addShadowRoot(Element* shadowHost, PassRefPtr<ShadowRoot>, ShadowRoot::ShadowRootType, ExceptionCode&);
58
59     void attach();
60     void detach();
61
62     bool childNeedsStyleRecalc();
63     bool needsStyleRecalc();
64     void recalcStyle(Node::StyleChange);
65
66     void setValidityUndetermined();
67     void invalidateDistribution();
68     void ensureDistribution();
69     void ensureDistributionFromDocument();
70
71     ContentDistributor& distributor();
72     const ContentDistributor& distributor() const;
73
74     bool shouldCollectSelectFeatureSet() const { return m_shouldCollectSelectFeatureSet; }
75     void setShouldCollectSelectFeatureSet();
76     void ensureSelectFeatureSetCollected();
77
78     const SelectRuleFeatureSet& selectRuleFeatureSet() const;
79
80     void reportMemoryUsage(MemoryObjectInfo*) const;
81
82 private:
83     void invalidateDistribution(Element* host);
84
85     void collectSelectFeatureSetFrom(ShadowRoot*);
86
87     DoublyLinkedList<ShadowRoot> m_shadowRoots;
88     ContentDistributor m_distributor;
89     SelectRuleFeatureSet m_selectFeatures;
90     bool m_shouldCollectSelectFeatureSet : 1;
91 };
92
93 void invalidateParentDistributionIfNecessary(Element*, SelectRuleFeatureSet::SelectRuleFeatureMask updatedFeatures);
94
95 inline ShadowRoot* ElementShadow::youngestShadowRoot() const
96 {
97     return m_shadowRoots.head();
98 }
99
100 inline ShadowRoot* ElementShadow::oldestShadowRoot() const
101 {
102     return m_shadowRoots.tail();
103 }
104
105 inline ContentDistributor& ElementShadow::distributor()
106 {
107     return m_distributor;
108 }
109
110 inline const ContentDistributor& ElementShadow::distributor() const
111 {
112     return m_distributor;
113 }
114
115 inline Element* ElementShadow::host() const
116 {
117     ASSERT(!m_shadowRoots.isEmpty());
118     return youngestShadowRoot()->host();
119 }
120
121 inline const SelectRuleFeatureSet& ElementShadow::selectRuleFeatureSet() const
122 {
123     ASSERT(!m_shouldCollectSelectFeatureSet);
124     return m_selectFeatures;
125 }
126
127 inline ShadowRoot* Node::youngestShadowRoot() const
128 {
129     if (!this->isElementNode())
130         return 0;
131     if (ElementShadow* shadow = toElement(this)->shadow())
132         return shadow->youngestShadowRoot();
133     return 0;
134 }
135
136 class ShadowRootVector : public Vector<RefPtr<ShadowRoot> > {
137 public:
138     explicit ShadowRootVector(ElementShadow* tree)
139     {
140         for (ShadowRoot* root = tree->youngestShadowRoot(); root; root = root->olderShadowRoot())
141             append(root);
142     }
143 };
144
145 } // namespace
146
147 #endif