W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2006

Re: Is appendChild/insertBefore() supposed to raise exceptions for attributes?

From: Ray Whitmer <ray@xmission.com>
Date: Fri, 22 Dec 2006 05:42:57 -0700 (MST)
To: Frans Englich <frans.englich@telia.com>
cc: www-dom@w3.org
Message-ID: <Pine.LNX.4.64.0612220512120.12576@xmission.xmission.com>

On Fri, 22 Dec 2006, Frans Englich wrote:



> * appendChild() always raise an exception when being passed an attribute
> because "they are not actually child nodes of the element". The only way to
> add an Attr node to an element is to use Element::setAttributeNode().
>
> Which one to choose? Or perhaps a third alternative?

It is correct that, as attribute nodes are not child nodes, you should not
be able to insert or append using insert/append methods designed for
child nodes.

"Attr objects inherit the Node interface, but since they are not actually 
child nodes of the element they describe, the DOM does not consider them 
part of the document tree."

"HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow 
children of the type of the newChild node ..."

Of course, whenever you use an absolute word like "only", you just beg to
have me show you how complex DOM is with multiple ways to do anything.
You can always call getAttributes, which returns a NamedNodeMap, which has
its own methods allowing you to add and remove nodes.  So calling
setAttributeNode (or setAttributeNodeNS) is not the only way.

But insertBefore/appendChild does not insert attribute nodes, entity nodes,
notation nodes, document nodes, document fragment nodes, etc. nor does
it allow you to insert an element as a child of a text, CDATASection, PI,
or comment, or anything else not permitted in the definition of the 
hierarchy, however logical it may seem to someone not sticking to the 
definitions.

Ray Whitmer
Received on Friday, 22 December 2006 12:43:09 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:58 GMT