Use a 1-byte enum class for TextDirection
[WebKit-https.git] / Source / WebCore / dom / NodeIterator.cpp
index c51b14a..45d860b 100644 (file)
 #include "NodeIterator.h"
 
 #include "Document.h"
-#include "ExceptionCode.h"
 #include "NodeTraversal.h"
 
-#include <runtime/JSCJSValueInlines.h>
-
 namespace WebCore {
 
-NodeIterator::NodePointer::NodePointer()
-{
-}
-
-NodeIterator::NodePointer::NodePointer(PassRefPtr<Node> n, bool b)
-    : node(n)
-    , isPointerBeforeNode(b)
+inline NodeIterator::NodePointer::NodePointer(Node& node, bool isPointerBeforeNode)
+    : node(&node)
+    , isPointerBeforeNode(isPointerBeforeNode)
 {
 }
 
-void NodeIterator::NodePointer::clear()
+inline void NodeIterator::NodePointer::clear()
 {
-    node.clear();
+    node = nullptr;
 }
 
-bool NodeIterator::NodePointer::moveToNext(Node* root)
+inline bool NodeIterator::NodePointer::moveToNext(Node& root)
 {
     if (!node)
         return false;
@@ -56,11 +49,11 @@ bool NodeIterator::NodePointer::moveToNext(Node* root)
         isPointerBeforeNode = false;
         return true;
     }
-    node = NodeTraversal::next(node.get(), root);
+    node = NodeTraversal::next(*node, &root);
     return node;
 }
 
-bool NodeIterator::NodePointer::moveToPrevious(Node* root)
+inline bool NodeIterator::NodePointer::moveToPrevious(Node& root)
 {
     if (!node)
         return false;
@@ -68,34 +61,33 @@ bool NodeIterator::NodePointer::moveToPrevious(Node* root)
         isPointerBeforeNode = true;
         return true;
     }
-    if (node == root) {
+    if (node == &root) {
         node = nullptr;
         return false;
     }
-    node = NodeTraversal::previous(node.get());
+    node = NodeTraversal::previous(*node);
     return node;
 }
 
-NodeIterator::NodeIterator(PassRefPtr<Node> rootNode, unsigned whatToShow, PassRefPtr<NodeFilter> filter, bool expandEntityReferences)
-    : NodeIteratorBase(rootNode, whatToShow, filter, expandEntityReferences)
-    , m_referenceNode(root(), true)
-    , m_detached(false)
+inline NodeIterator::NodeIterator(Node& rootNode, unsigned whatToShow, RefPtr<NodeFilter>&& filter)
+    : NodeIteratorBase(rootNode, whatToShow, WTFMove(filter))
+    , m_referenceNode(rootNode, true)
 {
-    root()->document().attachNodeIterator(this);
+    root().document().attachNodeIterator(this);
 }
 
-NodeIterator::~NodeIterator()
+Ref<NodeIterator> NodeIterator::create(Node& rootNode, unsigned whatToShow, RefPtr<NodeFilter>&& filter)
 {
-    root()->document().detachNodeIterator(this);
+    return adoptRef(*new NodeIterator(rootNode, whatToShow, WTFMove(filter)));
 }
 
-PassRefPtr<Node> NodeIterator::nextNode(JSC::ExecState* state, ExceptionCode& ec)
+NodeIterator::~NodeIterator()
 {
-    if (m_detached) {
-        ec = INVALID_STATE_ERR;
-        return 0;
-    }
+    root().document().detachNodeIterator(this);
+}
 
+ExceptionOr<RefPtr<Node>> NodeIterator::nextNode()
+{
     RefPtr<Node> result;
 
     m_candidateNode = m_referenceNode;
@@ -104,27 +96,27 @@ PassRefPtr<Node> NodeIterator::nextNode(JSC::ExecState* state, ExceptionCode& ec
         // In other words, FILTER_REJECT does not pass over descendants
         // of the rejected node. Hence, FILTER_REJECT is the same as FILTER_SKIP.
         RefPtr<Node> provisionalResult = m_candidateNode.node;
-        bool nodeWasAccepted = acceptNode(state, provisionalResult.get()) == NodeFilter::FILTER_ACCEPT;
-        if (state && state->hadException())
-            break;
+
+        auto filterResult = acceptNode(*provisionalResult);
+        if (filterResult.hasException()) {
+            m_candidateNode.clear();
+            return filterResult.releaseException();
+        }
+
+        bool nodeWasAccepted = filterResult.returnValue() == NodeFilter::FILTER_ACCEPT;
         if (nodeWasAccepted) {
             m_referenceNode = m_candidateNode;
-            result = provisionalResult.release();
+            result = WTFMove(provisionalResult);
             break;
         }
     }
 
     m_candidateNode.clear();
-    return result.release();
+    return WTFMove(result);
 }
 
