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 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 "NamedAttrMap.h"
27
28 #include "Document.h"
29 #include "Element.h"
30 #include "ExceptionCode.h"
31 #include "HTMLNames.h"
32
33 namespace WebCore {
34
35 using namespace HTMLNames;
36
shouldIgnoreAttributeCase(const Element * e)37 static inline bool shouldIgnoreAttributeCase(const Element* e)
38 {
39 return e && e->document()->isHTMLDocument() && e->isHTMLElement();
40 }
41
~NamedAttrMap()42 NamedAttrMap::~NamedAttrMap()
43 {
44 NamedAttrMap::clearAttributes(); // virtual function, qualify to be explicit and slightly faster
45 }
46
isMappedAttributeMap() const47 bool NamedAttrMap::isMappedAttributeMap() const
48 {
49 return false;
50 }
51
getNamedItem(const String & name) const52 PassRefPtr<Node> NamedAttrMap::getNamedItem(const String& name) const
53 {
54 Attribute* a = getAttributeItem(name, shouldIgnoreAttributeCase(m_element));
55 if (!a)
56 return 0;
57
58 return a->createAttrIfNeeded(m_element);
59 }
60
getNamedItemNS(const String & namespaceURI,const String & localName) const61 PassRefPtr<Node> NamedAttrMap::getNamedItemNS(const String& namespaceURI, const String& localName) const
62 {
63 return getNamedItem(QualifiedName(nullAtom, localName, namespaceURI));
64 }
65
removeNamedItem(const String & name,ExceptionCode & ec)66 PassRefPtr<Node> NamedAttrMap::removeNamedItem(const String& name, ExceptionCode& ec)
67 {
68 Attribute* a = getAttributeItem(name, shouldIgnoreAttributeCase(m_element));
69 if (!a) {
70 ec = NOT_FOUND_ERR;
71 return 0;
72 }
73
74 return removeNamedItem(a->name(), ec);
75 }
76
removeNamedItemNS(const String & namespaceURI,const String & localName,ExceptionCode & ec)77 PassRefPtr<Node> NamedAttrMap::removeNamedItemNS(const String& namespaceURI, const String& localName, ExceptionCode& ec)
78 {
79 return removeNamedItem(QualifiedName(nullAtom, localName, namespaceURI), ec);
80 }
81
getNamedItem(const QualifiedName & name) const82 PassRefPtr<Node> NamedAttrMap::getNamedItem(const QualifiedName& name) const
83 {
84 Attribute* a = getAttributeItem(name);
85 if (!a)
86 return 0;
87
88 return a->createAttrIfNeeded(m_element);
89 }
90
setNamedItem(Node * arg,ExceptionCode & ec)91 PassRefPtr<Node> NamedAttrMap::setNamedItem(Node* arg, ExceptionCode& ec)
92 {
93 if (!m_element || !arg) {
94 ec = NOT_FOUND_ERR;
95 return 0;
96 }
97
98 // WRONG_DOCUMENT_ERR: Raised if arg was created from a different document than the one that created this map.
99 if (arg->document() != m_element->document()) {
100 ec = WRONG_DOCUMENT_ERR;
101 return 0;
102 }
103
104 // Not mentioned in spec: throw a HIERARCHY_REQUEST_ERROR if the user passes in a non-attribute node
105 if (!arg->isAttributeNode()) {
106 ec = HIERARCHY_REQUEST_ERR;
107 return 0;
108 }
109 Attr *attr = static_cast<Attr*>(arg);
110
111 Attribute* a = attr->attr();
112 Attribute* old = getAttributeItem(a->name());
113 if (old == a)
114 return RefPtr<Node>(arg); // we know about it already
115
116 // INUSE_ATTRIBUTE_ERR: Raised if arg is an Attr that is already an attribute of another Element object.
117 // The DOM user must explicitly clone Attr nodes to re-use them in other elements.
118 if (attr->ownerElement()) {
119 ec = INUSE_ATTRIBUTE_ERR;
120 return 0;
121 }
122
123 if (a->name() == idAttr)
124 m_element->updateId(old ? old->value() : nullAtom, a->value());
125
126 // ### slightly inefficient - resizes attribute array twice.
127 RefPtr<Node> r;
128 if (old) {
129 r = old->createAttrIfNeeded(m_element);
130 removeAttribute(a->name());
131 }
132
133 addAttribute(a);
134 return r.release();
135 }
136
137 // The DOM2 spec doesn't say that removeAttribute[NS] throws NOT_FOUND_ERR
138 // if the attribute is not found, but at this level we have to throw NOT_FOUND_ERR
139 // because of removeNamedItem, removeNamedItemNS, and removeAttributeNode.
removeNamedItem(const QualifiedName & name,ExceptionCode & ec)140 PassRefPtr<Node> NamedAttrMap::removeNamedItem(const QualifiedName& name, ExceptionCode& ec)
141 {
142 Attribute* a = getAttributeItem(name);
143 if (!a) {
144 ec = NOT_FOUND_ERR;
145 return 0;
146 }
147
148 RefPtr<Node> r = a->createAttrIfNeeded(m_element);
149
150 if (name == idAttr)
151 m_element->updateId(a->value(), nullAtom);
152
153 removeAttribute(name);
154 return r.release();
155 }
156
item(unsigned index) const157 PassRefPtr<Node> NamedAttrMap::item (unsigned index) const
158 {
159 if (index >= length())
160 return 0;
161
162 return m_attributes[index]->createAttrIfNeeded(m_element);
163 }
164
165 // We use a boolean parameter instead of calling shouldIgnoreAttributeCase so that the caller
166 // can tune the behaviour (hasAttribute is case sensitive whereas getAttribute is not).
getAttributeItem(const String & name,bool shouldIgnoreAttributeCase) const167 Attribute* NamedAttrMap::getAttributeItem(const String& name, bool shouldIgnoreAttributeCase) const
168 {
169 unsigned len = length();
170 for (unsigned i = 0; i < len; ++i) {
171 if (!m_attributes[i]->name().hasPrefix() &&
172 m_attributes[i]->name().localName() == name)
173 return m_attributes[i].get();
174
175 if (shouldIgnoreAttributeCase ? equalIgnoringCase(m_attributes[i]->name().toString(), name) : name == m_attributes[i]->name().toString())
176 return m_attributes[i].get();
177 }
178 return 0;
179 }
180
getAttributeItem(const QualifiedName & name) const181 Attribute* NamedAttrMap::getAttributeItem(const QualifiedName& name) const
182 {
183 unsigned len = length();
184 for (unsigned i = 0; i < len; ++i) {
185 if (m_attributes[i]->name().matches(name))
186 return m_attributes[i].get();
187 }
188 return 0;
189 }
190
clearAttributes()191 void NamedAttrMap::clearAttributes()
192 {
193 unsigned len = length();
194 for (unsigned i = 0; i < len; i++)
195 if (Attr* attr = m_attributes[i]->attr())
196 attr->m_element = 0;
197
198 m_attributes.clear();
199 }
200
detachFromElement()201 void NamedAttrMap::detachFromElement()
202 {
203 // we allow a NamedAttrMap w/o an element in case someone still has a reference
204 // to if after the element gets deleted - but the map is now invalid
205 m_element = 0;
206 clearAttributes();
207 }
208
setAttributes(const NamedAttrMap & other)209 void NamedAttrMap::setAttributes(const NamedAttrMap& other)
210 {
211 // clone all attributes in the other map, but attach to our element
212 if (!m_element)
213 return;
214
215 // If assigning the map changes the id attribute, we need to call
216 // updateId.
217 Attribute *oldId = getAttributeItem(idAttr);
218 Attribute *newId = other.getAttributeItem(idAttr);
219
220 if (oldId || newId)
221 m_element->updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);
222
223 clearAttributes();
224 unsigned newLength = other.length();
225 m_attributes.resize(newLength);
226 for (unsigned i = 0; i < newLength; i++)
227 m_attributes[i] = other.m_attributes[i]->clone();
228
229 // FIXME: This is wasteful. The class list could be preserved on a copy, and we
230 // wouldn't have to waste time reparsing the attribute.
231 // The derived class, HTMLNamedAttrMap, which manages a parsed class list for the CLASS attribute,
232 // will update its member variable when parse attribute is called.
233 for (unsigned i = 0; i < newLength; i++)
234 m_element->attributeChanged(m_attributes[i].get(), true);
235 }
236
addAttribute(PassRefPtr<Attribute> prpAttribute)237 void NamedAttrMap::addAttribute(PassRefPtr<Attribute> prpAttribute)
238 {
239 RefPtr<Attribute> attribute = prpAttribute;
240
241 // Add the attribute to the list
242 m_attributes.append(attribute);
243
244 if (Attr* attr = attribute->attr())
245 attr->m_element = m_element;
246
247 // Notify the element that the attribute has been added, and dispatch appropriate mutation events
248 // Note that element may be null here if we are called from insertAttr() during parsing
249 if (m_element) {
250 m_element->attributeChanged(attribute.get());
251 // Because of our updateStyleAttribute() style modification events are never sent at the right time, so don't bother sending them.
252 if (attribute->name() != styleAttr) {
253 m_element->dispatchAttrAdditionEvent(attribute.get());
254 m_element->dispatchSubtreeModifiedEvent();
255 }
256 }
257 }
258
removeAttribute(const QualifiedName & name)259 void NamedAttrMap::removeAttribute(const QualifiedName& name)
260 {
261 unsigned len = length();
262 unsigned index = len + 1;
263 for (unsigned i = 0; i < len; ++i)
264 if (m_attributes[i]->name().matches(name)) {
265 index = i;
266 break;
267 }
268
269 if (index >= len)
270 return;
271
272 // Remove the attribute from the list
273 RefPtr<Attribute> attr = m_attributes[index].get();
274 if (Attr* a = m_attributes[index]->attr())
275 a->m_element = 0;
276
277 m_attributes.remove(index);
278
279 // Notify the element that the attribute has been removed
280 // dispatch appropriate mutation events
281 if (m_element && !attr->m_value.isNull()) {
282 AtomicString value = attr->m_value;
283 attr->m_value = nullAtom;
284 m_element->attributeChanged(attr.get());
285 attr->m_value = value;
286 }
287 if (m_element) {
288 m_element->dispatchAttrRemovalEvent(attr.get());
289 m_element->dispatchSubtreeModifiedEvent();
290 }
291 }
292
mapsEquivalent(const NamedAttrMap * otherMap) const293 bool NamedAttrMap::mapsEquivalent(const NamedAttrMap* otherMap) const
294 {
295 if (!otherMap)
296 return false;
297
298 unsigned len = length();
299 if (len != otherMap->length())
300 return false;
301
302 for (unsigned i = 0; i < len; i++) {
303 Attribute *attr = attributeItem(i);
304 Attribute *otherAttr = otherMap->getAttributeItem(attr->name());
305
306 if (!otherAttr || attr->value() != otherAttr->value())
307 return false;
308 }
309
310 return true;
311 }
312
313 }
314