You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by gr...@apache.org on 2020/04/15 09:46:57 UTC

[royale-asjs] 05/05: XML fixes for appendChild when passed an XMLList. Tests for what happens with different node types from source. Tests include some variations observed in flash player (browser vs. standalone).

This is an automated email from the ASF dual-hosted git repository.

gregdove pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git

commit dfa105d8993cbc1df37d5cc16cf59cfe5cbd8df6
Author: greg-dove <gr...@gmail.com>
AuthorDate: Wed Apr 15 21:46:11 2020 +1200

    XML fixes for appendChild when passed an XMLList. Tests for what happens with different node types from source.
    Tests include some variations observed in flash player (browser vs. standalone).
---
 frameworks/projects/XML/src/main/royale/XML.as     |  46 ++++++--
 .../royale/flexUnitTests/xml/XMLNamespaceTest.as   |  17 ++-
 .../flexUnitTests/xml/XMLTesterGeneralTest.as      | 119 +++++++++++++++++++++
 3 files changed, 172 insertions(+), 10 deletions(-)

diff --git a/frameworks/projects/XML/src/main/royale/XML.as b/frameworks/projects/XML/src/main/royale/XML.as
index f876507..de0278c 100644
--- a/frameworks/projects/XML/src/main/royale/XML.as
+++ b/frameworks/projects/XML/src/main/royale/XML.as
@@ -918,22 +918,48 @@ package
 			//normalize();
 			return this;
 		}
-		
+
+		/**
+		 *
+		 * @royaleignorecoercion XML
+		 */
 		private function appendChildInternal(child:*):void
 		{
+			var kind:String;
+			var alreadyPresent:int
+			var children:Array = getChildren();
 			if(child is XMLList)
 			{
 				var len:int = child.length();
 				for(var i:int=0; i<len; i++)
 				{
-					appendChildInternal(child[0]);
+					//reproduce swf behavior... leaves a phantom child in the source
+					var childItem:XML = child[i] as XML;
+					kind = childItem.getNodeRef();
+					if (kind == ATTRIBUTE) {
+						var name:String = childItem.localName();
+						var content:String = '<'+name+'>'+childItem.toString()+'</'+name+'>';
+						childItem = new XML(content)
+					} else {
+						alreadyPresent = children.indexOf(childItem);
+						if (alreadyPresent != -1) children.splice(alreadyPresent, 1);
+					}
+					childItem.setParent(this, true);
+					children.push(childItem);
 				}
 			}
 			else
 			{
 				assertType(child,XML,"Type must be XML");
-				child.setParent(this);
-				getChildren().push(child);
+				kind = child.getNodeRef();
+				if (kind == ATTRIBUTE) {
+					child = new XML(child.toString());
+				} else {
+					alreadyPresent = children.indexOf(child);
+					if (alreadyPresent != -1) children.splice(alreadyPresent, 1);
+				}
+				(child as XML).setParent(this);
+				children.push(child);
 			}
 		}
 		
@@ -2754,13 +2780,17 @@ package
 			}
 			
 		}
-		
-		public function setParent(parent:XML):void
+
+		/**
+		 * @private
+		 * @royalesuppressexport
+		 */
+		public function setParent(parent:XML, keep:Boolean=false):void
 		{
 			if(parent == _parent)
 				return;
 			var oldParent:XML = _parent;
-			if(oldParent)
+			if(oldParent && !keep)
 				oldParent.removeChild(this);
 			_parent = parent;
 		}
@@ -2909,7 +2939,7 @@ package
 			
 			if(str.indexOf("@") == 0)
 				return toAttributeName(name);
-			
+
 			return new QName(str);
 		}
 		
diff --git a/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLNamespaceTest.as b/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLNamespaceTest.as
index f4dbc8a..dc970c7 100644
--- a/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLNamespaceTest.as
+++ b/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLNamespaceTest.as
@@ -23,6 +23,10 @@ package flexUnitTests.xml
     import flexUnitTests.xml.support.NamespaceTest;
     
     import org.apache.royale.test.asserts.*;
+
+    COMPILE::SWF{
+        import flash.system.Capabilities;
+    }
     
    // import testshim.RoyaleUnitTestRunner;
     
@@ -101,6 +105,15 @@ package flexUnitTests.xml
             //something for js, indicating javascript 'playerversion' is consistent with more recent flash player versions:
             return 30;
         }
+
+        public function getPlayerType():String{
+            COMPILE::SWF{
+                return Capabilities.playerType;
+            }
+            COMPILE::JS{
+                return 'Browser';
+            }
+        }
         
     
         [Test]
@@ -114,12 +127,12 @@ package flexUnitTests.xml
             //account for what appears to be a player bug in a range of player versions (not verified on Mac)
             // Javascript conforms to the latest swf behavior
             
-            var permitEmptyString:Boolean  = /*playerVersion >= 11.2 &&*/ playerVersion <= 20.0;
+            var permitEmptyString:Boolean  = /*playerVersion >= 11.2 &&*/ playerVersion <= 20.0 || getPlayerType() == 'StandAlone';
             var prefix:* = ns.prefix;
             var testIsOK:Boolean = permitEmptyString ? prefix === '' || prefix === undefined : prefix === undefined;
             
             //assertStrictlyEquals(ns.prefix, undefined, 'unexpected prefix value ');
