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 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.
28 #include "ExceptionCode.h"
33 Attr::Attr(Element* element, Document* docPtr, PassRefPtr<Attribute> a)
34 : ContainerNode(docPtr),
37 m_ignoreChildrenChanged(0)
39 ASSERT(!m_attribute->attr());
40 m_attribute->m_impl = this;
41 m_attrWasSpecifiedOrElementHasRareData = true;
46 ASSERT(m_attribute->attr() == this);
47 m_attribute->m_impl = 0;
50 void Attr::createTextChild()
53 if (!m_attribute->value().isEmpty()) {
54 RefPtr<Text> textNode = document()->createTextNode(m_attribute->value().string());
56 // This does everything appendChild() would do in this situation (assuming m_ignoreChildrenChanged was set),
57 // but much more efficiently.
58 textNode->setParent(this);
59 setFirstChild(textNode.get());
60 setLastChild(textNode.get());
64 String Attr::nodeName() const
69 Node::NodeType Attr::nodeType() const
71 return ATTRIBUTE_NODE;
74 const AtomicString& Attr::localName() const
76 return m_attribute->localName();
79 const AtomicString& Attr::namespaceURI() const
81 return m_attribute->namespaceURI();
84 const AtomicString& Attr::prefix() const
86 return m_attribute->prefix();
89 void Attr::setPrefix(const AtomicString &_prefix, ExceptionCode& ec)
92 checkSetPrefix(_prefix, ec);
96 m_attribute->setPrefix(_prefix);
99 String Attr::nodeValue() const
104 void Attr::setValue( const String& v, ExceptionCode& ec)
108 // do not interprete entities in the string, its literal!
110 // NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly
111 if (isReadOnlyNode()) {
112 ec = NO_MODIFICATION_ALLOWED_ERR;
117 m_ignoreChildrenChanged++;
119 appendChild(document()->createTextNode(v), e);
120 m_ignoreChildrenChanged--;
122 m_attribute->setValue(v.impl());
124 m_element->attributeChanged(m_attribute.get());
127 void Attr::setNodeValue(const String& v, ExceptionCode& ec)
129 // NO_MODIFICATION_ALLOWED_ERR: taken care of by setValue()
133 PassRefPtr<Node> Attr::cloneNode(bool /*deep*/)
135 RefPtr<Attr> clone = new Attr(0, document(), m_attribute->clone());
136 cloneChildNodes(clone.get());
137 return clone.release();
141 bool Attr::childTypeAllowed(NodeType type)
145 case ENTITY_REFERENCE_NODE:
152 void Attr::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
154 if (m_ignoreChildrenChanged > 0)
157 Node::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
159 // FIXME: We should include entity references in the value
162 for (Node *n = firstChild(); n; n = n->nextSibling()) {
164 val += static_cast<Text *>(n)->data();
167 m_attribute->setValue(val.impl());
169 m_element->attributeChanged(m_attribute.get());
172 String Attr::toString() const
176 result += nodeName();
178 // FIXME: substitute entities for any instances of " or ' --
179 // maybe easier to just use text value and ignore existing
182 if (firstChild() != NULL) {
185 for (Node *child = firstChild(); child != NULL; child = child->nextSibling()) {
186 result += child->toString();