Index: third_party/WebKit/Source/core/dom/shadow/SlotAssignment.cpp |
diff --git a/third_party/WebKit/Source/core/dom/shadow/SlotAssignment.cpp b/third_party/WebKit/Source/core/dom/shadow/SlotAssignment.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6c791be04e33e81dadf9e2aef9daf3cd07409966 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/dom/shadow/SlotAssignment.cpp |
@@ -0,0 +1,94 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "core/dom/shadow/SlotAssignment.h" |
+ |
+#include "core/HTMLNames.h" |
+#include "core/dom/ElementTraversal.h" |
+#include "core/dom/NodeTraversal.h" |
+#include "core/dom/shadow/InsertionPoint.h" |
+#include "core/dom/shadow/ShadowRoot.h" |
+#include "core/html/HTMLSlotElement.h" |
+ |
+namespace blink { |
+ |
+HTMLSlotElement* SlotAssignment::assignedSlotFor(const Node& node) const |
+{ |
+ return m_assignment.get(const_cast<Node*>(&node)); |
+} |
+ |
+static void detachNotAssignedNode(Node& node) |
+{ |
+ if (node.layoutObject()) |
+ node.lazyReattachIfAttached(); |
+} |
+ |
+void SlotAssignment::resolveAssignment(const ShadowRoot& shadowRoot) |
+{ |
+ m_assignment.clear(); |
+ |
+ using Name2Slot = HashMap<AtomicString, HTMLSlotElement*>; |
+ Name2Slot name2slot; |
+ HTMLSlotElement* defaultSlot = nullptr; |
+ |
+ // TODO(hayato): Cache slots elements so that we do not have to travese the shadow tree. See ShadowRoot::descendantInsertionPoints() |
+ for (HTMLSlotElement& slot : Traversal<HTMLSlotElement>::descendantsOf(shadowRoot)) { |
+ slot.clearDistribution(); |
+ |
+ AtomicString name = slot.fastGetAttribute(HTMLNames::nameAttr); |
+ if (name.isNull() || name.isEmpty()) { |
+ if (!defaultSlot) |
+ defaultSlot = &slot; |
+ } else { |
+ name2slot.add(name, &slot); |
+ } |
+ } |
+ |
+ for (Node& child : NodeTraversal::childrenOf(*shadowRoot.host())) { |
+ if (child.isElementNode()) { |
+ if (isActiveInsertionPoint(child)) { |
+ // TODO(hayato): Support re-distribution across v0 and v1 shadow trees |
+ detachNotAssignedNode(child); |
+ continue; |
+ } |
+ AtomicString slotName = toElement(child).fastGetAttribute(HTMLNames::slotAttr); |
+ if (slotName.isNull() || slotName.isEmpty()) { |
+ if (defaultSlot) |
+ assign(child, *defaultSlot); |
+ else |
+ detachNotAssignedNode(child); |
+ } else { |
+ HTMLSlotElement* slot = name2slot.get(slotName); |
+ if (slot) |
+ assign(child, *slot); |
+ else |
+ detachNotAssignedNode(child); |
+ } |
+ } else if (defaultSlot) { |
+ assign(child, *defaultSlot); |
+ } else { |
+ detachNotAssignedNode(child); |
+ } |
+ } |
+} |
+ |
+void SlotAssignment::assign(Node& hostChild, HTMLSlotElement& slot) |
+{ |
+ m_assignment.add(&hostChild, &slot); |
+ slot.appendAssignedNode(hostChild); |
+ if (isHTMLSlotElement(hostChild)) |
+ slot.appendDistributedNodes(toHTMLSlotElement(hostChild).getDistributedNodes()); |
+ else |
+ slot.appendDistributedNode(hostChild); |
+} |
+ |
+DEFINE_TRACE(SlotAssignment) |
+{ |
+#if ENABLE(OILPAN) |
+ visitor->trace(m_assignment); |
+#endif |
+} |
+ |
+} // namespace blink |