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 Peter Kelly (pmk (at) post.com)
      5  *           (C) 2001 Dirk Mueller (mueller (at) kde.org)
      6  * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Library General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Library General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Library General Public License
     19  * along with this library; see the file COPYING.LIB.  If not, write to
     20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     21  * Boston, MA 02110-1301, USA.
     22  *
     23  */
     24 
     25 #ifndef NamedAttrMap_h
     26 #define NamedAttrMap_h
     27 
     28 #include "Attribute.h"
     29 
     30 #ifdef __OBJC__
     31 #define id id_AVOID_KEYWORD
     32 #endif
     33 
     34 namespace WebCore {
     35 
     36 class Node;
     37 
     38 typedef int ExceptionCode;
     39 
     40 class NamedNodeMap : public RefCounted<NamedNodeMap> {
     41     friend class Element;
     42 
     43 protected:
     44     NamedNodeMap(Element* element) : m_element(element) { }
     45 
     46 public:
     47     static PassRefPtr<NamedNodeMap> create(Element* element) { return adoptRef(new NamedNodeMap(element)); }
     48 
     49     virtual ~NamedNodeMap();
     50 
     51     // Public DOM interface.
     52 
     53     PassRefPtr<Node> getNamedItem(const String& name) const;
     54     PassRefPtr<Node> removeNamedItem(const String& name, ExceptionCode&);
     55 
     56     PassRefPtr<Node> getNamedItemNS(const String& namespaceURI, const String& localName) const;
     57     PassRefPtr<Node> removeNamedItemNS(const String& namespaceURI, const String& localName, ExceptionCode&);
     58 
     59     PassRefPtr<Node> getNamedItem(const QualifiedName& name) const;
     60     PassRefPtr<Node> removeNamedItem(const QualifiedName& name, ExceptionCode&);
     61     PassRefPtr<Node> setNamedItem(Node*, ExceptionCode&);
     62     PassRefPtr<Node> setNamedItemNS(Node* node, ExceptionCode& ec) { return setNamedItem(node, ec); }
     63 
     64     PassRefPtr<Node> item(unsigned index) const;
     65     size_t length() const { return m_attributes.size(); }
     66     bool isEmpty() const { return !length(); }
     67 
     68     // Internal interface.
     69 
     70     void setAttributes(const NamedNodeMap&);
     71 
     72     Attribute* attributeItem(unsigned index) const { return m_attributes[index].get(); }
     73     Attribute* getAttributeItem(const QualifiedName&) const;
     74 
     75     void shrinkToLength() { m_attributes.shrinkCapacity(length()); }
     76     void reserveInitialCapacity(unsigned capacity) { m_attributes.reserveInitialCapacity(capacity); }
     77 
     78     // Used during parsing: only inserts if not already there. No error checking!
     79     void insertAttribute(PassRefPtr<Attribute> newAttribute, bool allowDuplicates)
     80     {
     81         ASSERT(!m_element);
     82         if (allowDuplicates || !getAttributeItem(newAttribute->name()))
     83             addAttribute(newAttribute);
     84     }
     85 
     86     virtual bool isMappedAttributeMap() const;
     87 
     88     const AtomicString& id() const { return m_id; }
     89     void setID(const AtomicString& newId) { m_id = newId; }
     90 
     91     bool mapsEquivalent(const NamedNodeMap* otherMap) const;
     92 
     93     // These functions do no error checking.
     94     void addAttribute(PassRefPtr<Attribute>);
     95     void removeAttribute(const QualifiedName&);
     96 
     97     Element* element() const { return m_element; }
     98 
     99 protected:
    100     virtual void clearAttributes();
    101 
    102 private:
    103     void detachAttributesFromElement();
    104     void detachFromElement();
    105     Attribute* getAttributeItem(const String& name, bool shouldIgnoreAttributeCase) const;
    106     Attribute* getAttributeItemSlowCase(const String& name, bool shouldIgnoreAttributeCase) const;
    107 
    108     Element* m_element;
    109     Vector<RefPtr<Attribute> > m_attributes;
    110     AtomicString m_id;
    111 };
    112 
    113 inline Attribute* NamedNodeMap::getAttributeItem(const QualifiedName& name) const
    114 {
    115     unsigned len = length();
    116     for (unsigned i = 0; i < len; ++i) {
    117         if (m_attributes[i]->name().matches(name))
    118             return m_attributes[i].get();
    119     }
    120     return 0;
    121 }
    122 
    123 // We use a boolean parameter instead of calling shouldIgnoreAttributeCase so that the caller
    124 // can tune the behaviour (hasAttribute is case sensitive whereas getAttribute is not).
    125 inline Attribute* NamedNodeMap::getAttributeItem(const String& name, bool shouldIgnoreAttributeCase) const
    126 {
    127     unsigned len = length();
    128     bool doSlowCheck = shouldIgnoreAttributeCase;
    129 
    130     // Optimize for the case where the attribute exists and its name exactly matches.
    131     for (unsigned i = 0; i < len; ++i) {
    132         const QualifiedName& attrName = m_attributes[i]->name();
    133         if (!attrName.hasPrefix()) {
    134             if (name == attrName.localName())
    135                 return m_attributes[i].get();
    136         } else
    137             doSlowCheck = true;
    138     }
    139 
    140     if (doSlowCheck)
    141         return getAttributeItemSlowCase(name, shouldIgnoreAttributeCase);
    142     return 0;
    143 }
    144 
    145 } //namespace
    146 
    147 #undef id
    148 
    149 #endif
    150