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