1/*
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)
8 *
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.
13 *
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.
18 *
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.
23 */
24
25#include "config.h"
26#include "NamedNodeMap.h"
27
28#include "Attr.h"
29#include "Element.h"
30#include "ExceptionCode.h"
31
32namespace WebCore {
33
34using namespace HTMLNames;
35
36static inline bool shouldIgnoreAttributeCase(const Element& element)
37{
38    return element.isHTMLElement() && element.document().isHTMLDocument();
39}
40
41void NamedNodeMap::ref()
42{
43    m_element.ref();
44}
45
46void NamedNodeMap::deref()
47{
48    m_element.deref();
49}
50
51PassRefPtr<Node> NamedNodeMap::getNamedItem(const AtomicString& name) const
52{
53    return m_element.getAttributeNode(name);
54}
55
56PassRefPtr<Node> NamedNodeMap::getNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName) const
57{
58    return m_element.getAttributeNodeNS(namespaceURI, localName);
59}
60
61PassRefPtr<Node> NamedNodeMap::removeNamedItem(const AtomicString& name, ExceptionCode& ec)
62{
63    unsigned index = m_element.hasAttributes() ? m_element.findAttributeIndexByName(name, shouldIgnoreAttributeCase(m_element)) : ElementData::attributeNotFound;
64    if (index == ElementData::attributeNotFound) {
65        ec = NOT_FOUND_ERR;
66        return 0;
67    }
68    return m_element.detachAttribute(index);
69}
70
71PassRefPtr<Node> NamedNodeMap::removeNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName, ExceptionCode& ec)
72{
73    unsigned index = m_element.hasAttributes() ? m_element.findAttributeIndexByName(QualifiedName(nullAtom, localName, namespaceURI)) : ElementData::attributeNotFound;
74    if (index == ElementData::attributeNotFound) {
75        ec = NOT_FOUND_ERR;
76        return 0;
77    }
78    return m_element.detachAttribute(index);
79}
80
81PassRefPtr<Node> NamedNodeMap::setNamedItem(Node* node, ExceptionCode& ec)
82{
83    if (!node) {
84        ec = NOT_FOUND_ERR;
85        return 0;
86    }
87
88    // Not mentioned in spec: throw a HIERARCHY_REQUEST_ERROR if the user passes in a non-attribute node
89    if (!node->isAttributeNode()) {
90        ec = HIERARCHY_REQUEST_ERR;
91        return 0;
92    }
93
94    return m_element.setAttributeNode(toAttr(node), ec);
95}
96
97PassRefPtr<Node> NamedNodeMap::setNamedItemNS(Node* node, ExceptionCode& ec)
98{
99    return setNamedItem(node, ec);
100}
101
102PassRefPtr<Node> NamedNodeMap::item(unsigned index) const
103{
104    if (index >= length())
105        return 0;
106    return m_element.ensureAttr(m_element.attributeAt(index).name());
107}
108
109unsigned NamedNodeMap::length() const
110{
111    if (!m_element.hasAttributes())
112        return 0;
113    return m_element.attributeCount();
114}
115
116} // namespace WebCore
117