|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Test for nsIDOMWindowUtils.getTranslationNodes</title> |
|
5 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
|
7 </head> |
|
8 <body onload="runTest()"> |
|
9 <script type="application/javascript"> |
|
10 var utils = SpecialPowers.wrap(window). |
|
11 QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor). |
|
12 getInterface(SpecialPowers.Ci.nsIDOMWindowUtils); |
|
13 |
|
14 |
|
15 function testTranslationRoot(rootNode) { |
|
16 var translationNodes = utils.getTranslationNodes(rootNode); |
|
17 |
|
18 var expectedResult = rootNode.getAttribute("expected"); |
|
19 var expectedLength = expectedResult.split(" ").length; |
|
20 |
|
21 is(translationNodes.length, expectedLength, |
|
22 "Correct number of translation nodes for testcase " + rootNode.id); |
|
23 |
|
24 var resultList = []; |
|
25 for (var i = 0; i < translationNodes.length; i++) { |
|
26 var node = translationNodes.item(i).localName; |
|
27 if (translationNodes.isTranslationRootAtIndex(i)) { |
|
28 node += "[root]" |
|
29 } |
|
30 resultList.push(node); |
|
31 } |
|
32 |
|
33 is(resultList.length, translationNodes.length, |
|
34 "Correct number of translation nodes for testcase " + rootNode.id); |
|
35 |
|
36 is(resultList.join(" "), expectedResult, |
|
37 "Correct list of translation nodes for testcase " + rootNode.id); |
|
38 } |
|
39 |
|
40 function runTest() { |
|
41 isnot(utils, null, "nsIDOMWindowUtils"); |
|
42 |
|
43 var testcases = document.querySelectorAll("div[expected]"); |
|
44 for (var testcase of testcases) { |
|
45 testTranslationRoot(testcase); |
|
46 } |
|
47 |
|
48 var testiframe = document.getElementById("testiframe"); |
|
49 var iframediv = testiframe.contentDocument.querySelector("div"); |
|
50 try { |
|
51 var foo = utils.getTranslationNodes(iframediv); |
|
52 ok(false, "Cannot use a node from a different document"); |
|
53 } catch (e) { |
|
54 is(e.name, "WrongDocumentError", "Cannot use a node from a different document"); |
|
55 } |
|
56 |
|
57 SimpleTest.finish(); |
|
58 } |
|
59 |
|
60 SimpleTest.waitForExplicitFinish(); |
|
61 </script> |
|
62 |
|
63 <!-- Test that an inline element inside a root is not a root --> |
|
64 <div id="testcase1" |
|
65 expected="div[root] span"> |
|
66 <div> |
|
67 lorem ipsum <span>dolor</span> sit amet |
|
68 </div> |
|
69 </div> |
|
70 |
|
71 <!-- Test that a usually inline element becomes a root if it is |
|
72 displayed as a block --> |
|
73 <div id="testcase2" |
|
74 expected="div[root] span[root]"> |
|
75 <div> |
|
76 lorem ipsum <span style="display: block;">dolor</span> sit amet |
|
77 </div> |
|
78 </div> |
|
79 |
|
80 <!-- Test that the content-less <div> is ignored and only the |
|
81 <p> with content is returned --> |
|
82 <div id="testcase3" |
|
83 expected="p[root]"> |
|
84 <div> |
|
85 <p>lorem ipsum</p> |
|
86 </div> |
|
87 </div> |
|
88 |
|
89 <!-- Test that an inline element which the parent is not a root |
|
90 becomes a root --> |
|
91 <div id="testcase4" |
|
92 expected="span[root]"> |
|
93 <div> |
|
94 <span>lorem ipsum</span> |
|
95 </div> |
|
96 </div> |
|
97 |
|
98 <!-- Test siblings --> |
|
99 <div id="testcase5" |
|
100 expected="li[root] li[root]"> |
|
101 <ul> |
|
102 <li>lorem</li> |
|
103 <li>ipsum</li> |
|
104 </ul> |
|
105 </div> |
|
106 |
|
107 <!-- Test <ul> with content outside li --> |
|
108 <div id="testcase6" |
|
109 expected="ul[root] li[root] li[root]"> |
|
110 <ul>Lorem |
|
111 <li>lorem</li> |
|
112 <li>ipsum</li> |
|
113 </ul> |
|
114 </div> |
|
115 |
|
116 <!-- Test inline siblings --> |
|
117 <div id="testcase7" |
|
118 expected="ul[root] li li"> |
|
119 <ul>Lorem |
|
120 <li style="display: inline">lorem</li> |
|
121 <li style="display: inline">ipsum</li> |
|
122 </ul> |
|
123 </div> |
|
124 |
|
125 <!-- Test inline siblings becoming roots --> |
|
126 <div id="testcase8" |
|
127 expected="li[root] li[root]"> |
|
128 <ul> |
|
129 <li style="display: inline">lorem</li> |
|
130 <li style="display: inline">ipsum</li> |
|
131 </ul> |
|
132 </div> |
|
133 |
|
134 <!-- Test that nodes with only punctuation, whitespace |
|
135 or numbers are ignored --> |
|
136 <div id="testcase9" |
|
137 expected="li[root] li[root]"> |
|
138 <ul> |
|
139 <li>lorem</li> |
|
140 <li>ipsum</li> |
|
141 <li>-.,;'/!@#$%^*()</li> |
|
142 <li>0123456789</li> |
|
143 <li> |
|
144 </li> |
|
145 </ul> |
|
146 </div> |
|
147 |
|
148 <!-- Test paragraphs --> |
|
149 <div id="testcase10" |
|
150 expected="p[root] a b p[root] a b"> |
|
151 <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b>amet</b>, consetetur</p> |
|
152 <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b>amet</b>, consetetur</p> |
|
153 </div> |
|
154 |
|
155 <!-- Test that a display:none element is not ignored --> |
|
156 <div id="testcase11" |
|
157 expected="p[root] a b"> |
|
158 <p>Lorem ipsum <a href="a.htm">dolor</a> sit <b style="display:none">amet</b>, consetetur</p> |
|
159 </div> |
|
160 |
|
161 <!-- Test that deep nesting does not cause useless content to be returned --> |
|
162 <div id="testcase12" |
|
163 expected="p[root]"> |
|
164 <div> |
|
165 <div> |
|
166 <div> |
|
167 <p>Lorem ipsum</p> |
|
168 </div> |
|
169 </div> |
|
170 </div> |
|
171 </div> |
|
172 |
|
173 <!-- Test that deep nesting does not cause useless content to be returned --> |
|
174 <div id="testcase13" |
|
175 expected="div[root] p[root]"> |
|
176 <div>Lorem ipsum |
|
177 <div> |
|
178 <div> |
|
179 <p>Lorem ipsum</p> |
|
180 </div> |
|
181 </div> |
|
182 </div> |
|
183 </div> |
|
184 |
|
185 <!-- Test that non-html elements and elements that usually have non-translatable |
|
186 content are ignored --> |
|
187 <div id="testcase14" |
|
188 expected="div[root]"> |
|
189 <div> |
|
190 Lorem Ipsum |
|
191 <noscript>Lorem Ipsum</noscript> |
|
192 <style>.dummyClass { color: blue; }</style> |
|
193 <script> /* script tag */ </script> |
|
194 <code> code </code> |
|
195 <iframe id="testiframe" |
|
196 src="data:text/html,<div>Lorem ipsum</div>"> |
|
197 </iframe> |
|
198 <svg>lorem</svg> |
|
199 <math>ipsum</math> |
|
200 </div> |
|
201 </div> |
|
202 |
|
203 <!-- Test that nesting of inline elements won't produce roots as long as |
|
204 the parents are in the list of translation nodes --> |
|
205 <div id="testcase15" |
|
206 expected="p[root] a b span em"> |
|
207 <p>Lorem <a>ipsum <b>dolor <span>sit</span> amet</b></a>, <em>consetetur</em></p> |
|
208 </div> |
|
209 </body> |
|
210 </html> |