OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/shadow/SlotAssignment.h" | 5 #include "core/dom/shadow/SlotAssignment.h" |
6 | 6 |
7 #include "core/HTMLNames.h" | 7 #include "core/HTMLNames.h" |
8 #include "core/dom/ElementTraversal.h" | 8 #include "core/dom/ElementTraversal.h" |
9 #include "core/dom/NodeTraversal.h" | 9 #include "core/dom/NodeTraversal.h" |
10 #include "core/dom/shadow/ElementShadow.h" | 10 #include "core/dom/shadow/ElementShadow.h" |
11 #include "core/dom/shadow/InsertionPoint.h" | 11 #include "core/dom/shadow/InsertionPoint.h" |
12 #include "core/dom/shadow/ShadowRoot.h" | 12 #include "core/dom/shadow/ShadowRoot.h" |
13 #include "core/html/HTMLSlotElement.h" | 13 #include "core/html/HTMLSlotElement.h" |
14 | 14 |
15 namespace blink { | 15 namespace blink { |
16 | 16 |
17 HTMLSlotElement* SlotAssignment::assignedSlotFor(const Node& node) const | 17 HTMLSlotElement* SlotAssignment::assignedSlotFor(const Node& node) const |
18 { | 18 { |
19 return m_assignment.get(const_cast<Node*>(&node)); | 19 return m_assignment.get(const_cast<Node*>(&node)); |
20 } | 20 } |
21 | 21 |
22 static void detachNotAssignedNode(Node& node) | 22 static void detachNotAssignedNode(Node& node) |
23 { | 23 { |
24 if (node.layoutObject()) | 24 if (node.layoutObject()) |
25 node.lazyReattachIfAttached(); | 25 node.lazyReattachIfAttached(); |
26 } | 26 } |
27 | 27 |
28 inline static bool isDefaultSlotName(const AtomicString& name) | |
29 { | |
30 return name.isNull() || name.isEmpty(); | |
31 } | |
32 | |
33 void SlotAssignment::resolveAssignment(ShadowRoot& shadowRoot) | 28 void SlotAssignment::resolveAssignment(ShadowRoot& shadowRoot) |
34 { | 29 { |
35 m_assignment.clear(); | 30 m_assignment.clear(); |
36 | 31 |
37 using Name2Slot = HeapHashMap<AtomicString, Member<HTMLSlotElement>>; | 32 using Name2Slot = HeapHashMap<AtomicString, Member<HTMLSlotElement>>; |
38 Name2Slot name2slot; | 33 Name2Slot name2slot; |
39 HTMLSlotElement* defaultSlot = nullptr; | |
40 | 34 |
41 const HeapVector<Member<HTMLSlotElement>>& slots = shadowRoot.descendantSlot
s(); | 35 const HeapVector<Member<HTMLSlotElement>>& slots = shadowRoot.descendantSlot
s(); |
42 | 36 |
43 for (Member<HTMLSlotElement> slot : slots) { | 37 for (Member<HTMLSlotElement> slot : slots) { |
44 slot->willUpdateDistribution(); | 38 slot->willUpdateDistribution(); |
45 AtomicString name = slot->fastGetAttribute(HTMLNames::nameAttr); | 39 name2slot.add(slot->name(), slot.get()); |
46 if (isDefaultSlotName(name)) { | |
47 if (!defaultSlot) | |
48 defaultSlot = slot.get(); | |
49 } else { | |
50 name2slot.add(name, slot.get()); | |
51 } | |
52 } | 40 } |
53 | 41 |
54 for (Node& child : NodeTraversal::childrenOf(*shadowRoot.host())) { | 42 for (Node& child : NodeTraversal::childrenOf(*shadowRoot.host())) { |
55 if (child.isElementNode()) { | 43 if (child.isElementNode() && isActiveInsertionPoint(child)) { |
56 if (isActiveInsertionPoint(child)) { | 44 // TODO(hayato): Support re-distribution across v0 and v1 shadow tre
es |
57 // TODO(hayato): Support re-distribution across v0 and v1 shadow
trees | |
58 detachNotAssignedNode(child); | |
59 continue; | |
60 } | |
61 AtomicString slotName = toElement(child).fastGetAttribute(HTMLNames:
:slotAttr); | |
62 if (isDefaultSlotName(slotName)) { | |
63 if (defaultSlot) | |
64 assign(child, *defaultSlot); | |
65 else | |
66 detachNotAssignedNode(child); | |
67 } else { | |
68 HTMLSlotElement* slot = name2slot.get(slotName); | |
69 if (slot) | |
70 assign(child, *slot); | |
71 else | |
72 detachNotAssignedNode(child); | |
73 } | |
74 } else if (defaultSlot && child.isTextNode()) { | |
75 assign(child, *defaultSlot); | |
76 } else { | |
77 detachNotAssignedNode(child); | 45 detachNotAssignedNode(child); |
| 46 continue; |
78 } | 47 } |
| 48 if (!child.slottable()) { |
| 49 detachNotAssignedNode(child); |
| 50 continue; |
| 51 } |
| 52 AtomicString slotName = child.slotName(); |
| 53 HTMLSlotElement* slot = name2slot.get(slotName); |
| 54 if (slot) |
| 55 assign(child, *slot); |
| 56 else |
| 57 detachNotAssignedNode(child); |
79 } | 58 } |
80 | 59 |
81 // Update each slot's distribution in reverse tree order so that a child slo
t is visited before its parent slot. | 60 // Update each slot's distribution in reverse tree order so that a child slo
t is visited before its parent slot. |
82 for (auto slot = slots.rbegin(); slot != slots.rend(); ++slot) | 61 for (auto slot = slots.rbegin(); slot != slots.rend(); ++slot) |
83 (*slot)->updateDistributedNodesWithFallback(); | 62 (*slot)->updateDistributedNodesWithFallback(); |
84 for (const auto& slot : slots) | 63 for (const auto& slot : slots) |
85 slot->didUpdateDistribution(); | 64 slot->didUpdateDistribution(); |
86 } | 65 } |
87 | 66 |
88 void SlotAssignment::assign(Node& hostChild, HTMLSlotElement& slot) | 67 void SlotAssignment::assign(Node& hostChild, HTMLSlotElement& slot) |
89 { | 68 { |
90 DCHECK(hostChild.isSlotAssignable()); | 69 DCHECK(hostChild.isSlotAssignable()); |
91 m_assignment.add(&hostChild, &slot); | 70 m_assignment.add(&hostChild, &slot); |
92 slot.appendAssignedNode(hostChild); | 71 slot.appendAssignedNode(hostChild); |
93 if (isHTMLSlotElement(hostChild)) | 72 if (isHTMLSlotElement(hostChild)) |
94 slot.appendDistributedNodesFrom(toHTMLSlotElement(hostChild)); | 73 slot.appendDistributedNodesFrom(toHTMLSlotElement(hostChild)); |
95 else | 74 else |
96 slot.appendDistributedNode(hostChild); | 75 slot.appendDistributedNode(hostChild); |
97 if (slot.isChildOfV1ShadowHost()) | 76 if (slot.isChildOfV1ShadowHost()) |
98 slot.parentElementShadow()->setNeedsDistributionRecalc(); | 77 slot.parentElementShadow()->setNeedsDistributionRecalc(); |
99 } | 78 } |
100 | 79 |
101 DEFINE_TRACE(SlotAssignment) | 80 DEFINE_TRACE(SlotAssignment) |
102 { | 81 { |
103 visitor->trace(m_assignment); | 82 visitor->trace(m_assignment); |
104 } | 83 } |
105 | 84 |
106 } // namespace blink | 85 } // namespace blink |
OLD | NEW |