Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / dom / script-tests / domListEnumeration.js
1 description(
2 'This tests enumerating the elements of DOM lists.'
3 );
4
5 if (window.testRunner)
6     testRunner.dumpAsText();
7
8 // Create a testing environment that can be cleanup up easily.
9 var testingGround = document.createElement('div');
10 document.body.appendChild(testingGround);
11
12 function createFromMarkup(markup)
13 {
14     var range = document.createRange();
15     var fragmentContainer = document.createElement("div");
16     range.selectNodeContents(fragmentContainer);
17     testingGround.appendChild(fragmentContainer);
18     var fragment = range.createContextualFragment(markup);
19     fragmentContainer.appendChild(fragment);
20     return fragmentContainer.firstChild;
21 }
22
23 function setup()
24 {
25     var head = document.getElementsByTagName('head')[0];
26
27     // 2 additional <style>s needed for StyleSheetList test (one is included in the template).
28     // 3 rules needed in the first addtional <style> for the CSSRuleList test.
29     // 3 declarations needed in the first rule needed for the CSSStyleDeclaration test.
30     // @media rule in the second addtional <style> for the MediaList test.
31     head.appendChild(createFromMarkup("<style> ol { width: 100px; height: 100px; color: green; } ol { } ol { } </style>"));
32     head.appendChild(createFromMarkup("<style> @media screen, projector, printer { ol { } } </style>"));
33
34     // 3 <ol>s for NodeList test.
35     // 3 attributes in the first <ol> for the NamedNodeMap test.
36     testingGround.appendChild(createFromMarkup("<ol class='foo' id='bar' name='baz'></ol>"));
37     testingGround.appendChild(document.createElement('ol'));
38     testingGround.appendChild(document.createElement('ol'));
39
40     // 3 <form>s for forms for HTMLCollection test.
41     var form = document.createElement('form');
42     testingGround.appendChild(form);
43     testingGround.appendChild(document.createElement('form'));
44     testingGround.appendChild(document.createElement('form'));
45
46     // 3 <select>s for HTMLFormElement test.
47     var select = document.createElement('select');
48     form.appendChild(select);
49     form.appendChild(document.createElement('select'));
50     form.appendChild(document.createElement('select'));
51
52     // 3 <option>s for HTMLSelectElement test.
53     select.appendChild(document.createElement('option'));
54     select.appendChild(document.createElement('option'));
55     select.appendChild(document.createElement('option'));
56
57     document.body.appendChild(testingGround);
58 }
59
60 function iterateList(list)
61 {
62     debug("");
63     debug(Object.prototype.toString.call(list));
64     var temp = new Array();
65     for (var i in list) {
66         temp.push(i);
67     }
68     temp.sort();
69
70     var a = new Array();
71     for (var i = 0; i < temp.length; ++i) {
72         a.push({"i" : temp[i], "item" : list[temp[i]]});
73     }
74     return a;
75 }
76
77 // ** Firefox DOES include the indexGetter results in enumeration **
78 // NodeList
79 // HTMLCollection
80 // CSSRuleList
81 // CSSStyleDeclaration
82 // CSSValueList
83 // StyleSheetList
84 // MediaList
85 // NamedNodeMap
86 // HTMLFormElement
87 // HTMLSelectElement
88
89 // ** Firefox DOESN'T include the indexGetter results in enumeration **
90 // Window
91
92 setup();
93
94 var resultArray = new Array();
95
96 // NodeList
97 var nodeList = document.querySelectorAll('ol');
98 resultArray = iterateList(nodeList);
99
100 shouldBe("resultArray.length", "5");
101 shouldBe("resultArray[0].i", "'0'");
102 shouldBe("resultArray[0].item", "nodeList.item(0)");
103 shouldBe("resultArray[1].i", "'1'");
104 shouldBe("resultArray[1].item", "nodeList.item(1)");
105 shouldBe("resultArray[2].i", "'2'");
106 shouldBe("resultArray[2].item", "nodeList.item(2)");
107
108 // HTMLCollection
109 var htmlCollection = document.forms;
110 resultArray = iterateList(htmlCollection);
111 shouldBe("resultArray.length", "6");
112 shouldBe("resultArray[0].i", "'0'");
113 shouldBe("resultArray[0].item", "htmlCollection.item(0)");
114 shouldBe("resultArray[1].i", "'1'");
115 shouldBe("resultArray[1].item", "htmlCollection.item(1)");
116 shouldBe("resultArray[2].i", "'2'");
117 shouldBe("resultArray[2].item", "htmlCollection.item(2)");
118
119 // NamedNodeMap
120 var namedNodeMap = document.getElementsByTagName('ol')[0].attributes;
121 resultArray = iterateList(namedNodeMap);
122 shouldBe("resultArray.length", "11");
123 shouldBe("resultArray[0].i", "'0'");
124 shouldBe("resultArray[0].item", "namedNodeMap.item(0)");
125 shouldBe("resultArray[1].i", "'1'");
126 shouldBe("resultArray[1].item", "namedNodeMap.item(1)");
127 shouldBe("resultArray[2].i", "'2'");
128 shouldBe("resultArray[2].item", "namedNodeMap.item(2)");
129
130 // HTMLFormElement
131 var htmlFormElement = document.getElementsByTagName('form')[0];
132 resultArray = iterateList(htmlFormElement);
133 shouldBe("resultArray[0].i", "'0'");
134 shouldBe("resultArray[0].item", "document.getElementsByTagName('select')[0]");
135 shouldBe("resultArray[1].i", "'1'");
136 shouldBe("resultArray[1].item", "document.getElementsByTagName('select')[1]");
137 shouldBe("resultArray[2].i", "'2'");
138 shouldBe("resultArray[2].item", "document.getElementsByTagName('select')[2]");
139
140 // HTMLSelectElement
141 var htmlSelectElement = document.getElementsByTagName('select')[0];
142 resultArray = iterateList(htmlSelectElement);
143 shouldBe("resultArray[0].i", "'0'");
144 shouldBe("resultArray[0].item", "document.getElementsByTagName('option')[0]");
145 shouldBe("resultArray[1].i", "'1'");
146 shouldBe("resultArray[1].item", "document.getElementsByTagName('option')[1]");
147 shouldBe("resultArray[2].i", "'2'");
148 shouldBe("resultArray[2].item", "document.getElementsByTagName('option')[2]");
149
150 // StyleSheetList
151 var styleSheetList = document.styleSheets;
152 resultArray = iterateList(styleSheetList);
153 shouldBe("resultArray.length", "6");
154 shouldBe("resultArray[0].i", "'0'");
155 shouldBe("resultArray[0].item", "styleSheetList.item(0)");
156 shouldBe("resultArray[1].i", "'1'");
157 shouldBe("resultArray[1].item", "styleSheetList.item(1)");
158 shouldBe("resultArray[2].i", "'2'");
159 shouldBe("resultArray[2].item", "styleSheetList.item(2)");
160
161 // CSSRuleList
162 var cssRuleList = document.styleSheets[1].cssRules;
163 resultArray = iterateList(cssRuleList);
164 shouldBe("resultArray.length", "5");
165 shouldBe("resultArray[0].i", "'0'");
166 shouldBe("resultArray[0].item", "cssRuleList.item(0)");
167 shouldBe("resultArray[1].i", "'1'");
168 shouldBe("resultArray[1].item", "cssRuleList.item(1)");
169 shouldBe("resultArray[2].i", "'2'");
170 shouldBe("resultArray[2].item", "cssRuleList.item(2)");
171
172 // CSSStyleDeclaration
173 //debug(escapeHTML(document.getElementsByTagName('style')));
174 var cssStyleDeclaration = document.styleSheets[2].cssRules[0].style;
175 resultArray = iterateList(cssStyleDeclaration);
176 shouldBe("resultArray[0].i", "'0'");
177 shouldBe("resultArray[0].item", "cssStyleDeclaration.item(0)");
178 shouldBe("resultArray[1].i", "'1'");
179 shouldBe("resultArray[1].item", "cssStyleDeclaration.item(1)");
180 shouldBe("resultArray[2].i", "'2'");
181 shouldBe("resultArray[2].item", "cssStyleDeclaration.item(2)");
182
183 // CSSValueList
184 var cssValueList = window.getComputedStyle(document.getElementsByTagName('ol')[0]).getPropertyCSSValue('border-spacing');
185 resultArray = iterateList(cssValueList);
186 shouldBe("resultArray.length", "10");
187 shouldBe("resultArray[0].i", "'0'");
188 shouldBe("resultArray[0].item", "cssValueList.item(0)");
189 shouldBe("resultArray[1].i", "'1'");
190 shouldBe("resultArray[1].item", "cssValueList.item(1)");
191
192 // MediaList
193 var mediaList = document.styleSheets[3].cssRules[0].media;
194 resultArray = iterateList(mediaList);
195 shouldBe("resultArray.length", "8");
196 shouldBe("resultArray[0].i", "'0'");
197 shouldBe("resultArray[0].item", "mediaList.item(0)");
198 shouldBe("resultArray[1].i", "'1'");
199 shouldBe("resultArray[1].item", "mediaList.item(1)");
200 shouldBe("resultArray[2].i", "'2'");
201 shouldBe("resultArray[2].item", "mediaList.item(2)");
202
203 debug("");
204
205 document.body.removeChild(testingGround);