Changeset 261028 in webkit


Ignore:
Timestamp:
May 1, 2020 3:16:19 PM (4 years ago)
Author:
dbates@webkit.org
Message:

Change HitTestResult::NodeSet from set of RefPtrs to set of Refs
https://bugs.webkit.org/show_bug.cgi?id=211306

Reviewed by Simon Fraser.

HitTestResult::listBasedTestResult() never returns a set with nullptrs in it.
So, change the set declaration from ListHashSet<RefPtr<Node>> to ListHashSet<Ref<Node>>.
This way people are not tempted to unnecessarily null check the nodes in the set.

As I made this change to TreeScope::elementsFromPoint() I noticed that retargetToScope(),
which is called by it, returned a Node&. So, I changed it to return a Ref<Node>. That
required me to fix up caretRangeFromPoint(), which lead me to fix up nodeFromPoint() as well.

  • dom/Document.cpp:

(WebCore::Document::caretRangeFromPoint):

  • dom/EventPath.cpp:

(WebCore::RelatedNodeRetargeter::checkConsistency):

  • dom/TreeScope.cpp:

(WebCore::TreeScope::retargetToScope const):
(WebCore::TreeScope::nodeFromPoint):
(WebCore::TreeScope::elementFromPoint):
(WebCore::TreeScope::elementsFromPoint):

  • dom/TreeScope.h:
  • page/Page.cpp:

(WebCore::Page::editableElementsInRect const):

  • rendering/HitTestResult.cpp:

(WebCore::appendToNodeSet):
(WebCore::HitTestResult::HitTestResult):
(WebCore::HitTestResult::operator=):
(WebCore::HitTestResult::addNodeToListBasedTestResultCommon):
(WebCore::HitTestResult::append):

  • rendering/HitTestResult.h:
  • testing/Internals.cpp:

(WebCore::Internals::nodesFromRect const):

