1 | dojo.provide("dojox.html.tests.format"); |
---|
2 | dojo.require("dojox.html.format"); |
---|
3 | |
---|
4 | |
---|
5 | doh.register("dojox.html.tests.format", |
---|
6 | [ |
---|
7 | { |
---|
8 | name: "Format: Basic HTML Format test", |
---|
9 | runTest: function(t) { |
---|
10 | // summary: |
---|
11 | // Simple test of basic HTML formatting. |
---|
12 | // description: |
---|
13 | // Simple test of basic HTML formatting. |
---|
14 | var txt = "<div><b>hello</b> this is some text.</div>"; |
---|
15 | var expected = "<div>\n" + |
---|
16 | "\t<b>hello</b> this is some text.\n" + |
---|
17 | "</div>\n"; |
---|
18 | var formattedTxt = dojox.html.format.prettyPrint(txt); |
---|
19 | doh.assertEqual(expected, formattedTxt); |
---|
20 | } |
---|
21 | }, |
---|
22 | { |
---|
23 | name: "Format: Basic HTML Format test with three space indent", |
---|
24 | runTest: function(t) { |
---|
25 | // summary: |
---|
26 | // Simple test of basic HTML formatting with spaced indenting instead of tab |
---|
27 | // description: |
---|
28 | // Simple test of basic HTML formatting with spaced indenting instead of tab |
---|
29 | var txt = "<div><b>hello</b> this is some text.</div>"; |
---|
30 | var expected = "<div>\n" + |
---|
31 | " <b>hello</b> this is some text.\n" + |
---|
32 | "</div>\n"; |
---|
33 | var formattedTxt = dojox.html.format.prettyPrint(txt, 3); |
---|
34 | doh.assertEqual(expected, formattedTxt); |
---|
35 | } |
---|
36 | }, |
---|
37 | { |
---|
38 | name: "Format: Basic HTML Format test with three space indent and custom encoding", |
---|
39 | runTest: function(t) { |
---|
40 | // summary: |
---|
41 | // Simple test of basic HTML formatting with spaced indenting instead of tab |
---|
42 | // description: |
---|
43 | // Simple test of basic HTML formatting with spaced indenting instead of tab |
---|
44 | var txt = "<div><b>hello</b> this is \"some\" entities & text.</div>"; |
---|
45 | var expected = "<div>\n" + |
---|
46 | " <b>hello</b> this is \"some\" entities & text.\n" + |
---|
47 | "</div>\n"; |
---|
48 | var formattedTxt = dojox.html.format.prettyPrint(txt, 3, -1, [["&", "amp"]]); |
---|
49 | doh.assertEqual(expected, formattedTxt); |
---|
50 | } |
---|
51 | }, |
---|
52 | { |
---|
53 | name: "Format: Basic HTML Format test with comment node", |
---|
54 | runTest: function(t) { |
---|
55 | // summary: |
---|
56 | // Simple test of basic HTML formatting with a comment node" |
---|
57 | // description: |
---|
58 | // Simple test of basic HTML formatting with a comment node" |
---|
59 | if(!dojo.isIE){ |
---|
60 | // Hurray for IE, it sometimes just strips comment nodes from the |
---|
61 | // dom. So we can't test this reliably. |
---|
62 | var txt = "<div><!-- This is a comment! --><br></div>"; |
---|
63 | var expected = "<div>\n" + |
---|
64 | " <!--\n" + |
---|
65 | " This is a comment!\n" + |
---|
66 | " -->\n" + |
---|
67 | " <br>\n" + |
---|
68 | "</div>\n"; |
---|
69 | var formattedTxt = dojox.html.format.prettyPrint(txt, 3); |
---|
70 | doh.assertEqual(expected, formattedTxt); |
---|
71 | } |
---|
72 | } |
---|
73 | }, |
---|
74 | { |
---|
75 | name: "Format: Basic HTML Format test with inline tags", |
---|
76 | runTest: function(t) { |
---|
77 | // summary: |
---|
78 | // Simple test of basic HTML formatting with spaced indenting instead of tab |
---|
79 | // description: |
---|
80 | // Simple test of basic HTML formatting with spaced indenting instead of tab |
---|
81 | var txt = "<div><b>hello</b> <a href=\"http://example.com/\">Example.com</a> this is some text after the link.</div>"; |
---|
82 | var expected = "<div>\n" + |
---|
83 | " <b>hello</b> <a href=\"http://example.com/\">Example.com</a> this is some text after the link.\n" + |
---|
84 | "</div>\n"; |
---|
85 | var formattedTxt = dojox.html.format.prettyPrint(txt, 3); |
---|
86 | doh.assertEqual(expected, formattedTxt); |
---|
87 | } |
---|
88 | }, |
---|
89 | { |
---|
90 | name: "Format: Basic HTML Format test with inline tags (2)", |
---|
91 | runTest: function(t) { |
---|
92 | // summary: |
---|
93 | // Simple test of basic HTML formatting with spaced indenting instead of tab |
---|
94 | // description: |
---|
95 | // Simple test of basic HTML formatting with spaced indenting instead of tab |
---|
96 | var txt = "<div><b>hello</b> <br> <a href=\"http://example.com/\">Example.com</a> this is some text after the link.</div>"; |
---|
97 | var expected = "<div>\n" + |
---|
98 | " <b>hello</b>\n"+ |
---|
99 | " <br>\n" + |
---|
100 | " <a href=\"http://example.com/\">Example.com</a> this is some text after the link.\n" + |
---|
101 | "</div>\n"; |
---|
102 | var formattedTxt = dojox.html.format.prettyPrint(txt, 3); |
---|
103 | doh.assertEqual(expected, formattedTxt); |
---|
104 | } |
---|
105 | }, |
---|
106 | { |
---|
107 | name: "Format: Basic HTML Format test with id", |
---|
108 | runTest: function(t) { |
---|
109 | // summary: |
---|
110 | // Simple test of basic HTML formatting with an id attr set. |
---|
111 | // description: |
---|
112 | // Simple test of basic HTML formatting with an id attr set. |
---|
113 | var txt = "<div id=\"myID\"><b>hello</b> this is some text.</div>"; |
---|
114 | var expected = "<div id=\"myID\">\n" + |
---|
115 | "\t<b>hello</b> this is some text.\n" + |
---|
116 | "</div>\n"; |
---|
117 | var formattedTxt = dojox.html.format.prettyPrint(txt); |
---|
118 | doh.assertEqual(expected, formattedTxt); |
---|
119 | } |
---|
120 | }, |
---|
121 | { |
---|
122 | name: "Format: Basic HTML Format test with attributes (sorting attributes)", |
---|
123 | runTest: function(t) { |
---|
124 | // summary: |
---|
125 | // Simple test of basic HTML formatting with an id attr set. |
---|
126 | // description: |
---|
127 | // Simple test of basic HTML formatting with an id attr set. |
---|
128 | var txt = "<div id=\"myID\" style=\"font-weight: bold; font-style: italic;\" foo=\"bar\"><b>hello</b> this is some text.</div>"; |
---|
129 | var expected = "<div foo=\"bar\" id=\"myID\" style=\"font-style: italic; font-weight: bold;\">\n" + |
---|
130 | "\t<b>hello</b> this is some text.\n" + |
---|
131 | "</div>\n"; |
---|
132 | var formattedTxt = dojox.html.format.prettyPrint(txt); |
---|
133 | doh.assertEqual(expected, formattedTxt); |
---|
134 | } |
---|
135 | }, |
---|
136 | { |
---|
137 | name: "Format: Basic HTML Format test with attributes (multiple unquoted)", |
---|
138 | runTest: function(t) { |
---|
139 | // summary: |
---|
140 | // Simple test of basic HTML formatting with an id attr set. |
---|
141 | // description: |
---|
142 | // Simple test of basic HTML formatting with an id attr set. |
---|
143 | var txt = "<p><font id=\"myID\" size=\"6\"><b>hello</b> this is some text.</font></p>"; |
---|
144 | var expected = "<p>\n\t<font id=\"myID\" size=\"6\">" + |
---|
145 | "<b>hello</b> this is some text." + |
---|
146 | "</font>\n</p>\n"; |
---|
147 | var formattedTxt = dojox.html.format.prettyPrint(txt); |
---|
148 | doh.assertEqual(expected, formattedTxt); |
---|
149 | } |
---|
150 | }, |
---|
151 | { |
---|
152 | name: "Format: Basic HTML Format test with style", |
---|
153 | runTest: function(t) { |
---|
154 | // summary: |
---|
155 | // Simple test of basic HTML formatting with an id attr set. |
---|
156 | // description: |
---|
157 | // Simple test of basic HTML formatting with an id attr set. |
---|
158 | var txt = "<div style=\"font-weight: bold;\"><b>hello</b> this is some text.</div>"; |
---|
159 | var expected = "<div style=\"font-weight: bold;\">\n" + |
---|
160 | "\t<b>hello</b> this is some text.\n" + |
---|
161 | "</div>\n"; |
---|
162 | var formattedTxt = dojox.html.format.prettyPrint(txt); |
---|
163 | doh.assertEqual(expected, formattedTxt); |
---|
164 | } |
---|
165 | }, |
---|
166 | { |
---|
167 | name: "Format: Basic HTML Format test with multi style", |
---|
168 | runTest: function(t) { |
---|
169 | // summary: |
---|
170 | // Simple test of basic HTML formatting with an id attr set. |
---|
171 | // description: |
---|
172 | // Simple test of basic HTML formatting with an id attr set. |
---|
173 | var txt = "<div style=\"font-weight: bold; color: red\"><b>hello</b> this is some text.</div>"; |
---|
174 | var expected = "<div style=\"color: red; font-weight: bold;\">\n" + |
---|
175 | "\t<b>hello</b> this is some text.\n" + |
---|
176 | "</div>\n"; |
---|
177 | var formattedTxt = dojox.html.format.prettyPrint(txt); |
---|
178 | doh.assertEqual(expected, formattedTxt); |
---|
179 | } |
---|
180 | }, |
---|
181 | { |
---|
182 | name: "Format: Basic HTML Format with script test", |
---|
183 | runTest: function(t) { |
---|
184 | // summary: |
---|
185 | // Simple test of basic HTML formatting with an embedded script tag. |
---|
186 | // description: |
---|
187 | // Simple test of basic HTML formatting with an embedded script tag. |
---|
188 | var txt = "<div><div>hello</div>this is some text.<script>var foo=\"bar\";\nif(foo !== \"bar\"){\n alert(\"Should not be here!\");\n}</script></div>"; |
---|
189 | var expected = "<div>\n" + |
---|
190 | "\t<div>\n" + |
---|
191 | "\t\thello\n" + |
---|
192 | "\t</div>\n"+ |
---|
193 | "\tthis is some text.\n" + |
---|
194 | "\t<script>\n"+ |
---|
195 | "\t\tvar foo=\"bar\";\n" + |
---|
196 | "\t\tif(foo !== \"bar\"){\n" + |
---|
197 | "\t\t\talert(\"Should not be here!\");\n" + |
---|
198 | "\t\t}\n" + |
---|
199 | "\t</script>\n" + |
---|
200 | "</div>\n"; |
---|
201 | var formattedTxt = dojox.html.format.prettyPrint(txt); |
---|
202 | doh.assertEqual(dojo.trim(expected), dojo.trim(formattedTxt)); |
---|
203 | } |
---|
204 | }, |
---|
205 | { |
---|
206 | name: "Format: Basic HTML Format with script test and three space indent", |
---|
207 | runTest: function(t) { |
---|
208 | // summary: |
---|
209 | // Simple test of basic HTML formatting with an embedded script tag. |
---|
210 | // description: |
---|
211 | // Simple test of basic HTML formatting with an embedded script tag. |
---|
212 | var txt = "<div><div>hello</div>this is some text.<script>var foo=\"bar\";\nif(foo !== \"bar\"){\n alert(\"Should not be here!\");\n}</script></div>"; |
---|
213 | var expected = "<div>\n" + |
---|
214 | " <div>\n" + |
---|
215 | " hello\n" + |
---|
216 | " </div>\n"+ |
---|
217 | " this is some text.\n" + |
---|
218 | " <script>\n"+ |
---|
219 | " var foo=\"bar\";\n" + |
---|
220 | " if(foo !== \"bar\"){\n" + |
---|
221 | " alert(\"Should not be here!\");\n" + |
---|
222 | " }\n" + |
---|
223 | " </script>\n" + |
---|
224 | "</div>\n"; |
---|
225 | var formattedTxt = dojox.html.format.prettyPrint(txt, 3); |
---|
226 | doh.assertEqual(expected, formattedTxt); |
---|
227 | } |
---|
228 | }, |
---|
229 | { |
---|
230 | name: "Format: Basic HTML Format with script test and three space indent, XHTML", |
---|
231 | runTest: function(t) { |
---|
232 | // summary: |
---|
233 | // Simple test of basic HTML formatting with an embedded script tag. |
---|
234 | // description: |
---|
235 | // Simple test of basic HTML formatting with an embedded script tag. |
---|
236 | var txt = "<div><div>hello<br><hr></div>this is some text.<script>var foo=\"bar\";\nif(foo !== \"bar\"){\n alert(\"Should not be here!\");\n}</script></div>"; |
---|
237 | var expected = "<div>\n" + |
---|
238 | " <div>\n" + |
---|
239 | " hello\n" + |
---|
240 | " <br />\n" + |
---|
241 | " <hr />\n" + |
---|
242 | " </div>\n"+ |
---|
243 | " this is some text.\n" + |
---|
244 | " <script>\n"+ |
---|
245 | " var foo=\"bar\";\n" + |
---|
246 | " if(foo !== \"bar\"){\n" + |
---|
247 | " alert(\"Should not be here!\");\n" + |
---|
248 | " }\n" + |
---|
249 | " </script>\n" + |
---|
250 | "</div>\n"; |
---|
251 | var formattedTxt = dojox.html.format.prettyPrint(txt, 3, -1, null, true); |
---|
252 | doh.assertEqual(expected, formattedTxt); |
---|
253 | } |
---|
254 | }, |
---|
255 | { |
---|
256 | name: "Format: Basic HTML Format with <pre> tag", |
---|
257 | runTest: function(t) { |
---|
258 | // summary: |
---|
259 | // Simple test of basic HTML formatting with an embedded pre tag. |
---|
260 | // description: |
---|
261 | // Simple test of basic HTML formatting with an embedded pre tag. |
---|
262 | |
---|
263 | if(!dojo.isIE){ |
---|
264 | // IE generates good pre tags, but I think the endline chars differ |
---|
265 | // so direct comparison fails. When I figure that out, I can enable |
---|
266 | // this test. |
---|
267 | var txt = "<div><pre>hello\nthis is spaced\nWhee!\n</pre></div>"; |
---|
268 | var expected = "<div>\n" + |
---|
269 | "\t<pre>\n" + |
---|
270 | "hello\n" + |
---|
271 | "this is spaced\n" + |
---|
272 | "Whee!\n" + |
---|
273 | "\t</pre>\n"+ |
---|
274 | "</div>\n"; |
---|
275 | var formattedTxt = dojox.html.format.prettyPrint(txt); |
---|
276 | doh.assertEqual(expected, formattedTxt); |
---|
277 | } |
---|
278 | } |
---|
279 | }, |
---|
280 | { |
---|
281 | name: "Format: Basic HTML Format with <pre> tag and three space indent", |
---|
282 | runTest: function(t) { |
---|
283 | // summary: |
---|
284 | // Simple test of basic HTML formatting with an embedded pre tag. |
---|
285 | // description: |
---|
286 | // Simple test of basic HTML formatting with an embedded pre tag. |
---|
287 | if(!dojo.isIE){ |
---|
288 | // IE generates good pre tags, but I think the endline chars differ |
---|
289 | // so direct comparison fails. When I figure that out, I can enable |
---|
290 | // this test. |
---|
291 | var txt = "<div><pre>hello\nthis is spaced\nWhee!\n</pre></div>"; |
---|
292 | var expected = "<div>\n" + |
---|
293 | " <pre>\n" + |
---|
294 | "hello\n" + |
---|
295 | "this is spaced\n" + |
---|
296 | "Whee!\n" + |
---|
297 | " </pre>\n"+ |
---|
298 | "</div>\n"; |
---|
299 | var formattedTxt = dojox.html.format.prettyPrint(txt, 3); |
---|
300 | doh.assertEqual(expected, formattedTxt); |
---|
301 | } |
---|
302 | } |
---|
303 | }, |
---|
304 | { |
---|
305 | name: "Format: Semi-complex HTML format", |
---|
306 | timeout: 10000, |
---|
307 | runTest: function(t) { |
---|
308 | // summary: |
---|
309 | // Simple test of somewhat complex HTML in an external file getting formatted. |
---|
310 | // description: |
---|
311 | // Simple test of basic HTML formatting with an embedded pre tag. |
---|
312 | if(!dojo.isIE){ |
---|
313 | // Still working out minor comparison issues on IE. Sigh. |
---|
314 | // the output is pretty accurate, just need to fix a few things. |
---|
315 | // Like I think the newlines differ or somesuch. |
---|
316 | var deferred = new doh.Deferred(); |
---|
317 | |
---|
318 | var args = { |
---|
319 | url: dojo.moduleUrl("dojox.html.tests", "unformatted.html").toString(), |
---|
320 | handleAs: "text", |
---|
321 | preventCache: true |
---|
322 | } |
---|
323 | var ufd = dojo.xhrGet(args); |
---|
324 | ufd.addCallback(function(html){ |
---|
325 | html = dojox.html.format.prettyPrint(html, 3); |
---|
326 | var fArgs = { |
---|
327 | url: dojo.moduleUrl("dojox.html.tests", "formatted.html").toString(), |
---|
328 | preventCache: true, |
---|
329 | handleAs: "text" |
---|
330 | } |
---|
331 | var fd = dojo.xhrGet(fArgs); |
---|
332 | fd.addCallback(function(fHtml){ |
---|
333 | try{ |
---|
334 | doh.assertEqual(fHtml, html); |
---|
335 | deferred.callback(true); |
---|
336 | }catch(e){ |
---|
337 | deferred.errback(e); |
---|
338 | } |
---|
339 | }); |
---|
340 | fd.addErrback(function(error){ |
---|
341 | deferred.errback(error); |
---|
342 | }); |
---|
343 | }); |
---|
344 | ufd.addErrback(function(err){ |
---|
345 | console.log("Boom!"); |
---|
346 | deferred.errback(err); |
---|
347 | }); |
---|
348 | return deferred; |
---|
349 | } |
---|
350 | return null; |
---|
351 | } |
---|
352 | } |
---|
353 | ] |
---|
354 | ); |
---|