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;
39 void NamedNodeMap::ref()
44 void NamedNodeMap::deref()
50 PassRefPtr<Node> NamedNodeMap::getNamedItem(const AtomicString& name) const
52 return m_element->getAttributeNode(name);
55 PassRefPtr<Node> NamedNodeMap::getNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName) const
57 return m_element->getAttributeNodeNS(namespaceURI, localName);
60 PassRefPtr<Node> NamedNodeMap::removeNamedItem(const AtomicString& name, ExceptionState& exceptionState)
62 size_t index = m_element->hasAttributes() ? m_element->getAttributeItemIndex(name, m_element->shouldIgnoreAttributeCase()) : kNotFound;
63 if (index == kNotFound) {
64 exceptionState.throwDOMException(NotFoundError, "No item with name '" + name + "' was found.");
67 return m_element->detachAttribute(index);
70 PassRefPtr<Node> NamedNodeMap::removeNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName, ExceptionState& exceptionState)
72 size_t index = m_element->hasAttributes() ? m_element->getAttributeItemIndex(QualifiedName(nullAtom, localName, namespaceURI)) : kNotFound;
73 if (index == kNotFound) {
74 exceptionState.throwDOMException(NotFoundError, "No item with name '" + namespaceURI + "::" + localName + "' was found.");
77 return m_element->detachAttribute(index);
80 PassRefPtr<Node> NamedNodeMap::setNamedItem(Node* node, ExceptionState& exceptionState)
83 exceptionState.throwDOMException(NotFoundError, "The node provided was null.");
87 // Not mentioned in spec: throw a HIERARCHY_REQUEST_ERROR if the user passes in a non-attribute node
88 if (!node->isAttributeNode()) {
89 exceptionState.throwDOMException(HierarchyRequestError, "The node provided is not an attribute node.");
93 return m_element->setAttributeNode(toAttr(node), exceptionState);
96 PassRefPtr<Node> NamedNodeMap::setNamedItemNS(Node* node, ExceptionState& exceptionState)
98 return setNamedItem(node, exceptionState);
101 PassRefPtr<Node> NamedNodeMap::item(unsigned index) const
103 if (index >= length())
105 return m_element->ensureAttr(m_element->attributeItem(index).name());
108 size_t NamedNodeMap::length() const
110 if (!m_element->hasAttributes())
112 return m_element->attributeCount();
115 void NamedNodeMap::trace(Visitor* visitor)
117 visitor->trace(m_element);
120 } // namespace WebCore