Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / NamedNodeMap.cpp
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 "core/dom/NamedNodeMap.h"
27
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"
33
34 namespace WebCore {
35
36 using namespace HTMLNames;
37
38 #if !ENABLE(OILPAN)
39 void NamedNodeMap::ref()
40 {
41     m_element->ref();
42 }
43
44 void NamedNodeMap::deref()
45 {
46     m_element->deref();
47 }
48 #endif
49
50 PassRefPtr<Node> NamedNodeMap::getNamedItem(const AtomicString& name) const
51 {
52     return m_element->getAttributeNode(name);
53 }
54
55 PassRefPtr<Node> NamedNodeMap::getNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName) const
56 {
57     return m_element->getAttributeNodeNS(namespaceURI, localName);
58 }
59
60 PassRefPtr<Node> NamedNodeMap::removeNamedItem(const AtomicString& name, ExceptionState& exceptionState)
61 {
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.");
65         return nullptr;
66     }
67     return m_element->detachAttribute(index);
68 }
69
70 PassRefPtr<Node> NamedNodeMap::removeNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName, ExceptionState& exceptionState)
71 {
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.");
75         return nullptr;
76     }
77     return m_element->detachAttribute(index);
78 }
79
80 PassRefPtr<Node> NamedNodeMap::setNamedItem(Node* node, ExceptionState& exceptionState)
81 {
82     if (!node) {
83         exceptionState.throwDOMException(NotFoundError, "The node provided was null.");
84         return nullptr;
85     }
86
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.");
90         return nullptr;
91     }
92
93     return m_element->setAttributeNode(toAttr(node), exceptionState);
94 }
95
96 PassRefPtr<Node> NamedNodeMap::setNamedItemNS(Node* node, ExceptionState& exceptionState)
97 {
98     return setNamedItem(node, exceptionState);
99 }
100
101 PassRefPtr<Node> NamedNodeMap::item(unsigned index) const
102 {
103     if (index >= length())
104         return nullptr;
105     return m_element->ensureAttr(m_element->attributeItem(index).name());
106 }
107
108 size_t NamedNodeMap::length() const
109 {
110     if (!m_element->hasAttributes())
111         return 0;
112     return m_element->attributeCount();
113 }
114
115 void NamedNodeMap::trace(Visitor* visitor)
116 {
117     visitor->trace(m_element);
118 }
119
120 } // namespace WebCore