| 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/Node.h" |
| 9 #include "core/dom/NodeTraversal.h" | 10 #include "core/dom/NodeTraversal.h" |
| 10 #include "core/dom/shadow/ElementShadow.h" | 11 #include "core/dom/shadow/ElementShadow.h" |
| 11 #include "core/dom/shadow/InsertionPoint.h" | 12 #include "core/dom/shadow/InsertionPoint.h" |
| 12 #include "core/dom/shadow/ShadowRoot.h" | 13 #include "core/dom/shadow/ShadowRoot.h" |
| 13 #include "core/html/HTMLSlotElement.h" | 14 #include "core/html/HTMLSlotElement.h" |
| 14 | 15 |
| 15 namespace blink { | 16 namespace blink { |
| 16 | 17 |
| 17 void SlotAssignment::slotAdded(HTMLSlotElement& slot) { | 18 void SlotAssignment::slotAdded(HTMLSlotElement& slot) { |
| 18 // Relevant DOM Standard: | 19 // Relevant DOM Standard: |
| 19 // https://dom.spec.whatwg.org/#concept-node-insert | 20 // https://dom.spec.whatwg.org/#concept-node-insert |
| 20 // 6.4: Run assign slotables for a tree with node's tree and a set containing | 21 // 6.4: Run assign slotables for a tree with node's tree and a set containing |
| 21 // each inclusive descendant of node that is a slot. | 22 // each inclusive descendant of node that is a slot. |
| 22 | 23 |
| 23 ++m_slotCount; | 24 ++m_slotCount; |
| 24 m_needsCollectSlots = true; | 25 m_needsCollectSlots = true; |
| 25 | 26 |
| 26 if (!m_slotMap->contains(slot.name())) { | 27 if (!m_slotMap->contains(slot.name())) { |
| 27 m_slotMap->add(slot.name(), &slot); | 28 m_slotMap->add(slot.name(), &slot); |
| 28 return; | 29 return; |
| 29 } | 30 } |
| 30 | 31 |
| 31 HTMLSlotElement& oldActive = *findSlotByName(slot.name()); | 32 HTMLSlotElement& oldActive = *findSlotByName(slot.name()); |
| 32 DCHECK_NE(oldActive, slot); | 33 DCHECK_NE(oldActive, slot); |
| 33 m_slotMap->add(slot.name(), &slot); | 34 m_slotMap->add(slot.name(), &slot); |
| 34 if (findSlotByName(slot.name()) == oldActive) | 35 if (findSlotByName(slot.name()) == oldActive) |
| 35 return; | 36 return; |
| 36 // |oldActive| is no longer an active slot. | 37 // |oldActive| is no longer an active slot. |
| 37 if (oldActive.findHostChildWithSameSlotName()) | 38 if (oldActive.findHostChildWithSameSlotName()) |
| 38 oldActive.enqueueSlotChangeEvent(); | 39 oldActive.didSlotChange(SlotChangeType::Initial); |
| 39 // TODO(hayato): We should not enqeueue a slotchange event for |oldActive| | 40 // TODO(hayato): We should not enqeueue a slotchange event for |oldActive| |
| 40 // if |oldActive| was inserted together with |slot|. | 41 // if |oldActive| was inserted together with |slot|. |
| 41 // This could happen if |oldActive| and |slot| are descendants of the inserted | 42 // This could happen if |oldActive| and |slot| are descendants of the inserted |
| 42 // node, and |oldActive| is preceding |slot|. | 43 // node, and |oldActive| is preceding |slot|. |
| 43 } | 44 } |
| 44 | 45 |
| 45 void SlotAssignment::slotRemoved(HTMLSlotElement& slot) { | 46 void SlotAssignment::slotRemoved(HTMLSlotElement& slot) { |
| 46 DCHECK_GT(m_slotCount, 0u); | 47 DCHECK_GT(m_slotCount, 0u); |
| 47 --m_slotCount; | 48 --m_slotCount; |
| 48 m_needsCollectSlots = true; | 49 m_needsCollectSlots = true; |
| 49 | 50 |
| 50 DCHECK(m_slotMap->contains(slot.name())); | 51 DCHECK(m_slotMap->contains(slot.name())); |
| 51 HTMLSlotElement* oldActive = findSlotByName(slot.name()); | 52 HTMLSlotElement* oldActive = findSlotByName(slot.name()); |
| 52 m_slotMap->remove(slot.name(), &slot); | 53 m_slotMap->remove(slot.name(), &slot); |
| 53 HTMLSlotElement* newActive = findSlotByName(slot.name()); | 54 HTMLSlotElement* newActive = findSlotByName(slot.name()); |
| 54 if (newActive && newActive != oldActive) { | 55 if (newActive && newActive != oldActive) { |
| 55 // |newActive| slot becomes an active slot. | 56 // |newActive| slot becomes an active slot. |
| 56 if (newActive->findHostChildWithSameSlotName()) | 57 if (newActive->findHostChildWithSameSlotName()) |
| 57 newActive->enqueueSlotChangeEvent(); | 58 newActive->didSlotChange(SlotChangeType::Initial); |
| 58 // TODO(hayato): Prevent a false-positive slotchange. | 59 // TODO(hayato): Prevent a false-positive slotchange. |
| 59 // This could happen if more than one slots which have the same name are | 60 // This could happen if more than one slots which have the same name are |
| 60 // descendants of the removed node. | 61 // descendants of the removed node. |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 | 64 |
| 64 bool SlotAssignment::findHostChildBySlotName( | 65 bool SlotAssignment::findHostChildBySlotName( |
| 65 const AtomicString& slotName) const { | 66 const AtomicString& slotName) const { |
| 66 // TODO(hayato): Avoid traversing children every time. | 67 // TODO(hayato): Avoid traversing children every time. |
| 67 for (Node& child : NodeTraversal::childrenOf(m_owner->host())) { | 68 for (Node& child : NodeTraversal::childrenOf(m_owner->host())) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 79 // slot.hasAssignedNodesSynchronously. | 80 // slot.hasAssignedNodesSynchronously. |
| 80 bool hasAssignedNodesBefore = (findSlotByName(oldSlotName) == &slot) && | 81 bool hasAssignedNodesBefore = (findSlotByName(oldSlotName) == &slot) && |
| 81 findHostChildBySlotName(oldSlotName); | 82 findHostChildBySlotName(oldSlotName); |
| 82 | 83 |
| 83 m_slotMap->remove(oldSlotName, &slot); | 84 m_slotMap->remove(oldSlotName, &slot); |
| 84 m_slotMap->add(slot.name(), &slot); | 85 m_slotMap->add(slot.name(), &slot); |
| 85 | 86 |
| 86 bool hasAssignedNodesAfter = slot.hasAssignedNodesSlow(); | 87 bool hasAssignedNodesAfter = slot.hasAssignedNodesSlow(); |
| 87 | 88 |
| 88 if (hasAssignedNodesBefore || hasAssignedNodesAfter) | 89 if (hasAssignedNodesBefore || hasAssignedNodesAfter) |
| 89 slot.enqueueSlotChangeEvent(); | 90 slot.didSlotChange(SlotChangeType::Initial); |
| 90 } | 91 } |
| 91 | 92 |
| 92 void SlotAssignment::hostChildSlotNameChanged(const AtomicString& oldValue, | 93 void SlotAssignment::hostChildSlotNameChanged(const AtomicString& oldValue, |
| 93 const AtomicString& newValue) { | 94 const AtomicString& newValue) { |
| 94 if (HTMLSlotElement* slot = | 95 if (HTMLSlotElement* slot = |
| 95 findSlotByName(HTMLSlotElement::normalizeSlotName(oldValue))) { | 96 findSlotByName(HTMLSlotElement::normalizeSlotName(oldValue))) { |
| 96 slot->enqueueSlotChangeEvent(); | 97 slot->didSlotChange(SlotChangeType::Initial); |
| 97 m_owner->owner()->setNeedsDistributionRecalc(); | 98 m_owner->owner()->setNeedsDistributionRecalc(); |
| 98 } | 99 } |
| 99 if (HTMLSlotElement* slot = | 100 if (HTMLSlotElement* slot = |
| 100 findSlotByName(HTMLSlotElement::normalizeSlotName(newValue))) { | 101 findSlotByName(HTMLSlotElement::normalizeSlotName(newValue))) { |
| 101 slot->enqueueSlotChangeEvent(); | 102 slot->didSlotChange(SlotChangeType::Initial); |
| 102 m_owner->owner()->setNeedsDistributionRecalc(); | 103 m_owner->owner()->setNeedsDistributionRecalc(); |
| 103 } | 104 } |
| 104 } | 105 } |
| 105 | 106 |
| 106 SlotAssignment::SlotAssignment(ShadowRoot& owner) | 107 SlotAssignment::SlotAssignment(ShadowRoot& owner) |
| 107 : m_slotMap(DocumentOrderedMap::create()), | 108 : m_slotMap(DocumentOrderedMap::create()), |
| 108 m_owner(&owner), | 109 m_owner(&owner), |
| 109 m_needsCollectSlots(false), | 110 m_needsCollectSlots(false), |
| 110 m_slotCount(0) { | 111 m_slotCount(0) { |
| 111 DCHECK(owner.isV1()); | 112 DCHECK(owner.isV1()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 DCHECK_EQ(m_slots.size(), m_slotCount); | 176 DCHECK_EQ(m_slots.size(), m_slotCount); |
| 176 } | 177 } |
| 177 | 178 |
| 178 DEFINE_TRACE(SlotAssignment) { | 179 DEFINE_TRACE(SlotAssignment) { |
| 179 visitor->trace(m_slots); | 180 visitor->trace(m_slots); |
| 180 visitor->trace(m_slotMap); | 181 visitor->trace(m_slotMap); |
| 181 visitor->trace(m_owner); | 182 visitor->trace(m_owner); |
| 182 } | 183 } |
| 183 | 184 |
| 184 } // namespace blink | 185 } // namespace blink |
| OLD | NEW |