cb9b68bf7d990a0d127301b050e5cf2fcc03e35d
[swftools.git] / lib / as3 / ok / xml.as
1 package {
2     import flash.display.MovieClip
3     public class Main extends flash.display.MovieClip {
4         var count:int = 1;
5         var num:int = 29;
6         function assert(b:Boolean) {
7             if(b) {
8                 trace("ok "+count+"/"+num);
9             } else {
10                 trace("error "+count+"/"+num);
11             }
12             count = count + 1
13         }
14         function Main() {
15             /* ------ basic sanity tests ------ */
16             var x:XML = <test>}</test>;
17
18             <test>This XML doesn't do anything!
19             </test>
20             ;
21             /Neither does this regexp!/
22             ;
23             
24             var y = 3;
25             <test>test</test>
26
27             assert(true);
28
29             var f2 = function(s:String) {assert(true);} 
30             var f3 = function(s:String) {assert(true);} 
31
32             f2(<![CDATA[ bla bla bla /* package String uint 
33              int finally try for ]]>);
34             
35             //f3(<![CDATA[ ]]]>);
36
37             /* ----- xml objects ----- */
38     
39             var x1:XML = <tree name="test">
40                             <branch bname="t1">Branch 1</branch>
41                             <branch bname="t2">Branch 2</branch>
42                         </tree>
43
44             assert(typeof(x1)=="xml");
45
46             /* ------ access --------- */
47
48             assert(x1. * == x1.children())
49             assert((x1.*)[0].text() == "Branch 1")
50             assert(x1.branch[0] == x1.child("branch")[0])
51             assert((x1.*.@bname)[0] == "t1");
52
53             assert(x1.@name.parent() == x1);
54             assert(x1.@["name"].parent() == x1);
55             var s1= <t x="a" y="b" z="c" />
56             assert(s1.@* == s1.attributes())
57
58             /* ------ comments ------- */
59
60             var x3 = <x><!-- </x>xxxx -->Test</x>;
61             assert(x3.toString() == "Test")
62
63             /* ------ descendants ---- */
64
65             var x4 = <l1>
66                         <l2>
67                             <l3>v1</l3>
68                             <x3>e1</x3>
69                         </l2>
70                         <r2>
71                             <x3>e2</x3>
72                             <l3>v2</l3>
73                         </r2>
74                      </l1>
75             assert(x4..l3[0] == x4.l2.l3)
76
77             var s:String="";
78             for each(var c:XML in x4..l3) {
79                 s += c.toString();
80             }
81             assert(s=="v1v2");
82            
83             s="";
84             for each(var e:XML in x1..@bname) {
85                 s += e.toString();
86             }
87             assert(s=="t1t2");
88             
89             s="";
90             for each(var e in x1..@*) {
91                 s += e.toString();
92             }
93             assert(s=="testt1t2");
94
95             /* ----- filtering ----- */
96             
97             var x5:XML = <participants>
98                             <person gender="m" id="1">John Doe</person>
99                             <person gender="f" id="2">Ellen Doe</person>
100                             <person gender="m">John Smith</person>
101                             <person gender="f" id="4">Jane Smith</person>
102                         </participants>
103
104             var l:XMLList = x5.*;
105             var l2:XMLList = l.(@gender=="m");
106             assert(l2.length()==2 && l2[0].text()=="John Doe" && l2[1].text()=="John Smith");
107             l2 = x5.*.(@gender=="f").(hasOwnProperty("@id") && @id>2)
108             assert(l2.text() =="Jane Smith");
109             
110             /* ----- replacement ----- */
111
112             var x10:XML = <a><b a="x"><c/></b></a>
113             var x11:XML = <a><b a="y"><d/></b></a>
114             
115             assert(x10 != x11);
116             x11.b.d = <c/>;
117             assert(x10 != x11);
118             x11.b.@a = "x";
119             assert(x10 == x11);
120
121             var x12:XML = <x><a/></x>;
122             x12.a += <b/>;
123
124             assert(x12 == <x><a/><b/></x>);
125
126             x12.* = <c/>;
127             assert(x12 == <x><c/></x>);
128             x12.* = <b/>;
129
130             /* ----- deletion ----- */
131
132             delete x12.a
133             assert(x12 == <x><b/></x>);
134             delete x12.*
135             assert(x12 == <x/>);
136             delete x10.b.c
137             delete x10.b.@a
138             assert(x10 == <a><b/></a>);
139             delete x11.b.c
140             delete x11.b.@*
141             assert(x10 == x11);
142
143             /* ----- special characters ----- */
144
145             var x20 = <x> > & </x>;
146             assert(x20.toString() == ">&");
147             var x21 = <x a="&quot;&lt;&gt;"/>;
148             assert(x21.@a == "\"<>");
149             var x22 = <x/>;
150
151             /* ----- substitution ----- */
152
153             var x2:XML = <{"tree"} {"na"+"me"}="test">
154                              <{{3: "branch"}[3]} bname={"t1"}>{"Branch" + " " + 1}</{"branch"}>
155                             <branch bname="t2">Branch 2</branch>
156                         </tree>
157             assert(x1 == x2);
158
159             trace("[exit]");
160         }
161     }
162 }