Use references and more const in SVGPathUtilities
[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         for (auto& point : ensureInsertionPointList(root)) {
82             if (!point->isActive())
83                 continue;
84
85             distributeSelectionsTo(point.get(), 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         for (auto& point : ensureInsertionPointList(root)) {
97             needsReattach = true;
98             point->clearDistribution();
99         }
100     }
101
102     m_validity = Invalidating;
103     m_nodeToInsertionPoint.clear();
104     return needsReattach;
105 }
106
107 void ContentDistributor::distributeSelectionsTo(InsertionPoint* insertionPoint, Element* host)
108 {
109     for (Node* child = host->firstChild(); child; child = child->nextSibling()) {
110         ASSERT(!child->isInsertionPoint());
111
112         if (insertionPoint->matchTypeFor(child) != InsertionPoint::AlwaysMatches)
113             continue;
114
115         m_nodeToInsertionPoint.add(child, insertionPoint);
116     }
117
118     if (m_nodeToInsertionPoint.isEmpty())
119         return;
120     insertionPoint->setHasDistribution();
121 }
122
123 void ContentDistributor::ensureDistribution(ShadowRoot* shadowRoot)
124 {
125     ASSERT(shadowRoot);
126
127     Vector<ShadowRoot*, 8> shadowRoots;
128     for (Element* current = shadowRoot->host(); current; current = current->shadowHost()) {
129         ShadowRoot* currentRoot = current->shadowRoot();
130         if (!currentRoot->distributor() || !currentRoot->distributor()->needsDistribution())
131             break;
132         shadowRoots.append(currentRoot);
133     }
134
135     for (size_t i = shadowRoots.size(); i > 0; --i)
136         shadowRoots[i - 1]->distributor()->distribute(shadowRoots[i - 1]->host());
137 }
138
139 void ContentDistributor::invalidateDistribution(Element* host)
140 {
141     bool didNeedInvalidation = needsInvalidation();
142     bool needsReattach = didNeedInvalidation ? invalidate(host) : false;
143
144     if (needsReattach)
145         host->setNeedsStyleRecalc(ReconstructRenderTree);
146
147     if (didNeedInvalidation) {
148         ASSERT(m_validity == Invalidating);
149         m_validity = Invalidated;
150     }
151 }
152
153 void ContentDistributor::didShadowBoundaryChange(Element* host)
154 {
155     setValidity(Undetermined);
156     invalidateDistribution(host);
157 }
158
159 }