| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
if(!dojo.isIE){ |
| 60 |
|
| 61 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 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 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 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 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 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 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 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 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 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 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
if(!dojo.isIE){ |
| 264 |
|
| 265 |
|
| 266 |
|
| 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 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
if(!dojo.isIE){ |
| 288 |
|
| 289 |
|
| 290 |
|
| 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 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
if(!dojo.isIE){ |
| 313 |
|
| 314 |
|
| 315 |
|
| 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 |
); |