1/**
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2007, 2008, 2013 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#include "config.h"
24#include "ChildNodeList.h"
25
26#include "ElementIterator.h"
27#include "NodeRareData.h"
28#include <wtf/IsoMallocInlines.h>
29
30namespace WebCore {
31
32WTF_MAKE_ISO_ALLOCATED_IMPL(EmptyNodeList);
33WTF_MAKE_ISO_ALLOCATED_IMPL(ChildNodeList);
34
35EmptyNodeList::~EmptyNodeList()
36{
37 m_owner.get().nodeLists()->removeEmptyChildNodeList(this);
38}
39
40ChildNodeList::ChildNodeList(ContainerNode& parent)
41 : m_parent(parent)
42 , m_indexCache(*this)
43{
44}
45
46ChildNodeList::~ChildNodeList()
47{
48 m_parent.get().nodeLists()->removeChildNodeList(this);
49}
50
51unsigned ChildNodeList::length() const
52{
53 return m_indexCache.nodeCount(*this);
54}
55
56Node* ChildNodeList::item(unsigned index) const
57{
58 return m_indexCache.nodeAt(*this, index);
59}
60
61Node* ChildNodeList::collectionBegin() const
62{
63 return m_parent->firstChild();
64}
65
66Node* ChildNodeList::collectionLast() const
67{
68 return m_parent->lastChild();
69}
70
71void ChildNodeList::collectionTraverseForward(Node*& current, unsigned count, unsigned& traversedCount) const
72{
73 ASSERT(count);
74 for (traversedCount = 0; traversedCount < count; ++traversedCount) {
75 current = current->nextSibling();
76 if (!current)
77 return;
78 }
79}
80
81void ChildNodeList::collectionTraverseBackward(Node*& current, unsigned count) const
82{
83 ASSERT(count);
84 for (; count && current ; --count)
85 current = current->previousSibling();
86}
87
88void ChildNodeList::invalidateCache()
89{
90 m_indexCache.invalidate(*this);
91}
92
93} // namespace WebCore
94