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, 2006, 2007, 2008, 2010 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 "core/dom/LiveNodeList.h"
25
26 #include "core/dom/Element.h"
27 #include "core/html/HTMLCollection.h"
28
29 namespace WebCore {
30
rootNode() const31 Node& LiveNodeListBase::rootNode() const
32 {
33 if (isRootedAtDocument() && m_ownerNode->inDocument())
34 return m_ownerNode->document();
35 return *m_ownerNode;
36 }
37
rootContainerNode() const38 ContainerNode* LiveNodeListBase::rootContainerNode() const
39 {
40 Node& rootNode = this->rootNode();
41 if (!rootNode.isContainerNode())
42 return 0;
43 return toContainerNode(&rootNode);
44 }
45
invalidateCache() const46 void LiveNodeListBase::invalidateCache() const
47 {
48 m_cachedItem = 0;
49 m_isLengthCacheValid = false;
50 m_isItemCacheValid = false;
51 m_isNameCacheValid = false;
52 m_isItemRefElementsCacheValid = false;
53 if (isNodeList(type()))
54 return;
55
56 const HTMLCollection* cacheBase = static_cast<const HTMLCollection*>(this);
57 cacheBase->m_idCache.clear();
58 cacheBase->m_nameCache.clear();
59 cacheBase->m_cachedElementsArrayOffset = 0;
60 }
61
invalidateIdNameCacheMaps() const62 void LiveNodeListBase::invalidateIdNameCacheMaps() const
63 {
64 ASSERT(hasIdNameCache());
65 const HTMLCollection* cacheBase = static_cast<const HTMLCollection*>(this);
66 cacheBase->m_idCache.clear();
67 cacheBase->m_nameCache.clear();
68 }
69
namedItem(const AtomicString & elementId) const70 Node* LiveNodeList::namedItem(const AtomicString& elementId) const
71 {
72 Node& rootNode = this->rootNode();
73
74 if (rootNode.inDocument()) {
75 Element* element = rootNode.treeScope().getElementById(elementId);
76 if (element && nodeMatches(element) && element->isDescendantOf(&rootNode))
77 return element;
78 if (!element)
79 return 0;
80 // In the case of multiple nodes with the same name, just fall through.
81 }
82
83 unsigned length = this->length();
84 for (unsigned i = 0; i < length; i++) {
85 Node* node = item(i);
86 // FIXME: This should probably be using getIdAttribute instead of idForStyleResolution.
87 if (node->hasID() && toElement(node)->idForStyleResolution() == elementId)
88 return node;
89 }
90
91 return 0;
92 }
93
94 } // namespace WebCore
95