Use a 1-byte enum class for TextDirection
[WebKit-https.git] / Source / WebCore / dom / NodeIterator.cpp
index e436c78..45d860b 100644 (file)
 #include "NodeIterator.h"
 
 #include "Document.h"
-#include "ExceptionCode.h"
 #include "NodeTraversal.h"
 
-#include <runtime/JSCJSValueInlines.h>
-
 namespace WebCore {
 
-NodeIterator::NodePointer::NodePointer()
+inline NodeIterator::NodePointer::NodePointer(Node& node, bool isPointerBeforeNode)
+    : node(&node)
+    , isPointerBeforeNode(isPointerBeforeNode)
 {
 }
 
-NodeIterator::NodePointer::NodePointer(PassRefPtr<Node> n, bool b)
-    : node(n)
-    , isPointerBeforeNode(b)
-{
-}
-
-void NodeIterator::NodePointer::clear()
+inline void NodeIterator::NodePointer::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, 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,7 +61,7 @@ bool NodeIterator::NodePointer::moveToPrevious(Node* root)
         isPointerBeforeNode = true;
         return true;
     }
-    if (node == root) {
+    if (node == &root) {
         node = nullptr;
         return false;
     }
@@ -76,19 +69,24 @@ bool NodeIterator::NodePointer::moveToPrevious(Node* root)
     return node;
 }
 
-NodeIterator::NodeIterator(PassRefPtr<Node> rootNode, unsigned long whatToShow, PassRefPtr<NodeFilter> filter, bool expandEntityReferences)
-    : NodeIteratorBase(rootNode, whatToShow, filter, expandEntityReferences)
-    , m_referenceNode(root(), true)
+inline NodeIterator::NodeIterator(Node& rootNode, unsigned whatToShow, RefPtr<NodeFilter>&& filter)
+    : NodeIteratorBase(rootNode, whatToShow, WTFMove(filter))
+    , m_referenceNode(rootNode, true)
+{
+    root().document().attachNodeIterator(this);
+}
+
+Ref<NodeIterator> NodeIterator::create(Node& rootNode, unsigned whatToShow, RefPtr<NodeFilter>&& filter)
 {
-    root()->document().attachNodeIterator(this);
+    return adoptRef(*new NodeIterator(rootNode, whatToShow, WTFMove(filter)));
 }
 
 NodeIterator::~NodeIterator()
 {
-    root()->document().detachNodeIterator(this);
+    root().document().detachNodeIterator(this);
 }
 
-RefPtr<Node> NodeIterator::nextNode(JSC::ExecState* state)
+ExceptionOr<RefPtr<Node>> NodeIterator::nextNode()
 {
     RefPtr<Node> result;
 
@@ -98,21 +96,26 @@ RefPtr<Node> NodeIterator::nextNode(JSC::ExecState* state)
         // 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;
+    return WTFMove(result);
 }
 
-RefPtr<Node> NodeIterator::previousNode(JSC::ExecState* state)
+ExceptionOr<RefPtr<Node>> NodeIterator::previousNode()
 {
     RefPtr<Node> result;
 
@@ -122,23 +125,23 @@ RefPtr<Node> NodeIterator::previousNode(JSC::ExecState* state)
         // 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;
-}
-
-void NodeIterator::detach()
-{
-    // This is now a no-op as per the DOM specification.
+    return WTFMove(result);
 }
 
 void NodeIterator::nodeWillBeRemoved(Node& removedNode)
@@ -149,24 +152,24 @@ void NodeIterator::nodeWillBeRemoved(Node& removedNode)
 
 void NodeIterator::updateForNodeRemoval(Node& removedNode, NodePointer& referenceNode) const
 {
-    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()))
         return;
     bool willRemoveReferenceNode = &removedNode == referenceNode.node;
-    bool willRemoveReferenceNodeAncestor = referenceNode.node && referenceNode.node->isDescendantOf(&removedNode);
+    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());
+            while (node && node->isDescendantOf(removedNode))
+                node = NodeTraversal::next(*node, &root());
             if (node)
                 referenceNode.node = node;
         } else {
@@ -193,18 +196,18 @@ 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))
+                while (node && node->isDescendantOf(removedNode))
                     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))
+                while (node && node->isDescendantOf(removedNode))
                     node = NodeTraversal::previous(*node);
             }
             if (node)
@@ -213,5 +216,4 @@ void NodeIterator::updateForNodeRemoval(Node& removedNode, NodePointer& referenc
     }
 }
 
-
 } // namespace WebCore