Changeset 155287 in webkit


Ignore:
Timestamp:
Sep 8, 2013 12:02:42 AM (11 years ago)
Author:
Antti Koivisto
Message:

Separate forward and backward paths in ComposedShadowTreeWalker
https://bugs.webkit.org/show_bug.cgi?id=120979

Reviewed by Andreas Kling.

Have separate first/last and next/previous paths instead of using a direction enum.

Reduce the number of helper functions and give them more understandable names.

  • dom/ComposedShadowTreeWalker.cpp:

(WebCore::findFirstSiblingEnteringInsertionPoints):
(WebCore::findFirstEnteringInsertionPoints):
(WebCore::findFirstFromDistributedNode):
(WebCore::findLastSiblingEnteringInsertionPoints):
(WebCore::findLastEnteringInsertionPoints):
(WebCore::findLastFromDistributedNode):
(WebCore::ComposedShadowTreeWalker::firstChild):
(WebCore::ComposedShadowTreeWalker::traverseFirstChild):
(WebCore::ComposedShadowTreeWalker::lastChild):
(WebCore::ComposedShadowTreeWalker::traverseLastChild):
(WebCore::ComposedShadowTreeWalker::nextSibling):
(WebCore::ComposedShadowTreeWalker::previousSibling):
(WebCore::ComposedShadowTreeWalker::traverseNextSibling):
(WebCore::ComposedShadowTreeWalker::traversePreviousSibling):

  • dom/ComposedShadowTreeWalker.h:
Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r155283 r155287  
     12013-09-07  Antti Koivisto  <antti@apple.com>
     2
     3        Separate forward and backward paths in ComposedShadowTreeWalker
     4        https://bugs.webkit.org/show_bug.cgi?id=120979
     5
     6        Reviewed by Andreas Kling.
     7
     8        Have separate first/last and next/previous paths instead of using a direction enum.
     9       
     10        Reduce the number of helper functions and give them more understandable names.
     11
     12        * dom/ComposedShadowTreeWalker.cpp:
     13        (WebCore::findFirstSiblingEnteringInsertionPoints):
     14        (WebCore::findFirstEnteringInsertionPoints):
     15        (WebCore::findFirstFromDistributedNode):
     16        (WebCore::findLastSiblingEnteringInsertionPoints):
     17        (WebCore::findLastEnteringInsertionPoints):
     18        (WebCore::findLastFromDistributedNode):
     19        (WebCore::ComposedShadowTreeWalker::firstChild):
     20        (WebCore::ComposedShadowTreeWalker::traverseFirstChild):
     21        (WebCore::ComposedShadowTreeWalker::lastChild):
     22        (WebCore::ComposedShadowTreeWalker::traverseLastChild):
     23        (WebCore::ComposedShadowTreeWalker::nextSibling):
     24        (WebCore::ComposedShadowTreeWalker::previousSibling):
     25        (WebCore::ComposedShadowTreeWalker::traverseNextSibling):
     26        (WebCore::ComposedShadowTreeWalker::traversePreviousSibling):
     27        * dom/ComposedShadowTreeWalker.h:
     28
    1292013-09-07  Andreas Kling  <akling@apple.com>
    230
  • trunk/Source/WebCore/dom/ComposedShadowTreeWalker.cpp

    r155264 r155287  
    22/*
    33 * Copyright (C) 2012 Google Inc. All rights reserved.
     4 * Copyright (C) 2013 Apple Inc. All rights reserved.
    45 *
    56 * Redistribution and use in source and binary forms, with or without
     
    5354}
    5455
    55 void ComposedShadowTreeWalker::firstChild()
    56 {
    57     assertPrecondition();
    58     m_node = traverseChild(m_node, TraversalDirectionForward);
    59     assertPostcondition();
    60 }
    61 
    62 Node* ComposedShadowTreeWalker::traverseFirstChild(const Node* node) const
    63 {
    64     ASSERT(node);
    65     return traverseChild(node, TraversalDirectionForward);
    66 }
    67 
    68 void ComposedShadowTreeWalker::lastChild()
    69 {
    70     assertPrecondition();
    71     m_node = traverseLastChild(m_node);
    72     assertPostcondition();
    73 }
    74 
    75 Node* ComposedShadowTreeWalker::traverseLastChild(const Node* node) const
    76 {
    77     ASSERT(node);
    78     return traverseChild(node, TraversalDirectionBackward);
    79 }
    80 
    81 Node* ComposedShadowTreeWalker::traverseChild(const Node* node, TraversalDirection direction) const
    82 {
    83     ASSERT(node);
    84     if (canCrossUpperBoundary()) {
    85         return node->shadowRoot() ? traverseLightChildren(node->shadowRoot(), direction)
    86             : traverseLightChildren(node, direction);
    87     }
    88     if (isShadowHost(node))
    89         return 0;
    90     return traverseLightChildren(node, direction);
    91 }
    92 
    93 Node* ComposedShadowTreeWalker::traverseLightChildren(const Node* node, TraversalDirection direction)
    94 {
    95     ASSERT(node);
    96     return traverseSiblings(direction == TraversalDirectionForward ? node->firstChild() : node->lastChild(), direction);
    97 }
    98 
    99 Node* ComposedShadowTreeWalker::traverseSiblings(const Node* node, TraversalDirection direction)
    100 {
    101     for (const Node* sibling = node; sibling; sibling = (direction == TraversalDirectionForward ? sibling->nextSibling() : sibling->previousSibling())) {
    102         if (Node* found = traverseNode(sibling, direction))
    103             return found;
    104     }
    105     return 0;
    106 }
    107 
    108 Node* ComposedShadowTreeWalker::traverseNode(const Node* node, TraversalDirection direction)
     56static Node* findFirstSiblingEnteringInsertionPoints(const Node*);
     57static Node* findFirstEnteringInsertionPoints(const Node*);
     58static Node* findFirstFromDistributedNode(const Node*, const InsertionPoint*);
     59static Node* findLastSiblingEnteringInsertionPoints(const Node*);
     60static Node* findLastEnteringInsertionPoints(const Node*);
     61static Node* findLastFromDistributedNode(const Node*, const InsertionPoint*);
     62
     63static Node* findFirstSiblingEnteringInsertionPoints(const Node* node)
     64{
     65    for (const Node* sibling = node; sibling; sibling = sibling->nextSibling()) {
     66        if (Node* found = findFirstEnteringInsertionPoints(sibling))
     67            return found;
     68    }
     69    return nullptr;
     70}
     71
     72static Node* findFirstEnteringInsertionPoints(const Node* node)
    10973{
    11074    ASSERT(node);
     
    11276        return const_cast<Node*>(node);
    11377    const InsertionPoint* insertionPoint = toInsertionPoint(node);
    114     if (Node* found = traverseDistributedNodes(direction == TraversalDirectionForward ? insertionPoint->firstDistributed() : insertionPoint->lastDistributed(), insertionPoint, direction))
     78    if (Node* found = findFirstFromDistributedNode(insertionPoint->firstDistributed(), insertionPoint))
    11579        return found;
    116     return traverseLightChildren(node, direction);
     80    return findFirstSiblingEnteringInsertionPoints(node->firstChild());
     81}
     82
     83static Node* findFirstFromDistributedNode(const Node* node, const InsertionPoint* insertionPoint)
     84{
     85    for (const Node* next = node; next; next = insertionPoint->nextDistributedTo(next)) {
     86        if (Node* found = findFirstEnteringInsertionPoints(next))
     87            return found;
     88    }
     89    return nullptr;
     90}
     91
     92static Node* findLastSiblingEnteringInsertionPoints(const Node* node)
     93{
     94    for (const Node* sibling = node; sibling; sibling = sibling->previousSibling()) {
     95        if (Node* found = findLastEnteringInsertionPoints(sibling))
     96            return found;
     97    }
     98    return nullptr;
     99}
     100
     101static Node* findLastEnteringInsertionPoints(const Node* node)
     102{
     103    ASSERT(node);
     104    if (!isActiveInsertionPoint(node))
     105        return const_cast<Node*>(node);
     106    const InsertionPoint* insertionPoint = toInsertionPoint(node);
     107    if (Node* found = findLastFromDistributedNode(insertionPoint->lastDistributed(), insertionPoint))
     108        return found;
     109    return findLastSiblingEnteringInsertionPoints(node->lastChild());
     110}
     111
     112static Node* findLastFromDistributedNode(const Node* node, const InsertionPoint* insertionPoint)
     113{
     114    for (const Node* next = node; next; next = insertionPoint->previousDistributedTo(next)) {
     115        if (Node* found = findLastEnteringInsertionPoints(next))
     116            return found;
     117    }
     118    return nullptr;
     119}
     120
     121void ComposedShadowTreeWalker::firstChild()
     122{
     123    assertPrecondition();
     124    m_node = traverseFirstChild(m_node);
     125    assertPostcondition();
     126}
     127
     128Node* ComposedShadowTreeWalker::traverseFirstChild(const Node* node) const
     129{
     130    ASSERT(node);
     131    if (node->shadowRoot()) {
     132        if (!canCrossUpperBoundary())
     133            return nullptr;
     134        node = node->shadowRoot();
     135    }
     136    return findFirstSiblingEnteringInsertionPoints(node->firstChild());
     137}
     138
     139void ComposedShadowTreeWalker::lastChild()
     140{
     141    assertPrecondition();
     142    m_node = traverseLastChild(m_node);
     143    assertPostcondition();
     144}
     145
     146Node* ComposedShadowTreeWalker::traverseLastChild(const Node* node) const
     147{
     148    ASSERT(node);
     149    if (node->shadowRoot()) {
     150        if (!canCrossUpperBoundary())
     151            return nullptr;
     152        node = node->shadowRoot();
     153    }
     154    return findLastSiblingEnteringInsertionPoints(node->lastChild());
    117155}
    118156
     
    120158{
    121159    assertPrecondition();
    122     m_node = traverseSiblingOrBackToInsertionPoint(m_node, TraversalDirectionForward);
     160    m_node = traverseNextSibling(m_node);
    123161    assertPostcondition();
    124162}
     
    127165{
    128166    assertPrecondition();
    129     m_node = traverseSiblingOrBackToInsertionPoint(m_node, TraversalDirectionBackward);
    130     assertPostcondition();
    131 }
    132 
    133 Node* ComposedShadowTreeWalker::traverseDistributedNodes(const Node* node, const InsertionPoint* insertionPoint, TraversalDirection direction)
    134 {
    135     for (const Node* next = node; next; next = (direction == TraversalDirectionForward ? insertionPoint->nextDistributedTo(next) : insertionPoint->previousDistributedTo(next))) {
    136         if (Node* found = traverseNode(next, direction))
    137             return found;
    138     }
    139     return 0;
    140 }
    141 
    142 Node* ComposedShadowTreeWalker::traverseSiblingOrBackToInsertionPoint(const Node* node, TraversalDirection direction)
    143 {
    144     ASSERT(node);
    145 
    146     if (!nodeCanBeDistributed(node))
    147         return traverseSiblingInCurrentTree(node, direction);
    148 
    149     InsertionPoint* insertionPoint = findInsertionPointOf(node);
    150     if (!insertionPoint)
    151         return traverseSiblingInCurrentTree(node, direction);
    152 
    153     if (Node* found = traverseDistributedNodes(direction == TraversalDirectionForward ? insertionPoint->nextDistributedTo(node) : insertionPoint->previousDistributedTo(node), insertionPoint, direction))
    154         return found;
    155     return traverseSiblingOrBackToInsertionPoint(insertionPoint, direction);
    156 }
    157 
    158 Node* ComposedShadowTreeWalker::traverseSiblingInCurrentTree(const Node* node, TraversalDirection direction)
    159 {
    160     ASSERT(node);
    161     if (Node* found = traverseSiblings(direction == TraversalDirectionForward ? node->nextSibling() : node->previousSibling(), direction))
    162         return found;
    163     return escapeFallbackContentElement(node, direction);
    164 }
    165 
    166 inline Node* ComposedShadowTreeWalker::escapeFallbackContentElement(const Node* node, TraversalDirection direction)
    167 {
    168     ASSERT(node);
    169     if (node->parentNode() && isActiveInsertionPoint(node->parentNode()))
    170         return traverseSiblingOrBackToInsertionPoint(node->parentNode(), direction);
    171     return 0;
     167    m_node = traversePreviousSibling(m_node);
     168    assertPostcondition();
    172169}
    173170
     
    212209{
    213210    ASSERT(node);
    214     return traverseSiblingOrBackToInsertionPoint(node, TraversalDirectionForward);
     211
     212    InsertionPoint* insertionPoint;
     213    if (nodeCanBeDistributed(node) && (insertionPoint = findInsertionPointOf(node))) {
     214        Node* found = findFirstFromDistributedNode(insertionPoint->nextDistributedTo(node), insertionPoint);
     215        if (found)
     216            return found;
     217        return traverseNextSibling(insertionPoint);
     218    }
     219
     220    for (const Node* sibling = node->nextSibling(); sibling; sibling = sibling->nextSibling()) {
     221        if (Node* found = findFirstEnteringInsertionPoints(sibling))
     222            return found;
     223    }
     224    if (node->parentNode() && isActiveInsertionPoint(node->parentNode()))
     225        return traverseNextSibling(node->parentNode());
     226
     227    return nullptr;
    215228}
    216229
     
    218231{
    219232    ASSERT(node);
    220     return traverseSiblingOrBackToInsertionPoint(node, TraversalDirectionBackward);
     233
     234    InsertionPoint* insertionPoint;
     235    if (nodeCanBeDistributed(node) && (insertionPoint = findInsertionPointOf(node))) {
     236        Node* found = findLastFromDistributedNode(insertionPoint->previousDistributedTo(node), insertionPoint);
     237        if (found)
     238            return found;
     239        return traversePreviousSibling(insertionPoint);
     240    }
     241
     242    for (const Node* sibling = node->previousSibling(); sibling; sibling = sibling->previousSibling()) {
     243        if (Node* found = findLastEnteringInsertionPoints(sibling))
     244            return found;
     245    }
     246    if (node->parentNode() && isActiveInsertionPoint(node->parentNode()))
     247        return traversePreviousSibling(node->parentNode());
     248
     249    return nullptr;
    221250}
    222251
  • trunk/Source/WebCore/dom/ComposedShadowTreeWalker.h

    r155264 r155287  
    6969
    7070private:
    71     enum TraversalDirection {
    72         TraversalDirectionForward,
    73         TraversalDirectionBackward
    74     };
    75 
    7671    bool canCrossUpperBoundary() const { return m_policy == CrossUpperBoundary; }
    7772
     
    9489    }
    9590
    96     static Node* traverseNode(const Node*, TraversalDirection);
    97     static Node* traverseLightChildren(const Node*, TraversalDirection);
    98 
    9991    Node* traverseFirstChild(const Node*) const;
    10092    Node* traverseLastChild(const Node*) const;
    101     Node* traverseChild(const Node*, TraversalDirection) const;
    10293
    10394    static Node* traverseNextSibling(const Node*);
    10495    static Node* traversePreviousSibling(const Node*);
    105 
    106     static Node* traverseSiblingOrBackToInsertionPoint(const Node*, TraversalDirection);
    107     static Node* traverseSiblingInCurrentTree(const Node*, TraversalDirection);
    108 
    109     static Node* traverseSiblings(const Node*, TraversalDirection);
    110     static Node* traverseDistributedNodes(const Node*, const InsertionPoint*, TraversalDirection);
    111 
    112     static Node* escapeFallbackContentElement(const Node*, TraversalDirection);
    11396
    11497    const Node* m_node;
Note: See TracChangeset for help on using the changeset viewer.