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/. */
6 * Test that mozITXTToHTMLConv works properly.
12 let converter = Cc["@mozilla.org/txttohtmlconv;1"].getService(
16 const scanTXTtests = [
19 input: "RFC1738: <URL:http://mozilla.org> then",
20 url: "http://mozilla.org",
23 input: "RFC1738: <URL:mailto:john.doe+test@mozilla.org> then",
24 url: "mailto:john.doe+test@mozilla.org",
28 input: "RFC2396E: <http://mozilla.org/> then",
29 url: "http://mozilla.org/",
32 input: "RFC2396E: <john.doe+test@mozilla.org> then",
33 url: "mailto:john.doe+test@mozilla.org",
37 input: "see www.mozilla.org maybe",
38 url: "http://www.mozilla.org",
41 input: "mail john.doe+test@mozilla.org maybe",
42 url: "mailto:john.doe+test@mozilla.org",
46 input: "see http://www.mozilla.org/maybe today", // Spaces
47 url: "http://www.mozilla.org/maybe",
50 input: 'see "http://www.mozilla.org/maybe today"', // Double quotes
51 url: "http://www.mozilla.org/maybetoday", // spaces ignored
54 input: "see <http://www.mozilla.org/maybe today>", // Angle brackets
55 url: "http://www.mozilla.org/maybetoday", // spaces ignored
59 input: "I mean http://www.mozilla.org/.",
60 url: "http://www.mozilla.org/",
63 input: "you mean http://mozilla.org:80, right?",
64 url: "http://mozilla.org:80",
67 input: "go to http://mozilla.org; then go home",
68 url: "http://mozilla.org",
71 input: "http://mozilla.org! yay!",
72 url: "http://mozilla.org",
75 input: "er, http://mozilla.com?",
76 url: "http://mozilla.com",
79 input: "http://example.org- where things happen",
80 url: "http://example.org",
83 input: "see http://mozilla.org: front page",
84 url: "http://mozilla.org",
87 input: "'http://mozilla.org/': that's the url",
88 url: "http://mozilla.org/",
91 input: "some special http://mozilla.org/?x=.,;!-:x",
92 url: "http://mozilla.org/?x=.,;!-:x",
95 // escape & when producing html
96 input: "'http://example.org/?test=true&success=true': ok",
97 url: "http://example.org/?test=true&success=true",
100 input: "bracket: http://localhost/[1] etc.",
101 url: "http://localhost/",
104 input: "bracket: john.doe+test@mozilla.org[1] etc.",
105 url: "mailto:john.doe+test@mozilla.org",
108 input: "parenthesis: (http://localhost/) etc.",
109 url: "http://localhost/",
112 input: "parenthesis: (john.doe+test@mozilla.org) etc.",
113 url: "mailto:john.doe+test@mozilla.org",
116 input: "(thunderbird)http://mozilla.org/thunderbird",
117 url: "http://mozilla.org/thunderbird",
120 input: "(mail)john.doe+test@mozilla.org",
121 url: "mailto:john.doe+test@mozilla.org",
124 input: "()http://mozilla.org",
125 url: "http://mozilla.org",
129 "parenthesis included: http://kb.mozillazine.org/Performance_(Thunderbird) etc.",
130 url: "http://kb.mozillazine.org/Performance_(Thunderbird)",
133 input: "parenthesis slash bracket: (http://localhost/)[1] etc.",
134 url: "http://localhost/",
137 input: "parenthesis bracket: (http://example.org[1]) etc.",
138 url: "http://example.org",
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",
145 input: "ipv6 2: http://[::ffff:127.0.0.1]/#yay test",
146 url: "http://[::ffff:127.0.0.1]/#yay",
149 input: "ipv6 parenthesis port: (http://[2001:db8::1]:80/) test",
150 url: "http://[2001:db8::1]:80/",
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",
158 input: "bug#1509493 (john@mozilla.org)john@mozilla.org test",
159 url: "mailto:john@mozilla.org",
160 text: "john@mozilla.org",
163 input: "bug#1509493 {john@mozilla.org}john@mozilla.org test",
164 url: "mailto:john@mozilla.org",
165 text: "john@mozilla.org",
169 const scanTXTglyph = [
170 // Some "glyph" testing (not exhaustive, the system supports 16 different
173 input: "this is superscript: x^2",
174 results: ["<sup", "2", "</sup>"],
177 input: "this is plus-minus: +/-",
178 results: ["±"],
181 input: "this is a smiley :)",
185 input: "this is a smiley :-)",
189 input: "this is a smiley :-(",
194 const scanTXTstrings = [
195 "underline", // ASCII
196 "äöüßáéíóúî", // Latin-1
197 "a\u0301c\u0327c\u030Ce\u0309n\u0303t\u0326e\u0308d\u0323",
198 // áçčẻñțëḍ Latin
199 "\u016B\u00F1\u0257\u0119\u0211\u0142\u00ED\u00F1\u0119",
200 // Pseudo-ese ūñɗęȑłíñę
201 "\u01DDu\u0131\u0283\u0279\u01DDpun", // Upside down ǝuıʃɹǝpun
202 "\u03C5\u03C0\u03BF\u03B3\u03C1\u03AC\u03BC\u03BC\u03B9\u03C3\u03B7",
204 "\u0441\u0438\u043B\u044C\u043D\u0443\u044E", // Russian сильную
205 "\u0C2C\u0C32\u0C2E\u0C46\u0C56\u0C28", // Telugu బలమైన
206 "\u508D\u7DDA\u3059\u308B", // Japanese 傍線する
207 "\uD841\uDF0E\uD841\uDF31\uD841\uDF79\uD843\uDC53\uD843\uDC78",
208 // Chinese (supplementary plane)
209 "\uD801\uDC14\uD801\uDC2F\uD801\uDC45\uD801\uDC28\uD801\uDC49\uD801\uDC2F\uD801\uDC3B",
213 const scanTXTstructs = [
217 class: "moz-txt-slash",
222 class: "moz-txt-star",
227 class: "moz-txt-underscore",
232 class: "moz-txt-verticalline",
236 const scanHTMLtests = [
238 input: "http://foo.example.com",
242 input: " <a href='http://a.example.com/'>foo</a>",
246 input: "<abbr>see http://abbr.example.com</abbr>",
250 input: "<!-- see http://comment.example.com/ -->",
254 input: "<!-- greater > -->",
258 input: "<!-- lesser < -->",
263 "<style id='ex'>background-image: url(http://example.com/ex.png);</style>",
267 input: "<style>body > p, body > div { color:blue }</style>",
271 input: "<script>window.location='http://script.example.com/';</script>",
275 input: "<head><title>http://head.example.com/</title></head>",
279 input: "<header>see http://header.example.com</header>",
283 input: "<iframe src='http://iframe.example.com/' />",
287 input: "broken end <script",
292 function hrefLink(url) {
293 return ' href="' + url + '"';
296 function linkText(plaintext) {
297 return ">" + plaintext + "</a>";
300 for (let i = 0; i < scanTXTtests.length; i++) {
301 let t = scanTXTtests[i];
302 let output = converter.scanTXT(t.input, Ci.mozITXTToHTMLConv.kURLs);
303 let link = hrefLink(t.url);
306 text = linkText(t.text);
308 if (!output.includes(link)) {
310 "Unexpected conversion by scanTXT: input=" +
318 if (text && !output.includes(text)) {
320 "Unexpected conversion by scanTXT: input=" +
330 for (let i = 0; i < scanTXTglyph.length; i++) {
331 let t = scanTXTglyph[i];
332 let output = converter.scanTXT(
334 Ci.mozITXTToHTMLConv.kGlyphSubstitution
336 for (let j = 0; j < t.results.length; j++) {
337 if (!output.includes(t.results[j])) {
339 "Unexpected conversion by scanTXT: input=" +
350 for (let i = 0; i < scanTXTstrings.length; ++i) {
351 for (let j = 0; j < scanTXTstructs.length; ++j) {
353 scanTXTstructs[j].delimiter +
355 scanTXTstructs[j].delimiter;
358 scanTXTstructs[j].tag +
360 scanTXTstructs[j].class +
362 '<span class="moz-txt-tag">' +
363 scanTXTstructs[j].delimiter +
366 '<span class="moz-txt-tag">' +
367 scanTXTstructs[j].delimiter +
370 scanTXTstructs[j].tag +
372 let actual = converter.scanTXT(input, Ci.mozITXTToHTMLConv.kStructPhrase);
373 Assert.equal(encodeURIComponent(actual), encodeURIComponent(expected));
377 for (let i = 0; i < scanHTMLtests.length; i++) {
378 let t = scanHTMLtests[i];
379 let output = converter.scanHTML(t.input, Ci.mozITXTToHTMLConv.kURLs);
380 let changed = t.input != output;
381 if (changed != t.shouldChange) {
383 "Unexpected change by scanHTML: changed=" +