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)
48 Attr::Attr(Document& document, const QualifiedName& name, const AtomicString& standaloneValue)
49 : ContainerNode(&document)
52 , m_standaloneValueOrAttachedLocalName(standaloneValue)
53 , m_ignoreChildrenChanged(0)
57 PassRefPtrWillBeRawPtr<Attr> Attr::create(Element& element, const QualifiedName& name)
59 RefPtrWillBeRawPtr<Attr> attr = adoptRefWillBeNoop(new Attr(element, name));
60 attr->createTextChild();
61 return attr.release();
64 PassRefPtrWillBeRawPtr<Attr> Attr::create(Document& document, const QualifiedName& name, const AtomicString& value)
66 RefPtrWillBeRawPtr<Attr> attr = adoptRefWillBeNoop(new Attr(document, name, value));
67 attr->createTextChild();
68 return attr.release();
75 const QualifiedName Attr::qualifiedName() const
77 if (m_element && !m_standaloneValueOrAttachedLocalName.isNull()) {
78 // In the unlikely case the Element attribute has a local name
79 // that differs by case, construct the qualified name based on
80 // it. This is the qualified name that must be used when
81 // looking up the attribute on the element.
82 return QualifiedName(m_name.prefix(), m_standaloneValueOrAttachedLocalName, m_name.namespaceURI());
88 void Attr::createTextChild()
93 if (!value().isEmpty()) {
94 RefPtrWillBeRawPtr<Text> textNode = document().createTextNode(value().string());
96 // This does everything appendChild() would do in this situation (assuming m_ignoreChildrenChanged was set),
97 // but much more efficiently.
98 textNode->setParentOrShadowHostNode(this);
99 treeScope().adoptIfNeeded(*textNode);
100 setFirstChild(textNode.get());
101 setLastChild(textNode.get());
105 void Attr::setValue(const AtomicString& value)
107 EventQueueScope scope;
108 m_ignoreChildrenChanged++;
111 elementAttribute().setValue(value);
113 m_standaloneValueOrAttachedLocalName = value;
115 m_ignoreChildrenChanged--;
117 QualifiedName name = qualifiedName();
118 invalidateNodeListCachesInAncestors(&name, m_element);
121 void Attr::setValueInternal(const AtomicString& value)
124 m_element->willModifyAttribute(qualifiedName(), this->value(), value);
129 m_element->didModifyAttribute(qualifiedName(), value);
132 const AtomicString& Attr::valueForBindings() const
134 UseCounter::count(document(), UseCounter::AttrGetValue);
138 void Attr::setValueForBindings(const AtomicString& value)
140 UseCounter::count(document(), UseCounter::AttrSetValue);
142 UseCounter::count(document(), UseCounter::AttrSetValueWithElement);
143 setValueInternal(value);
146 void Attr::setNodeValue(const String& v)
148 // Attr uses AtomicString type for its value to save memory as there
149 // is duplication among Elements' attributes values.
150 setValueInternal(AtomicString(v));
153 PassRefPtrWillBeRawPtr<Node> Attr::cloneNode(bool /*deep*/)
155 RefPtrWillBeRawPtr<Attr> clone = adoptRefWillBeNoop(new Attr(document(), m_name, value()));
156 cloneChildNodes(clone.get());
157 return clone.release();
161 bool Attr::childTypeAllowed(NodeType type) const
163 return TEXT_NODE == type;
166 void Attr::childrenChanged(const ChildrenChange&)
168 if (m_ignoreChildrenChanged > 0)
171 QualifiedName name = qualifiedName();
172 invalidateNodeListCachesInAncestors(&name, m_element);
174 StringBuilder valueBuilder;
175 for (Node *n = firstChild(); n; n = n->nextSibling()) {
177 valueBuilder.append(toText(n)->data());
180 AtomicString newValue = valueBuilder.toAtomicString();
182 m_element->willModifyAttribute(qualifiedName(), value(), newValue);
185 elementAttribute().setValue(newValue);
187 m_standaloneValueOrAttachedLocalName = newValue;
190 m_element->attributeChanged(qualifiedName(), newValue);
193 const AtomicString& Attr::value() const
196 return m_element->getAttribute(qualifiedName());
197 return m_standaloneValueOrAttachedLocalName;
200 Attribute& Attr::elementAttribute()
203 ASSERT(m_element->elementData());
204 return *m_element->ensureUniqueElementData().attributes().find(qualifiedName());
207 void Attr::detachFromElementWithValue(const AtomicString& value)
210 m_standaloneValueOrAttachedLocalName = value;
214 void Attr::attachToElement(Element* element, const AtomicString& attachedLocalName)
218 m_standaloneValueOrAttachedLocalName = attachedLocalName;
221 void Attr::trace(Visitor* visitor)
223 visitor->trace(m_element);
224 ContainerNode::trace(visitor);