Changeset 235483 in webkit
- Timestamp:
- Aug 29, 2018 3:23:30 PM (6 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r235481 r235483 1 2018-08-29 Ryosuke Niwa <rniwa@webkit.org> 2 3 Modernize SlotAssignment 4 https://bugs.webkit.org/show_bug.cgi?id=189075 5 6 Reviewed by Antti Koivisto. 7 8 Modernized the code related to SlotAssignment. Namely, use HashMap<>::get instead of HashMap<>::find, 9 and use HashMap<>::ensure instead of HashMap<>::add. Also use WeakPtr to keep track of HTMLSlotElement 10 instead of a raw pointer. 11 12 * dom/SlotAssignment.cpp: 13 (WebCore::SlotAssignment::findAssignedSlot): 14 (WebCore::SlotAssignment::addSlotElementByName): 15 (WebCore::SlotAssignment::removeSlotElementByName): 16 (WebCore::SlotAssignment::didChangeSlot): 17 (WebCore::SlotAssignment::findFirstSlotElement): 18 (WebCore::SlotAssignment::resolveAllSlotElements): 19 (WebCore::SlotAssignment::assignToSlot): 20 * dom/SlotAssignment.h: 21 (WebCore::SlotAssignment::Slot::Slot): Renamed from SlotInfo since "Info" doesn't add any value. 22 * html/HTMLSlotElement.h: 23 1 24 2018-08-29 Chris Dumez <cdumez@apple.com> 2 25 -
trunk/Source/WebCore/dom/SlotAssignment.cpp
r235458 r235483 58 58 return nullptr; 59 59 60 auto slotName = slotNameForHostChild(node); 61 auto it = m_slots.find(slotName); 62 if (it == m_slots.end()) 63 return nullptr; 64 65 return findFirstSlotElement(*it->value, shadowRoot); 60 auto* slot = m_slots.get(slotNameForHostChild(node)); 61 if (!slot) 62 return nullptr; 63 64 return findFirstSlotElement(*slot, shadowRoot); 66 65 } 67 66 … … 77 76 78 77 const AtomicString& slotName = slotNameFromAttributeValue(name); 79 auto addResult = m_slots. add(slotName, std::unique_ptr<SlotInfo>());80 if (addResult.isNewEntry) {81 addResult.iterator->value = std::make_unique<SlotInfo>(slotElement);82 if (slotName == defaultSlotName()) // Because assignSlots doesn't collect nodes assigned to the default slot as an optimzation.78 auto addResult = m_slots.ensure(slotName, [&] { 79 // Unlike named slots, assignSlots doesn't collect nodes assigned to the default slot 80 // to avoid always having a vector of all child nodes of a shadow host. 81 if (slotName == defaultSlotName()) 83 82 m_slotAssignmentsIsValid = false; 84 return; 85 }86 87 auto& slotInfo = *addResult.iterator->value; 88 89 if (!slot Info.hasSlotElements())90 slot Info.element = &slotElement;83 84 return std::make_unique<Slot>(); 85 }); 86 87 auto& slot = *addResult.iterator->value; 88 if (!slot.hasSlotElements()) 89 slot.element = makeWeakPtr(slotElement); 91 90 else { 92 slot Info.element = nullptr;91 slot.element = nullptr; 93 92 #ifndef NDEBUG 94 93 m_needsToResolveSlotElements = true; 95 94 #endif 96 95 } 97 slot Info.elementCount++;96 slot.elementCount++; 98 97 } 99 98 … … 108 107 host->invalidateStyleAndRenderersForSubtree(); 109 108 110 auto it = m_slots.find(slotNameFromAttributeValue(name)); 111 RELEASE_ASSERT(it != m_slots.end()); 112 113 auto& slotInfo = *it->value; 114 RELEASE_ASSERT(slotInfo.hasSlotElements()); 115 116 slotInfo.elementCount--; 117 if (slotInfo.element == &slotElement) { 118 slotInfo.element = nullptr; 109 auto* slot = m_slots.get(slotNameFromAttributeValue(name)); 110 RELEASE_ASSERT(slot && slot->hasSlotElements()); 111 112 slot->elementCount--; 113 if (slot->element == &slotElement) { 114 slot->element = nullptr; 119 115 #ifndef NDEBUG 120 116 m_needsToResolveSlotElements = true; 121 117 #endif 122 118 } 123 ASSERT(slot Info.element || m_needsToResolveSlotElements);119 ASSERT(slot->element || m_needsToResolveSlotElements); 124 120 } 125 121 … … 137 133 { 138 134 auto& slotName = slotNameFromAttributeValue(slotAttrValue); 139 auto it = m_slots.find(slotName);140 if ( it == m_slots.end())135 auto* slot = m_slots.get(slotName); 136 if (!slot) 141 137 return; 142 138 143 it->value->assignedNodes.clear();139 slot->assignedNodes.clear(); 144 140 m_slotAssignmentsIsValid = false; 145 141 146 RefPtr<HTMLSlotElement> slotElement = findFirstSlotElement(*it->value, shadowRoot);142 auto slotElement = makeRefPtr(findFirstSlotElement(*slot, shadowRoot)); 147 143 if (!slotElement) 148 144 return; … … 165 161 ASSERT(slotElement.containingShadowRoot() == &shadowRoot); 166 162 const AtomicString& slotName = slotNameFromAttributeValue(slotElement.attributeWithoutSynchronization(nameAttr)); 167 auto it = m_slots.find(slotName); 168 RELEASE_ASSERT(it != m_slots.end()); 169 170 auto& slotInfo = *it->value; 163 auto* slot = m_slots.get(slotName); 164 RELEASE_ASSERT(slot); 165 171 166 if (!m_slotAssignmentsIsValid) 172 167 assignSlots(shadowRoot); 173 168 174 if ( !slotInfo.assignedNodes.size())175 return nullptr; 176 177 RELEASE_ASSERT(slot Info.hasSlotElements());178 if (slot Info.hasDuplicatedSlotElements() && findFirstSlotElement(slotInfo, shadowRoot) != &slotElement)179 return nullptr; 180 181 return &slot Info.assignedNodes;169 if (slot->assignedNodes.isEmpty()) 170 return nullptr; 171 172 RELEASE_ASSERT(slot->hasSlotElements()); 173 if (slot->hasDuplicatedSlotElements() && findFirstSlotElement(*slot, shadowRoot) != &slotElement) 174 return nullptr; 175 176 return &slot->assignedNodes; 182 177 } 183 178 … … 187 182 } 188 183 189 HTMLSlotElement* SlotAssignment::findFirstSlotElement(Slot Info& slotInfo, ShadowRoot& shadowRoot)190 { 191 if (slot Info.shouldResolveSlotElement())184 HTMLSlotElement* SlotAssignment::findFirstSlotElement(Slot& slot, ShadowRoot& shadowRoot) 185 { 186 if (slot.shouldResolveSlotElement()) 192 187 resolveAllSlotElements(shadowRoot); 193 188 194 189 #ifndef NDEBUG 195 ASSERT(!slot Info.element || m_slotElementsForConsistencyCheck.contains(slotInfo.element));196 ASSERT(!!slot Info.element == !!slotInfo.elementCount);197 #endif 198 199 return slot Info.element;190 ASSERT(!slot.element || m_slotElementsForConsistencyCheck.contains(slot.element.get())); 191 ASSERT(!!slot.element == !!slot.elementCount); 192 #endif 193 194 return slot.element.get(); 200 195 } 201 196 … … 215 210 auto& slotName = slotNameFromAttributeValue(slotElement.attributeWithoutSynchronization(nameAttr)); 216 211 217 auto it = m_slots.find(slotName); 218 RELEASE_ASSERT(it != m_slots.end()); 219 220 SlotInfo& slotInfo = *it->value; 221 bool hasSeenSlotWithSameName = !!slotInfo.element; 212 auto* slot = m_slots.get(slotName); 213 RELEASE_ASSERT(slot); // slot must have been created when a slot was inserted. 214 215 bool hasSeenSlotWithSameName = !!slot->element; 222 216 if (hasSeenSlotWithSameName) 223 217 continue; 224 218 225 slot Info.element = &slotElement;219 slot->element = makeWeakPtr(slotElement); 226 220 slotCount--; 227 221 if (!slotCount) … … 260 254 } 261 255 262 auto addResult = m_slots.add(slotName, std::make_unique<SlotInfo>()); 256 auto addResult = m_slots.ensure(slotName, [] { 257 return std::make_unique<Slot>(); 258 }); 263 259 addResult.iterator->value->assignedNodes.append(&child); 264 260 } -
trunk/Source/WebCore/dom/SlotAssignment.h
r235458 r235483 31 31 #include <wtf/HashSet.h> 32 32 #include <wtf/Vector.h> 33 #include <wtf/WeakPtr.h> 33 34 #include <wtf/text/AtomicString.h> 34 35 #include <wtf/text/AtomicStringHash.h> … … 62 63 63 64 private: 64 struct Slot Info{65 struct Slot { 65 66 WTF_MAKE_FAST_ALLOCATED; 66 67 public: 67 SlotInfo() { } 68 SlotInfo(HTMLSlotElement& slotElement) 69 : element(&slotElement) 70 , elementCount(1) 71 { } 68 Slot() { } 72 69 73 70 bool hasSlotElements() { return !!elementCount; } … … 75 72 bool shouldResolveSlotElement() { return !element && elementCount; } 76 73 77 HTMLSlotElement* element { nullptr };74 WeakPtr<HTMLSlotElement> element; 78 75 unsigned elementCount { 0 }; 79 76 Vector<Node*> assignedNodes; … … 82 79 virtual const AtomicString& slotNameForHostChild(const Node&) const; 83 80 84 HTMLSlotElement* findFirstSlotElement(Slot Info&, ShadowRoot&);81 HTMLSlotElement* findFirstSlotElement(Slot&, ShadowRoot&); 85 82 void resolveAllSlotElements(ShadowRoot&); 86 83 … … 88 85 void assignToSlot(Node& child, const AtomicString& slotName); 89 86 90 HashMap<AtomicString, std::unique_ptr<Slot Info>> m_slots;87 HashMap<AtomicString, std::unique_ptr<Slot>> m_slots; 91 88 92 89 #ifndef NDEBUG -
trunk/Source/WebCore/html/HTMLSlotElement.h
r235458 r235483 31 31 namespace WebCore { 32 32 33 class HTMLSlotElement final : public HTMLElement {33 class HTMLSlotElement final : public HTMLElement, public CanMakeWeakPtr<HTMLSlotElement> { 34 34 WTF_MAKE_ISO_ALLOCATED(HTMLSlotElement); 35 35 public:
Note: See TracChangeset
for help on using the changeset viewer.