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;
};