Location:
trunk/Source/WebCore
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r261025 r261028  
     12020-05-01  Daniel Bates  <dabates@apple.com>
     2
     3        Change HitTestResult::NodeSet from set of RefPtrs to set of Refs
     4        https://bugs.webkit.org/show_bug.cgi?id=211306
     5
     6        Reviewed by Simon Fraser.
     7
     8        HitTestResult::listBasedTestResult() never returns a set with nullptrs in it.
     9        So, change the set declaration from ListHashSet<RefPtr<Node>> to ListHashSet<Ref<Node>>.
     10        This way people are not tempted to unnecessarily null check the nodes in the set.
     11
     12        As I made this change to TreeScope::elementsFromPoint() I noticed that retargetToScope(),
     13        which is called by it, returned a Node&. So, I changed it to return a Ref<Node>. That
     14        required me to fix up caretRangeFromPoint(), which lead me to fix up nodeFromPoint() as well.
     15
     16        * dom/Document.cpp:
     17        (WebCore::Document::caretRangeFromPoint):
     18        * dom/EventPath.cpp:
     19        (WebCore::RelatedNodeRetargeter::checkConsistency):
     20        * dom/TreeScope.cpp:
     21        (WebCore::TreeScope::retargetToScope const):
     22        (WebCore::TreeScope::nodeFromPoint):
     23        (WebCore::TreeScope::elementFromPoint):
     24        (WebCore::TreeScope::elementsFromPoint):
     25        * dom/TreeScope.h:
     26        * page/Page.cpp:
     27        (WebCore::Page::editableElementsInRect const):
     28        * rendering/HitTestResult.cpp:
     29        (WebCore::appendToNodeSet):
     30        (WebCore::HitTestResult::HitTestResult):
     31        (WebCore::HitTestResult::operator=):
     32        (WebCore::HitTestResult::addNodeToListBasedTestResultCommon):
     33        (WebCore::HitTestResult::append):
     34        * rendering/HitTestResult.h:
     35        * testing/Internals.cpp:
     36        (WebCore::Internals::nodesFromRect const):
     37
    1382020-05-01  Chris Dumez  <cdumez@apple.com>
    239
  • trunk/Source/WebCore/dom/Document.cpp

    r261013 r261028  
    15131513
    15141514    LayoutPoint localPoint;
    1515     Node* node = nodeFromPoint(clientPoint, &localPoint);
     1515    auto node = nodeFromPoint(clientPoint, &localPoint);
    15161516    if (!node)
    15171517        return nullptr;
    15181518
    1519     RenderObject* renderer = node->renderer();
     1519    auto* renderer = node->renderer();
    15201520    if (!renderer)
    15211521        return nullptr;
    1522     Position rangeCompliantPosition = renderer->positionForPoint(localPoint).parentAnchoredEquivalent();
     1522    auto rangeCompliantPosition = renderer->positionForPoint(localPoint).parentAnchoredEquivalent();
    15231523    if (rangeCompliantPosition.isNull())
    15241524        return nullptr;
    15251525
    1526     unsigned offset = rangeCompliantPosition.offsetInContainerNode();
    1527     node = &retargetToScope(*rangeCompliantPosition.containerNode());
     1526    auto offset = rangeCompliantPosition.offsetInContainerNode();
     1527    node = retargetToScope(*rangeCompliantPosition.containerNode());
    15281528    if (node != rangeCompliantPosition.containerNode())
    15291529        offset = 0;
    15301530
    1531     return Range::create(*this, node, offset, node, offset);
     1531    return Range::create(*this, node.get(), offset, node.get(), offset);
    15321532}
    15331533
  • trunk/Source/WebCore/dom/EventPath.cpp

    r254087 r261028  
    452452        return;
    453453    ASSERT(!currentTarget.isClosedShadowHidden(*m_retargetedRelatedNode));
    454     ASSERT(m_retargetedRelatedNode == &currentTarget.treeScope().retargetToScope(m_relatedNode));
     454    ASSERT(m_retargetedRelatedNode == currentTarget.treeScope().retargetToScope(m_relatedNode).ptr());
    455455}
    456456
  • trunk/Source/WebCore/dom/TreeScope.cpp

    r260707 r261028  
    181181
    182182
    183 Node& TreeScope::retargetToScope(Node& node) const
     183Ref<Node> TreeScope::retargetToScope(Node& node) const
    184184{
    185185    auto& scope = node.treeScope();
     
    197197        ancestorScopes.append(currentScope);
    198198
    199     size_t i = nodeTreeScopes.size();
    200     size_t j = ancestorScopes.size();
     199    auto i = nodeTreeScopes.size();
     200    auto j = ancestorScopes.size();
    201201    while (i > 0 && j > 0 && nodeTreeScopes[i - 1] == ancestorScopes[j - 1]) {
    202202        --i;
     
    208208        return node;
    209209
    210     ShadowRoot& shadowRootInLowestCommonTreeScope = downcast<ShadowRoot>(nodeTreeScopes[i - 1]->rootNode());
     210    auto& shadowRootInLowestCommonTreeScope = downcast<ShadowRoot>(nodeTreeScopes[i - 1]->rootNode());
    211211    return *shadowRootInLowestCommonTreeScope.host();
    212212}
     
    350350}
    351351
    352 Node* TreeScope::nodeFromPoint(const LayoutPoint& clientPoint, LayoutPoint* localPoint)
     352RefPtr<Node> TreeScope::nodeFromPoint(const LayoutPoint& clientPoint, LayoutPoint* localPoint)
    353353{
    354354    auto absolutePoint = absolutePointIfNotClipped(documentScope(), clientPoint);
     
    369369        return nullptr;
    370370
    371     Node* node = nodeFromPoint(LayoutPoint(clientX, clientY), nullptr);
     371    auto node = nodeFromPoint(LayoutPoint { clientX, clientY }, nullptr);
    372372    if (!node)
    373373        return nullptr;
    374374
    375     node = &retargetToScope(*node);
     375    node = retargetToScope(*node);
    376376    while (!is<Element>(*node)) {
    377377        node = node->parentInComposedTree();
    378378        if (!node)
    379379            break;
    380         node = &retargetToScope(*node);
    381     }
    382 
    383     return downcast<Element>(node);
     380        node = retargetToScope(*node);
     381    }
     382
     383    return static_pointer_cast<Element>(node);
    384384}
    385385
     
    400400    documentScope().hitTest(hitType, result);
    401401
    402     Node* lastNode = nullptr;
    403     for (const auto& listBasedNode : result.listBasedTestResult()) {
    404         Node* node = listBasedNode.get();
    405         node = &retargetToScope(*node);
    406         while (!is<Element>(*node)) {
     402    RefPtr<Node> lastNode;
     403    auto& nodeSet = result.listBasedTestResult();
     404    elements.reserveInitialCapacity(nodeSet.size());
     405    for (auto& listBasedNode : nodeSet) {
     406        RefPtr<Node> node = retargetToScope(listBasedNode);
     407        while (!is<Element>(node)) {
    407408            node = node->parentInComposedTree();
    408409            if (!node)
    409410                break;
    410             node = &retargetToScope(*node);
     411            node = retargetToScope(*node);
    411412        }
    412413
     
    422423            continue;
    423424
    424         elements.append(downcast<Element>(node));
     425        elements.append(static_pointer_cast<Element>(node));
    425426        lastNode = node;
    426427    }
  • trunk/Source/WebCore/dom/TreeScope.h

    r250708 r261028  
    7777
    7878    // https://dom.spec.whatwg.org/#retarget
    79     Node& retargetToScope(Node&) const;
     79    Ref<Node> retargetToScope(Node&) const;
    8080
    8181    WEBCORE_EXPORT Node* ancestorNodeInThisScope(Node*) const;
     
    124124    }
    125125
    126     Node* nodeFromPoint(const LayoutPoint& clientPoint, LayoutPoint* localPoint);
     126    RefPtr<Node> nodeFromPoint(const LayoutPoint& clientPoint, LayoutPoint* localPoint);
    127127
    128128private:
  • trunk/Source/WebCore/page/Page.cpp

    r261025 r261028  
    943943    result.reserveInitialCapacity(nodeSet.size());
    944944    for (auto& node : nodeSet) {
    945         if (is<Element>(node) && isEditableTextInputElement(downcast<Element>(*node))) {
    946             ASSERT(searchRectInRootViewCoordinates.intersects(downcast<Element>(*node).clientRect()));
    947             result.uncheckedAppend(downcast<Element>(*node));
     945        if (is<Element>(node) && isEditableTextInputElement(downcast<Element>(node.get()))) {
     946            ASSERT(searchRectInRootViewCoordinates.intersects(downcast<Element>(node.get()).clientRect()));
     947            result.uncheckedAppend(static_reference_cast<Element>(node));
    948948        }
    949949    }
  • trunk/Source/WebCore/rendering/HitTestResult.cpp

    r260725 r261028  
    5555using namespace HTMLNames;
    5656
     57static inline void appendToNodeSet(const HitTestResult::NodeSet& source, HitTestResult::NodeSet& destination)
     58{
     59    for (auto& node : source)
     60        destination.add(node.copyRef());
     61}
     62
    5763HitTestResult::HitTestResult() = default;
    5864
     
    9298{
    9399    // Only copy the NodeSet in case of list hit test.
    94     m_listBasedTestResult = other.m_listBasedTestResult ? makeUnique<NodeSet>(*other.m_listBasedTestResult) : nullptr;
     100    if (other.m_listBasedTestResult) {
     101        m_listBasedTestResult = makeUnique<NodeSet>();
     102        appendToNodeSet(*other.m_listBasedTestResult, *m_listBasedTestResult);
     103    }
    95104}
    96105
     
    109118
    110119    // Only copy the NodeSet in case of list hit test.
    111     m_listBasedTestResult = other.m_listBasedTestResult ? makeUnique<NodeSet>(*other.m_listBasedTestResult) : nullptr;
     120    if (other.m_listBasedTestResult) {
     121        m_listBasedTestResult = makeUnique<NodeSet>();
     122        appendToNodeSet(*other.m_listBasedTestResult, *m_listBasedTestResult);
     123    }
    112124
    113125    return *this;
     
    635647        node = node->document().ancestorNodeInThisScope(node);
    636648
    637     mutableListBasedTestResult().add(node);
     649    mutableListBasedTestResult().add(*node);
    638650
    639651    if (request.includesAllElementsUnderPoint())
     
    668680    }
    669681
    670     if (other.m_listBasedTestResult) {
    671         NodeSet& set = mutableListBasedTestResult();
    672         for (const auto& node : *other.m_listBasedTestResult)
    673             set.add(node.get());
    674     }
     682    if (other.m_listBasedTestResult)
     683        appendToNodeSet(*other.m_listBasedTestResult, mutableListBasedTestResult());
    675684}
    676685
  • trunk/Source/WebCore/rendering/HitTestResult.h

    r258468 r261028  
    4141    WTF_MAKE_FAST_ALLOCATED;
    4242public:
    43     using NodeSet = ListHashSet<RefPtr<Node>>;
     43    using NodeSet = ListHashSet<Ref<Node>>;
    4444
    4545    WEBCORE_EXPORT HitTestResult();
  • trunk/Source/WebCore/testing/Internals.cpp

    r260848 r261028  
    22282228    HitTestResult result(point, topPadding, rightPadding, bottomPadding, leftPadding);
    22292229    document.hitTest(request, result);
    2230     const HitTestResult::NodeSet& nodeSet = result.listBasedTestResult();
    2231     Vector<Ref<Node>> matches;
    2232     matches.reserveInitialCapacity(nodeSet.size());
    2233     for (auto& node : nodeSet)
    2234         matches.uncheckedAppend(*node);
    2235 
     2230    auto matches = WTF::map(result.listBasedTestResult(), [](const auto& node) { return node.copyRef(); });
    22362231    return RefPtr<NodeList> { StaticNodeList::create(WTFMove(matches)) };
    22372232}
Note: See TracChangeset for help on using the changeset viewer.