Repository: flex-asjs
Updated Branches:
  refs/heads/develop b730bd827 -> 5ae65eabc


Fixed some issues related to single item XMLLists


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/5ae65eab
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/5ae65eab
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/5ae65eab

Branch: refs/heads/develop
Commit: 5ae65eabcf2fcc87dfbed210c7595a4cfa50279d
Parents: b730bd8
Author: Harbs <[email protected]>
Authored: Mon Jul 17 14:52:21 2017 +0300
Committer: Harbs <[email protected]>
Committed: Mon Jul 17 14:52:21 2017 +0300

----------------------------------------------------------------------
 frameworks/projects/XML/src/main/flex/XML.as    | 87 ++++++++++++++------
 .../projects/XML/src/main/flex/XMLList.as       | 29 ++++++-
 2 files changed, 91 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5ae65eab/frameworks/projects/XML/src/main/flex/XML.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/XML/src/main/flex/XML.as 
b/frameworks/projects/XML/src/main/flex/XML.as
index efd1756..fd54fca 100644
--- a/frameworks/projects/XML/src/main/flex/XML.as
+++ b/frameworks/projects/XML/src/main/flex/XML.as
@@ -21,6 +21,8 @@ package
        COMPILE::JS
        public class XML
        {
+               import org.apache.flex.debugging.assert;
+               import org.apache.flex.debugging.assertType;
                /*
                 * Dealing with namespaces:
                 * If the name is qualified, it has a prefix. Otherwise, the 
prefix is null.
@@ -394,6 +396,7 @@ package
                }
                private function addChildInternal(child:XML):void
                {
+                       assertType(child,XML,"Type must be XML");
                        child.setParent(this);
                        if(child.nodeKind() =="attribute")
                        {
@@ -496,24 +499,30 @@ package
                        */
                        var childType:String = typeof child;
                        if(childType != "object")
-                       {
-                               child = child.toString();
-                               var xml:XML = new XML();
-                               xml.setNodeKind("text");
-                               xml.setValue(child);
-                               child = xml;                            
-                       }
-
-                       if(child is XMLList)
-                               child = child[0];
-
-                       child.setParent(this);
+                               child = xmlFromStringable(child);
                        
-                       _children.push(child);
+                       appendChildInternal(child);
                        normalize();
-                       return child;
+                       return this;
                }
                
+               private function appendChildInternal(child:*):void
+               {
+                       if(child is XMLList)
+                       {
+                               var len:int = child.length();
+                               for(var i:int=0; i<len; i++)
+                               {
+                                       appendChildInternal(child[0]);
+                               }
+                       }
+                       else
+                       {
+                               assertType(child,XML,"Type must be XML");
+                               child.setParent(this);
+                               _children.push(child);
+                       }
+               }
                
                /**
                 * Returns the XML value of the attribute that has the name 
matching the attributeName parameter.
@@ -1395,6 +1404,14 @@ package
                        return list.plus(rightHand);
                }
 
+               private function xmlFromStringable(value:*):XML
+               {
+                       var str:String = value.toString();
+                       var xml:XML = new XML();
+                       xml.setNodeKind("text");
+                       xml.setValue(str);
+                       return xml;
+               }
                /**
                 * Inserts the provided child object into the XML element 
before any existing XML properties for that element.
                 * @param value
@@ -1403,13 +1420,34 @@ package
                 */
                public function prependChild(child:XML):XML
                {
-                       child.setParent(this);
-                       
-                       _children.unshift(child);
+                       var childType:String = typeof child;
+                       if(childType != "object")
+                               child = xmlFromStringable(child);
 
-                       return child;
+                       prependChildInternal(child);
+                       normalize();
+                       return this;
                }
                
