Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2001 Dirk Mueller (mueller (at) 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/Document.h"
     27 #include "core/dom/Element.h"
     28 #include "core/html/HTMLCollection.h"
     29 
     30 namespace WebCore {
     31 
     32 Node* LiveNodeListBase::rootNode() const
     33 {
     34     if (isRootedAtDocument() && m_ownerNode->inDocument())
     35         return m_ownerNode->document();
     36     return m_ownerNode.get();
     37 }
     38 
     39 ContainerNode* LiveNodeListBase::rootContainerNode() const
     40 {
     41     Node* rootNode = this->rootNode();
     42     if (!rootNode->isContainerNode())
     43         return 0;
     44     return toContainerNode(rootNode);
     45 }
     46 
     47 void LiveNodeListBase::invalidateCache() const
     48 {
     49     m_cachedItem = 0;
     50     m_isLengthCacheValid = false;
     51     m_isItemCacheValid = false;
     52     m_isNameCacheValid = false;
     53     m_isItemRefElementsCacheValid = false;
     54     if (isNodeList(type()))
     55         return;
     56 
     57     const HTMLCollection* cacheBase = static_cast<const HTMLCollection*>(this);
     58     cacheBase->m_idCache.clear();
     59     cacheBase->m_nameCache.clear();
     60     cacheBase->m_cachedElementsArrayOffset = 0;
     61 }
     62 
     63 void LiveNodeListBase::invalidateIdNameCacheMaps() const
     64 {
     65     ASSERT(hasIdNameCache());
     66     const HTMLCollection* cacheBase = static_cast<const HTMLCollection*>(this);
     67     cacheBase->m_idCache.clear();
     68     cacheBase->m_nameCache.clear();
     69 }
     70 
     71 Node* LiveNodeList::namedItem(const AtomicString& elementId) const
     72 {
     73     Node* rootNode = this->rootNode();
     74 
     75     if (rootNode->inDocument()) {
     76         Element* element = rootNode->treeScope()->getElementById(elementId);
     77         if (element && nodeMatches(element) && element->isDescendantOf(rootNode))
     78             return element;
     79         if (!element)
     80             return 0;
     81         // In the case of multiple nodes with the same name, just fall through.
     82     }
     83 
     84     unsigned length = this->length();
     85     for (unsigned i = 0; i < length; i++) {
     86         Node* node = item(i);
     87         // FIXME: This should probably be using getIdAttribute instead of idForStyleResolution.
     88         if (node->hasID() && toElement(node)->idForStyleResolution() == elementId)
     89             return node;
     90     }
     91 
     92     return 0;
     93 }
     94 
     95 } // namespace WebCore
     96