Bug 105586

Summary: appendChild for tr element to table element fails with DOM Exception HIERARCHY_REQUEST_ERR
Product: [Applications] konqueror Reporter: Jos van den Oever <jos>
Component: kjsAssignee: Konqueror Developers <konq-bugs>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
Latest Commit: Version Fixed In:
Attachments: demonstration of the javascript

Description Jos van den Oever 2005-05-13 10:13:07 UTC
Version:            (using KDE KDE 3.4.0)
Installed from:    SuSE RPMs

Calling table.appendChild(tr) where table is a <table> element and tr is a <tr> element fails with error HIERARCHY_REQUEST_ERR. This code works in Mozilla and Opera.

What elements does <table> accept? Is there a workaround?
Comment 1 Jos van den Oever 2005-05-13 10:14:50 UTC
Created attachment 11016 [details]
demonstration of the javascript
Comment 2 Jos van den Oever 2005-05-13 10:36:26 UTC
Ok, I just found a workaround. Instead of writing:
  table.appendChild(tr);
One can write:
  var tbody = document.createElement('tbody');
  table.appendChild(tbody);
  var tr = document.createElement('tr');
  tbody.appendChild(tr);

I'm not sure what the spec says about this, so I'm not really sure if this is a bug anymore.
Comment 3 Jos van den Oever 2005-05-13 10:38:06 UTC
Since a table can have more than one tbody element, the Konqueror implementation is very strict, but not wrong. Therefore, the bug has been resolved.
Comment 4 David Faure 2005-05-24 01:22:48 UTC
Actually it's wrong that something, which works in Mozilla and IE, doesn't work in Konq (KHTML). Someone else reported this on IRC, and I fixed it.
Comment 5 David Faure 2005-05-24 01:24:42 UTC
SVN commit 417591 by dfaure:

Allow javascript to insert a TR directly into a TABLE, creating a TBODY section if needed
BUG: 105586


 M  +5 -0      ChangeLog  
 M  +13 -0     html/html_tableimpl.cpp  
 M  +1 -0      html/html_tableimpl.h  


--- trunk/KDE/kdelibs/khtml/ChangeLog #417590:417591
@@ -1,3 +1,8 @@
+2005-05-24  David Faure  <faure@kde.org>
+
+        * html/html_tableimpl.cpp (appendChild): Allow javascript to insert
+        a TR directly into a TABLE, creating a TBODY section if needed (bug #105586).
+
 2005-05-21  Harri Porten  <porten@kde.org>
 
 	* html/html_documentimpl.cpp: emit onload event even if the
--- trunk/KDE/kdelibs/khtml/html/html_tableimpl.cpp #417590:417591
@@ -307,6 +307,19 @@
         exceptioncode = DOMException::INDEX_SIZE_ERR;
 }
 
+NodeImpl *HTMLTableElementImpl::appendChild(NodeImpl *child, int &exceptioncode)
+{
+    if(child->id() == ID_TR) { // #105586, allow to insert a TR inside a TABLE, creation section as needed
+        // See insertRow
+        if (!firstBody && !head && !foot && !hasChildNodes())
+            setTBody( new HTMLTableSectionElementImpl(docPtr(), ID_TBODY, true /* implicit */) );
+        Q_ASSERT( firstBody );
+        if (firstBody)
+            return firstBody->appendChild( child, exceptioncode );
+    }
+    return HTMLElementImpl::appendChild( child, exceptioncode );
+}
+
 NodeImpl *HTMLTableElementImpl::addChild(NodeImpl *child)
 {
 #ifdef DEBUG_LAYOUT
--- trunk/KDE/kdelibs/khtml/html/html_tableimpl.h #417590:417591
@@ -95,6 +95,7 @@
 
     // overrides
     virtual NodeImpl *addChild(NodeImpl *child);
+    virtual NodeImpl *appendChild( NodeImpl *newChild, int &exceptioncode );
     virtual void parseAttribute(AttributeImpl *attr);
     virtual void attach();