| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Neither the name of Google Inc. nor the names of its | 10 * * Neither the name of Google Inc. nor the names of its |
| 11 * contributors may be used to endorse or promote products derived from | 11 * contributors may be used to endorse or promote products derived from |
| 12 * this software without specific prior written permission. | 12 * this software without specific prior written permission. |
| 13 * | 13 * |
| 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "core/dom/shadow/ElementShadow.h" | 27 #include "core/dom/shadow/ElementShadow.h" |
| 28 | 28 |
| 29 #include "core/css/StyleSheetList.h" | 29 #include "core/css/StyleSheetList.h" |
| 30 #include "core/dom/ElementTraversal.h" | |
| 31 #include "core/dom/NodeTraversal.h" | |
| 32 #include "core/dom/StyleChangeReason.h" | 30 #include "core/dom/StyleChangeReason.h" |
| 33 #include "core/dom/shadow/DistributedNodes.h" | 31 #include "core/dom/shadow/ElementShadowV0.h" |
| 34 #include "core/frame/Deprecation.h" | 32 #include "core/frame/Deprecation.h" |
| 35 #include "core/html/HTMLContentElement.h" | |
| 36 #include "core/html/HTMLShadowElement.h" | |
| 37 #include "core/inspector/InspectorInstrumentation.h" | 33 #include "core/inspector/InspectorInstrumentation.h" |
| 38 #include "platform/EventDispatchForbiddenScope.h" | 34 #include "platform/EventDispatchForbiddenScope.h" |
| 39 #include "platform/ScriptForbiddenScope.h" | 35 #include "platform/ScriptForbiddenScope.h" |
| 40 | 36 |
| 41 namespace blink { | 37 namespace blink { |
| 42 | 38 |
| 43 class DistributionPool final { | |
| 44 STACK_ALLOCATED(); | |
| 45 public: | |
| 46 explicit DistributionPool(const ContainerNode&); | |
| 47 void clear(); | |
| 48 ~DistributionPool(); | |
| 49 void distributeTo(InsertionPoint*, ElementShadow*); | |
| 50 void populateChildren(const ContainerNode&); | |
| 51 | |
| 52 private: | |
| 53 void detachNonDistributedNodes(); | |
| 54 HeapVector<Member<Node>, 32> m_nodes; | |
| 55 Vector<bool, 32> m_distributed; | |
| 56 }; | |
| 57 | |
| 58 inline DistributionPool::DistributionPool(const ContainerNode& parent) | |
| 59 { | |
| 60 populateChildren(parent); | |
| 61 } | |
| 62 | |
| 63 inline void DistributionPool::clear() | |
| 64 { | |
| 65 detachNonDistributedNodes(); | |
| 66 m_nodes.clear(); | |
| 67 m_distributed.clear(); | |
| 68 } | |
| 69 | |
| 70 inline void DistributionPool::populateChildren(const ContainerNode& parent) | |
| 71 { | |
| 72 clear(); | |
| 73 for (Node* child = parent.firstChild(); child; child = child->nextSibling())
{ | |
| 74 if (isHTMLSlotElement(child)) { | |
| 75 // TODO(hayato): Support re-distribution across v0 and v1 shadow tre
es | |
| 76 continue; | |
| 77 } | |
| 78 if (isActiveInsertionPoint(*child)) { | |
| 79 InsertionPoint* insertionPoint = toInsertionPoint(child); | |
| 80 for (size_t i = 0; i < insertionPoint->distributedNodesSize(); ++i) | |
| 81 m_nodes.append(insertionPoint->distributedNodeAt(i)); | |
| 82 } else { | |
| 83 m_nodes.append(child); | |
| 84 } | |
| 85 } | |
| 86 m_distributed.resize(m_nodes.size()); | |
| 87 m_distributed.fill(false); | |
| 88 } | |
| 89 | |
| 90 void DistributionPool::distributeTo(InsertionPoint* insertionPoint, ElementShado
w* elementShadow) | |
| 91 { | |
| 92 DistributedNodes distributedNodes; | |
| 93 | |
| 94 for (size_t i = 0; i < m_nodes.size(); ++i) { | |
| 95 if (m_distributed[i]) | |
| 96 continue; | |
| 97 | |
| 98 if (isHTMLContentElement(*insertionPoint) && !toHTMLContentElement(inser
tionPoint)->canSelectNode(m_nodes, i)) | |
| 99 continue; | |
| 100 | |
| 101 Node* node = m_nodes[i]; | |
| 102 distributedNodes.append(node); | |
| 103 elementShadow->didDistributeNode(node, insertionPoint); | |
| 104 m_distributed[i] = true; | |
| 105 } | |
| 106 | |
| 107 // Distributes fallback elements | |
| 108 if (insertionPoint->isContentInsertionPoint() && distributedNodes.isEmpty())
{ | |
| 109 for (Node* fallbackNode = insertionPoint->firstChild(); fallbackNode; fa
llbackNode = fallbackNode->nextSibling()) { | |
| 110 distributedNodes.append(fallbackNode); | |
| 111 elementShadow->didDistributeNode(fallbackNode, insertionPoint); | |
| 112 } | |
| 113 } | |
| 114 insertionPoint->setDistributedNodes(distributedNodes); | |
| 115 } | |
| 116 | |
| 117 inline DistributionPool::~DistributionPool() | |
| 118 { | |
| 119 detachNonDistributedNodes(); | |
| 120 } | |
| 121 | |
| 122 inline void DistributionPool::detachNonDistributedNodes() | |
| 123 { | |
| 124 for (size_t i = 0; i < m_nodes.size(); ++i) { | |
| 125 if (m_distributed[i]) | |
| 126 continue; | |
| 127 if (m_nodes[i]->layoutObject()) | |
| 128 m_nodes[i]->lazyReattachIfAttached(); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 ElementShadow* ElementShadow::create() | 39 ElementShadow* ElementShadow::create() |
| 133 { | 40 { |
| 134 return new ElementShadow(); | 41 return new ElementShadow(); |
| 135 } | 42 } |
| 136 | 43 |
| 137 ElementShadow::ElementShadow() | 44 ElementShadow::ElementShadow() |
| 138 : m_needsDistributionRecalc(false) | 45 : m_needsDistributionRecalc(false) |
| 139 , m_needsSelectFeatureSet(false) | |
| 140 { | 46 { |
| 141 } | 47 } |
| 142 | 48 |
| 143 ElementShadow::~ElementShadow() | 49 ElementShadow::~ElementShadow() |
| 144 { | 50 { |
| 145 } | 51 } |
| 146 | 52 |
| 147 ShadowRoot& ElementShadow::youngestShadowRoot() const | 53 ShadowRoot& ElementShadow::youngestShadowRoot() const |
| 148 { | 54 { |
| 149 ShadowRoot* current = m_shadowRoot; | 55 ShadowRoot* current = m_shadowRoot; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 160 | 66 |
| 161 if (type == ShadowRootType::V0 && m_shadowRoot) { | 67 if (type == ShadowRootType::V0 && m_shadowRoot) { |
| 162 DCHECK_EQ(m_shadowRoot->type(), ShadowRootType::V0); | 68 DCHECK_EQ(m_shadowRoot->type(), ShadowRootType::V0); |
| 163 Deprecation::countDeprecation(shadowHost.document(), UseCounter::Element
CreateShadowRootMultiple); | 69 Deprecation::countDeprecation(shadowHost.document(), UseCounter::Element
CreateShadowRootMultiple); |
| 164 } | 70 } |
| 165 | 71 |
| 166 if (m_shadowRoot) { | 72 if (m_shadowRoot) { |
| 167 // TODO(hayato): Is the order, from the youngest to the oldest, importan
t? | 73 // TODO(hayato): Is the order, from the youngest to the oldest, importan
t? |
| 168 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderS
hadowRoot()) | 74 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderS
hadowRoot()) |
| 169 root->lazyReattachIfAttached(); | 75 root->lazyReattachIfAttached(); |
| 76 } else if (type == ShadowRootType::V0 || type == ShadowRootType::UserAgent)
{ |
| 77 DCHECK(!m_elementShadowV0); |
| 78 m_elementShadowV0 = ElementShadowV0::create(*this); |
| 170 } | 79 } |
| 171 | 80 |
| 172 ShadowRoot* shadowRoot = ShadowRoot::create(shadowHost.document(), type); | 81 ShadowRoot* shadowRoot = ShadowRoot::create(shadowHost.document(), type); |
| 173 shadowRoot->setParentOrShadowHostNode(&shadowHost); | 82 shadowRoot->setParentOrShadowHostNode(&shadowHost); |
| 174 shadowRoot->setParentTreeScope(shadowHost.treeScope()); | 83 shadowRoot->setParentTreeScope(shadowHost.treeScope()); |
| 175 appendShadowRoot(*shadowRoot); | 84 appendShadowRoot(*shadowRoot); |
| 176 setNeedsDistributionRecalc(); | 85 setNeedsDistributionRecalc(); |
| 177 | 86 |
| 178 shadowRoot->insertedInto(&shadowHost); | 87 shadowRoot->insertedInto(&shadowHost); |
| 179 shadowHost.setChildNeedsStyleRecalc(); | 88 shadowHost.setChildNeedsStyleRecalc(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 root->detachLayoutTree(childrenContext); | 126 root->detachLayoutTree(childrenContext); |
| 218 } | 127 } |
| 219 | 128 |
| 220 void ElementShadow::setNeedsDistributionRecalc() | 129 void ElementShadow::setNeedsDistributionRecalc() |
| 221 { | 130 { |
| 222 if (m_needsDistributionRecalc) | 131 if (m_needsDistributionRecalc) |
| 223 return; | 132 return; |
| 224 m_needsDistributionRecalc = true; | 133 m_needsDistributionRecalc = true; |
| 225 host().markAncestorsWithChildNeedsDistributionRecalc(); | 134 host().markAncestorsWithChildNeedsDistributionRecalc(); |
| 226 if (!isV1()) | 135 if (!isV1()) |
| 227 clearDistributionV0(); | 136 v0().clearDistribution(); |
| 228 } | 137 } |
| 229 | 138 |
| 230 bool ElementShadow::hasSameStyles(const ElementShadow* other) const | 139 bool ElementShadow::hasSameStyles(const ElementShadow& other) const |
| 231 { | 140 { |
| 232 ShadowRoot* root = &youngestShadowRoot(); | 141 ShadowRoot* root = &youngestShadowRoot(); |
| 233 ShadowRoot* otherRoot = &other->youngestShadowRoot(); | 142 ShadowRoot* otherRoot = &other.youngestShadowRoot(); |
| 234 while (root || otherRoot) { | 143 while (root || otherRoot) { |
| 235 if (!root || !otherRoot) | 144 if (!root || !otherRoot) |
| 236 return false; | 145 return false; |
| 237 | 146 |
| 238 StyleSheetList& list = root->styleSheets(); | 147 StyleSheetList& list = root->styleSheets(); |
| 239 StyleSheetList& otherList = otherRoot->styleSheets(); | 148 StyleSheetList& otherList = otherRoot->styleSheets(); |
| 240 | 149 |
| 241 if (list.length() != otherList.length()) | 150 if (list.length() != otherList.length()) |
| 242 return false; | 151 return false; |
| 243 | 152 |
| 244 for (size_t i = 0; i < list.length(); i++) { | 153 for (size_t i = 0; i < list.length(); i++) { |
| 245 if (toCSSStyleSheet(list.item(i))->contents() != toCSSStyleSheet(oth
erList.item(i))->contents()) | 154 if (toCSSStyleSheet(list.item(i))->contents() != toCSSStyleSheet(oth
erList.item(i))->contents()) |
| 246 return false; | 155 return false; |
| 247 } | 156 } |
| 248 root = root->olderShadowRoot(); | 157 root = root->olderShadowRoot(); |
| 249 otherRoot = otherRoot->olderShadowRoot(); | 158 otherRoot = otherRoot->olderShadowRoot(); |
| 250 } | 159 } |
| 251 | 160 |
| 252 return true; | 161 return true; |
| 253 } | 162 } |
| 254 | 163 |
| 255 const InsertionPoint* ElementShadow::finalDestinationInsertionPointFor(const Nod
e* key) const | |
| 256 { | |
| 257 DCHECK(!isV1()); | |
| 258 DCHECK(key); | |
| 259 DCHECK(!key->needsDistributionRecalc()); | |
| 260 NodeToDestinationInsertionPoints::const_iterator it = m_nodeToInsertionPoint
s.find(key); | |
| 261 return it == m_nodeToInsertionPoints.end() ? nullptr : it->value->last(); | |
| 262 } | |
| 263 | |
| 264 const DestinationInsertionPoints* ElementShadow::destinationInsertionPointsFor(c
onst Node* key) const | |
| 265 { | |
| 266 DCHECK(!isV1()); | |
| 267 DCHECK(key); | |
| 268 DCHECK(!key->needsDistributionRecalc()); | |
| 269 NodeToDestinationInsertionPoints::const_iterator it = m_nodeToInsertionPoint
s.find(key); | |
| 270 return it == m_nodeToInsertionPoints.end() ? nullptr : it->value; | |
| 271 } | |
| 272 | |
| 273 void ElementShadow::distribute() | 164 void ElementShadow::distribute() |
| 274 { | 165 { |
| 275 if (isV1()) | 166 if (isV1()) |
| 276 youngestShadowRoot().distributeV1(); | 167 youngestShadowRoot().distributeV1(); |
| 277 else | 168 else |
| 278 distributeV0(); | 169 v0().distribute(); |
| 279 } | |
| 280 | |
| 281 void ElementShadow::distributeV0() | |
| 282 { | |
| 283 DCHECK(!isV1()); | |
| 284 HeapVector<Member<HTMLShadowElement>, 32> shadowInsertionPoints; | |
| 285 DistributionPool pool(host()); | |
| 286 | |
| 287 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderShado
wRoot()) { | |
| 288 HTMLShadowElement* shadowInsertionPoint = 0; | |
| 289 const HeapVector<Member<InsertionPoint>>& insertionPoints = root->descen
dantInsertionPoints(); | |
| 290 for (size_t i = 0; i < insertionPoints.size(); ++i) { | |
| 291 InsertionPoint* point = insertionPoints[i]; | |
| 292 if (!point->isActive()) | |
| 293 continue; | |
| 294 if (isHTMLShadowElement(*point)) { | |
| 295 DCHECK(!shadowInsertionPoint); | |
| 296 shadowInsertionPoint = toHTMLShadowElement(point); | |
| 297 shadowInsertionPoints.append(shadowInsertionPoint); | |
| 298 } else { | |
| 299 pool.distributeTo(point, this); | |
| 300 if (ElementShadow* shadow = shadowWhereNodeCanBeDistributedForV0
(*point)) | |
| 301 shadow->setNeedsDistributionRecalc(); | |
| 302 } | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 for (size_t i = shadowInsertionPoints.size(); i > 0; --i) { | |
| 307 HTMLShadowElement* shadowInsertionPoint = shadowInsertionPoints[i - 1]; | |
| 308 ShadowRoot* root = shadowInsertionPoint->containingShadowRoot(); | |
| 309 DCHECK(root); | |
| 310 if (root->isOldest()) { | |
| 311 pool.distributeTo(shadowInsertionPoint, this); | |
| 312 } else if (root->olderShadowRoot()->type() == root->type()) { | |
| 313 // Only allow reprojecting older shadow roots between the same type
to | |
| 314 // disallow reprojecting UA elements into author shadows. | |
| 315 DistributionPool olderShadowRootPool(*root->olderShadowRoot()); | |
| 316 olderShadowRootPool.distributeTo(shadowInsertionPoint, this); | |
| 317 root->olderShadowRoot()->setShadowInsertionPointOfYoungerShadowRoot(
shadowInsertionPoint); | |
| 318 } | |
| 319 if (ElementShadow* shadow = shadowWhereNodeCanBeDistributedForV0(*shadow
InsertionPoint)) | |
| 320 shadow->setNeedsDistributionRecalc(); | |
| 321 } | |
| 322 InspectorInstrumentation::didPerformElementShadowDistribution(&host()); | |
| 323 } | |
| 324 | |
| 325 void ElementShadow::didDistributeNode(const Node* node, InsertionPoint* insertio
nPoint) | |
| 326 { | |
| 327 DCHECK(!isV1()); | |
| 328 NodeToDestinationInsertionPoints::AddResult result = m_nodeToInsertionPoints
.add(node, nullptr); | |
| 329 if (result.isNewEntry) | |
| 330 result.storedValue->value = new DestinationInsertionPoints; | |
| 331 result.storedValue->value->append(insertionPoint); | |
| 332 } | |
| 333 | |
| 334 const SelectRuleFeatureSet& ElementShadow::ensureSelectFeatureSet() | |
| 335 { | |
| 336 DCHECK(!isV1()); | |
| 337 if (!m_needsSelectFeatureSet) | |
| 338 return m_selectFeatures; | |
| 339 | |
| 340 m_selectFeatures.clear(); | |
| 341 for (ShadowRoot* root = &oldestShadowRoot(); root; root = root->youngerShado
wRoot()) | |
| 342 collectSelectFeatureSetFrom(*root); | |
| 343 m_needsSelectFeatureSet = false; | |
| 344 return m_selectFeatures; | |
| 345 } | |
| 346 | |
| 347 void ElementShadow::collectSelectFeatureSetFrom(ShadowRoot& root) | |
| 348 { | |
| 349 DCHECK(!isV1()); | |
| 350 if (!root.containsShadowRoots() && !root.containsContentElements()) | |
| 351 return; | |
| 352 | |
| 353 for (Element& element : ElementTraversal::descendantsOf(root)) { | |
| 354 if (ElementShadow* shadow = element.shadow()) | |
| 355 m_selectFeatures.add(shadow->ensureSelectFeatureSet()); | |
| 356 if (!isHTMLContentElement(element)) | |
| 357 continue; | |
| 358 const CSSSelectorList& list = toHTMLContentElement(element).selectorList
(); | |
| 359 m_selectFeatures.collectFeaturesFromSelectorList(list); | |
| 360 } | |
| 361 } | |
| 362 | |
| 363 void ElementShadow::willAffectSelector() | |
| 364 { | |
| 365 DCHECK(!isV1()); | |
| 366 for (ElementShadow* shadow = this; shadow; shadow = shadow->containingShadow
()) { | |
| 367 if (shadow->needsSelectFeatureSet()) | |
| 368 break; | |
| 369 shadow->setNeedsSelectFeatureSet(); | |
| 370 } | |
| 371 setNeedsDistributionRecalc(); | |
| 372 } | |
| 373 | |
| 374 void ElementShadow::clearDistributionV0() | |
| 375 { | |
| 376 DCHECK(!isV1()); | |
| 377 m_nodeToInsertionPoints.clear(); | |
| 378 | |
| 379 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderShado
wRoot()) | |
| 380 root->setShadowInsertionPointOfYoungerShadowRoot(nullptr); | |
| 381 } | 170 } |
| 382 | 171 |
| 383 DEFINE_TRACE(ElementShadow) | 172 DEFINE_TRACE(ElementShadow) |
| 384 { | 173 { |
| 385 visitor->trace(m_nodeToInsertionPoints); | 174 visitor->trace(m_elementShadowV0); |
| 386 visitor->trace(m_selectFeatures); | |
| 387 visitor->trace(m_shadowRoot); | 175 visitor->trace(m_shadowRoot); |
| 388 } | 176 } |
| 389 | 177 |
| 390 DEFINE_TRACE_WRAPPERS(ElementShadow) | 178 DEFINE_TRACE_WRAPPERS(ElementShadow) |
| 391 { | 179 { |
| 180 visitor->traceWrappers(m_elementShadowV0); |
| 392 visitor->traceWrappers(m_shadowRoot); | 181 visitor->traceWrappers(m_shadowRoot); |
| 393 } | 182 } |
| 394 | 183 |
| 395 } // namespace blink | 184 } // namespace blink |
| OLD | NEW |