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

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


The following commit(s) were added to refs/heads/develop by this push:
     new 4c4e9ee  Fixed null and undefined in XML
4c4e9ee is described below

commit 4c4e9ee4e5f69d30b8bf25f3b236cf35aae70c4c
Author: Harbs <[email protected]>
AuthorDate: Sat Nov 13 20:49:56 2021 +0200

    Fixed null and undefined in XML
---
 frameworks/projects/XML/src/main/royale/XML.as     | 57 +++++++-------
 .../XML/src/test/royale/flexUnitTests/XMLTester.as |  4 +-
 .../royale/flexUnitTests/xml/XMLTesterNullTest.as  | 91 ++++++++++++++++++++++
 3 files changed, 125 insertions(+), 27 deletions(-)

diff --git a/frameworks/projects/XML/src/main/royale/XML.as 
b/frameworks/projects/XML/src/main/royale/XML.as
index 1ebc43e..39cd4f2 100644
--- a/frameworks/projects/XML/src/main/royale/XML.as
+++ b/frameworks/projects/XML/src/main/royale/XML.as
@@ -342,7 +342,7 @@ package
                        var xml:XML = new XML();
                        xml._parent = parent;
                        xml._name = getQName(att.localName, '', 
att.namespaceURI, true);
-                       xml._value = att.value;
+                       xml.setValue(att.value);
                        parent.addChildInternal(xml);
                        return xml;
                }
@@ -609,7 +609,7 @@ package
                                {
                                        // _nodeKind = TEXT;
                                        if (ignoreWhitespace) xmlStr = 
xmlStr.trim();
-                                       _value = xmlStr;
+                                       setValue(xmlStr);
                                }
                                else
                                {
@@ -618,7 +618,7 @@ package
                        } else {
                                if (!_internal) {
                                        // _nodeKind = TEXT;
-                                       _value = '';
+                                       setValue('');
                                }
                        }
 
@@ -969,7 +969,7 @@ package
                        if (child is XML  && (child as XML).getNodeRef() == 
ATTRIBUTE){
                                //convert to text node
                                var xml:XML= new XML();
-                               xml._value = child.toString();
+                               xml.setValue(child.toString());
                                child = xml;
                                isTextSet = true;
                        }
@@ -1049,7 +1049,7 @@ package
                                {
                                        if (isTextSet)
                                        {
-                                               xml$_notify("textSet", child, 
child._value, null);
+                                               xml$_notify("textSet", child, 
child.getValue(), null);
                                        }
                                        xml$_notify("attributeAdded", this, 
child.name().toString(), child.getValue());
                                }
@@ -1057,7 +1057,7 @@ package
                                {
                                        // the node with the _value may have a 
wrapper (with single child)
                                        var childValueNode:XML = 
(child._children ? child._children[0] : child);
-                                       xml$_notify("textSet", childValueNode, 
childValueNode._value, null);
+                                       xml$_notify("textSet", childValueNode, 
childValueNode.getValue(), null);
                                        // if no wrapper, then 
childValueNode._parent == this and childValueNode == child
                                        xml$_notify("nodeAdded", 
childValueNode._parent, childValueNode, null);
                                }
@@ -1297,9 +1297,7 @@ package
                        xml.resetNodeKind();
                        xml.setNodeKind(getNodeKindInternal());
                        xml._name = _name;
-                       if(_value){
-                               xml.setValue(_value);
-                       }
+                       xml.setValue(_value);
                        var len:int;
                        len = namespaceLength();
                        for(i=0;i<len;i++)
@@ -1576,7 +1574,7 @@ package
                
                public function getValue():String
                {
-                       return _value;
+                       return _value ? _value : "";
                }
                
                public function hasAncestor(obj:*):Boolean
@@ -2228,7 +2226,7 @@ package
                                                removed._parent = null;
                                                _attributes.splice(i,1);
                                                // "_name as QName" (and 
