c518ef3d1784a09e5db64d2e151e0524b4b65bf7
[WebKit-https.git] / Source / WebCore / html / shadow / ContentDistributor.cpp
1 /*
2  * Copyright (C) 2011 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 #include "config.h"
28 #include "ContentDistributor.h"
29
30 #include "ElementIterator.h"
31 #include "InsertionPoint.h"
32
33 namespace WebCore {
34
35 ContentDistributor::ContentDistributor()
36     : m_insertionPointListIsValid(true)
37     , m_validity(Undetermined)
38 {
39 }
40
41 ContentDistributor::~ContentDistributor()
42 {
43 }
44
45 void ContentDistributor::invalidateInsertionPointList()
46 {
47     m_insertionPointListIsValid = false;
48     m_insertionPointList.clear();
49 }
50
51 const Vector<RefPtr<InsertionPoint>>& ContentDistributor::ensureInsertionPointList(ShadowRoot* shadowRoot)
52 {
53     if (m_insertionPointListIsValid)
54         return m_insertionPointList;
55
56     m_insertionPointListIsValid = true;
57     ASSERT(m_insertionPointList.isEmpty());
58
59     for (auto& element : descendantsOfType<InsertionPoint>(*shadowRoot))
60         m_insertionPointList.append(&element);
61
62     return m_insertionPointList;
63 }
64
65 InsertionPoint* ContentDistributor::findInsertionPointFor(const Node* key) const
66 {
67     return m_nodeToInsertionPoint.get(key);
68 }
69
70 void ContentDistributor::distribute(Element* host)
71 {
72     ASSERT(needsDistribution());
73     ASSERT(m_nodeToInsertionPoint.isEmpty());
74     ASSERT(!host->containingShadowRoot() || host->containingShadowRoot()->distributor().isValid());
75
76     m_validity = Valid;
77
78     if (ShadowRoot* root = host->shadowRoot()) {
79         const Vector<RefPtr<InsertionPoint>>& insertionPoints = ensureInsertionPointList(root);
80         for (size_t i = 0; i < insertionPoints.size(); ++i) {
81             InsertionPoint* point = insertionPoints[i].get();
82             if (!point->isActive())
83                 continue;
84
85             distributeSelectionsTo(point, host);
86         }
87     }
88 }
89
90 bool ContentDistributor::invalidate(Element* host)
91 {
92     ASSERT(needsInvalidation());
93     bool needsReattach = (m_validity == Undetermined) || !m_nodeToInsertionPoint.isEmpty();
94
95     if (ShadowRoot* root = host->shadowRoot()) {
96         const Vector<RefPtr<InsertionPoint>>& insertionPoints = ensureInsertionPointList(root);
97         for (size_t i = 0; i < insertionPoints.size(); ++i) {
98             needsReattach = true;
99             insertionPoints[i]->clearDistribution();
100         }
101     }
102
103     m_validity = Invalidating;
104     m_nodeToInsertionPoint.clear();
105     return needsReattach;
106 }
107
108 void ContentDistributor::distributeSelectionsTo(InsertionPoint* insertionPoint, Element* host)
109 {
110     for (Node* child = host->firstChild(); child; child = child->nextSibling()) {
111         ASSERT(!child->isInsertionPoint());
112
113         if (insertionPoint->matchTypeFor(child) != InsertionPoint::AlwaysMatches)
114             continue;
115
116         m_nodeToInsertionPoint.add(child, insertionPoint);
117     }
118
119     if (m_nodeToInsertionPoint.isEmpty())
120         return;
121     insertionPoint->setHasDistribution();
122 }
123
124 void ContentDistributor::ensureDistribution(ShadowRoot* shadowRoot)
125 {
126     ASSERT(shadowRoot);
127
128     Vector<ShadowRoot*, 8> shadowRoots;
129     for (Element* current = shadowRoot->host(); current; current = current->shadowHost()) {
130         ShadowRoot* currentRoot = current->shadowRoot();
131         if (!currentRoot->distributor().needsDistribution())
132             break;
133         shadowRoots.append(currentRoot);
134     }
135
136     for (size_t i = shadowRoots.size(); i > 0; --i)
137         shadowRoots[i - 1]->distributor().distribute(shadowRoots[i - 1]->host());
138 }
139
140 void ContentDistributor::invalidateDistribution(Element* host)
141 {
142     bool didNeedInvalidation = needsInvalidation();
143     bool needsReattach = didNeedInvalidation ? invalidate(host) : false;
144
145     if (needsReattach)
146         host->setNeedsStyleRecalc(ReconstructRenderTree);
147
148     if (didNeedInvalidation) {
149         ASSERT(m_validity == Invalidating);
150         m_validity = Invalidated;
151     }
152 }
153
154 void ContentDistributor::didShadowBoundaryChange(Element* host)
155 {
156     setValidity(Undetermined);
157     invalidateDistribution(host);
158 }
159
160 }