Closed Bug 567484 Opened 14 years ago Closed 5 years ago

Copying nodes in an XML tree removes the source node

Categories

(Rhino Graveyard :: E4X, defect)

defect
Not set
major

Tracking

(Not tracked)

RESOLVED INACTIVE

People

(Reporter: albersato, Unassigned)

Details

Attachments

(1 file)

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);
}

Closing. Bug management is now done here:
https://github.com/mozilla/rhino

Status: UNCONFIRMED → RESOLVED
Closed: 5 years ago
Resolution: --- → INACTIVE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: