Browse Source

added tests for "bad nodetype" exception in Xml and fixed flash 8 + 9 Xml accordingly

Simon Krajewski 13 years ago
parent
commit
17a37b8dc4
3 changed files with 36 additions and 0 deletions
  1. 16 0
      std/flash/_std/Xml.hx
  2. 4 0
      std/flash8/_std/Xml.hx
  3. 16 0
      tests/unit/TestXML.hx

+ 16 - 0
std/flash/_std/Xml.hx

@@ -284,6 +284,8 @@ enum XmlType {
 	}
 	}
 
 
 	public function iterator() : Iterator<Xml> {
 	public function iterator() : Iterator<Xml> {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";		
 		var children:XMLList = _node.children();
 		var children:XMLList = _node.children();
 		var wrappers :Array<Xml> = wraps(children);
 		var wrappers :Array<Xml> = wraps(children);
 		var cur = 0;
 		var cur = 0;
@@ -298,6 +300,8 @@ enum XmlType {
 	}
 	}
 
 
 	public function elements() : Iterator<Xml> {
 	public function elements() : Iterator<Xml> {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";		
 		var elements:XMLList = _node.elements();
 		var elements:XMLList = _node.elements();
 		var wrappers :Array<Xml> = wraps(elements);
 		var wrappers :Array<Xml> = wraps(elements);
 		var cur = 0;
 		var cur = 0;
@@ -312,6 +316,8 @@ enum XmlType {
 	}
 	}
 
 
 	public function elementsNamed( name : String ) : Iterator<Xml> {
 	public function elementsNamed( name : String ) : Iterator<Xml> {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";	
 		var ns = name.split(":");
 		var ns = name.split(":");
 		var elements:XMLList;
 		var elements:XMLList;
 		if( ns.length == 1 )
 		if( ns.length == 1 )
@@ -335,6 +341,8 @@ enum XmlType {
 	}
 	}
 
 
 	public function firstChild() : Xml {
 	public function firstChild() : Xml {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";
 		var children:XMLList = _node.children();
 		var children:XMLList = _node.children();
 		if( children.length() == 0 )
 		if( children.length() == 0 )
 			return null;
 			return null;
@@ -342,6 +350,8 @@ enum XmlType {
 	}
 	}
 
 
 	public function firstElement() : Xml {
 	public function firstElement() : Xml {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";
 		var elements:XMLList = _node.elements();
 		var elements:XMLList = _node.elements();
 		if( elements.length() == 0 )
 		if( elements.length() == 0 )
 			return null;
 			return null;
@@ -349,11 +359,15 @@ enum XmlType {
 	}
 	}
 
 
 	public function addChild( x : Xml ) : Void {
 	public function addChild( x : Xml ) : Void {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";		
 		var children:XMLList = _node.children();
 		var children:XMLList = _node.children();
 		_node.appendChild(x._node);
 		_node.appendChild(x._node);
 	}
 	}
 
 
 	public function removeChild( x : Xml ) : Bool {
 	public function removeChild( x : Xml ) : Bool {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";		
 		var children:XMLList = _node.children();
 		var children:XMLList = _node.children();
 		if( _node != x._node.parent() )
 		if( _node != x._node.parent() )
 			return false;
 			return false;
@@ -363,6 +377,8 @@ enum XmlType {
 	}
 	}
 
 
 	public function insertChild( x : Xml, pos : Int ) : Void {
 	public function insertChild( x : Xml, pos : Int ) : Void {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";
 		var children:XMLList = _node.children();
 		var children:XMLList = _node.children();
 		if( pos < children.length() )
 		if( pos < children.length() )
 			_node.insertChildBefore(children[pos], x._node);
 			_node.insertChildBefore(children[pos], x._node);

+ 4 - 0
std/flash8/_std/Xml.hx

@@ -123,10 +123,14 @@ enum XmlType {
 	}
 	}
 
 
 	public function firstChild() : Xml {
 	public function firstChild() : Xml {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";		
 		return convert(this.__x[untyped "firstChild"]);
 		return convert(this.__x[untyped "firstChild"]);
 	}
 	}
 
 
 	public function firstElement() : Xml {
 	public function firstElement() : Xml {
+		if( nodeType != Xml.Element && nodeType != Xml.Document )
+			throw "bad nodeType";		
 		var e : Dynamic = __x[untyped "firstChild"];
 		var e : Dynamic = __x[untyped "firstChild"];
 		while( e != null && e[untyped "nodeType"] != 1 )
 		while( e != null && e[untyped "nodeType"] != 1 )
 			e = e[untyped "nextSibling"];
 			e = e[untyped "nextSibling"];

+ 16 - 0
tests/unit/TestXML.hx

@@ -174,4 +174,20 @@ class TestXML extends Test {
 		eq( h.nodeName, "xhtml:em" );
 		eq( h.nodeName, "xhtml:em" );
 	}
 	}
 
 
+	function testNodetype() {
+		var element = Xml.createElement("x");
+
+		var l = [Xml.createPCData("x"), Xml.createCData("x"), Xml.createDocType("x"), Xml.createProlog("x") #if !flash8, Xml.createComment("x") #end];
+		for (xml in l)
+		{
+			exc(function() xml.firstChild());
+			exc(function() xml.firstElement());
+			exc(function() xml.elements());
+			exc(function() xml.elementsNamed("x"));
+			exc(function() xml.addChild(element));
+			exc(function() xml.removeChild(element));
+			exc(function() xml.insertChild(element, 0));
+			exc(function() for (x in xml) null);
+		}	
+	}	
 }
 }