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 "LiveNodeList.h"
25
26#include "Document.h"
27#include "Element.h"
28#include "HTMLCollection.h"
29#include "HTMLPropertiesCollection.h"
30#include "PropertyNodeList.h"
31
32namespace WebCore {
33
34Node* LiveNodeListBase::rootNode() const
35{
36    if (isRootedAtDocument() && m_ownerNode->inDocument())
37        return m_ownerNode->document();
38
39#if ENABLE(MICRODATA)
40    if (m_rootType == NodeListIsRootedAtDocumentIfOwnerHasItemrefAttr && toElement(ownerNode())->fastHasAttribute(HTMLNames::itemrefAttr)) {
41        if (m_ownerNode->inDocument())
42            return m_ownerNode->document();
43
44        Node* root = m_ownerNode.get();
45        while (Node* parent = root->parentNode())
46            root = parent;
47        return root;
48    }
49#endif
50
51    return m_ownerNode.get();
52}
53
54ContainerNode* LiveNodeListBase::rootContainerNode() const
55{
56    Node* rootNode = this->rootNode();
57    if (!rootNode->isContainerNode())
58        return 0;
59    return toContainerNode(rootNode);
60}
61
62void LiveNodeListBase::invalidateCache() const
63{
64    m_cachedItem = 0;
65    m_isLengthCacheValid = false;
66    m_isItemCacheValid = false;
67    m_isNameCacheValid = false;
68    m_isItemRefElementsCacheValid = false;
69    if (isNodeList(type()))
70        return;
71
72    const HTMLCollection* cacheBase = static_cast<const HTMLCollection*>(this);
73    cacheBase->m_idCache.clear();
74    cacheBase->m_nameCache.clear();
75    cacheBase->m_cachedElementsArrayOffset = 0;
76
77#if ENABLE(MICRODATA)
78    // FIXME: There should be more generic mechanism to clear caches in subclasses.
79    if (type() == ItemProperties)
80        static_cast<const HTMLPropertiesCollection*>(this)->invalidateCache();
81#endif
82}
83
84void LiveNodeListBase::invalidateIdNameCacheMaps() const
85{
86    ASSERT(hasIdNameCache());
87    const HTMLCollection* cacheBase = static_cast<const HTMLCollection*>(this);
88    cacheBase->m_idCache.clear();
89    cacheBase->m_nameCache.clear();
90}
91
92Node* LiveNodeList::namedItem(const AtomicString& elementId) const
93{
94    Node* rootNode = this->rootNode();
95
96    if (rootNode->inDocument()) {
97#if ENABLE(MICRODATA)
98        if (type() == PropertyNodeListType)
99            static_cast<const PropertyNodeList*>(this)->updateRefElements();
100#endif
101
102        Element* element = rootNode->treeScope()->getElementById(elementId);
103        if (element && nodeMatches(element) && element->isDescendantOf(rootNode))
104            return element;
105        if (!element)
106            return 0;
107        // In the case of multiple nodes with the same name, just fall through.
108    }
109
110    unsigned length = this->length();
111    for (unsigned i = 0; i < length; i++) {
112        Node* node = item(i);
113        if (!node->isElementNode())
114            continue;
115        Element* element = toElement(node);
116        // FIXME: This should probably be using getIdAttribute instead of idForStyleResolution.
117        if (element->hasID() && element->idForStyleResolution() == elementId)
118            return node;
119    }
120
121    return 0;
122}
123
124} // namespace WebCore
125