+               private function prependChildInternal(child:*):void
+               {
+                       if(child is XMLList)
+                       {
+                               var len:int = child.length();
+                               for(var i:int=0; i<len; i++)
+                               {
+                                       prependChildInternal(child[0]);
+                               }
+                       }
+                       else
+                       {
+                               assertType(child,XML,"Type must be XML");
+                               child.setParent(this);
+                               _children.unshift(child);
+                       }
+               }
+
+               
                /**
                 * If a name parameter is provided, lists all the children of 
the XML object that contain processing instructions with that name.
                 * 
@@ -1465,14 +1503,14 @@ package
                        var removed:XML;
                        if(!child)
                                return false;
-                       if(!_attributes)
-                               return false;
 
                        if(!(child is XML))
                                return removeChildByName(child);
                        
                        if(child.nodeKind() == "attribute")
                        {
+                               if(!_attributes)
+                                       return false;
                                for(i=0;i<_attributes.length;i++)
                                {
                                        if(child.equals(_attributes[i]))
@@ -1483,13 +1521,14 @@ package
                                                return true;
                                        }
                                }
+                               return false;
                        }
                        var idx:int = _children.indexOf(child);
                        if(idx < 0)
                                return false;
                        removed = _children.splice(idx,1);
-                       child.setParent(null);
-                       return removed;
+                       child._parent = null;
+                       return true;
                }
                private function removeChildByName(name:*):Boolean
                {
@@ -1507,7 +1546,7 @@ package
                                        if(_attributes[i].name().matches(name))
                                        {
                                                child = _attributes[i];
-                                               child.setParent(null);
+                                               child._parent = null;
                                                _attributes.splice(i,1);
                                                removedItem = true;
                                        }
@@ -1522,7 +1561,7 @@ package
                                if(_children[i].name().matches(name))
                                {
                                        child = _children[i];
-                                       child.setParent(null);
+                                       child._parent = null;
                                        _children.splice(i,1);
                                        removedItem = true;
                                }

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/5ae65eab/frameworks/projects/XML/src/main/flex/XMLList.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/XML/src/main/flex/XMLList.as 
b/frameworks/projects/XML/src/main/flex/XMLList.as
index d07a00a..17c2e33 100644
--- a/frameworks/projects/XML/src/main/flex/XMLList.as
+++ b/frameworks/projects/XML/src/main/flex/XMLList.as
@@ -164,6 +164,9 @@ package
                 */
                public function attribute(attributeName:*):XMLList
                {
+                       if(isSingle())
+                               return _xmlArray[0].attribute(attributeName);
+
                        var retVal:XMLList = new XMLList();
                        var len:int = _xmlArray.length;
                        for (var i:int=0;i<len;i++)
@@ -182,6 +185,9 @@ package
                 */
                public function attributes():XMLList
                {
+                       if(isSingle())
+                               return _xmlArray[0].attributes();
+
                        var retVal:XMLList = new XMLList();
                        var len:int = _xmlArray.length;
                        for (var i:int=0;i<len;i++)
@@ -212,7 +218,9 @@ package
                                }
                                return retVal;
                        }
-
+                       if(isSingle())
+                               return _xmlArray[0].child(propertyName);
+                       
                        var len:int = _xmlArray.length;
                        for (var i:int=0;i<len;i++)
                        {
@@ -238,6 +246,9 @@ package
                 */
                public function children():XMLList
                {
+                       if(isSingle())
+                               return _xmlArray[0].children();
+
                        var retVal:XMLList = new XMLList();
                        var len:int = _xmlArray.length;
                        for (var i:int=0;i<len;i++)
@@ -256,6 +267,9 @@ package
                 */
                public function comments():XMLList
                {
+                       if(isSingle())
+                               return _xmlArray[0].comments();
+                       
                        var retVal:XMLList = new XMLList();
                        var len:int = _xmlArray.length;
                        for (var i:int=0;i<len;i++)
@@ -336,6 +350,9 @@ package
                 */
                public function descendants(name:Object = "*"):XMLList
                {
+                       if(isSingle())
+                               return _xmlArray[0].descendants(name);
+
                        var retVal:XMLList = new XMLList();
                        var len:int = _xmlArray.length;
                        for (var i:int=0;i<len;i++)
@@ -356,6 +373,9 @@ package
                 */
                public function elements(name:Object = "*"):XMLList
                {
+                       if(isSingle())
+                               return _xmlArray[0].elements(name);
+                       
                        var retVal:XMLList = new XMLList();
                        var len:int = _xmlArray.length;
                        for (var i:int=0;i<len;i++)
@@ -379,6 +399,8 @@ package
 
                public function equals(list:*):Boolean
                {
+                       if(isSingle())
+                               return _xmlArray[0].equals(list);
                        /*
                                Overview
                                The XMLList type adds the internal [[Equals]] 
method to the internal properties defined by the Object type.
@@ -443,6 +465,9 @@ package
                                a. If x[i].[[Class]] == "element" and the 
result of calling the [[HasProperty]] method of x[i] with argument P == true, 
return true
                                3. Return false                 
                        */
+                       if(isSingle())
+                               return 
_xmlArray[0].hasOwnProperty(propertyName);
+
                        if(parseInt(propertyName,10).toString() == propertyName)
                        {
                                return parseInt(propertyName,10) < 
_xmlArray.length;
@@ -597,6 +622,8 @@ package
                 */
                public function processingInstructions(name:String = 
"*"):XMLList
                {
+                       if(isSingle())
+                               return 
_xmlArray[0].processingInstructions(name);
                        var retVal:XMLList = new XMLList();
                        if(!name)
                                return retVal;

Reply via email to