File: ElementShadowV0.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (268 lines) | stat: -rw-r--r-- 9,432 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "core/dom/shadow/ElementShadowV0.h"

#include "core/dom/ElementTraversal.h"
#include "core/dom/shadow/DistributedNodes.h"
#include "core/dom/shadow/ElementShadow.h"
#include "core/html/HTMLContentElement.h"
#include "core/html/HTMLShadowElement.h"
#include "core/inspector/InspectorInstrumentation.h"

namespace blink {

class DistributionPool final {
  STACK_ALLOCATED();

 public:
  explicit DistributionPool(const ContainerNode&);
  void clear();
  ~DistributionPool();
  void distributeTo(InsertionPoint*, ElementShadowV0*);
  void populateChildren(const ContainerNode&);

 private:
  void detachNonDistributedNodes();
  HeapVector<Member<Node>, 32> m_nodes;
  Vector<bool, 32> m_distributed;
};

inline DistributionPool::DistributionPool(const ContainerNode& parent) {
  populateChildren(parent);
}

inline void DistributionPool::clear() {
  detachNonDistributedNodes();
  m_nodes.clear();
  m_distributed.clear();
}

inline void DistributionPool::populateChildren(const ContainerNode& parent) {
  clear();
  for (Node* child = parent.firstChild(); child; child = child->nextSibling()) {
    if (isHTMLSlotElement(child)) {
      // TODO(hayato): Support re-distribution across v0 and v1 shadow trees
      continue;
    }
    if (isActiveInsertionPoint(*child)) {
      InsertionPoint* insertionPoint = toInsertionPoint(child);
      for (size_t i = 0; i < insertionPoint->distributedNodesSize(); ++i)
        m_nodes.push_back(insertionPoint->distributedNodeAt(i));
    } else {
      m_nodes.push_back(child);
    }
  }
  m_distributed.resize(m_nodes.size());
  m_distributed.fill(false);
}

void DistributionPool::distributeTo(InsertionPoint* insertionPoint,
                                    ElementShadowV0* elementShadow) {
  DistributedNodes distributedNodes;

  for (size_t i = 0; i < m_nodes.size(); ++i) {
    if (m_distributed[i])
      continue;

    if (isHTMLContentElement(*insertionPoint) &&
        !toHTMLContentElement(insertionPoint)->canSelectNode(m_nodes, i))
      continue;

    Node* node = m_nodes[i];
    distributedNodes.append(node);
    elementShadow->didDistributeNode(node, insertionPoint);
    m_distributed[i] = true;
  }

  // Distributes fallback elements
  if (insertionPoint->isContentInsertionPoint() && distributedNodes.isEmpty()) {
    for (Node* fallbackNode = insertionPoint->firstChild(); fallbackNode;
         fallbackNode = fallbackNode->nextSibling()) {
      distributedNodes.append(fallbackNode);
      elementShadow->didDistributeNode(fallbackNode, insertionPoint);
    }
  }
  insertionPoint->setDistributedNodes(distributedNodes);
}

inline DistributionPool::~DistributionPool() {
  detachNonDistributedNodes();
}

inline void DistributionPool::detachNonDistributedNodes() {
  for (size_t i = 0; i < m_nodes.size(); ++i) {
    if (m_distributed[i])
      continue;
    if (m_nodes[i]->layoutObject())
      m_nodes[i]->lazyReattachIfAttached();
  }
}

ElementShadowV0* ElementShadowV0::create(ElementShadow& elementShadow) {
  return new ElementShadowV0(elementShadow);
}

ElementShadowV0::ElementShadowV0(ElementShadow& elementShadow)
    : m_elementShadow(&elementShadow), m_needsSelectFeatureSet(false) {}

ElementShadowV0::~ElementShadowV0() {}

ShadowRoot& ElementShadowV0::youngestShadowRoot() const {
  return m_elementShadow->youngestShadowRoot();
}

ShadowRoot& ElementShadowV0::oldestShadowRoot() const {
  return m_elementShadow->oldestShadowRoot();
}

const InsertionPoint* ElementShadowV0::finalDestinationInsertionPointFor(
    const Node* key) const {
  DCHECK(key);
  DCHECK(!key->needsDistributionRecalc());
  NodeToDestinationInsertionPoints::const_iterator it =
      m_nodeToInsertionPoints.find(key);
  return it == m_nodeToInsertionPoints.end() ? nullptr : it->value->back();
}

const DestinationInsertionPoints*
ElementShadowV0::destinationInsertionPointsFor(const Node* key) const {
  DCHECK(key);
  DCHECK(!key->needsDistributionRecalc());
  NodeToDestinationInsertionPoints::const_iterator it =
      m_nodeToInsertionPoints.find(key);
  return it == m_nodeToInsertionPoints.end() ? nullptr : it->value;
}

void ElementShadowV0::distribute() {
  HeapVector<Member<HTMLShadowElement>, 32> shadowInsertionPoints;
  DistributionPool pool(m_elementShadow->host());

  for (ShadowRoot* root = &youngestShadowRoot(); root;
       root = root->olderShadowRoot()) {
    HTMLShadowElement* shadowInsertionPoint = 0;
    for (const auto& point : root->descendantInsertionPoints()) {
      if (!point->isActive())
        continue;
      if (isHTMLShadowElement(*point)) {
        DCHECK(!shadowInsertionPoint);
        shadowInsertionPoint = toHTMLShadowElement(point);
        shadowInsertionPoints.push_back(shadowInsertionPoint);
      } else {
        pool.distributeTo(point, this);
        if (ElementShadow* shadow =
                shadowWhereNodeCanBeDistributedForV0(*point))
          shadow->setNeedsDistributionRecalc();
      }
    }
  }

  for (size_t i = shadowInsertionPoints.size(); i > 0; --i) {
    HTMLShadowElement* shadowInsertionPoint = shadowInsertionPoints[i - 1];
    ShadowRoot* root = shadowInsertionPoint->containingShadowRoot();
    DCHECK(root);
    if (root->isOldest()) {
      pool.distributeTo(shadowInsertionPoint, this);
    } else if (root->olderShadowRoot()->type() == root->type()) {
      // Only allow reprojecting older shadow roots between the same type to
      // disallow reprojecting UA elements into author shadows.
      DistributionPool olderShadowRootPool(*root->olderShadowRoot());
      olderShadowRootPool.distributeTo(shadowInsertionPoint, this);
      root->olderShadowRoot()->setShadowInsertionPointOfYoungerShadowRoot(
          shadowInsertionPoint);
    }
    if (ElementShadow* shadow =
            shadowWhereNodeCanBeDistributedForV0(*shadowInsertionPoint))
      shadow->setNeedsDistributionRecalc();
  }
  InspectorInstrumentation::didPerformElementShadowDistribution(
      &m_elementShadow->host());
}

void ElementShadowV0::didDistributeNode(const Node* node,
                                        InsertionPoint* insertionPoint) {
  NodeToDestinationInsertionPoints::AddResult result =
      m_nodeToInsertionPoints.add(node, nullptr);
  if (result.isNewEntry)
    result.storedValue->value = new DestinationInsertionPoints;
  result.storedValue->value->push_back(insertionPoint);
}

const SelectRuleFeatureSet& ElementShadowV0::ensureSelectFeatureSet() {
  if (!m_needsSelectFeatureSet)
    return m_selectFeatures;

  m_selectFeatures.clear();
  for (const ShadowRoot* root = &oldestShadowRoot(); root;
       root = root->youngerShadowRoot())
    collectSelectFeatureSetFrom(*root);
  m_needsSelectFeatureSet = false;
  return m_selectFeatures;
}

void ElementShadowV0::collectSelectFeatureSetFrom(const ShadowRoot& root) {
  if (!root.containsShadowRoots() && !root.containsContentElements())
    return;

  for (Element& element : ElementTraversal::descendantsOf(root)) {
    if (ElementShadow* shadow = element.shadow()) {
      if (!shadow->isV1())
        m_selectFeatures.add(shadow->v0().ensureSelectFeatureSet());
    }
    if (!isHTMLContentElement(element))
      continue;
    const CSSSelectorList& list = toHTMLContentElement(element).selectorList();
    m_selectFeatures.collectFeaturesFromSelectorList(list);
  }
}

void ElementShadowV0::willAffectSelector() {
  for (ElementShadow* shadow = m_elementShadow; shadow;
       shadow = shadow->containingShadow()) {
    if (shadow->isV1() || shadow->v0().needsSelectFeatureSet())
      break;
    shadow->v0().setNeedsSelectFeatureSet();
  }
  m_elementShadow->setNeedsDistributionRecalc();
}

void ElementShadowV0::clearDistribution() {
  m_nodeToInsertionPoints.clear();

  for (ShadowRoot* root = &m_elementShadow->youngestShadowRoot(); root;
       root = root->olderShadowRoot())
    root->setShadowInsertionPointOfYoungerShadowRoot(nullptr);
}

DEFINE_TRACE(ElementShadowV0) {
  visitor->trace(m_elementShadow);
  visitor->trace(m_nodeToInsertionPoints);
  visitor->trace(m_selectFeatures);
}

DEFINE_TRACE_WRAPPERS(ElementShadowV0) {}

}  // namespace blink