2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * (C) 2001 Peter Kelly (pmk@post.com)
7 * (C) 2001 Dirk Mueller (mueller@kde.org)
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.
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.
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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 #include "dom/dom_exception.h"
27 #include "dom/dom_node.h"
28 #include "xml/dom_textimpl.h"
29 #include "xml/dom_docimpl.h"
30 #include "xml/dom2_eventsimpl.h"
31 #include "xml/dom_elementimpl.h"
33 #include "khtml_part.h"
36 #include "html/htmlparser.h"
38 #include "rendering/render_canvas.h"
39 #include "misc/htmlhashes.h"
40 #include "css/css_valueimpl.h"
41 #include "css/css_stylesheetimpl.h"
42 #include "css/cssstyleselector.h"
43 #include "xml/dom_selection.h"
44 #include "xml/dom_xmlimpl.h"
46 #include <qtextstream.h>
50 using namespace khtml;
52 AttributeImpl* AttributeImpl::clone(bool) const
54 AttributeImpl* result = new AttributeImpl(m_id, _value);
55 result->setPrefix(_prefix);
59 void AttributeImpl::allocateImpl(ElementImpl* e) {
60 _impl = new AttrImpl(e, e->docPtr(), this);
63 AttrImpl::AttrImpl(ElementImpl* element, DocumentPtr* docPtr, AttributeImpl* a)
64 : NodeBaseImpl(docPtr),
68 assert(!m_attribute->_impl);
69 m_attribute->_impl = this;
76 assert(m_attribute->_impl == this);
77 m_attribute->_impl = 0;
81 DOMString AttrImpl::nodeName() const
83 return getDocument()->attrName(m_attribute->id());
86 unsigned short AttrImpl::nodeType() const
88 return Node::ATTRIBUTE_NODE;
91 DOMString AttrImpl::prefix() const
93 return m_attribute->prefix();
96 void AttrImpl::setPrefix(const DOMString &_prefix, int &exceptioncode )
98 checkSetPrefix(_prefix, exceptioncode);
102 m_attribute->setPrefix(_prefix.implementation());
105 DOMString AttrImpl::nodeValue() const {
106 return m_attribute->value();
109 void AttrImpl::setValue( const DOMString &v, int &exceptioncode )
113 // ### according to the DOM docs, we should create an unparsed Text child
115 // do not interprete entities in the string, its literal!
117 // NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly
119 exceptioncode = DOMException::NO_MODIFICATION_ALLOWED_ERR;
123 // ### what to do on 0 ?
125 exceptioncode = DOMException::DOMSTRING_SIZE_ERR;
129 m_attribute->setValue(v.implementation());
131 m_element->attributeChanged(m_attribute);
134 void AttrImpl::setNodeValue( const DOMString &v, int &exceptioncode )
137 // NO_MODIFICATION_ALLOWED_ERR: taken care of by setValue()
138 setValue(v, exceptioncode);
141 NodeImpl *AttrImpl::cloneNode ( bool /*deep*/)
143 return new AttrImpl(0, docPtr(), m_attribute->clone());
147 bool AttrImpl::childAllowed( NodeImpl *newChild )
152 return childTypeAllowed(newChild->nodeType());
155 bool AttrImpl::childTypeAllowed( unsigned short type )
158 case Node::TEXT_NODE:
159 case Node::ENTITY_REFERENCE_NODE:
167 DOMString AttrImpl::toString() const
171 result += nodeName();
173 // FIXME: substitute entities for any instances of " or ' --
174 // maybe easier to just use text value and ignore existing
177 if (firstChild() != NULL) {
180 for (NodeImpl *child = firstChild(); child != NULL; child = child->nextSibling()) {
181 result += child->toString();
190 // -------------------------------------------------------------------------
192 ElementImpl::ElementImpl(DocumentPtr *doc)
199 ElementImpl::~ElementImpl()
202 namedAttrMap->detachFromElement();
203 namedAttrMap->deref();
210 void ElementImpl::removeAttribute( NodeImpl::Id id, int &exceptioncode )
213 namedAttrMap->removeNamedItem(id, exceptioncode);
214 if (exceptioncode == DOMException::NOT_FOUND_ERR) {
220 void ElementImpl::setAttribute(NodeImpl::Id id, const DOMString &value)
222 int exceptioncode = 0;
223 setAttribute(id,value.implementation(),exceptioncode);
226 unsigned short ElementImpl::nodeType() const
228 return Node::ELEMENT_NODE;
231 const AtomicStringList* ElementImpl::getClassList() const
236 const AtomicString& ElementImpl::getIDAttribute() const
238 return namedAttrMap ? namedAttrMap->id() : nullAtom;
241 const AtomicString& ElementImpl::getAttribute(NodeImpl::Id id) const
244 AttributeImpl* a = namedAttrMap->getAttributeItem(id);
245 if (a) return a->value();
250 const AtomicString& ElementImpl::getAttributeNS(const DOMString &namespaceURI,
251 const DOMString &localName) const
253 NodeImpl::Id id = getDocument()->attrId(namespaceURI.implementation(),
254 localName.implementation(), true);
255 if (!id) return nullAtom;
256 return getAttribute(id);
259 void ElementImpl::setAttribute(NodeImpl::Id id, DOMStringImpl* value, int &exceptioncode )
261 // allocate attributemap if necessary
262 AttributeImpl* old = attributes(false)->getAttributeItem(id);
264 // NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly
265 if (namedAttrMap->isReadOnly()) {
266 exceptioncode = DOMException::NO_MODIFICATION_ALLOWED_ERR;
271 updateId(old ? old->value() : nullAtom, value);
275 namedAttrMap->removeAttribute(id);
276 else if (!old && value)
277 namedAttrMap->addAttribute(createAttribute(id, value));
278 else if (old && value) {
279 old->setValue(value);
280 attributeChanged(old);
284 AttributeImpl* ElementImpl::createAttribute(NodeImpl::Id id, DOMStringImpl* value)
286 return new AttributeImpl(id, value);
289 void ElementImpl::setAttributeMap( NamedAttrMapImpl* list )
291 // If setting the whole map changes the id attribute, we need to
294 AttributeImpl *oldId = namedAttrMap ? namedAttrMap->getAttributeItem(ATTR_ID) : 0;
295 AttributeImpl *newId = list ? list->getAttributeItem(ATTR_ID) : 0;
297 if (oldId || newId) {
298 updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);
302 namedAttrMap->deref();
308 namedAttrMap->element = this;
309 unsigned int len = namedAttrMap->length();
310 for(unsigned int i = 0; i < len; i++)
311 attributeChanged(namedAttrMap->attrs[i]);
315 bool ElementImpl::hasAttributes() const
317 return namedAttrMap && namedAttrMap->length() > 0;
320 NodeImpl *ElementImpl::cloneNode(bool deep)
322 // ### we loose the namespace here ... FIXME
324 ElementImpl *clone = getDocument()->createElement(tagName(), exceptioncode);
325 if (!clone) return 0;
329 *(static_cast<NamedAttrMapImpl*>(clone->attributes())) = *namedAttrMap;
332 cloneChildNodes(clone);
336 DOMString ElementImpl::nodeName() const
341 DOMString ElementImpl::tagName() const
343 DOMString tn = getDocument()->tagName(id());
346 return DOMString(m_prefix) + ":" + tn;
351 void ElementImpl::setPrefix( const DOMString &_prefix, int &exceptioncode )
353 checkSetPrefix(_prefix, exceptioncode);
359 m_prefix = _prefix.implementation();
364 void ElementImpl::createAttributeMap() const
366 namedAttrMap = new NamedAttrMapImpl(const_cast<ElementImpl*>(this));
370 bool ElementImpl::isURLAttribute(AttributeImpl *attr) const
376 void ElementImpl::defaultEventHandler(EventImpl *evt)
379 if (evt->id() == EventImpl::KEYPRESS_EVENT && isContentEditable()) {
380 KHTMLPart *part = getDocument()->part();
381 if (part && KWQ(part)->interceptEditingKeyEvent())
382 evt->setDefaultHandled();
385 NodeBaseImpl::defaultEventHandler(evt);
388 RenderStyle *ElementImpl::styleForRenderer(RenderObject *parentRenderer)
390 return getDocument()->styleSelector()->styleForElement(this);
393 RenderObject *ElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
395 if (getDocument()->documentElement() == this && style->display() == NONE) {
396 // Ignore display: none on root elements. Force a display of block in that case.
397 RenderBlock* result = new (arena) RenderBlock(this);
398 if (result) result->setStyle(style);
401 return RenderObject::createObject(this, style);
404 void ElementImpl::attach()
407 createRendererIfNeeded();
409 NodeBaseImpl::attach();
412 NamedAttrMapImpl *attrs = attributes(true);
414 AttributeImpl *idAttr = attrs->getAttributeItem(ATTR_ID);
415 if (idAttr && !idAttr->isNull()) {
416 updateId(nullAtom, idAttr->value());
422 void ElementImpl::detach()
425 NamedAttrMapImpl *attrs = attributes(true);
427 AttributeImpl *idAttr = attrs->getAttributeItem(ATTR_ID);
428 if (idAttr && !idAttr->isNull()) {
429 updateId(idAttr->value(), nullAtom);
434 NodeBaseImpl::detach();
437 void ElementImpl::recalcStyle( StyleChange change )
439 // ### should go away and be done in renderobject
440 RenderStyle* _style = m_render ? m_render->style() : 0;
441 bool hasParentRenderer = parent() ? parent()->renderer() : false;
446 case NoChange: debug = "NoChange";
448 case NoInherit: debug= "NoInherit";
450 case Inherit: debug = "Inherit";
452 case Force: debug = "Force";
455 qDebug("recalcStyle(%d: %s)[%p: %s]", change, debug, this, tagName().string().latin1());
457 if ( hasParentRenderer && (change >= Inherit || changed()) ) {
458 RenderStyle *newStyle = getDocument()->styleSelector()->styleForElement(this);
460 StyleChange ch = diff( _style, newStyle );
462 if (attached()) detach();
463 // ### Suboptimal. Style gets calculated again.
465 // attach recalulates the style for all children. No need to do it twice.
467 setHasChangedChild( false );
468 newStyle->deref(getDocument()->renderArena());
471 else if (ch != NoChange) {
472 if( m_render && newStyle ) {
473 //qDebug("--> setting style on render element bgcolor=%s", newStyle->backgroundColor().name().latin1());
474 m_render->setStyle(newStyle);
477 newStyle->deref(getDocument()->renderArena());
479 if ( change != Force) {
480 if (getDocument()->usesDescendantRules())
488 for (n = _first; n; n = n->nextSibling()) {
489 //qDebug(" (%p) calling recalcStyle on child %s/%p, change=%d", this, n, n->isElementNode() ? ((ElementImpl *)n)->tagName().string().latin1() : n->isTextNode() ? "text" : "unknown", change );
490 if ( change >= Inherit || n->isTextNode() ||
491 n->hasChangedChild() || n->changed() )
492 n->recalcStyle( change );
496 setHasChangedChild( false );
500 bool ElementImpl::childAllowed( NodeImpl *newChild )
502 if (!childTypeAllowed(newChild->nodeType()))
505 // For XML documents, we are non-validating and do not check against a DTD, even for HTML elements.
506 if (getDocument()->isHTMLDocument())
507 return checkChild(id(), newChild->id());
511 bool ElementImpl::childTypeAllowed( unsigned short type )
514 case Node::ELEMENT_NODE:
515 case Node::TEXT_NODE:
516 case Node::COMMENT_NODE:
517 case Node::PROCESSING_INSTRUCTION_NODE:
518 case Node::CDATA_SECTION_NODE:
519 case Node::ENTITY_REFERENCE_NODE:
527 void ElementImpl::dispatchAttrRemovalEvent(AttributeImpl *attr)
529 if (!getDocument()->hasListenerType(DocumentImpl::DOMATTRMODIFIED_LISTENER))
531 //int exceptioncode = 0;
532 // dispatchEvent(new MutationEventImpl(EventImpl::DOMATTRMODIFIED_EVENT,true,false,attr,attr->value(),
533 // attr->value(), getDocument()->attrName(attr->id()),MutationEvent::REMOVAL),exceptioncode);
536 void ElementImpl::dispatchAttrAdditionEvent(AttributeImpl *attr)
538 if (!getDocument()->hasListenerType(DocumentImpl::DOMATTRMODIFIED_LISTENER))
540 // int exceptioncode = 0;
541 // dispatchEvent(new MutationEventImpl(EventImpl::DOMATTRMODIFIED_EVENT,true,false,attr,attr->value(),
542 // attr->value(),getDocument()->attrName(attr->id()),MutationEvent::ADDITION),exceptioncode);
545 DOMString ElementImpl::openTagStartToString() const
547 DOMString result = DOMString("<") + tagName();
549 NamedAttrMapImpl *attrMap = attributes(true);
552 unsigned long numAttrs = attrMap->length();
553 for (unsigned long i = 0; i < numAttrs; i++) {
556 AttributeImpl *attribute = attrMap->attributeItem(i);
557 AttrImpl *attr = attribute->attrImpl();
560 result += attr->toString();
562 result += getDocument()->attrName(attribute->id());
563 if (!attribute->value().isNull()) {
565 // FIXME: substitute entities for any instances of " or '
566 result += attribute->value();
576 DOMString ElementImpl::toString() const
578 DOMString result = openTagStartToString();
580 if (hasChildNodes()) {
583 for (NodeImpl *child = firstChild(); child != NULL; child = child->nextSibling()) {
584 result += child->toString();
597 void ElementImpl::updateId(const AtomicString& oldId, const AtomicString& newId)
605 DocumentImpl* doc = getDocument();
606 if (!oldId.isEmpty())
607 doc->removeElementById(oldId, this);
608 if (!newId.isEmpty())
609 doc->addElementById(newId, this);
613 void ElementImpl::dump(QTextStream *stream, QString ind) const
616 for (uint i = 0; i < namedAttrMap->length(); i++) {
617 AttributeImpl *attr = namedAttrMap->attributeItem(i);
618 *stream << " " << DOMString(getDocument()->attrName(attr->id())).string().ascii()
619 << "=\"" << DOMString(attr->value()).string().ascii() << "\"";
623 NodeBaseImpl::dump(stream,ind);
628 void ElementImpl::formatForDebugger(char *buffer, unsigned length) const
634 if (s.length() > 0) {
638 s = getAttribute(ATTR_ID);
639 if (s.length() > 0) {
640 if (result.length() > 0)
646 s = getAttribute(ATTR_CLASS);
647 if (s.length() > 0) {
648 if (result.length() > 0)
654 strncpy(buffer, result.string().latin1(), length - 1);
658 // -------------------------------------------------------------------------
660 XMLElementImpl::XMLElementImpl(DocumentPtr *doc, DOMStringImpl *_tagName)
663 m_id = doc->document()->tagId(0 /* no namespace */, _tagName, false /* allocate */);
666 XMLElementImpl::XMLElementImpl(DocumentPtr *doc, DOMStringImpl *_qualifiedName, DOMStringImpl *_namespaceURI)
670 for (uint i = 0; i < _qualifiedName->l; ++i)
671 if (_qualifiedName->s[i] == ':') {
678 DOMStringImpl* localName = _qualifiedName->copy();
680 localName->remove(0,colonpos+1);
681 m_id = doc->document()->tagId(_namespaceURI, localName, false /* allocate */);
683 m_prefix = _qualifiedName->copy();
685 m_prefix->truncate(colonpos);
689 m_id = doc->document()->tagId(_namespaceURI, _qualifiedName, false /* allocate */);
694 XMLElementImpl::~XMLElementImpl()
698 DOMString XMLElementImpl::localName() const
700 return getDocument()->tagName(m_id);
704 NodeImpl *XMLElementImpl::cloneNode ( bool deep )
706 // ### we loose namespace here FIXME
707 // should pass id around
708 XMLElementImpl *clone = new XMLElementImpl(docPtr(), getDocument()->tagName(m_id).implementation());
713 *(static_cast<NamedAttrMapImpl*>(clone->attributes())) = *namedAttrMap;
716 cloneChildNodes(clone);
721 // -------------------------------------------------------------------------
723 NamedAttrMapImpl::NamedAttrMapImpl(ElementImpl *e)
730 NamedAttrMapImpl::~NamedAttrMapImpl()
732 NamedAttrMapImpl::clearAttributes(); // virtual method, so qualify just to be explicit
735 bool NamedAttrMapImpl::isHTMLAttributeMap() const
740 AttrImpl *NamedAttrMapImpl::getNamedItem ( NodeImpl::Id id ) const
742 AttributeImpl* a = getAttributeItem(id);
746 a->allocateImpl(element);
748 return a->attrImpl();
751 Node NamedAttrMapImpl::setNamedItem ( NodeImpl* arg, int &exceptioncode )
754 exceptioncode = DOMException::NOT_FOUND_ERR;
758 // NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
760 exceptioncode = DOMException::NO_MODIFICATION_ALLOWED_ERR;
764 // WRONG_DOCUMENT_ERR: Raised if arg was created from a different document than the one that created this map.
765 if (arg->getDocument() != element->getDocument()) {
766 exceptioncode = DOMException::WRONG_DOCUMENT_ERR;
770 // Not mentioned in spec: throw a HIERARCHY_REQUEST_ERROR if the user passes in a non-attribute node
771 if (!arg->isAttributeNode()) {
772 exceptioncode = DOMException::HIERARCHY_REQUEST_ERR;
775 AttrImpl *attr = static_cast<AttrImpl*>(arg);
777 AttributeImpl* a = attr->attrImpl();
778 AttributeImpl* old = getAttributeItem(a->id());
779 if (old == a) return arg; // we know about it already
781 // INUSE_ATTRIBUTE_ERR: Raised if arg is an Attr that is already an attribute of another Element object.
782 // The DOM user must explicitly clone Attr nodes to re-use them in other elements.
783 if (attr->ownerElement()) {
784 exceptioncode = DOMException::INUSE_ATTRIBUTE_ERR;
788 if (a->id() == ATTR_ID) {
789 element->updateId(old ? old->value() : nullAtom, a->value());
792 // ### slightly inefficient - resizes attribute array twice.
795 if (!old->attrImpl())
796 old->allocateImpl(element);
798 removeAttribute(a->id());
805 // The DOM2 spec doesn't say that removeAttribute[NS] throws NOT_FOUND_ERR
806 // if the attribute is not found, but at this level we have to throw NOT_FOUND_ERR
807 // because of removeNamedItem, removeNamedItemNS, and removeAttributeNode.
808 Node NamedAttrMapImpl::removeNamedItem ( NodeImpl::Id id, int &exceptioncode )
810 // ### should this really be raised when the attribute to remove isn't there at all?
811 // NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly
813 exceptioncode = DOMException::NO_MODIFICATION_ALLOWED_ERR;
817 AttributeImpl* a = getAttributeItem(id);
819 exceptioncode = DOMException::NOT_FOUND_ERR;
823 if (!a->attrImpl()) a->allocateImpl(element);
824 Node r(a->attrImpl());
827 element->updateId(a->value(), nullAtom);
834 AttrImpl *NamedAttrMapImpl::item ( unsigned long index ) const
839 if (!attrs[index]->attrImpl())
840 attrs[index]->allocateImpl(element);
842 return attrs[index]->attrImpl();
845 AttributeImpl* NamedAttrMapImpl::getAttributeItem(NodeImpl::Id id) const
847 bool matchAnyNamespace = (namespacePart(id) == anyNamespace);
848 for (unsigned long i = 0; i < len; ++i) {
849 if (attrs[i]->id() == id)
851 else if (matchAnyNamespace) {
852 if (localNamePart(attrs[i]->id()) == localNamePart(id))
859 NodeImpl::Id NamedAttrMapImpl::mapId(const DOMString& namespaceURI,
860 const DOMString& localName, bool readonly)
863 if (!element) return 0;
864 return element->getDocument()->attrId(namespaceURI.implementation(),
865 localName.implementation(), readonly);
868 void NamedAttrMapImpl::clearAttributes()
872 for (i = 0; i < len; i++) {
874 attrs[i]->_impl->m_element = 0;
883 void NamedAttrMapImpl::detachFromElement()
885 // we allow a NamedAttrMapImpl w/o an element in case someone still has a reference
886 // to if after the element gets deleted - but the map is now invalid
891 NamedAttrMapImpl& NamedAttrMapImpl::operator=(const NamedAttrMapImpl& other)
893 // clone all attributes in the other map, but attach to our element
894 if (!element) return *this;
896 // If assigning the map changes the id attribute, we need to call
899 AttributeImpl *oldId = getAttributeItem(ATTR_ID);
900 AttributeImpl *newId = other.getAttributeItem(ATTR_ID);
902 if (oldId || newId) {
903 element->updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);
908 attrs = new AttributeImpl* [len];
910 // first initialize attrs vector, then call attributeChanged on it
911 // this allows attributeChanged to use getAttribute
912 for (uint i = 0; i < len; i++) {
913 attrs[i] = other.attrs[i]->clone();
917 // FIXME: This is wasteful. The class list could be preserved on a copy, and we
918 // wouldn't have to waste time reparsing the attribute.
919 // The derived class, HTMLNamedAttrMapImpl, which manages a parsed class list for the CLASS attribute,
920 // will update its member variable when parse attribute is called.
921 for(uint i = 0; i < len; i++)
922 element->attributeChanged(attrs[i], true);
927 void NamedAttrMapImpl::addAttribute(AttributeImpl *attr)
929 // Add the attribute to the list
930 AttributeImpl **newAttrs = new AttributeImpl* [len+1];
932 for (uint i = 0; i < len; i++)
933 newAttrs[i] = attrs[i];
940 AttrImpl * const attrImpl = attr->_impl;
942 attrImpl->m_element = element;
944 // Notify the element that the attribute has been added, and dispatch appropriate mutation events
945 // Note that element may be null here if we are called from insertAttr() during parsing
947 element->attributeChanged(attr);
948 element->dispatchAttrAdditionEvent(attr);
949 element->dispatchSubtreeModifiedEvent();
953 void NamedAttrMapImpl::removeAttribute(NodeImpl::Id id)
955 unsigned long index = len+1;
956 for (unsigned long i = 0; i < len; ++i)
957 if (attrs[i]->id() == id) {
962 if (index >= len) return;
964 // Remove the attribute from the list
965 AttributeImpl* attr = attrs[index];
966 if (attrs[index]->_impl)
967 attrs[index]->_impl->m_element = 0;
974 AttributeImpl **newAttrs = new AttributeImpl* [len-1];
976 for (i = 0; i < uint(index); i++)
977 newAttrs[i] = attrs[i];
980 newAttrs[i] = attrs[i+1];
985 // Notify the element that the attribute has been removed
986 // dispatch appropriate mutation events
987 if (element && !attr->_value.isNull()) {
988 AtomicString value = attr->_value;
989 attr->_value = nullAtom;
990 element->attributeChanged(attr);
991 attr->_value = value;
994 element->dispatchAttrRemovalEvent(attr);
995 element->dispatchSubtreeModifiedEvent();