This source file includes following definitions.
- isPointerBeforeNode
- clear
- moveToNext
- moveToPrevious
- m_detached
- nextNode
- previousNode
- detach
- nodeWillBeRemoved
- updateForNodeRemoval
#include "config.h"
#include "core/dom/NodeIterator.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/NodeTraversal.h"
namespace WebCore {
NodeIterator::NodePointer::NodePointer()
{
}
NodeIterator::NodePointer::NodePointer(PassRefPtr<Node> n, bool b)
: node(n)
, isPointerBeforeNode(b)
{
}
void NodeIterator::NodePointer::clear()
{
node.clear();
}
bool NodeIterator::NodePointer::moveToNext(Node* root)
{
if (!node)
return false;
if (isPointerBeforeNode) {
isPointerBeforeNode = false;
return true;
}
node = NodeTraversal::next(*node, root);
return node;
}
bool NodeIterator::NodePointer::moveToPrevious(Node* root)
{
if (!node)
return false;
if (!isPointerBeforeNode) {
isPointerBeforeNode = true;
return true;
}
node = NodeTraversal::previous(*node, root);
return node;
}
NodeIterator::NodeIterator(PassRefPtr<Node> rootNode, unsigned whatToShow, PassRefPtr<NodeFilter> filter)
: NodeIteratorBase(rootNode, whatToShow, filter)
, m_referenceNode(root(), true)
, m_detached(false)
{
ScriptWrappable::init(this);
root()->document().attachNodeIterator(this);
}
NodeIterator::~NodeIterator()
{
root()->document().detachNodeIterator(this);
}
PassRefPtr<Node> NodeIterator::nextNode(ExceptionState& exceptionState)
{
if (m_detached) {
exceptionState.throwDOMException(InvalidStateError, "The iterator is detached.");
return nullptr;
}
RefPtr<Node> result;
m_candidateNode = m_referenceNode;
while (m_candidateNode.moveToNext(root())) {
RefPtr<Node> provisionalResult = m_candidateNode.node;
bool nodeWasAccepted = acceptNode(provisionalResult.get(), exceptionState) == NodeFilter::FILTER_ACCEPT;
if (exceptionState.hadException())
break;
if (nodeWasAccepted) {
m_referenceNode = m_candidateNode;
result = provisionalResult.release();
break;
}
}
m_candidateNode.clear();
return result.release();
}
PassRefPtr<Node> NodeIterator::previousNode(ExceptionState& exceptionState)
{
if (m_detached) {
exceptionState.throwDOMException(InvalidStateError, "The iterator is detached.");
return nullptr;
}
RefPtr<Node> result;
m_candidateNode = m_referenceNode;
while (m_candidateNode.moveToPrevious(root())) {
RefPtr<Node> provisionalResult = m_candidateNode.node;
bool nodeWasAccepted = acceptNode(provisionalResult.get(), exceptionState) == NodeFilter::FILTER_ACCEPT;
if (exceptionState.hadException())
break;
if (nodeWasAccepted) {
m_referenceNode = m_candidateNode;
result = provisionalResult.release();
break;
}
}
m_candidateNode.clear();
return result.release();
}
void NodeIterator::detach()
{
root()->document().detachNodeIterator(this);
m_detached = true;
m_referenceNode.node.clear();
}
void NodeIterator::nodeWillBeRemoved(Node& removedNode)
{
updateForNodeRemoval(removedNode, m_candidateNode);
updateForNodeRemoval(removedNode, m_referenceNode);
}
void NodeIterator::updateForNodeRemoval(Node& removedNode, NodePointer& referenceNode) const
{
ASSERT(!m_detached);
ASSERT(root()->document() == removedNode.document());
if (!removedNode.isDescendantOf(root()))
return;
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());
if (node) {
while (node && node->isDescendantOf(&removedNode))
node = NodeTraversal::next(*node, root());
if (node)
referenceNode.node = node;
} else {
node = NodeTraversal::previous(removedNode, root());
if (node) {
if (willRemoveReferenceNodeAncestor) {
while (node && node->isDescendantOf(&removedNode))
node = NodeTraversal::previous(*node, root());
}
if (node) {
referenceNode.node = node;
referenceNode.isPointerBeforeNode = false;
}
}
}
} else {
Node* node = NodeTraversal::previous(removedNode, root());
if (node) {
if (willRemoveReferenceNodeAncestor) {
while (node && node->isDescendantOf(&removedNode))
node = NodeTraversal::previous(*node, root());
}
if (node)
referenceNode.node = node;
} else {
node = NodeTraversal::next(removedNode, root());
if (willRemoveReferenceNodeAncestor) {
while (node && node->isDescendantOf(&removedNode))
node = NodeTraversal::previous(*node, root());
}
if (node)
referenceNode.node = node;
}
}
}
}