test_mozTXTToHTMLConv.js (10653B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * Test that mozITXTToHTMLConv works properly. 7 */ 8 9 "use strict"; 10 11 function run_test() { 12 let converter = Cc["@mozilla.org/txttohtmlconv;1"].getService( 13 Ci.mozITXTToHTMLConv 14 ); 15 16 const scanTXTtests = [ 17 // -- RFC1738 18 { 19 input: "RFC1738: <URL:http://mozilla.org> then", 20 url: "http://mozilla.org", 21 }, 22 { 23 input: "RFC1738: <URL:mailto:john.doe+test@mozilla.org> then", 24 url: "mailto:john.doe+test@mozilla.org", 25 }, 26 // -- RFC2396E 27 { 28 input: "RFC2396E: <http://mozilla.org/> then", 29 url: "http://mozilla.org/", 30 }, 31 { 32 input: "RFC2396E: <john.doe+test@mozilla.org> then", 33 url: "mailto:john.doe+test@mozilla.org", 34 }, 35 // -- abbreviated 36 { 37 input: "see www.mozilla.org maybe", 38 url: "http://www.mozilla.org", 39 }, 40 { 41 input: "mail john.doe+test@mozilla.org maybe", 42 url: "mailto:john.doe+test@mozilla.org", 43 }, 44 // -- delimiters 45 { 46 input: "see http://www.mozilla.org/maybe today", // Spaces 47 url: "http://www.mozilla.org/maybe", 48 }, 49 { 50 input: 'see "http://www.mozilla.org/maybe today"', // Double quotes 51 url: "http://www.mozilla.org/maybetoday", // spaces ignored 52 }, 53 { 54 input: "see <http://www.mozilla.org/maybe today>", // Angle brackets 55 url: "http://www.mozilla.org/maybetoday", // spaces ignored 56 }, 57 // -- freetext 58 { 59 input: "I mean http://www.mozilla.org/.", 60 url: "http://www.mozilla.org/", 61 }, 62 { 63 input: "you mean http://mozilla.org:80, right?", 64 url: "http://mozilla.org:80", 65 }, 66 { 67 input: "go to http://mozilla.org; then go home", 68 url: "http://mozilla.org", 69 }, 70 { 71 input: "http://mozilla.org! yay!", 72 url: "http://mozilla.org", 73 }, 74 { 75 input: "er, http://mozilla.com?", 76 url: "http://mozilla.com", 77 }, 78 { 79 input: "http://example.org- where things happen", 80 url: "http://example.org", 81 }, 82 { 83 input: "see http://mozilla.org: front page", 84 url: "http://mozilla.org", 85 }, 86 { 87 input: "'http://mozilla.org/': that's the url", 88 url: "http://mozilla.org/", 89 }, 90 { 91 input: "some special http://mozilla.org/?x=.,;!-:x", 92 url: "http://mozilla.org/?x=.,;!-:x", 93 }, 94 { 95 // escape & when producing html 96 input: "'http://example.org/?test=true&success=true': ok", 97 url: "http://example.org/?test=true&success=true", 98 }, 99 { 100 input: "bracket: http://localhost/[1] etc.", 101 url: "http://localhost/", 102 }, 103 { 104 input: "bracket: john.doe+test@mozilla.org[1] etc.", 105 url: "mailto:john.doe+test@mozilla.org", 106 }, 107 { 108 input: "parenthesis: (http://localhost/) etc.", 109 url: "http://localhost/", 110 }, 111 { 112 input: "parenthesis: (john.doe+test@mozilla.org) etc.", 113 url: "mailto:john.doe+test@mozilla.org", 114 }, 115 { 116 input: "(thunderbird)http://mozilla.org/thunderbird", 117 url: "http://mozilla.org/thunderbird", 118 }, 119 { 120 input: "(mail)john.doe+test@mozilla.org", 121 url: "mailto:john.doe+test@mozilla.org", 122 }, 123 { 124 input: "()http://mozilla.org", 125 url: "http://mozilla.org", 126 }, 127 { 128 input: 129 "parenthesis included: http://kb.mozillazine.org/Performance_(Thunderbird) etc.", 130 url: "http://kb.mozillazine.org/Performance_(Thunderbird)", 131 }, 132 { 133 input: "parenthesis slash bracket: (http://localhost/)[1] etc.", 134 url: "http://localhost/", 135 }, 136 { 137 input: "parenthesis bracket: (http://example.org[1]) etc.", 138 url: "http://example.org", 139 }, 140 { 141 input: "ipv6 1: https://[1080::8:800:200C:417A]/foo?bar=x test", 142 url: "https://[1080::8:800:200C:417A]/foo?bar=x", 143 }, 144 { 145 input: "ipv6 2: http://[::ffff:127.0.0.1]/#yay test", 146 url: "http://[::ffff:127.0.0.1]/#yay", 147 }, 148 { 149 input: "ipv6 parenthesis port: (http://[2001:db8::1]:80/) test", 150 url: "http://[2001:db8::1]:80/", 151 }, 152 { 153 input: 154 "test http://www.map.com/map.php?t=Nova_Scotia&markers=//Not_a_survey||description=plm2 test", 155 url: "http://www.map.com/map.php?t=Nova_Scotia&markers=//Not_a_survey||description=plm2", 156 }, 157 { 158 input: "bug#1509493 (john@mozilla.org)john@mozilla.org test", 159 url: "mailto:john@mozilla.org", 160 text: "john@mozilla.org", 161 }, 162 { 163 input: "bug#1509493 {john@mozilla.org}john@mozilla.org test", 164 url: "mailto:john@mozilla.org", 165 text: "john@mozilla.org", 166 }, 167 { 168 // stripped whitespace doesn't crash 169 input: "www\f\f\f\f\f.mozilla.org", 170 url: "http://www.mozilla.org", 171 }, 172 ]; 173 174 const scanTXTglyph = [ 175 // Some "glyph" testing (not exhaustive, the system supports 16 different 176 // smiley types). 177 { 178 input: "this is superscript: x^2", 179 results: ["<sup", "2", "</sup>"], 180 }, 181 { 182 input: "this is plus-minus: +/-", 183 results: ["±"], 184 }, 185 { 186 input: "this is a smiley :)", 187 results: ["🙂"], 188 }, 189 { 190 input: "this is a smiley :-)", 191 results: ["🙂"], 192 }, 193 { 194 input: "this is a smiley :-(", 195 results: ["🙁"], 196 }, 197 ]; 198 199 const scanTXTstrings = [ 200 "underline", // ASCII 201 "äöüßáéíóúî", // Latin-1 202 "a\u0301c\u0327c\u030Ce\u0309n\u0303t\u0326e\u0308d\u0323", 203 // áçčẻñțëḍ Latin 204 "\u016B\u00F1\u0257\u0119\u0211\u0142\u00ED\u00F1\u0119", 205 // Pseudo-ese ūñɗęȑłíñę 206 "\u01DDu\u0131\u0283\u0279\u01DDpun", // Upside down ǝuıʃɹǝpun 207 "\u03C5\u03C0\u03BF\u03B3\u03C1\u03AC\u03BC\u03BC\u03B9\u03C3\u03B7", 208 // Greek υπογράμμιση 209 "\u0441\u0438\u043B\u044C\u043D\u0443\u044E", // Russian сильную 210 "\u0C2C\u0C32\u0C2E\u0C46\u0C56\u0C28", // Telugu బలమైన 211 "\u508D\u7DDA\u3059\u308B", // Japanese 傍線する 212 "\uD841\uDF0E\uD841\uDF31\uD841\uDF79\uD843\uDC53\uD843\uDC78", 213 // Chinese (supplementary plane) 214 "\uD801\uDC14\uD801\uDC2F\uD801\uDC45\uD801\uDC28\uD801\uDC49\uD801\uDC2F\uD801\uDC3B", 215 // Deseret 𐐔𐐯𐑅𐐨𐑉𐐯𐐻 216 ]; 217 218 const scanTXTstructs = [ 219 { 220 delimiter: "/", 221 tag: "i", 222 class: "moz-txt-slash", 223 }, 224 { 225 delimiter: "*", 226 tag: "b", 227 class: "moz-txt-star", 228 }, 229 { 230 delimiter: "_", 231 tag: "span", 232 class: "moz-txt-underscore", 233 }, 234 { 235 delimiter: "|", 236 tag: "code", 237 class: "moz-txt-verticalline", 238 }, 239 ]; 240 241 const scanHTMLtests = [ 242 { 243 input: "http://foo.example.com", 244 shouldChange: true, 245 }, 246 { 247 input: " <a href='http://a.example.com/'>foo</a>", 248 shouldChange: false, 249 }, 250 { 251 input: "<abbr>see http://abbr.example.com</abbr>", 252 shouldChange: true, 253 }, 254 { 255 input: "<!-- see http://comment.example.com/ -->", 256 shouldChange: false, 257 }, 258 { 259 input: "<!-- greater > -->", 260 shouldChange: false, 261 }, 262 { 263 input: "<!-- lesser < -->", 264 shouldChange: false, 265 }, 266 { 267 input: 268 "<style id='ex'>background-image: url(http://example.com/ex.png);</style>", 269 shouldChange: false, 270 }, 271 { 272 input: "<style>body > p, body > div { color:blue }</style>", 273 shouldChange: false, 274 }, 275 { 276 input: "<script>window.location='http://script.example.com/';</script>", 277 shouldChange: false, 278 }, 279 { 280 input: "<head><title>http://head.example.com/</title></head>", 281 shouldChange: false, 282 }, 283 { 284 input: "<header>see http://header.example.com</header>", 285 shouldChange: true, 286 }, 287 { 288 input: "<iframe src='http://iframe.example.com/' />", 289 shouldChange: false, 290 }, 291 { 292 input: "broken end <script", 293 shouldChange: false, 294 }, 295 ]; 296 297 function hrefLink(url) { 298 return ' href="' + url + '"'; 299 } 300 301 function linkText(plaintext) { 302 return ">" + plaintext + "</a>"; 303 } 304 305 for (let i = 0; i < scanTXTtests.length; i++) { 306 let t = scanTXTtests[i]; 307 let output = converter.scanTXT(t.input, Ci.mozITXTToHTMLConv.kURLs); 308 let link = hrefLink(t.url); 309 let text; 310 if (t.text) { 311 text = linkText(t.text); 312 } 313 if (!output.includes(link)) { 314 do_throw( 315 "Unexpected conversion by scanTXT: input=" + 316 t.input + 317 ", output=" + 318 output + 319 ", link=" + 320 link 321 ); 322 } 323 if (text && !output.includes(text)) { 324 do_throw( 325 "Unexpected conversion by scanTXT: input=" + 326 t.input + 327 ", output=" + 328 output + 329 ", text=" + 330 text 331 ); 332 } 333 } 334 335 for (let i = 0; i < scanTXTglyph.length; i++) { 336 let t = scanTXTglyph[i]; 337 let output = converter.scanTXT( 338 t.input, 339 Ci.mozITXTToHTMLConv.kGlyphSubstitution 340 ); 341 for (let j = 0; j < t.results.length; j++) { 342 if (!output.includes(t.results[j])) { 343 do_throw( 344 "Unexpected conversion by scanTXT: input=" + 345 t.input + 346 ", output=" + 347 output + 348 ", expected=" + 349 t.results[j] 350 ); 351 } 352 } 353 } 354 355 for (let i = 0; i < scanTXTstrings.length; ++i) { 356 for (let j = 0; j < scanTXTstructs.length; ++j) { 357 let input = 358 scanTXTstructs[j].delimiter + 359 scanTXTstrings[i] + 360 scanTXTstructs[j].delimiter; 361 let expected = 362 "<" + 363 scanTXTstructs[j].tag + 364 ' class="' + 365 scanTXTstructs[j].class + 366 '">' + 367 '<span class="moz-txt-tag">' + 368 scanTXTstructs[j].delimiter + 369 "</span>" + 370 scanTXTstrings[i] + 371 '<span class="moz-txt-tag">' + 372 scanTXTstructs[j].delimiter + 373 "</span>" + 374 "</" + 375 scanTXTstructs[j].tag + 376 ">"; 377 let actual = converter.scanTXT(input, Ci.mozITXTToHTMLConv.kStructPhrase); 378 Assert.equal(encodeURIComponent(actual), encodeURIComponent(expected)); 379 } 380 } 381 382 for (let i = 0; i < scanHTMLtests.length; i++) { 383 let t = scanHTMLtests[i]; 384 let output = converter.scanHTML(t.input, Ci.mozITXTToHTMLConv.kURLs); 385 let changed = t.input != output; 386 if (changed != t.shouldChange) { 387 do_throw( 388 "Unexpected change by scanHTML: changed=" + 389 changed + 390 ", shouldChange=" + 391 t.shouldChange + 392 ", \ninput=" + 393 t.input + 394 ", \noutput=" + 395 output 396 ); 397 } 398 } 399 }