2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Peter Kelly (pmk@post.com)
5 * (C) 2001 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All rights reserved.
7 * (C) 2007 Eric Seidel (eric@webkit.org)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 #include "core/dom/NamedNodeMap.h"
28 #include "bindings/v8/ExceptionState.h"
29 #include "core/dom/Attr.h"
30 #include "core/dom/Document.h"
31 #include "core/dom/Element.h"
32 #include "core/dom/ExceptionCode.h"
36 using namespace HTMLNames;
38 void NamedNodeMap::ref()
43 void NamedNodeMap::deref()
48 PassRefPtr<Node> NamedNodeMap::getNamedItem(const AtomicString& name) const
50 return m_element->getAttributeNode(name);
53 PassRefPtr<Node> NamedNodeMap::getNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName) const
55 return m_element->getAttributeNodeNS(namespaceURI, localName);
58 PassRefPtr<Node> NamedNodeMap::removeNamedItem(const AtomicString& name, ExceptionState& es)
60 size_t index = m_element->hasAttributes() ? m_element->getAttributeItemIndex(name, m_element->shouldIgnoreAttributeCase()) : kNotFound;
61 if (index == kNotFound) {
62 es.throwUninformativeAndGenericDOMException(NotFoundError);
65 return m_element->detachAttribute(index);
68 PassRefPtr<Node> NamedNodeMap::removeNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName, ExceptionState& es)
70 size_t index = m_element->hasAttributes() ? m_element->getAttributeItemIndex(QualifiedName(nullAtom, localName, namespaceURI)) : kNotFound;
71 if (index == kNotFound) {
72 es.throwUninformativeAndGenericDOMException(NotFoundError);
75 return m_element->detachAttribute(index);
78 PassRefPtr<Node> NamedNodeMap::setNamedItem(Node* node, ExceptionState& es)
81 es.throwUninformativeAndGenericDOMException(NotFoundError);
85 // Not mentioned in spec: throw a HIERARCHY_REQUEST_ERROR if the user passes in a non-attribute node
86 if (!node->isAttributeNode()) {
87 es.throwUninformativeAndGenericDOMException(HierarchyRequestError);
91 return m_element->setAttributeNode(toAttr(node), es);
94 PassRefPtr<Node> NamedNodeMap::setNamedItemNS(Node* node, ExceptionState& es)
96 return setNamedItem(node, es);
99 PassRefPtr<Node> NamedNodeMap::item(unsigned index) const
101 if (index >= length())
103 return m_element->ensureAttr(m_element->attributeItem(index)->name());
106 size_t NamedNodeMap::length() const
108 if (!m_element->hasAttributes())
110 return m_element->attributeCount();
113 } // namespace WebCore