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, 2009, 2010, 2012 Apple Inc. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include "core/dom/Attr.h"
26 #include "bindings/core/v8/ExceptionState.h"
27 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
28 #include "core/dom/Document.h"
29 #include "core/dom/Element.h"
30 #include "core/dom/Text.h"
31 #include "core/events/ScopedEventQueue.h"
32 #include "core/frame/UseCounter.h"
33 #include "wtf/text/AtomicString.h"
34 #include "wtf/text/StringBuilder.h"
38 using namespace HTMLNames;
40 Attr::Attr(Element& element, const QualifiedName& name)
41 : ContainerNode(&element.document())
44 , m_ignoreChildrenChanged(0)
46 ScriptWrappable::init(this);
49 Attr::Attr(Document& document, const QualifiedName& name, const AtomicString& standaloneValue)
50 : ContainerNode(&document)
53 , m_standaloneValueOrAttachedLocalName(standaloneValue)
54 , m_ignoreChildrenChanged(0)
56 ScriptWrappable::init(this);
59 PassRefPtrWillBeRawPtr<Attr> Attr::create(Element& element, const QualifiedName& name)
61 RefPtrWillBeRawPtr<Attr> attr = adoptRefWillBeNoop(new Attr(element, name));
62 attr->createTextChild();
63 return attr.release();
66 PassRefPtrWillBeRawPtr<Attr> Attr::create(Document& document, const QualifiedName& name, const AtomicString& value)
68 RefPtrWillBeRawPtr<Attr> attr = adoptRefWillBeNoop(new Attr(document, name, value));
69 attr->createTextChild();
70 return attr.release();
77 const QualifiedName Attr::qualifiedName() const
79 if (m_element && !m_standaloneValueOrAttachedLocalName.isNull()) {
80 // In the unlikely case the Element attribute has a local name
81 // that differs by case, construct the qualified name based on
82 // it. This is the qualified name that must be used when
83 // looking up the attribute on the element.
84 return QualifiedName(m_name.prefix(), m_standaloneValueOrAttachedLocalName, m_name.namespaceURI());
90 void Attr::createTextChild()
95 if (!value().isEmpty()) {
96 RefPtrWillBeRawPtr<Text> textNode = document().createTextNode(value().string());
98 // This does everything appendChild() would do in this situation (assuming m_ignoreChildrenChanged was set),
99 // but much more efficiently.
100 textNode->setParentOrShadowHostNode(this);
101 treeScope().adoptIfNeeded(*textNode);
102 setFirstChild(textNode.get());
103 setLastChild(textNode.get());
107 void Attr::setValue(const AtomicString& value)
109 EventQueueScope scope;
110 m_ignoreChildrenChanged++;
113 elementAttribute().setValue(value);
115 m_standaloneValueOrAttachedLocalName = value;
117 m_ignoreChildrenChanged--;
119 QualifiedName name = qualifiedName();
120 invalidateNodeListCachesInAncestors(&name, m_element);
123 void Attr::setValueInternal(const AtomicString& value)
126 m_element->willModifyAttribute(qualifiedName(), this->value(), value);
131 m_element->didModifyAttribute(qualifiedName(), value);
134 const AtomicString& Attr::valueForBindings() const
136 UseCounter::count(document(), UseCounter::AttrGetValue);
140 void Attr::setValueForBindings(const AtomicString& value)
142 UseCounter::count(document(), UseCounter::AttrSetValue);
144 UseCounter::count(document(), UseCounter::AttrSetValueWithElement);
145 setValueInternal(value);
148 void Attr::setNodeValue(const String& v)
150 // Attr uses AtomicString type for its value to save memory as there
151 // is duplication among Elements' attributes values.
152 setValueInternal(AtomicString(v));
155 PassRefPtrWillBeRawPtr<Node> Attr::cloneNode(bool /*deep*/)
157 RefPtrWillBeRawPtr<Attr> clone = adoptRefWillBeNoop(new Attr(document(), m_name, value()));
158 cloneChildNodes(clone.get());
159 return clone.release();
163 bool Attr::childTypeAllowed(NodeType type) const
165 return TEXT_NODE == type;
168 void Attr::childrenChanged(const ChildrenChange&)
170 if (m_ignoreChildrenChanged > 0)
173 QualifiedName name = qualifiedName();
174 invalidateNodeListCachesInAncestors(&name, m_element);
176 StringBuilder valueBuilder;
177 for (Node *n = firstChild(); n; n = n->nextSibling()) {
179 valueBuilder.append(toText(n)->data());
182 AtomicString newValue = valueBuilder.toAtomicString();
184 m_element->willModifyAttribute(qualifiedName(), value(), newValue);
187 elementAttribute().setValue(newValue);
189 m_standaloneValueOrAttachedLocalName = newValue;
192 m_element->attributeChanged(qualifiedName(), newValue);
195 const AtomicString& Attr::value() const
198 return m_element->getAttribute(qualifiedName());
199 return m_standaloneValueOrAttachedLocalName;
202 Attribute& Attr::elementAttribute()
205 ASSERT(m_element->elementData());
206 return *m_element->ensureUniqueElementData().attributes().find(qualifiedName());
209 void Attr::detachFromElementWithValue(const AtomicString& value)
212 m_standaloneValueOrAttachedLocalName = value;
216 void Attr::attachToElement(Element* element, const AtomicString& attachedLocalName)
220 m_standaloneValueOrAttachedLocalName = attachedLocalName;
223 void Attr::trace(Visitor* visitor)
225 visitor->trace(m_element);
226 ContainerNode::trace(visitor);