ContentDistribution should be only used for details elements
[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()
75         || !host->containingShadowRoot()->distributor()
76         || host->containingShadowRoot()->distributor()->isValid());
77
78     m_validity = Valid;
79
80     if (ShadowRoot* root = host->shadowRoot()) {
81         const Vector<RefPtr<InsertionPoint>>& insertionPoints = ensureInsertionPointList(root);
82         for (size_t i = 0; i < insertionPoints.size(); ++i) {
83             InsertionPoint* point = insertionPoints[i].get();
84             if (!point->isActive())
85                 continue;
86
87             distributeSelectionsTo(point, host);
88         }
89     }
90 }
91
92 bool ContentDistributor::invalidate(Element* host)
93 {
94     ASSERT(needsInvalidation());
95     bool needsReattach = (m_validity == Undetermined) || !m_nodeToInsertionPoint.isEmpty();
96
97     if (ShadowRoot* root = host->shadowRoot()) {
98         const Vector<RefPtr<InsertionPoint>>& insertionPoints = ensureInsertionPointList(root);
99         for (size_t i = 0; i < insertionPoints.size(); ++i) {
100             needsReattach = true;
101             insertionPoints[i]->clearDistribution();
102         }
103     }
104
105     m_validity = Invalidating;
106     m_nodeToInsertionPoint.clear();
107     return needsReattach;
108 }
109
110 void ContentDistributor::distributeSelectionsTo(InsertionPoint* insertionPoint, Element* host)
111 {
112     for (Node* child = host->firstChild(); child; child = child->nextSibling()) {
113         ASSERT(!child->isInsertionPoint());
114
115         if (insertionPoint->matchTypeFor(child) != InsertionPoint::AlwaysMatches)
116             continue;
117
118         m_nodeToInsertionPoint.add(child, insertionPoint);
119     }
120
121     if (m_nodeToInsertionPoint.isEmpty())
122         return;
123     insertionPoint->setHasDistribution();
124 }
125
126 void ContentDistributor::ensureDistribution(ShadowRoot* shadowRoot)
127 {
128     ASSERT(shadowRoot);
129
130     Vector<ShadowRoot*, 8> shadowRoots;
131     for (Element* current = shadowRoot->host(); current; current = current->shadowHost()) {
132         ShadowRoot* currentRoot = current->shadowRoot();
133         if (!currentRoot->distributor() || !currentRoot->distributor()->needsDistribution())
134             break;
135         shadowRoots.append(currentRoot);
136     }
137
138     for (size_t i = shadowRoots.size(); i > 0; --i)
139         shadowRoots[i - 1]->distributor()->distribute(shadowRoots[i - 1]->host());
140 }
141
142 void ContentDistributor::invalidateDistribution(Element* host)
143 {
144     bool didNeedInvalidation = needsInvalidation();
145     bool needsReattach = didNeedInvalidation ? invalidate(host) : false;
146
147     if (needsReattach)
148         host->setNeedsStyleRecalc(ReconstructRenderTree);
149
150     if (didNeedInvalidation) {
151         ASSERT(m_validity == Invalidating);
152         m_validity = Invalidated;
153     }
154 }
155
156 void ContentDistributor::didShadowBoundaryChange(Element* host)
157 {
158     setValidity(Undetermined);
159     invalidateDistribution(host);
160 }
161
162 }