• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (a->name() == idAttr)
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<Node> r = a->createAttrIfNeeded(m_element);
159 
160     if (name == idAttr)
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 
175 // We use a boolean parameter instead of calling shouldIgnoreAttributeCase so that the caller
176 // can tune the behaviour (hasAttribute is case sensitive whereas getAttribute is not).
getAttributeItem(const String & name,bool shouldIgnoreAttributeCase) const177 Attribute* NamedNodeMap::getAttributeItem(const String& name, bool shouldIgnoreAttributeCase) const
178 {
179     unsigned len = length();
180     for (unsigned i = 0; i < len; ++i) {
181         if (!m_attributes[i]->name().hasPrefix() &&
182             m_attributes[i]->name().localName() == name)
183                 return m_attributes[i].get();
184 
185         if (shouldIgnoreAttributeCase ? equalIgnoringCase(m_attributes[i]->name().toString(), name) : name == m_attributes[i]->name().toString())
186             return m_attributes[i].get();
187     }
188     return 0;
189 }
190 
getAttributeItem(const QualifiedName & name) const191 Attribute* NamedNodeMap::getAttributeItem(const QualifiedName& name) const
192 {
193     unsigned len = length();
194     for (unsigned i = 0; i < len; ++i) {
195         if (m_attributes[i]->name().matches(name))
196             return m_attributes[i].get();
197     }
198     return 0;
199 }
200 
clearAttributes()201 void NamedNodeMap::clearAttributes()
202 {
203     detachAttributesFromElement();
204     m_attributes.clear();
205 }
206 
detachFromElement()207 void NamedNodeMap::detachFromElement()
208 {
209     // we allow a NamedNodeMap w/o an element in case someone still has a reference
210     // to if after the element gets deleted - but the map is now invalid
211     m_element = 0;
212     detachAttributesFromElement();
213 }
214 
setAttributes(const NamedNodeMap & other)215 void NamedNodeMap::setAttributes(const NamedNodeMap& other)
216 {
217     // clone all attributes in the other map, but attach to our element
218     if (!m_element)
219         return;
220 
221     // If assigning the map changes the id attribute, we need to call
222     // updateId.
223     Attribute *oldId = getAttributeItem(idAttr);
224     Attribute *newId = other.getAttributeItem(idAttr);
225 
226     if (oldId || newId)
227         m_element->updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);
228 
229     clearAttributes();
230     unsigned newLength = other.length();
231     m_attributes.resize(newLength);
232     for (unsigned i = 0; i < newLength; i++)
233         m_attributes[i] = other.m_attributes[i]->clone();
234 
235     // FIXME: This is wasteful.  The class list could be preserved on a copy, and we
236     // wouldn't have to waste time reparsing the attribute.
237     // The derived class, HTMLNamedNodeMap, which manages a parsed class list for the CLASS attribute,
238     // will update its member variable when parse attribute is called.
239     for (unsigned i = 0; i < newLength; i++)
240         m_element->attributeChanged(m_attributes[i].get(), true);
241 }
242 
addAttribute(PassRefPtr<Attribute> prpAttribute)243 void NamedNodeMap::addAttribute(PassRefPtr<Attribute> prpAttribute)
244 {
245     RefPtr<Attribute> attribute = prpAttribute;
246 
247     // Add the attribute to the list
248     m_attributes.append(attribute);
249 
250     if (Attr* attr = attribute->attr())
251         attr->m_element = m_element;
252 
253     // Notify the element that the attribute has been added, and dispatch appropriate mutation events
254     // Note that element may be null here if we are called from insertAttr() during parsing
255     if (m_element) {
256         m_element->attributeChanged(attribute.get());
257         // Because of our updateStyleAttribute() style modification events are never sent at the right time, so don't bother sending them.
258         if (attribute->name() != styleAttr) {
259             m_element->dispatchAttrAdditionEvent(attribute.get());
260             m_element->dispatchSubtreeModifiedEvent();
261         }
262     }
263 }
264 
removeAttribute(const QualifiedName & name)265 void NamedNodeMap::removeAttribute(const QualifiedName& name)
266 {
267     unsigned len = length();
268     unsigned index = len + 1;
269     for (unsigned i = 0; i < len; ++i)
270         if (m_attributes[i]->name().matches(name)) {
271             index = i;
272             break;
273         }
274 
275     if (index >= len)
276         return;
277 
278     // Remove the attribute from the list
279     RefPtr<Attribute> attr = m_attributes[index].get();
280     if (Attr* a = m_attributes[index]->attr())
281         a->m_element = 0;
282 
283     m_attributes.remove(index);
284 
285     // Notify the element that the attribute has been removed
286     // dispatch appropriate mutation events
287     if (m_element && !attr->m_value.isNull()) {
288         AtomicString value = attr->m_value;
289         attr->m_value = nullAtom;
290         m_element->attributeChanged(attr.get());
291         attr->m_value = value;
292     }
293     if (m_element) {
294         m_element->dispatchAttrRemovalEvent(attr.get());
295         m_element->dispatchSubtreeModifiedEvent();
296     }
297 }
298 
mapsEquivalent(const NamedNodeMap * otherMap) const299 bool NamedNodeMap::mapsEquivalent(const NamedNodeMap* otherMap) const
300 {
301     if (!otherMap)
302         return false;
303 
304     unsigned len = length();
305     if (len != otherMap->length())
306         return false;
307 
308     for (unsigned i = 0; i < len; i++) {
309         Attribute *attr = attributeItem(i);
310         Attribute *otherAttr = otherMap->getAttributeItem(attr->name());
311 
312         if (!otherAttr || attr->value() != otherAttr->value())
313             return false;
314     }
315 
316     return true;
317 }
318 
319 }
320