ignorecoercion) needed to avoid compiler from writing ".child()" due to 
"removed" being XML
-                                               xml$_notify("attributeRemoved", 
this, (removed._name as QName).localName, removed._value);
+                                               xml$_notify("attributeRemoved", 
this, (removed._name as QName).localName, removed.getValue());
                                                return true;
                                        }
                                }
@@ -2268,7 +2266,7 @@ package
                                                _attributes.splice(i,1);
                                                removedItem = true;
                                                // "_name as QName" (and 
ignorecoercion) needed to avoid compiler from writing ".child()" due to "child" 
being XML
-                                               xml$_notify("attributeRemoved", 
this, (child._name as QName).localName, child._value);
+                                               xml$_notify("attributeRemoved", 
this, (child._name as QName).localName, child.getValue());
                                        }
                                }
                                return removedItem;
@@ -2546,6 +2544,8 @@ package
                public function setAttribute(attr:*,value:String):String
                {
                        var i:int;
+                       // stringify the value if not already a string
+                       value = "" + value;
                        //make sure _attributes is not null
                        getAttributes();
                        
@@ -2564,8 +2564,7 @@ package
                                                        return value;
                                                }
                                        }
-                                       if(value)
-                                               attr.setValue(value);
+                                       attr.setValue(value);
                                        addChild(attr);
                                }
                                return value;
@@ -2865,7 +2864,7 @@ package
                        var oldName:QName = _name;
                        _name = 
getQName(name,_name.prefix,_name.uri,_name.isAttribute)
                        // _name.localName = name;
