Browse Source

Update Xml.addChild() to behave similarly to 3.1.3

If it's already present as a child, remove and re-add, rather than ignore.
Jason O'Neil 10 years ago
parent
commit
4f36b96e5e
2 changed files with 8 additions and 5 deletions
  1. 3 5
      std/Xml.hx
  2. 5 0
      tests/unit/src/unit/issues/Issue4094.hx

+ 3 - 5
std/Xml.hx

@@ -266,14 +266,12 @@ class Xml {
 	/**
 		Adds a child node to the Document or Element.
 		A child node can only be inside one given parent node, which is indicated by the [parent] property.
-		If the child is already inside this Document or Element, there will be no effect.
+		If the child is already inside this Document or Element, it will be moved to the last position among the Document or Element's children.
 		If the child node was previously inside a different node, it will be moved to this Document or Element.
 	**/
 	public function addChild( x : Xml ) : Void {
 		ensureElementType();
-		if (x.parent == this) {
-			return;
-		} else if (x.parent != null) {
+		if (x.parent != null) {
 			x.parent.removeChild(x);
 		}
 		children.push(x);
@@ -296,7 +294,7 @@ class Xml {
 	/**
 		Inserts a child at the given position among the other childs.
 		A child node can only be inside one given parent node, which is indicated by the [parent] property.
-		If the child is already inside this Document or Element, it will be removed and added at the new position.
+		If the child is already inside this Document or Element, it will be moved to the new position among the Document or Element's children.
 		If the child node was previously inside a different node, it will be moved to this Document or Element.
 	**/
 	public function insertChild( x : Xml, pos : Int ) : Void {

+ 5 - 0
tests/unit/src/unit/issues/Issue4094.hx

@@ -38,5 +38,10 @@ class Issue4094 extends Test {
         eq( comment.parent, div );
         eq( innerDiv.parent, div );
         eq( divText.parent, div );
+
+        // Test addChild() moves a current child to the end.
+
+        div.addChild( span );
+        eq( div.toString(), '<div>Text1<!--Comment--><div/>Div<span>Span</span></div>' );
     }
 }