Changeset 235483 in webkit


Ignore:
Timestamp:
Aug 29, 2018 3:23:30 PM (6 years ago)
Author:
rniwa@webkit.org
Message:

Modernize SlotAssignment
https://bugs.webkit.org/show_bug.cgi?id=189075

Reviewed by Antti Koivisto.

Modernized the code related to SlotAssignment. Namely, use HashMap<>::get instead of HashMap<>::find,
and use HashMap<>::ensure instead of HashMap<>::add. Also use WeakPtr to keep track of HTMLSlotElement
instead of a raw pointer.

  • dom/SlotAssignment.cpp:

(WebCore::SlotAssignment::findAssignedSlot):
(WebCore::SlotAssignment::addSlotElementByName):
(WebCore::SlotAssignment::removeSlotElementByName):
(WebCore::SlotAssignment::didChangeSlot):
(WebCore::SlotAssignment::findFirstSlotElement):
(WebCore::SlotAssignment::resolveAllSlotElements):
(WebCore::SlotAssignment::assignToSlot):

  • dom/SlotAssignment.h:

(WebCore::SlotAssignment::Slot::Slot): Renamed from SlotInfo since "Info" doesn't add any value.

  • html/HTMLSlotElement.h:
Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r235481 r235483  
     12018-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
    1242018-08-29  Chris Dumez  <cdumez@apple.com>
    225
  • trunk/Source/WebCore/dom/SlotAssignment.cpp

    r235458 r235483  
    5858        return nullptr;
    5959
    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);
    6665}
    6766
     
    7776
    7877    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())
    8382            m_slotAssignmentsIsValid = false;
    84         return;
    85     }
    86 
    87     auto& slotInfo = *addResult.iterator->value;
    88 
    89     if (!slotInfo.hasSlotElements())
    90         slotInfo.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);
    9190    else {
    92         slotInfo.element = nullptr;
     91        slot.element = nullptr;
    9392#ifndef NDEBUG
    9493        m_needsToResolveSlotElements = true;
    9594#endif
    9695    }
    97     slotInfo.elementCount++;
     96    slot.elementCount++;
    9897}
    9998
     
    108107        host->invalidateStyleAndRenderersForSubtree();
    109108
    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;
    119115#ifndef NDEBUG
    120116        m_needsToResolveSlotElements = true;
    121117#endif
    122118    }
    123     ASSERT(slotInfo.element || m_needsToResolveSlotElements);
     119    ASSERT(slot->element || m_needsToResolveSlotElements);
    124120}
    125121
     
    137133{
    138134    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)
    141137        return;
    142138   
    143     it->value->assignedNodes.clear();
     139    slot->assignedNodes.clear();
    144140    m_slotAssignmentsIsValid = false;
    145141
    146     RefPtr<HTMLSlotElement> slotElement = findFirstSlotElement(*it->value, shadowRoot);
     142    auto slotElement = makeRefPtr(findFirstSlotElement(*slot, shadowRoot));
    147143    if (!slotElement)
    148144        return;
     
    165161    ASSERT(slotElement.containingShadowRoot() == &shadowRoot);
    166162    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
    171166    if (!m_slotAssignmentsIsValid)
    172167        assignSlots(shadowRoot);
    173168
    174     if (!slotInfo.assignedNodes.size())
    175         return nullptr;
    176 
    177     RELEASE_ASSERT(slotInfo.hasSlotElements());
    178     if (slotInfo.hasDuplicatedSlotElements() && findFirstSlotElement(slotInfo, shadowRoot) != &slotElement)
    179         return nullptr;
    180 
    181     return &slotInfo.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;
    182177}
    183178
     
    187182}
    188183
    189 HTMLSlotElement* SlotAssignment::findFirstSlotElement(SlotInfo& slotInfo, ShadowRoot& shadowRoot)
    190 {
    191     if (slotInfo.shouldResolveSlotElement())
     184HTMLSlotElement* SlotAssignment::findFirstSlotElement(Slot& slot, ShadowRoot& shadowRoot)
     185{
     186    if (slot.shouldResolveSlotElement())
    192187        resolveAllSlotElements(shadowRoot);
    193188
    194189#ifndef NDEBUG
    195     ASSERT(!slotInfo.element || m_slotElementsForConsistencyCheck.contains(slotInfo.element));
    196     ASSERT(!!slotInfo.element == !!slotInfo.elementCount);
    197 #endif
    198 
    199     return slotInfo.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();
    200195}
    201196
     
    215210        auto& slotName = slotNameFromAttributeValue(slotElement.attributeWithoutSynchronization(nameAttr));
    216211
    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;
    222216        if (hasSeenSlotWithSameName)
    223217            continue;
    224218
    225         slotInfo.element = &slotElement;
     219        slot->element = makeWeakPtr(slotElement);
    226220        slotCount--;
    227221        if (!slotCount)
     
    260254    }
    261255
    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    });
    263259    addResult.iterator->value->assignedNodes.append(&child);
    264260}
  • trunk/Source/WebCore/dom/SlotAssignment.h

    r235458 r235483  
    3131#include <wtf/HashSet.h>
    3232#include <wtf/Vector.h>
     33#include <wtf/WeakPtr.h>
    3334#include <wtf/text/AtomicString.h>
    3435#include <wtf/text/AtomicStringHash.h>
     
    6263
    6364private:
    64     struct SlotInfo {
     65    struct Slot {
    6566        WTF_MAKE_FAST_ALLOCATED;
    6667    public:
    67         SlotInfo() { }
    68         SlotInfo(HTMLSlotElement& slotElement)
    69             : element(&slotElement)
    70             , elementCount(1)
    71         { }
     68        Slot() { }
    7269
    7370        bool hasSlotElements() { return !!elementCount; }
     
    7572        bool shouldResolveSlotElement() { return !element && elementCount; }
    7673
    77         HTMLSlotElement* element { nullptr };
     74        WeakPtr<HTMLSlotElement> element;
    7875        unsigned elementCount { 0 };
    7976        Vector<Node*> assignedNodes;
     
    8279    virtual const AtomicString& slotNameForHostChild(const Node&) const;
    8380
    84     HTMLSlotElement* findFirstSlotElement(SlotInfo&, ShadowRoot&);
     81    HTMLSlotElement* findFirstSlotElement(Slot&, ShadowRoot&);
    8582    void resolveAllSlotElements(ShadowRoot&);
    8683
     
    8885    void assignToSlot(Node& child, const AtomicString& slotName);
    8986
    90     HashMap<AtomicString, std::unique_ptr<SlotInfo>> m_slots;
     87    HashMap<AtomicString, std::unique_ptr<Slot>> m_slots;
    9188
    9289#ifndef NDEBUG
  • trunk/Source/WebCore/html/HTMLSlotElement.h

    r235458 r235483  
    3131namespace WebCore {
    3232
    33 class HTMLSlotElement final : public HTMLElement {
     33class HTMLSlotElement final : public HTMLElement, public CanMakeWeakPtr<HTMLSlotElement> {
    3434    WTF_MAKE_ISO_ALLOCATED(HTMLSlotElement);
    3535public:
Note: See TracChangeset for help on using the changeset viewer.