-                       xml$_notify("nameSet", (getNodeRef() == ATTRIBUTE ? 
xmlFromStringable(_value) : this), _name.toString(), oldName.toString());
+                       xml$_notify("nameSet", (getNodeRef() == ATTRIBUTE ? 
xmlFromStringable(getValue()) : this), _name.toString(), oldName.toString());
                }
                
                /**
@@ -2904,7 +2903,7 @@ package
                                delete this._nodeKind;
                        }
                        // oldName cannot be null, normally, but we're calling 
setName() from within parseXMLStr() for processing instructions
-                       if (oldName) xml$_notify("nameSet", (ref == ATTRIBUTE ? 
xmlFromStringable(_value) : this), (name is QName ? _name : _name.toString()), 
oldName.toString());
+                       if (oldName) xml$_notify("nameSet", (ref == ATTRIBUTE ? 
xmlFromStringable(getValue()) : this), (name is QName ? _name : 
_name.toString()), oldName.toString());
                }
                
                /**
@@ -2971,7 +2970,12 @@ package
                
                public function setValue(value:String):void
                {
-                       _value = value;
+                       if(value)
+                       {
+                               _value = value;
+                       } else {
+                               delete this._value;
+                       }
                }
                
                /**
@@ -3031,10 +3035,10 @@ package
                        // text, comment, processing-instruction, attribute, or 
element
                        var kind:String = getNodeRef();
                        if( kind == ATTRIBUTE)
-                               return _value == null ? "" : _value;
+                               return getValue();
                        if(kind == TEXT)
                        {
-                               var textVal:String = _value == null ? "" : 
_value;
+                               var textVal:String = getValue();
                                return textVal.indexOf('<![CDATA[') == 0 ? 
textVal.substring(9, textVal.length-3): textVal;
                        }
                        if(kind == COMMENT)
@@ -3226,28 +3230,29 @@ package
                        
                        var indent:String = indentArr.join("");
                        const nodeType:String = getNodeRef();
+                       var strValue:String = getValue();
                        if(nodeType == TEXT) //4.
                        {
                                if(prettyPrinting)
                                {
-                                       var v:String = (_value+'').trim();
+                                       var v:String = strValue.trim();
                                        if (v.indexOf('<![CDATA[') == 0) {
                                                return indent + v;
                                        }
                                        return indent + escapeElementValue(v);
                                }
-                               if (_value.indexOf('<![CDATA[') == 0)
-                                       return _value;
-                               return escapeElementValue(_value);
+                               if (strValue.indexOf('<![CDATA[') == 0)
+                                       return strValue;
+                               return escapeElementValue(strValue);
                        }
                        if(nodeType == ATTRIBUTE)
-                               return indent + escapeAttributeValue(_value);
+                               return indent + escapeAttributeValue(strValue);
                        
                        if(nodeType == COMMENT)
-                               return indent + "<!--" +  _value + "-->";
+                               return indent + "<!--" +  strValue + "-->";
                        
                        if(nodeType == PROCESSING_INSTRUCTION)
-                               return indent + "<?" + name().localName + " " + 
_value + "?>";
+                               return indent + "<?" + name().localName + " " + 
strValue + "?>";
                        
                        // We excluded the other types, so it's a normal element
                        // step 8.
diff --git a/frameworks/projects/XML/src/test/royale/flexUnitTests/XMLTester.as 
b/frameworks/projects/XML/src/test/royale/flexUnitTests/XMLTester.as
index c7c0347..6f2f450 100644
--- a/frameworks/projects/XML/src/test/royale/flexUnitTests/XMLTester.as
+++ b/frameworks/projects/XML/src/test/royale/flexUnitTests/XMLTester.as
@@ -53,6 +53,8 @@ package flexUnitTests
         public var xmlLiteralTest:XMLLiteralTest;
 
         public var xmlNotificationTest:XMLNotificationTest;
-        
+
+        public var xmlNullTest:XMLTesterNullTest;
+
     }
 }
diff --git 
a/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLTesterNullTest.as
 
b/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLTesterNullTest.as
new file mode 100644
index 0000000..12de99d
--- /dev/null
+++ 
b/frameworks/projects/XML/src/test/royale/flexUnitTests/xml/XMLTesterNullTest.as
@@ -0,0 +1,91 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package flexUnitTests.xml
+{
+    
+    
+    import org.apache.royale.test.asserts.*;
+    
+    //import testshim.RoyaleUnitTestRunner;
+    
+    /**
+     * @royalesuppresspublicvarwarning
+     */
+    public class XMLTesterNullTest
+    {
+    
+        public static var isJS:Boolean = COMPILE::JS;
+    
+        private var settings:Object;
+        
+        [Before]
+        public function setUp():void
+        {
+            settings = XML.settings();
+        }
+        
+        [After]
+        public function tearDown():void
+        {
+            XML.setSettings(settings);
+        }
+        
+        [BeforeClass]
+        public static function setUpBeforeClass():void
+        {
+        }
+        
+        [AfterClass]
+        public static function tearDownAfterClass():void
+        {
+        }
+        
+        
+        [Test]
+        public function testSettingAttributes():void
+        {
+            var xml:XML = <foo/>;
+            xml.@name = "";
+            assertEquals( xml.toXMLString(), '<foo name=""/>', 'Error in blank 
attribute');
+
+            xml = <foo/>;
+            xml.@name = undefined;
+            assertEquals( xml.toXMLString(), '<foo name="undefined"/>', 'Error 
in undefined attribute');
+
+            xml = <foo/>;
+            xml.@name = null;
+            assertEquals( xml.toXMLString(), '<foo name="null"/>', 'Error in 
null attribute');
+
+            xml = <foo name="undefined"/>;
+            assertEquals( xml.toXMLString(), '<foo name="undefined"/>', 'Error 
in undefined attribute');
+
+            xml = <foo name="null"/>;
+            assertEquals( xml.toXMLString(), '<foo name="null"/>', 'Error in 
null attribute');
+
+            xml = new XML('<foo name="undefined"/>');
+            assertEquals( xml.toXMLString(), '<foo name="undefined"/>', 'Error 
in undefined attribute');
+
+            xml = new XML('<foo name="null"/>');
+            assertEquals( xml.toXMLString(), '<foo name="null"/>', 'Error in 
null attribute');
+
+        }
+
+        
+    }
+}

Reply via email to