blob: 1fad1f7fd9a1daaab945848d69d5709d4161e2f6 [file] [log] [blame]
darinb4483822006-06-02 06:30:49 +00001/*
darinb4483822006-06-02 06:30:49 +00002 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2000 Frederik Holljen (frederik.holljen@hig.no)
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
5 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
darin@apple.come4f9a662008-02-08 10:35:20 +00006 * Copyright (C) 2004, 2008 Apple Inc. All rights reserved.
darinb4483822006-06-02 06:30:49 +00007 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000020 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
darinb4483822006-06-02 06:30:49 +000022 *
23 */
24
25#ifndef NodeIterator_h
26#define NodeIterator_h
27
dino@apple.com54b5cc12008-07-08 03:19:30 +000028#include "NodeFilter.h"
eric@webkit.org1373a742012-11-18 03:37:49 +000029#include "ScriptWrappable.h"
darinb4483822006-06-02 06:30:49 +000030#include "Traversal.h"
darin@apple.come4f9a662008-02-08 10:35:20 +000031#include <wtf/PassRefPtr.h>
darin@apple.com1edff432008-06-24 05:23:17 +000032#include <wtf/RefCounted.h>
darinb4483822006-06-02 06:30:49 +000033
34namespace WebCore {
35
darinb4483822006-06-02 06:30:49 +000036 typedef int ExceptionCode;
37
eric@webkit.org1373a742012-11-18 03:37:49 +000038 class NodeIterator : public ScriptWrappable, public RefCounted<NodeIterator>, public Traversal {
darinb4483822006-06-02 06:30:49 +000039 public:
andersca@apple.com0f509b12008-06-04 21:12:16 +000040 static PassRefPtr<NodeIterator> create(PassRefPtr<Node> rootNode, unsigned whatToShow, PassRefPtr<NodeFilter> filter, bool expandEntityReferences)
41 {
42 return adoptRef(new NodeIterator(rootNode, whatToShow, filter, expandEntityReferences));
43 }
darin@apple.com1edff432008-06-24 05:23:17 +000044 ~NodeIterator();
darinb4483822006-06-02 06:30:49 +000045
eric@webkit.org406d6422008-12-04 20:51:21 +000046 PassRefPtr<Node> nextNode(ScriptState*, ExceptionCode&);
47 PassRefPtr<Node> previousNode(ScriptState*, ExceptionCode&);
anderscac86bc242006-08-15 00:59:42 +000048 void detach();
darinb4483822006-06-02 06:30:49 +000049
darin@apple.come4f9a662008-02-08 10:35:20 +000050 Node* referenceNode() const { return m_referenceNode.node.get(); }
51 bool pointerBeforeReferenceNode() const { return m_referenceNode.isPointerBeforeNode; }
darinb4483822006-06-02 06:30:49 +000052
darin@apple.come4f9a662008-02-08 10:35:20 +000053 // This function is called before any node is removed from the document tree.
darin@apple.comf28fa1b2008-03-15 17:26:28 +000054 void nodeWillBeRemoved(Node*);
darin@apple.come4f9a662008-02-08 10:35:20 +000055
barraclough@apple.com6539bbe2009-11-30 21:48:23 +000056 // Do not call these functions. They are just scaffolding to support the Objective-C bindings.
57 // They operate in the main thread normal world, and they swallow JS exceptions.
58 PassRefPtr<Node> nextNode(ExceptionCode& ec) { return nextNode(scriptStateFromNode(mainThreadNormalWorld(), referenceNode()), ec); }
59 PassRefPtr<Node> previousNode(ExceptionCode& ec) { return previousNode(scriptStateFromNode(mainThreadNormalWorld(), referenceNode()), ec); }
darinb4483822006-06-02 06:30:49 +000060
61 private:
andersca@apple.com0f509b12008-06-04 21:12:16 +000062 NodeIterator(PassRefPtr<Node>, unsigned whatToShow, PassRefPtr<NodeFilter>, bool expandEntityReferences);
63
darin@apple.come4f9a662008-02-08 10:35:20 +000064 struct NodePointer {
65 RefPtr<Node> node;
66 bool isPointerBeforeNode;
67 NodePointer();
68 NodePointer(PassRefPtr<Node>, bool);
69 void clear();
70 bool moveToNext(Node* root);
71 bool moveToPrevious(Node* root);
72 };
darinb4483822006-06-02 06:30:49 +000073
darin@apple.come4f9a662008-02-08 10:35:20 +000074 void updateForNodeRemoval(Node* nodeToBeRemoved, NodePointer&) const;
75
76 NodePointer m_referenceNode;
77 NodePointer m_candidateNode;
darinb4483822006-06-02 06:30:49 +000078 bool m_detached;
darinb4483822006-06-02 06:30:49 +000079 };
80
81} // namespace WebCore
82
83#endif // NodeIterator_h