Title: [155287] trunk/Source/WebCore
Revision
155287
Author
[email protected]
Date
2013-09-08 00:02:42 -0700 (Sun, 08 Sep 2013)

Log 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:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (155286 => 155287)


--- trunk/Source/WebCore/ChangeLog	2013-09-08 06:11:31 UTC (rev 155286)
+++ trunk/Source/WebCore/ChangeLog	2013-09-08 07:02:42 UTC (rev 155287)
@@ -1,3 +1,31 @@
+2013-09-07  Antti Koivisto  <[email protected]>
+
+        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:
+
 2013-09-07  Andreas Kling  <[email protected]>
 
         Beat FrameView with the FINAL stick.

Modified: trunk/Source/WebCore/dom/ComposedShadowTreeWalker.cpp (155286 => 155287)


--- trunk/Source/WebCore/dom/ComposedShadowTreeWalker.cpp	2013-09-08 06:11:31 UTC (rev 155286)
+++ trunk/Source/WebCore/dom/ComposedShadowTreeWalker.cpp	2013-09-08 07:02:42 UTC (rev 155287)
@@ -1,6 +1,7 @@
 
 /*
  * Copyright (C) 2012 Google Inc. All rights reserved.
+ * Copyright (C) 2013 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -52,125 +53,121 @@
     return false;
 }
 
-void ComposedShadowTreeWalker::firstChild()
-{
-    assertPrecondition();
-    m_node = traverseChild(m_node, TraversalDirectionForward);
-    assertPostcondition();
-}
+static Node* findFirstSiblingEnteringInsertionPoints(const Node*);
+static Node* findFirstEnteringInsertionPoints(const Node*);
+static Node* findFirstFromDistributedNode(const Node*, const InsertionPoint*);
+static Node* findLastSiblingEnteringInsertionPoints(const Node*);
+static Node* findLastEnteringInsertionPoints(const Node*);
+static Node* findLastFromDistributedNode(const Node*, const InsertionPoint*);
 
-Node* ComposedShadowTreeWalker::traverseFirstChild(const Node* node) const
+static Node* findFirstSiblingEnteringInsertionPoints(const Node* node)
 {
-    ASSERT(node);
-    return traverseChild(node, TraversalDirectionForward);
+    for (const Node* sibling = node; sibling; sibling = sibling->nextSibling()) {
+        if (Node* found = findFirstEnteringInsertionPoints(sibling))
+            return found;
+    }
+    return nullptr;
 }
 
-void ComposedShadowTreeWalker::lastChild()
+static Node* findFirstEnteringInsertionPoints(const Node* node)
 {
-    assertPrecondition();
-    m_node = traverseLastChild(m_node);
-    assertPostcondition();
-}
-
-Node* ComposedShadowTreeWalker::traverseLastChild(const Node* node) const
-{
     ASSERT(node);
-    return traverseChild(node, TraversalDirectionBackward);
+    if (!isActiveInsertionPoint(node))
+        return const_cast<Node*>(node);
+    const InsertionPoint* insertionPoint = toInsertionPoint(node);
+    if (Node* found = findFirstFromDistributedNode(insertionPoint->firstDistributed(), insertionPoint))
+        return found;
+    return findFirstSiblingEnteringInsertionPoints(node->firstChild());
 }
 
-Node* ComposedShadowTreeWalker::traverseChild(const Node* node, TraversalDirection direction) const
+static Node* findFirstFromDistributedNode(const Node* node, const InsertionPoint* insertionPoint)
 {
-    ASSERT(node);
-    if (canCrossUpperBoundary()) {
-        return node->shadowRoot() ? traverseLightChildren(node->shadowRoot(), direction)
-            : traverseLightChildren(node, direction);
+    for (const Node* next = node; next; next = insertionPoint->nextDistributedTo(next)) {
+        if (Node* found = findFirstEnteringInsertionPoints(next))
+            return found;
     }
-    if (isShadowHost(node))
-        return 0;
-    return traverseLightChildren(node, direction);
+    return nullptr;
 }
 
-Node* ComposedShadowTreeWalker::traverseLightChildren(const Node* node, TraversalDirection direction)
+static Node* findLastSiblingEnteringInsertionPoints(const Node* node)
 {
-    ASSERT(node);
-    return traverseSiblings(direction == TraversalDirectionForward ? node->firstChild() : node->lastChild(), direction);
-}
-
-Node* ComposedShadowTreeWalker::traverseSiblings(const Node* node, TraversalDirection direction)
-{
-    for (const Node* sibling = node; sibling; sibling = (direction == TraversalDirectionForward ? sibling->nextSibling() : sibling->previousSibling())) {
-        if (Node* found = traverseNode(sibling, direction))
+    for (const Node* sibling = node; sibling; sibling = sibling->previousSibling()) {
+        if (Node* found = findLastEnteringInsertionPoints(sibling))
             return found;
     }
-    return 0;
+    return nullptr;
 }
 
-Node* ComposedShadowTreeWalker::traverseNode(const Node* node, TraversalDirection direction)
+static Node* findLastEnteringInsertionPoints(const Node* node)
 {
     ASSERT(node);
     if (!isActiveInsertionPoint(node))
         return const_cast<Node*>(node);
     const InsertionPoint* insertionPoint = toInsertionPoint(node);
-    if (Node* found = traverseDistributedNodes(direction == TraversalDirectionForward ? insertionPoint->firstDistributed() : insertionPoint->lastDistributed(), insertionPoint, direction))
+    if (Node* found = findLastFromDistributedNode(insertionPoint->lastDistributed(), insertionPoint))
         return found;
-    return traverseLightChildren(node, direction);
+    return findLastSiblingEnteringInsertionPoints(node->lastChild());
 }
 
-void ComposedShadowTreeWalker::nextSibling()
+static Node* findLastFromDistributedNode(const Node* node, const InsertionPoint* insertionPoint)
 {
-    assertPrecondition();
-    m_node = traverseSiblingOrBackToInsertionPoint(m_node, TraversalDirectionForward);
-    assertPostcondition();
+    for (const Node* next = node; next; next = insertionPoint->previousDistributedTo(next)) {
+        if (Node* found = findLastEnteringInsertionPoints(next))
+            return found;
+    }
+    return nullptr;
 }
 
-void ComposedShadowTreeWalker::previousSibling()
+void ComposedShadowTreeWalker::firstChild()
 {
     assertPrecondition();
-    m_node = traverseSiblingOrBackToInsertionPoint(m_node, TraversalDirectionBackward);
+    m_node = traverseFirstChild(m_node);
     assertPostcondition();
 }
 
-Node* ComposedShadowTreeWalker::traverseDistributedNodes(const Node* node, const InsertionPoint* insertionPoint, TraversalDirection direction)
+Node* ComposedShadowTreeWalker::traverseFirstChild(const Node* node) const
 {
-    for (const Node* next = node; next; next = (direction == TraversalDirectionForward ? insertionPoint->nextDistributedTo(next) : insertionPoint->previousDistributedTo(next))) {
-        if (Node* found = traverseNode(next, direction))
-            return found;
+    ASSERT(node);
+    if (node->shadowRoot()) {
+        if (!canCrossUpperBoundary())
+            return nullptr;
+        node = node->shadowRoot();
     }
-    return 0;
+    return findFirstSiblingEnteringInsertionPoints(node->firstChild());
 }
 
-Node* ComposedShadowTreeWalker::traverseSiblingOrBackToInsertionPoint(const Node* node, TraversalDirection direction)
+void ComposedShadowTreeWalker::lastChild()
 {
-    ASSERT(node);
-
-    if (!nodeCanBeDistributed(node))
-        return traverseSiblingInCurrentTree(node, direction);
-
-    InsertionPoint* insertionPoint = findInsertionPointOf(node);
-    if (!insertionPoint)
-        return traverseSiblingInCurrentTree(node, direction);
-
-    if (Node* found = traverseDistributedNodes(direction == TraversalDirectionForward ? insertionPoint->nextDistributedTo(node) : insertionPoint->previousDistributedTo(node), insertionPoint, direction))
-        return found;
-    return traverseSiblingOrBackToInsertionPoint(insertionPoint, direction);
+    assertPrecondition();
+    m_node = traverseLastChild(m_node);
+    assertPostcondition();
 }
 
-Node* ComposedShadowTreeWalker::traverseSiblingInCurrentTree(const Node* node, TraversalDirection direction)
+Node* ComposedShadowTreeWalker::traverseLastChild(const Node* node) const
 {
     ASSERT(node);
-    if (Node* found = traverseSiblings(direction == TraversalDirectionForward ? node->nextSibling() : node->previousSibling(), direction))
-        return found;
-    return escapeFallbackContentElement(node, direction);
+    if (node->shadowRoot()) {
+        if (!canCrossUpperBoundary())
+            return nullptr;
+        node = node->shadowRoot();
+    }
+    return findLastSiblingEnteringInsertionPoints(node->lastChild());
 }
 
-inline Node* ComposedShadowTreeWalker::escapeFallbackContentElement(const Node* node, TraversalDirection direction)
+void ComposedShadowTreeWalker::nextSibling()
 {
-    ASSERT(node);
-    if (node->parentNode() && isActiveInsertionPoint(node->parentNode()))
-        return traverseSiblingOrBackToInsertionPoint(node->parentNode(), direction);
-    return 0;
+    assertPrecondition();
+    m_node = traverseNextSibling(m_node);
+    assertPostcondition();
 }
 
+void ComposedShadowTreeWalker::previousSibling()
+{
+    assertPrecondition();
+    m_node = traversePreviousSibling(m_node);
+    assertPostcondition();
+}
+
 void ComposedShadowTreeWalker::parent()
 {
     assertPrecondition();
@@ -211,13 +208,45 @@
 Node* ComposedShadowTreeWalker::traverseNextSibling(const Node* node)
 {
     ASSERT(node);
-    return traverseSiblingOrBackToInsertionPoint(node, TraversalDirectionForward);
+
+    InsertionPoint* insertionPoint;
+    if (nodeCanBeDistributed(node) && (insertionPoint = findInsertionPointOf(node))) {
+        Node* found = findFirstFromDistributedNode(insertionPoint->nextDistributedTo(node), insertionPoint);
+        if (found)
+            return found;
+        return traverseNextSibling(insertionPoint);
+    }
+
+    for (const Node* sibling = node->nextSibling(); sibling; sibling = sibling->nextSibling()) {
+        if (Node* found = findFirstEnteringInsertionPoints(sibling))
+            return found;
+    }
+    if (node->parentNode() && isActiveInsertionPoint(node->parentNode()))
+        return traverseNextSibling(node->parentNode());
+
+    return nullptr;
 }
 
 Node* ComposedShadowTreeWalker::traversePreviousSibling(const Node* node)
 {
     ASSERT(node);
-    return traverseSiblingOrBackToInsertionPoint(node, TraversalDirectionBackward);
+
+    InsertionPoint* insertionPoint;
+    if (nodeCanBeDistributed(node) && (insertionPoint = findInsertionPointOf(node))) {
+        Node* found = findLastFromDistributedNode(insertionPoint->previousDistributedTo(node), insertionPoint);
+        if (found)
+            return found;
+        return traversePreviousSibling(insertionPoint);
+    }
+
+    for (const Node* sibling = node->previousSibling(); sibling; sibling = sibling->previousSibling()) {
+        if (Node* found = findLastEnteringInsertionPoints(sibling))
+            return found;
+    }
+    if (node->parentNode() && isActiveInsertionPoint(node->parentNode()))
+        return traversePreviousSibling(node->parentNode());
+
+    return nullptr;
 }
 
 void ComposedShadowTreeWalker::next()

Modified: trunk/Source/WebCore/dom/ComposedShadowTreeWalker.h (155286 => 155287)


--- trunk/Source/WebCore/dom/ComposedShadowTreeWalker.h	2013-09-08 06:11:31 UTC (rev 155286)
+++ trunk/Source/WebCore/dom/ComposedShadowTreeWalker.h	2013-09-08 07:02:42 UTC (rev 155287)
@@ -68,11 +68,6 @@
     Node* traverseParent(const Node*) const;
 
 private:
-    enum TraversalDirection {
-        TraversalDirectionForward,
-        TraversalDirectionBackward
-    };
-
     bool canCrossUpperBoundary() const { return m_policy == CrossUpperBoundary; }
 
     void assertPrecondition() const
@@ -93,24 +88,12 @@
 #endif
     }
 
-    static Node* traverseNode(const Node*, TraversalDirection);
-    static Node* traverseLightChildren(const Node*, TraversalDirection);
-
     Node* traverseFirstChild(const Node*) const;
     Node* traverseLastChild(const Node*) const;
-    Node* traverseChild(const Node*, TraversalDirection) const;
 
     static Node* traverseNextSibling(const Node*);
     static Node* traversePreviousSibling(const Node*);
 
-    static Node* traverseSiblingOrBackToInsertionPoint(const Node*, TraversalDirection);
-    static Node* traverseSiblingInCurrentTree(const Node*, TraversalDirection);
-
-    static Node* traverseSiblings(const Node*, TraversalDirection);
-    static Node* traverseDistributedNodes(const Node*, const InsertionPoint*, TraversalDirection);
-
-    static Node* escapeFallbackContentElement(const Node*, TraversalDirection);
-
     const Node* m_node;
     Policy m_policy;
 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to