- add third_party src.
[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 void NamedNodeMap::ref()
39 {
40     m_element->ref();
41 }
42
43 void NamedNodeMap::deref()
44 {
45     m_element->deref();
46 }
47
48 PassRefPtr<Node> NamedNodeMap::getNamedItem(const AtomicString& name) const
49 {
50     return m_element->getAttributeNode(name);
51 }
52
53 PassRefPtr<Node> NamedNodeMap::getNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName) const
54 {
55     return m_element->getAttributeNodeNS(namespaceURI, localName);
56 }
57
58 PassRefPtr<Node> NamedNodeMap::removeNamedItem(const AtomicString& name, ExceptionState& es)
59 {
60     size_t index = m_element->hasAttributes() ? m_element->getAttributeItemIndex(name, m_element->shouldIgnoreAttributeCase()) : kNotFound;
61     if (index == kNotFound) {
62         es.throwUninformativeAndGenericDOMException(NotFoundError);
63         return 0;
64     }
65     return m_element->detachAttribute(index);
66 }
67
68 PassRefPtr<Node> NamedNodeMap::removeNamedItemNS(const AtomicString& namespaceURI, const AtomicString& localName, ExceptionState& es)
69 {
70     size_t index = m_element->hasAttributes() ? m_element->getAttributeItemIndex(QualifiedName(nullAtom, localName, namespaceURI)) : kNotFound;
71     if (index == kNotFound) {
72         es.throwUninformativeAndGenericDOMException(NotFoundError);
73         return 0;
74     }
75     return m_element->detachAttribute(index);
76 }
77
78 PassRefPtr<Node> NamedNodeMap::setNamedItem(Node* node, ExceptionState& es)
79 {
80     if (!node) {
81         es.throwUninformativeAndGenericDOMException(NotFoundError);
82         return 0;
83     }
84
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);
88         return 0;
89     }
90
91     return m_element->setAttributeNode(toAttr(node), es);
92 }
93
94 PassRefPtr<Node> NamedNodeMap::setNamedItemNS(Node* node, ExceptionState& es)
95 {
96     return setNamedItem(node, es);
97 }
98
99 PassRefPtr<Node> NamedNodeMap::item(unsigned index) const
100 {
101     if (index >= length())
102         return 0;
103     return m_element->ensureAttr(m_element->attributeItem(index)->name());
104 }
105
106 size_t NamedNodeMap::length() const
107 {
108     if (!m_element->hasAttributes())
109         return 0;
110     return m_element->attributeCount();
111 }
112
113 } // namespace WebCore