PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/unit/TestXML.hx

http://github.com/MarcWeber/haxe-compiler-experiments
Haxe | 243 lines | 192 code | 40 blank | 11 comment | 7 complexity | 1a3a49030feb613a38f063a645769523 MD5 | raw file
  1. package unit;
  2. class TestXML extends Test {
  3. function checkExc( x : Xml, ?pos ) {
  4. exc( function() x.nodeName, pos );
  5. exc( function() x.nodeValue, pos );
  6. exc( function() x.attributes(), pos );
  7. exc( function() x.get("att"), pos );
  8. exc( function() x.exists("att"), pos );
  9. }
  10. function testBasic() {
  11. var x = Xml.parse('<a href="hello">World<b/></a>');
  12. t( x.firstChild() == x.firstChild() );
  13. eq( x.nodeType, Xml.Document );
  14. checkExc(x);
  15. x = x.firstChild();
  16. eq( x.nodeType, Xml.Element );
  17. // nodeName
  18. eq( x.nodeName, "a" );
  19. x.nodeName = "b";
  20. eq( x.nodeName, "b" );
  21. eq( x.toString(), '<b href="hello">World<b/></b>');
  22. // attributes
  23. eq( x.get("href"), "hello" );
  24. eq( x.get("other"), null );
  25. eq( x.exists("href"), true );
  26. eq( x.exists("other"), false );
  27. eq( Lambda.array({ iterator : x.attributes }).join("#"), "href" );
  28. x.remove("href");
  29. eq( Lambda.array({ iterator : x.attributes }).join("#"), "" );
  30. eq( x.toString(), '<b>World<b/></b>');
  31. // children
  32. eq( x.firstChild().nodeValue, "World" );
  33. eq( x.firstElement().nodeName, "b" );
  34. // errors
  35. exc( function() Xml.parse("<node>") );
  36. }
  37. function testFormat() {
  38. #if flash8
  39. // flash8 does not parse CDATA sections as PCDATA...
  40. eq( Xml.parse("<a><b><c/> <d/> \n <e/><![CDATA[<x>]]></b></a>").toString(), "<a><b><c/> <d/> \n <e/>&lt;x&gt;</b></a>" );
  41. #else
  42. eq( Xml.parse("<a><b><c/> <d/> \n <e/><![CDATA[<x>]]></b></a>").toString(), "<a><b><c/> <d/> \n <e/><![CDATA[<x>]]></b></a>" );
  43. #end
  44. #if (flash8 || php)
  45. eq( Xml.parse('"').toString(), '&quot;' ); // flash8 has bad habits of escaping entities
  46. #else
  47. eq( Xml.parse('"').toString(), '"' );
  48. #end
  49. #if flash9
  50. eq( Xml.parse('&quot; &lt; &gt;').toString(), '" &lt; &gt;' ); // some entities are resolved but not escaped on printing
  51. #elseif
  52. eq( Xml.parse('&quot; &lt; &gt;').toString(), '&quot; &lt; &gt;' );
  53. #end
  54. }
  55. function testComplex() {
  56. // this is showing some quirks with flash XML parser
  57. var header = '<?some header?>';
  58. var doctype = '<!DOCTYPE root SYSTEM "">';
  59. var comment = '<!--Comment-->';
  60. var xml = '<html><body><![CDATA[<a href="CDATA"/>&lt;]]></body></html>';
  61. #if flash8
  62. return; // too hard for him
  63. #end
  64. var x = Xml.parse(header + doctype + comment + xml);
  65. #if flash
  66. // doctype is well parsed but is not present in the parsed Xml (f8 and f9)
  67. doctype = '';
  68. #end
  69. eq( x.toString(), header + doctype + comment + xml);
  70. }
  71. function testWhitespaces() {
  72. // whitespaces
  73. var x = Xml.parse('<a> </a><b></b> \n <c/>');
  74. var childs = Lambda.array(x);
  75. eq( childs.length, 4 );
  76. var d = childs[2];
  77. eq( d.nodeType, Xml.PCData );
  78. eq( d.nodeValue, " \n " );
  79. var el = x.elements();
  80. var a = el.next();
  81. eq( a.firstChild().nodeValue, " ");
  82. var b = el.next();
  83. #if (flash || php)
  84. eq( b.firstChild(), null);
  85. eq( x.toString().split("\n").join("\\n"), '<a> </a><b/> \\n <c/>' );
  86. #else
  87. eq( b.firstChild().nodeValue, "");
  88. eq( x.toString().split("\n").join("\\n"), '<a> </a><b></b> \\n <c/>' );
  89. #end
  90. var c = el.next();
  91. eq( c.firstChild(), null);
  92. }
  93. function testCreate() {
  94. eq( Xml.createDocument().toString(), "");
  95. eq( Xml.createPCData("Hello").toString(), "Hello" );
  96. #if flash8
  97. // too hard for him
  98. return;
  99. #end
  100. eq( Xml.createCData("<x>").toString(), "<![CDATA[<x>]]>" );
  101. eq( Xml.createComment("Hello").toString(), "<!--Hello-->" );
  102. #if flash9
  103. eq( Xml.createProcessingInstruction("XHTML").toString(), "<?XHTML ?>");
  104. // doctype is parsed but not printed
  105. eq( Xml.createDocType("XHTML").toString(), "" );
  106. #else
  107. eq( Xml.createProcessingInstruction("XHTML").toString(), "<?XHTML?>");
  108. eq( Xml.createDocType("XHTML").toString(), "<!DOCTYPE XHTML>" );
  109. #end
  110. eq( Xml.parse("<!--Hello-->").firstChild().nodeValue, "Hello" );
  111. var c = Xml.createComment("Hello");
  112. eq( c.nodeValue, "Hello" );
  113. c.nodeValue = "Blabla";
  114. eq( c.nodeValue, "Blabla" );
  115. eq( c.toString(), "<!--Blabla-->");
  116. eq( Xml.parse("<![CDATA[Hello]]>").firstChild().nodeValue, "Hello" );
  117. var c = Xml.createCData("Hello");
  118. eq( c.nodeValue, "Hello" );
  119. c.nodeValue = "Blabla";
  120. eq( c.nodeValue, "Blabla" );
  121. eq( c.toString(), "<![CDATA[Blabla]]>");
  122. eq( Xml.createPCData("Hello").nodeValue, "Hello" );
  123. }
  124. function testNS() {
  125. var x = Xml.parse('<xhtml:br xmlns:xhtml="http://www.w3.org/1999/xhtml" xhtml:alt="test"><hello/></xhtml:br>').firstChild();
  126. eq( x.nodeType, Xml.Element );
  127. eq( x.nodeName, "xhtml:br" );
  128. t( x.exists("xhtml:alt") );
  129. eq( x.get("xhtml:alt"), "test" );
  130. eq( x.get("xhtml:other"), null );
  131. x.set("xhtml:alt", "bye" );
  132. eq( x.get("xhtml:alt"), "bye" );
  133. var h = x.firstElement();
  134. eq( h.nodeName, "hello" );
  135. h.nodeName = "em";
  136. eq( h.nodeName, "em" );
  137. eq( Lambda.count({ iterator : x.elementsNamed.bind("em") }), 1 );
  138. h.nodeName = "xhtml:em";
  139. eq( Lambda.count({ iterator : x.elementsNamed.bind("xhtml:em") }), 1 );
  140. eq( Lambda.count({ iterator : x.elementsNamed.bind("em") }), 0 );
  141. eq( h.nodeName, "xhtml:em" );
  142. }
  143. function testNodetype() {
  144. var element = Xml.createElement("x");
  145. var l = [Xml.createPCData("x"), Xml.createCData("x"), Xml.createDocType("x"), Xml.createProcessingInstruction("x") #if !flash8, Xml.createComment("x") #end];
  146. for (xml in l)
  147. {
  148. exc(function() xml.firstChild());
  149. exc(function() xml.firstElement());
  150. exc(function() xml.elements());
  151. exc(function() xml.elementsNamed("x"));
  152. exc(function() xml.addChild(element));
  153. exc(function() xml.removeChild(element));
  154. exc(function() xml.insertChild(element, 0));
  155. exc(function() for (x in xml) null);
  156. }
  157. }
  158. function testEntities() {
  159. var entities = ["&lt;", "&gt;", "&quot;", "&amp;", "&apos;", "&nbsp;", "&euro;", "&#64;", "&#244;", "&#x3F;", "&#xFF;"];
  160. var values = entities.copy();
  161. #if (flash || js)
  162. // flash parser does support XML + some HTML entities (nbsp only ?) + character codes entities
  163. values = ['<', '>', '"', '&', "'", String.fromCharCode(160), '&euro;', '@', 'ô', '?', 'ÿ'];
  164. #end
  165. #if flash9
  166. // for a very strange reason, flash9 uses a non standard charcode for non breaking space
  167. values[5] = String.fromCharCode(65440);
  168. #end
  169. #if php
  170. // &nbsp; and &euro; creates an invalid entity error (first time I see PHP being strict !)
  171. entities[5] = "x";
  172. entities[6] = "x";
  173. // character codes entities are supported
  174. values = ["&lt;", "&gt;", "&quot;", "&amp;", "&apos;", "x", "x", '@', 'ô', '?', 'ÿ'];
  175. #end
  176. for( i in 0...entities.length ) {
  177. infos(entities[i]);
  178. eq( Xml.parse(entities[i]).firstChild().nodeValue, values[i] );
  179. }
  180. }
  181. function testCustomXmlParser() {
  182. var entities = ["&lt;", "&gt;", "&quot;", "&amp;", "&apos;", "&euro;", "&#64;", "&#244;", "&#x3F;", "&#xFF;"];
  183. var values = ['<', '>', '"', '&', "'", '&euro;', '@', String.fromCharCode(244), String.fromCharCode(0x3F), String.fromCharCode(0xFF)];
  184. for( i in 0...entities.length ) {
  185. infos(entities[i]);
  186. eq( haxe.xml.Parser.parse(entities[i]).firstChild().nodeValue, values[i] );
  187. }
  188. var s = "<a>&gt;<b>&lt;</b>&lt;&gt;<b>&gt;&lt;</b>\"</a>";
  189. var xml = haxe.xml.Parser.parse(s);
  190. eq(s, xml.toString());
  191. }
  192. function testMore() {
  193. var doc = Xml.parse("<a>A</a><i>I</i>");
  194. var aElement = doc.elementsNamed('a').next();
  195. var iElement = doc.elementsNamed('i').next();
  196. iElement.addChild(aElement);
  197. eq(doc.toString(), "<i>I<a>A</a></i>");
  198. }
  199. }