-            assertTrue(testIsOK, 'unexpected prefix value ');
+            assertTrue(testIsOK, playerVersion+' unexpected prefix value :'+prefix);
             
             var uri:String = ns.uri;
             testIsOK = permitEmptyString ? uri == '' || uri == 'test' : uri == 'test';
diff --git a/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLTesterGeneralTest.as b/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLTesterGeneralTest.as
index 0a7b9a9..fd6c175 100644
--- a/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLTesterGeneralTest.as
+++ b/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLTesterGeneralTest.as
@@ -1155,6 +1155,125 @@ package flexUnitTests.xml
             assertEquals(xml.@myAtt1,'myAttTestVal', 'unexpected attributes value');
 
         }
+
+
+        [Test]
+        public function testAppendChildContentTransfer():void{
+            var source:XML = <source att='attribute'><dog/><cat/><rat/><pig/><cow/><hen/></source>;
+            var sourceChildren:XMLList = source.children();
+            var attList:XMLList = source.@att;
+            var att:XML = attList[0];
+            var orig1:XML = sourceChildren[0];
+            var dest:XML = <dest/>;
+
+            assertTrue(orig1.parent() === source, 'unexpected parent');
+            assertTrue(att.parent() === source, 'unexpected parent');
+            dest.appendChild(sourceChildren);
+            //element was re-parented:
+            assertFalse(orig1.parent() === source, 'unexpected parent');
+            //attribute was not moved:
+            assertTrue(att.parent() === source, 'unexpected parent');
+            assertTrue(orig1.parent() === dest, 'unexpected parent');
+
+            //at this point the actual nodes are present in both xml trees when iterating downwards,
+            //but the 'parent()' evaluation only resolves to the latest parent
+            //this is the swf behavior, but may not conform to standard
+
+            assertEquals(source.toXMLString(),
+                    '<source att="attribute">\n' +
+                    '  <dog/>\n' +
+                    '  <cat/>\n' +
+                    '  <rat/>\n' +
+                    '  <pig/>\n' +
+                    '  <cow/>\n' +
+                    '  <hen/>\n' +
+                    '</source>', 'unexpected source toXMLString')
+
+            assertEquals(dest.toXMLString(),
+                    '<dest>\n' +
+                    '  <dog/>\n' +
+                    '  <cat/>\n' +
+                    '  <rat/>\n' +
+                    '  <pig/>\n' +
+                    '  <cow/>\n' +
+                    '  <hen/>\n' +
+                    '</dest>', 'unexpected dest toXMLString')
+
+            dest.appendChild(attList);
+            //swf has a strange variation here:
+            var appendedAtt1:String = isJS ? '  <att>attribute</att>\n' : '  <att xmlns="flexUnitTests.xml:XMLTesterGeneralTest">attribute</att>\n'
+
+            assertEquals(dest.toXMLString(),
+                    '<dest>\n' +
+                    '  <dog/>\n' +
+                    '  <cat/>\n' +
+                    '  <rat/>\n' +
+                    '  <pig/>\n' +
+                    '  <cow/>\n' +
+                    '  <hen/>\n' +
+                    appendedAtt1 +
+                    '</dest>', 'unexpected dest toXMLString');
+
+            dest.appendChild(att);
+
+            assertEquals(dest.toXMLString(),
+                    '<dest>\n' +
+                    '  <dog/>\n' +
+                    '  <cat/>\n' +
+                    '  <rat/>\n' +
+                    '  <pig/>\n' +
+                    '  <cow/>\n' +
+                    '  <hen/>\n' +
+                    appendedAtt1 +
+                    '  attribute\n' +
+                    '</dest>', 'unexpected dest toXMLString');
+
+
+            dest.appendChild(orig1);
+
+
+            //the source remains unchanged
+            assertEquals(source.toXMLString(),
+                    '<source att="attribute">\n' +
+                    '  <dog/>\n' +
+                    '  <cat/>\n' +
+                    '  <rat/>\n' +
+                    '  <pig/>\n' +
+                    '  <cow/>\n' +
+                    '  <hen/>\n' +
+                    '</source>', 'unexpected source toXMLString');
+
+            //this is effectively a re-ordering inside dest
+            var expected1:String =     '<dest>\n' +
+                    '  <cat/>\n' +
+                    '  <rat/>\n' +
+                    '  <pig/>\n' +
+                    '  <cow/>\n' +
+                    '  <hen/>\n' +
+                    appendedAtt1 +
+                    '  attribute\n' +
+                    '  <dog/>\n' +
+                    '</dest>'
+
+            var expected2:String = '<dest>\n' +
+                    '  <dog/>\n' + //this seems to be the case in standalone debug player
+                    '  <cat/>\n' +
+                    '  <rat/>\n' +
+                    '  <pig/>\n' +
+                    '  <cow/>\n' +
+                    '  <hen/>\n' +
+                    appendedAtt1 +
+                    '  attribute\n' +
+                    '  <dog/>\n' +
+                    '</dest>'
+
+
+            var actual:String = dest.toXMLString();
+
+            assertTrue(actual==expected1 || actual==expected2
+                   , 'unexpected dest toXMLString');
+
+        }
         
         //@todo - Passes in Swf, fails in browser:
         /*[Test]