You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Michael Gratton <mi...@vee.net> on 2003/08/21 06:39:46 UTC

importNode() / adoptNode() breaks getElementById()

Hi guys,

I'm looking for some feedback on the correct way to fix a bug with 
importNode() and adoptNode(). Let me explain the bug first.

I want to copy or move some elemets from one document (docA) into 
another (docB). The elements in question have an IDREF attribute set on 
them, and using Document.getElementbyId() on docA works as expected. 
However after importing the elements into docB using either importNode() 
or adoptNode(), calling getElementbyId() on DocB fails to return the 
imported/adopted elements.

The importNode() method copies attributes by constructing new instances 
of the attribute node with Document.createAttribiute[NS](). Hence the 
NodeImpl.ID flag on the attribute is not preserved and so when 
importNode() calls setNodeValue() to set the copied attribute's value, 
AttrImpl.setNodeValue() does not end up calling putIdentifier() on it's 
ownerDocument as it should.

Would a correct fix for this be to copy NodeImpl.flags across to the new 
attribute when the source attribute is also an instance of NodeImpl? Or 
perhaps examine isIdAttribute() on the original and set it accordingly 
on the copy?

The adoptNode() method does preserve the NodeImpl.ID flag, but does not 
do anything to cause the identifier to be removed from the old owner 
document and be added to the new owner document.

A potential fix for this would be in AttrImpl.setOwnerDocument(), when 
the NodeImpl.ID flag is set, to call removeIdentifier() on the existing 
owner document, if any, and to call putIdentifier() on the new owner 
document, if any.

Thanks for your comments!

Mike.

-- 
Mike Gratton <ja...@jabber.vee.net> <http://web.vee.net/>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


RE: importNode() / adoptNode() breaks getElementById()

Posted by Arnaud Le Hors <le...@us.ibm.com>.
Just for the record, ideally as we unregister an element with an ID, we'd
register any other element with the same ID we have in that document. One
could probably even claim that what's we'd have to do to be compliant.

Indeed, the spec says that when there are several elements with the same ID
in a document which one getElementById returns is undefined (meaning
implementation specific). But if you remove enough of these elements so that
you only have one, getElementById should (must really) return that element.

However this would require much more work and I'm not proposing that we do
this. I just wanted to mention it, so that we can label this has a "known
bug". Which is slightly better. :-)
--
Arnaud  Le Hors - IBM, XML Standards Strategy Group / W3C AC Rep.


> -----Original Message-----
> From: Arnaud Le Hors [mailto:lehors@us.ibm.com]
> Sent: Thursday, August 21, 2003 7:38 AM
> To: xerces-j-dev@xml.apache.org
> Subject: RE: importNode() / adoptNode() breaks getElementById()
>
>
> I had thought about this and I guess never got to fix it.
> Copying all the flags isn't the right thing to do. On the other
> hand the two
> other proposals sound good to me.
> --
> Arnaud  Le Hors - IBM, XML Standards Strategy Group / W3C AC Rep.
>
>
> > -----Original Message-----
> > From: Michael Gratton [mailto:mike@vee.net]
> > Sent: Wednesday, August 20, 2003 9:40 PM
> > To: xerces-j-dev@xml.apache.org
> > Subject: importNode() / adoptNode() breaks getElementById()
> >
> >
> >
> > Hi guys,
> >
> > I'm looking for some feedback on the correct way to fix a bug with
> > importNode() and adoptNode(). Let me explain the bug first.
> >
> > I want to copy or move some elemets from one document (docA) into
> > another (docB). The elements in question have an IDREF attribute set on
> > them, and using Document.getElementbyId() on docA works as expected.
> > However after importing the elements into docB using either importNode()
> > or adoptNode(), calling getElementbyId() on DocB fails to return the
> > imported/adopted elements.
> >
> > The importNode() method copies attributes by constructing new instances
> > of the attribute node with Document.createAttribiute[NS](). Hence the
> > NodeImpl.ID flag on the attribute is not preserved and so when
> > importNode() calls setNodeValue() to set the copied attribute's value,
> > AttrImpl.setNodeValue() does not end up calling putIdentifier() on it's
> > ownerDocument as it should.
> >
> > Would a correct fix for this be to copy NodeImpl.flags across to the new
> > attribute when the source attribute is also an instance of NodeImpl? Or
> > perhaps examine isIdAttribute() on the original and set it accordingly
> > on the copy?
> >
> > The adoptNode() method does preserve the NodeImpl.ID flag, but does not
> > do anything to cause the identifier to be removed from the old owner
> > document and be added to the new owner document.
> >
> > A potential fix for this would be in AttrImpl.setOwnerDocument(), when
> > the NodeImpl.ID flag is set, to call removeIdentifier() on the existing
> > owner document, if any, and to call putIdentifier() on the new owner
> > document, if any.
> >
> > Thanks for your comments!
> >
> > Mike.
> >
> > --
> > Mike Gratton <ja...@jabber.vee.net> <http://web.vee.net/>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


RE: importNode() / adoptNode() breaks getElementById()

Posted by Arnaud Le Hors <le...@us.ibm.com>.
I had thought about this and I guess never got to fix it.
Copying all the flags isn't the right thing to do. On the other hand the two
other proposals sound good to me.
--
Arnaud  Le Hors - IBM, XML Standards Strategy Group / W3C AC Rep.


> -----Original Message-----
> From: Michael Gratton [mailto:mike@vee.net]
> Sent: Wednesday, August 20, 2003 9:40 PM
> To: xerces-j-dev@xml.apache.org
> Subject: importNode() / adoptNode() breaks getElementById()
>
>
>
> Hi guys,
>
> I'm looking for some feedback on the correct way to fix a bug with
> importNode() and adoptNode(). Let me explain the bug first.
>
> I want to copy or move some elemets from one document (docA) into
> another (docB). The elements in question have an IDREF attribute set on
> them, and using Document.getElementbyId() on docA works as expected.
> However after importing the elements into docB using either importNode()
> or adoptNode(), calling getElementbyId() on DocB fails to return the
> imported/adopted elements.
>
> The importNode() method copies attributes by constructing new instances
> of the attribute node with Document.createAttribiute[NS](). Hence the
> NodeImpl.ID flag on the attribute is not preserved and so when
> importNode() calls setNodeValue() to set the copied attribute's value,
> AttrImpl.setNodeValue() does not end up calling putIdentifier() on it's
> ownerDocument as it should.
>
> Would a correct fix for this be to copy NodeImpl.flags across to the new
> attribute when the source attribute is also an instance of NodeImpl? Or
> perhaps examine isIdAttribute() on the original and set it accordingly
> on the copy?
>
> The adoptNode() method does preserve the NodeImpl.ID flag, but does not
> do anything to cause the identifier to be removed from the old owner
> document and be added to the new owner document.
>
> A potential fix for this would be in AttrImpl.setOwnerDocument(), when
> the NodeImpl.ID flag is set, to call removeIdentifier() on the existing
> owner document, if any, and to call putIdentifier() on the new owner
> document, if any.
>
> Thanks for your comments!
>
> Mike.
>
> --
> Mike Gratton <ja...@jabber.vee.net> <http://web.vee.net/>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org