-PassRefPtr<Node> NodeIterator::previousNode(JSC::ExecState* state, ExceptionCode& ec)
+ExceptionOr<RefPtr<Node>> NodeIterator::previousNode()
 {
-    if (m_detached) {
-        ec = INVALID_STATE_ERR;
-        return 0;
-    }
-
     RefPtr<Node> result;
 
     m_candidateNode = m_referenceNode;
@@ -133,55 +125,51 @@ PassRefPtr<Node> NodeIterator::previousNode(JSC::ExecState* state, ExceptionCode
         // In other words, FILTER_REJECT does not pass over descendants
         // of the rejected node. Hence, FILTER_REJECT is the same as FILTER_SKIP.
         RefPtr<Node> provisionalResult = m_candidateNode.node;
-        bool nodeWasAccepted = acceptNode(state, provisionalResult.get()) == NodeFilter::FILTER_ACCEPT;
-        if (state && state->hadException())
-            break;
+
+        auto filterResult = acceptNode(*provisionalResult);
+        if (filterResult.hasException()) {
+            m_candidateNode.clear();
+            return filterResult.releaseException();
+        }
+
+        bool nodeWasAccepted = filterResult.returnValue() == NodeFilter::FILTER_ACCEPT;
         if (nodeWasAccepted) {
             m_referenceNode = m_candidateNode;
-            result = provisionalResult.release();
+            result = WTFMove(provisionalResult);
             break;
         }
     }
 
     m_candidateNode.clear();
-    return result.release();
+    return WTFMove(result);
 }
 
-void NodeIterator::detach()
-{
-    root()->document().detachNodeIterator(this);
-    m_detached = true;
-    m_referenceNode.node.clear();
-}
-
-void NodeIterator::nodeWillBeRemoved(Node* removedNode)
+void NodeIterator::nodeWillBeRemoved(Node& removedNode)
 {
     updateForNodeRemoval(removedNode, m_candidateNode);
     updateForNodeRemoval(removedNode, m_referenceNode);
 }
 
-void NodeIterator::updateForNodeRemoval(Node* removedNode, NodePointer& referenceNode) const
+void NodeIterator::updateForNodeRemoval(Node& removedNode, NodePointer& referenceNode) const
 {
-    ASSERT(!m_detached);
-    ASSERT(removedNode);
-    ASSERT(&root()->document() == &removedNode->document());
+    ASSERT(&root().document() == &removedNode.document());
 
     // Iterator is not affected if the removed node is the reference node and is the root.
     // or if removed node is not the reference node, or the ancestor of the reference node.
-    if (!removedNode->isDescendantOf(root()))
+    if (!removedNode.isDescendantOf(root()))
         return;
-    bool willRemoveReferenceNode = removedNode == referenceNode.node;
+    bool willRemoveReferenceNode = &removedNode == referenceNode.node;
     bool willRemoveReferenceNodeAncestor = referenceNode.node && referenceNode.node->isDescendantOf(removedNode);
     if (!willRemoveReferenceNode && !willRemoveReferenceNodeAncestor)
         return;
 
     if (referenceNode.isPointerBeforeNode) {
-        Node* node = NodeTraversal::next(removedNode, root());
+        Node* node = NodeTraversal::next(removedNode, &root());
         if (node) {
             // Move out from under the node being removed if the new reference
             // node is a descendant of the node being removed.
             while (node && node->isDescendantOf(removedNode))
-                node = NodeTraversal::next(node, root());
+                node = NodeTraversal::next(*node, &root());
             if (node)
                 referenceNode.node = node;
         } else {
@@ -190,8 +178,8 @@ void NodeIterator::updateForNodeRemoval(Node* removedNode, NodePointer& referenc
                 // Move out from under the node being removed if the reference node is
                 // a descendant of the node being removed.
                 if (willRemoveReferenceNodeAncestor) {
-                    while (node && node->isDescendantOf(removedNode))
-                        node = NodeTraversal::previous(node);
+                    while (node && node->isDescendantOf(&removedNode))
+                        node = NodeTraversal::previous(*node);
                 }
                 if (node) {
                     // Removing last node.
@@ -209,18 +197,18 @@ void NodeIterator::updateForNodeRemoval(Node* removedNode, NodePointer& referenc
             // a descendant of the node being removed.
             if (willRemoveReferenceNodeAncestor) {
                 while (node && node->isDescendantOf(removedNode))
-                    node = NodeTraversal::previous(node);
+                    node = NodeTraversal::previous(*node);
             }
             if (node)
                 referenceNode.node = node;
         } else {
             // FIXME: This branch doesn't appear to have any LayoutTests.
-            node = NodeTraversal::next(removedNode, root());
+            node = NodeTraversal::next(removedNode, &root());
             // Move out from under the node being removed if the reference node is
             // a descendant of the node being removed.
             if (willRemoveReferenceNodeAncestor) {
                 while (node && node->isDescendantOf(removedNode))
-                    node = NodeTraversal::previous(node);
+                    node = NodeTraversal::previous(*node);
             }
             if (node)
                 referenceNode.node = node;
@@ -228,5 +216,4 @@ void NodeIterator::updateForNodeRemoval(Node* removedNode, NodePointer& referenc
     }
 }
 
-
 } // namespace WebCore