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
29namespace WebCore {
30
31EmptyNodeList::~EmptyNodeList()
32{
33    m_owner.get().nodeLists()->removeEmptyChildNodeList(this);
34}
35
36ChildNodeList::ChildNodeList(ContainerNode& parent)
37    : m_parent(parent)
38    , m_indexCache(*this)
39{
40}
41
42ChildNodeList::~ChildNodeList()
43{
44    m_parent.get().nodeLists()->removeChildNodeList(this);
45}
46
47unsigned ChildNodeList::length() const
48{
49    return m_indexCache.nodeCount(*this);
50}
51
52Node* ChildNodeList::item(unsigned index) const
53{
54    return m_indexCache.nodeAt(*this, index);
55}
56
57Node* ChildNodeList::collectionBegin() const
58{
59    return m_parent->firstChild();
60}
61
62Node* ChildNodeList::collectionLast() const
63{
64    return m_parent->lastChild();
65}
66
67void ChildNodeList::collectionTraverseForward(Node*& current, unsigned count, unsigned& traversedCount) const
68{
69    ASSERT(count);
70    for (traversedCount = 0; traversedCount < count; ++traversedCount) {
71        current = current->nextSibling();
72        if (!current)
73            return;
74    }
75}
76
77void ChildNodeList::collectionTraverseBackward(Node*& current, unsigned count) const
78{
79    ASSERT(count);
80    for (; count && current ; --count)
81        current = current->previousSibling();
82}
83
84Node* ChildNodeList::namedItem(const AtomicString& name) const
85{
86    // FIXME: According to the spec child node list should not have namedItem().
87    if (m_parent.get().inDocument()) {
88        Element* element = m_parent.get().treeScope().getElementById(name);
89        if (element && element->parentNode() == &m_parent.get())
90            return element;
91        if (!element || !m_parent.get().treeScope().containsMultipleElementsWithId(name))
92            return nullptr;
93    }
94    for (auto& element : childrenOfType<Element>(m_parent.get())) {
95        if (element.hasID() && element.idForStyleResolution() == name)
96            return const_cast<Element*>(&element);
97    }
98    return nullptr;
99}
100
101void ChildNodeList::invalidateCache()
102{
103    m_indexCache.invalidate(*this);
104}
105
106} // namespace WebCore
107