Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_mozTXTToHTMLConv.js
blob1e93a440add9a1bb91062eb34edace6ddfeec761
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/. */
5 /**
6  * Test that mozITXTToHTMLConv works properly.
7  */
9 "use strict";
11 function run_test() {
12   let converter = Cc["@mozilla.org/txttohtmlconv;1"].getService(
13     Ci.mozITXTToHTMLConv
14   );
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&amp;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&amp;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   ];
169   const scanTXTglyph = [
170     // Some "glyph" testing (not exhaustive, the system supports 16 different
171     // smiley types).
172     {
173       input: "this is superscript: x^2",
174       results: ["<sup", "2", "</sup>"],
175     },
176     {
177       input: "this is plus-minus: +/-",
178       results: ["&plusmn;"],
179     },
180     {
181       input: "this is a smiley :)",
182       results: ["🙂"],
183     },
184     {
185       input: "this is a smiley :-)",
186       results: ["🙂"],
187     },
188     {
189       input: "this is a smiley :-(",
190       results: ["🙁"],
191     },
192   ];
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",
203     // Greek υπογράμμιση
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",
210     // Deseret 𐐔𐐯𐑅𐐨𐑉𐐯𐐻
211   ];
213   const scanTXTstructs = [
214     {
215       delimiter: "/",
216       tag: "i",
217       class: "moz-txt-slash",
218     },
219     {
220       delimiter: "*",
221       tag: "b",
222       class: "moz-txt-star",
223     },
224     {
225       delimiter: "_",
226       tag: "span",
227       class: "moz-txt-underscore",
228     },
229     {
230       delimiter: "|",
231       tag: "code",
232       class: "moz-txt-verticalline",
233     },
234   ];
236   const scanHTMLtests = [
237     {
238       input: "http://foo.example.com",
239       shouldChange: true,
240     },
241     {
242       input: " <a href='http://a.example.com/'>foo</a>",
243       shouldChange: false,
244     },
245     {
246       input: "<abbr>see http://abbr.example.com</abbr>",
247       shouldChange: true,
248     },
249     {
250       input: "<!-- see http://comment.example.com/ -->",
251       shouldChange: false,
252     },
253     {
254       input: "<!-- greater > -->",
255       shouldChange: false,
256     },
257     {
258       input: "<!-- lesser < -->",
259       shouldChange: false,
260     },
261     {
262       input:
263         "<style id='ex'>background-image: url(http://example.com/ex.png);</style>",
264       shouldChange: false,
265     },
266     {
267       input: "<style>body > p, body > div { color:blue }</style>",
268       shouldChange: false,
269     },
270     {
271       input: "<script>window.location='http://script.example.com/';</script>",
272       shouldChange: false,
273     },
274     {
275       input: "<head><title>http://head.example.com/</title></head>",
276       shouldChange: false,
277     },
278     {
279       input: "<header>see http://header.example.com</header>",
280       shouldChange: true,
281     },
282     {
283       input: "<iframe src='http://iframe.example.com/' />",
284       shouldChange: false,
285     },
286     {
287       input: "broken end <script",
288       shouldChange: false,
289     },
290   ];
292   function hrefLink(url) {
293     return ' href="' + url + '"';
294   }
296   function linkText(plaintext) {
297     return ">" + plaintext + "</a>";
298   }
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);
304     let text;
305     if (t.text) {
306       text = linkText(t.text);
307     }
308     if (!output.includes(link)) {
309       do_throw(
310         "Unexpected conversion by scanTXT: input=" +
311           t.input +
312           ", output=" +
313           output +
314           ", link=" +
315           link
316       );
317     }
318     if (text && !output.includes(text)) {
319       do_throw(
320         "Unexpected conversion by scanTXT: input=" +
321           t.input +
322           ", output=" +
323           output +
324           ", text=" +
325           text
326       );
327     }
328   }
330   for (let i = 0; i < scanTXTglyph.length; i++) {
331     let t = scanTXTglyph[i];
332     let output = converter.scanTXT(
333       t.input,
334       Ci.mozITXTToHTMLConv.kGlyphSubstitution
335     );
336     for (let j = 0; j < t.results.length; j++) {
337       if (!output.includes(t.results[j])) {
338         do_throw(
339           "Unexpected conversion by scanTXT: input=" +
340             t.input +
341             ", output=" +
342             output +
343             ", expected=" +
344             t.results[j]
345         );
346       }
347     }
348   }
350   for (let i = 0; i < scanTXTstrings.length; ++i) {
351     for (let j = 0; j < scanTXTstructs.length; ++j) {
352       let input =
353         scanTXTstructs[j].delimiter +
354         scanTXTstrings[i] +
355         scanTXTstructs[j].delimiter;
356       let expected =
357         "<" +
358         scanTXTstructs[j].tag +
359         ' class="' +
360         scanTXTstructs[j].class +
361         '">' +
362         '<span class="moz-txt-tag">' +
363         scanTXTstructs[j].delimiter +
364         "</span>" +
365         scanTXTstrings[i] +
366         '<span class="moz-txt-tag">' +
367         scanTXTstructs[j].delimiter +
368         "</span>" +
369         "</" +
370         scanTXTstructs[j].tag +
371         ">";
372       let actual = converter.scanTXT(input, Ci.mozITXTToHTMLConv.kStructPhrase);
373       Assert.equal(encodeURIComponent(actual), encodeURIComponent(expected));
374     }
375   }
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) {
382       do_throw(
383         "Unexpected change by scanHTML: changed=" +
384           changed +
385           ", shouldChange=" +
386           t.shouldChange +
387           ", \ninput=" +
388           t.input +
389           ", \noutput=" +
390           output
391       );
392     }
393   }