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, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
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.
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.
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.
24 #include "DynamicNodeList.h"
31 DynamicNodeList::DynamicNodeList(PassRefPtr<Node> rootNode)
32 : m_rootNode(rootNode)
33 , m_caches(Caches::create())
36 m_rootNode->registerDynamicNodeList(this);
39 DynamicNodeList::DynamicNodeList(PassRefPtr<Node> rootNode, DynamicNodeList::Caches* caches)
40 : m_rootNode(rootNode)
44 m_rootNode->registerDynamicNodeList(this);
47 DynamicNodeList::~DynamicNodeList()
49 m_rootNode->unregisterDynamicNodeList(this);
52 unsigned DynamicNodeList::length() const
54 if (m_caches->isLengthCacheValid)
55 return m_caches->cachedLength;
59 for (Node* n = m_rootNode->firstChild(); n; n = n->traverseNextNode(m_rootNode.get()))
60 length += n->isElementNode() && nodeMatches(static_cast<Element*>(n));
62 m_caches->cachedLength = length;
63 m_caches->isLengthCacheValid = true;
68 Node* DynamicNodeList::itemForwardsFromCurrent(Node* start, unsigned offset, int remainingOffset) const
70 ASSERT(remainingOffset >= 0);
71 for (Node* n = start; n; n = n->traverseNextNode(m_rootNode.get())) {
72 if (n->isElementNode() && nodeMatches(static_cast<Element*>(n))) {
73 if (!remainingOffset) {
74 m_caches->lastItem = n;
75 m_caches->lastItemOffset = offset;
76 m_caches->isItemCacheValid = true;
83 return 0; // no matching node in this subtree
86 Node* DynamicNodeList::itemBackwardsFromCurrent(Node* start, unsigned offset, int remainingOffset) const
88 ASSERT(remainingOffset < 0);
89 for (Node* n = start; n; n = n->traversePreviousNode(m_rootNode.get())) {
90 if (n->isElementNode() && nodeMatches(static_cast<Element*>(n))) {
91 if (!remainingOffset) {
92 m_caches->lastItem = n;
93 m_caches->lastItemOffset = offset;
94 m_caches->isItemCacheValid = true;
101 return 0; // no matching node in this subtree
104 Node* DynamicNodeList::item(unsigned offset) const
106 int remainingOffset = offset;
107 Node* start = m_rootNode->firstChild();
108 if (m_caches->isItemCacheValid) {
109 if (offset == m_caches->lastItemOffset)
110 return m_caches->lastItem;
111 else if (offset > m_caches->lastItemOffset || m_caches->lastItemOffset - offset < offset) {
112 start = m_caches->lastItem;
113 remainingOffset -= m_caches->lastItemOffset;
117 if (remainingOffset < 0)
118 return itemBackwardsFromCurrent(start, offset, remainingOffset);
119 return itemForwardsFromCurrent(start, offset, remainingOffset);
122 Node* DynamicNodeList::itemWithName(const AtomicString& elementId) const
124 if (m_rootNode->isDocumentNode() || m_rootNode->inDocument()) {
125 Element* node = m_rootNode->treeScope()->getElementById(elementId);
126 if (node && nodeMatches(node)) {
127 for (ContainerNode* p = node->parentNode(); p; p = p->parentNode()) {
134 // In the case of multiple nodes with the same name, just fall through.
137 unsigned length = this->length();
138 for (unsigned i = 0; i < length; i++) {
139 Node* node = item(i);
140 // FIXME: This should probably be using getIdAttribute instead of idForStyleResolution.
141 if (node->hasID() && static_cast<Element*>(node)->idForStyleResolution() == elementId)
148 bool DynamicNodeList::isDynamicNodeList() const
153 void DynamicNodeList::invalidateCache()
155 // This should only be called for node lists that own their own caches.
156 ASSERT(m_ownsCaches);
160 DynamicNodeList::Caches::Caches()
162 , isLengthCacheValid(false)
163 , isItemCacheValid(false)
167 PassRefPtr<DynamicNodeList::Caches> DynamicNodeList::Caches::create()
169 return adoptRef(new Caches());
172 void DynamicNodeList::Caches::reset()
175 isLengthCacheValid = false;
176 isItemCacheValid = false;
179 } // namespace WebCore