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/v8/ExceptionState.h"
27 #include "bindings/v8/ExceptionStatePlaceholder.h"
28 #include "core/dom/Element.h"
29 #include "core/dom/Text.h"
30 #include "core/events/ScopedEventQueue.h"
31 #include "core/frame/UseCounter.h"
32 #include "wtf/text/AtomicString.h"
33 #include "wtf/text/StringBuilder.h"
37 using namespace HTMLNames;
39 Attr::Attr(Element& element, const QualifiedName& name)
40 : ContainerNode(&element.document())
43 , m_ignoreChildrenChanged(0)
45 ScriptWrappable::init(this);
48 Attr::Attr(Document& document, const QualifiedName& name, const AtomicString& standaloneValue)
49 : ContainerNode(&document)
52 , m_standaloneValue(standaloneValue)
53 , m_ignoreChildrenChanged(0)
55 ScriptWrappable::init(this);
58 PassRefPtr<Attr> Attr::create(Element& element, const QualifiedName& name)
60 RefPtr<Attr> attr = adoptRef(new Attr(element, name));
61 attr->createTextChild();
62 return attr.release();
65 PassRefPtr<Attr> Attr::create(Document& document, const QualifiedName& name, const AtomicString& value)
67 RefPtr<Attr> attr = adoptRef(new Attr(document, name, value));
68 attr->createTextChild();
69 return attr.release();
76 void Attr::createTextChild()
81 if (!value().isEmpty()) {
82 RefPtr<Text> textNode = document().createTextNode(value().string());
84 // This does everything appendChild() would do in this situation (assuming m_ignoreChildrenChanged was set),
85 // but much more efficiently.
86 textNode->setParentOrShadowHostNode(this);
87 treeScope().adoptIfNeeded(*textNode);
88 setFirstChild(textNode.get());
89 setLastChild(textNode.get());
93 void Attr::setValue(const AtomicString& value)
95 EventQueueScope scope;
96 m_ignoreChildrenChanged++;
99 elementAttribute().setValue(value);
101 m_standaloneValue = value;
103 m_ignoreChildrenChanged--;
105 invalidateNodeListCachesInAncestors(&m_name, m_element);
108 void Attr::setValueInternal(const AtomicString& value)
111 m_element->willModifyAttribute(qualifiedName(), this->value(), value);
116 m_element->didModifyAttribute(qualifiedName(), value);
119 const AtomicString& Attr::valueForBindings() const
121 UseCounter::count(document(), UseCounter::AttrGetValue);
125 void Attr::setValueForBindings(const AtomicString& value)
127 UseCounter::count(document(), UseCounter::AttrSetValue);
129 UseCounter::count(document(), UseCounter::AttrSetValueWithElement);
130 setValueInternal(value);
133 void Attr::setNodeValue(const String& v)
135 // Attr uses AtomicString type for its value to save memory as there
136 // is duplication among Elements' attributes values.
137 setValueInternal(AtomicString(v));
140 PassRefPtr<Node> Attr::cloneNode(bool /*deep*/)
142 RefPtr<Attr> clone = adoptRef(new Attr(document(), qualifiedName(), value()));
143 cloneChildNodes(clone.get());
144 return clone.release();
148 bool Attr::childTypeAllowed(NodeType type) const
150 return TEXT_NODE == type;
153 void Attr::childrenChanged(bool, Node*, Node*, int)
155 if (m_ignoreChildrenChanged > 0)
158 invalidateNodeListCachesInAncestors(&qualifiedName(), m_element);
160 StringBuilder valueBuilder;
161 for (Node *n = firstChild(); n; n = n->nextSibling()) {
163 valueBuilder.append(toText(n)->data());
166 AtomicString newValue = valueBuilder.toAtomicString();
168 m_element->willModifyAttribute(qualifiedName(), value(), newValue);
171 elementAttribute().setValue(newValue);
173 m_standaloneValue = newValue;
176 m_element->attributeChanged(qualifiedName(), newValue);
179 const AtomicString& Attr::value() const
182 return m_element->getAttribute(qualifiedName());
183 return m_standaloneValue;
186 Attribute& Attr::elementAttribute()
189 ASSERT(m_element->elementData());
190 return *m_element->ensureUniqueElementData().getAttributeItem(qualifiedName());
193 void Attr::detachFromElementWithValue(const AtomicString& value)
196 ASSERT(m_standaloneValue.isNull());
197 m_standaloneValue = value;
201 void Attr::attachToElement(Element* element)
205 m_standaloneValue = nullAtom;