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 "HTMLElement.h"
30#include <wtf/IsoMallocInlines.h>
31
32namespace WebCore {
33
34using namespace HTMLNames;
35
36WTF_MAKE_ISO_ALLOCATED_IMPL(NamedNodeMap);
37
38void NamedNodeMap::ref()
39{
40 m_element.ref();
41}
42
43void NamedNodeMap::deref()
44{
45 m_element.deref();
46}
47
48RefPtr<Attr> NamedNodeMap::getNamedItem(const AtomString& name) const
49{
50 return m_element.getAttributeNode(name);
51}
52
53RefPtr<Attr> NamedNodeMap::getNamedItemNS(const AtomString& namespaceURI, const AtomString& localName) const
54{
55 return m_element.getAttributeNodeNS(namespaceURI, localName);
56}
57
58ExceptionOr<Ref<Attr>> NamedNodeMap::removeNamedItem(const AtomString& name)
59{
60 if (!m_element.hasAttributes())
61 return Exception { NotFoundError };
62 auto index = m_element.findAttributeIndexByName(name, shouldIgnoreAttributeCase(m_element));
63 if (index == ElementData::attributeNotFound)
64 return Exception { NotFoundError };
65 return m_element.detachAttribute(index);
66}
67
68Vector<String> NamedNodeMap::supportedPropertyNames() const
69{
70 Vector<String> names = m_element.getAttributeNames();
71 if (is<HTMLElement>(m_element) && m_element.document().isHTMLDocument()) {
72 names.removeAllMatching([](String& name) {
73 for (auto character : StringView { name }.codeUnits()) {
74 if (isASCIIUpper(character))
75 return true;
76 }
77 return false;
78 });
79 }
80 return names;
81}
82
83ExceptionOr<Ref<Attr>> NamedNodeMap::removeNamedItemNS(const AtomString& namespaceURI, const AtomString& localName)
84{
85 if (!m_element.hasAttributes())
86 return Exception { NotFoundError };
87 auto index = m_element.findAttributeIndexByName(QualifiedName { nullAtom(), localName, namespaceURI });
88 if (index == ElementData::attributeNotFound)
89 return Exception { NotFoundError };
90 return m_element.detachAttribute(index);
91}
92
93ExceptionOr<RefPtr<Attr>> NamedNodeMap::setNamedItem(Attr& attr)
94{
95 return m_element.setAttributeNode(attr);
96}
97
98RefPtr<Attr> NamedNodeMap::item(unsigned index) const
99{
100 if (index >= length())
101 return nullptr;
102 return m_element.ensureAttr(m_element.attributeAt(index).name());
103}
104
105unsigned NamedNodeMap::length() const
106{
107 if (!m_element.hasAttributes())
108 return 0;
109 return m_element.attributeCount();
110}
111
112} // namespace WebCore
113