The default bug view has changed. See this FAQ.

Copying nodes in an XML tree removes the source node

UNCONFIRMED
Unassigned

Status

Rhino
E4X
--
major
UNCONFIRMED
7 years ago
7 years ago

People

(Reporter: Alberto Saez Torres, Unassigned)

Tracking

Details

Attachments

(1 attachment)

(Reporter)

Description

7 years ago
User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; es-ES; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Build Identifier: rhino1_7R3pre

When copying nodes in a node tree, removes the original Name.

For example, for the next code:

var tmp =<xml>
   <group>Group One</group>
   <group>Group Two</group>
   <group>Group Three</group>
</xml>;

tmp.group[0]=tmp.group[2];

The result XML of "tmp" is:

<xml>
   <group>Group Three</group>
   <group>Group Two</group>
</xml>

The 3ยบ "group" element has been removed after.



Reproducible: Always

Actual Results:  
<xml>
   <group>Group Three</group>
   <group>Group Two</group>
</xml>

Expected Results:  
tmp result as seeing in spiderMonkey

<xml>
   <group>Group Three</group>
   <group>Group Two</group>
   <group>Group Three</group>
</xml>

The problem is related to the function of XmlNode 

void replaceWith(XmlNode other) {
  Node replacement = other.dom;
  if (replacement.getOwnerDocument() != this.dom.getOwnerDocument()) {
     replacement = this.dom.getOwnerDocument().importNode(replacement, true);
  }
  this.dom.getParentNode().replaceChild(replacement, this.dom);
}

When the function is aplied to a node of the same xml tree, the function importNode has the following behaviour:

Node replaceChild(Node newChild, Node oldChild)  throws DOMException

Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node. 
If newChild is a DocumentFragment object, oldChild is replaced by all of the DocumentFragment children, which are inserted in the same order. If the newChild is already in the tree, it is first removed.

So, my proposal is to change the method to the next:

void replaceWith(XmlNode other) {
   Node replacement = this.dom.getOwnerDocument().importNode(other.dom, true);
   this.dom.getParentNode().replaceChild(replacement, this.dom);
}
(Reporter)

Comment 1

7 years ago
Created attachment 446843 [details] [diff] [review]
Avoid the use of replaceChild method
You need to log in before you can comment on or make changes to this bug.