summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_mozTXTToHTMLConv.js
blob: 1e93a440add9a1bb91062eb34edace6ddfeec761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Test that mozITXTToHTMLConv works properly.
 */

"use strict";

function run_test() {
  let converter = Cc["@mozilla.org/txttohtmlconv;1"].getService(
    Ci.mozITXTToHTMLConv
  );

  const scanTXTtests = [
    // -- RFC1738
    {
      input: "RFC1738: <URL:http://mozilla.org> then",
      url: "http://mozilla.org",
    },
    {
      input: "RFC1738: <URL:mailto:john.doe+test@mozilla.org> then",
      url: "mailto:john.doe+test@mozilla.org",
    },
    // -- RFC2396E
    {
      input: "RFC2396E: <http://mozilla.org/> then",
      url: "http://mozilla.org/",
    },
    {
      input: "RFC2396E: <john.doe+test@mozilla.org> then",
      url: "mailto:john.doe+test@mozilla.org",
    },
    // -- abbreviated
    {
      input: "see www.mozilla.org maybe",
      url: "http://www.mozilla.org",
    },
    {
      input: "mail john.doe+test@mozilla.org maybe",
      url: "mailto:john.doe+test@mozilla.org",
    },
    // -- delimiters
    {
      input: "see http://www.mozilla.org/maybe today", // Spaces
      url: "http://www.mozilla.org/maybe",
    },
    {
      input: 'see "http://www.mozilla.org/maybe today"', // Double quotes
      url: "http://www.mozilla.org/maybetoday", // spaces ignored
    },
    {
      input: "see <http://www.mozilla.org/maybe today>", // Angle brackets
      url: "http://www.mozilla.org/maybetoday", // spaces ignored
    },
    // -- freetext
    {
      input: "I mean http://www.mozilla.org/.",
      url: "http://www.mozilla.org/",
    },
    {
      input: "you mean http://mozilla.org:80, right?",
      url: "http://mozilla.org:80",
    },
    {
      input: "go to http://mozilla.org; then go home",
      url: "http://mozilla.org",
    },
    {
      input: "http://mozilla.org! yay!",
      url: "http://mozilla.org",
    },
    {
      input: "er, http://mozilla.com?",
      url: "http://mozilla.com",
    },
    {
      input: "http://example.org- where things happen",
      url: "http://example.org",
    },
    {
      input: "see http://mozilla.org: front page",
      url: "http://mozilla.org",
    },
    {
      input: "'http://mozilla.org/': that's the url",
      url: "http://mozilla.org/",
    },
    {
      input: "some special http://mozilla.org/?x=.,;!-:x",
      url: "http://mozilla.org/?x=.,;!-:x",
    },
    {
      // escape & when producing html
      input: "'http://example.org/?test=true&success=true': ok",
      url: "http://example.org/?test=true&amp;success=true",
    },
    {
      input: "bracket: http://localhost/[1] etc.",
      url: "http://localhost/",
    },
    {
      input: "bracket: john.doe+test@mozilla.org[1] etc.",
      url: "mailto:john.doe+test@mozilla.org",
    },
    {
      input: "parenthesis: (http://localhost/) etc.",
      url: "http://localhost/",
    },
    {
      input: "parenthesis: (john.doe+test@mozilla.org) etc.",
      url: "mailto:john.doe+test@mozilla.org",
    },
    {
      input: "(thunderbird)http://mozilla.org/thunderbird",
      url: "http://mozilla.org/thunderbird",
    },
    {
      input: "(mail)john.doe+test@mozilla.org",
      url: "mailto:john.doe+test@mozilla.org",
    },
    {
      input: "()http://mozilla.org",
      url: "http://mozilla.org",
    },
    {
      input:
        "parenthesis included: http://kb.mozillazine.org/Performance_(Thunderbird) etc.",
      url: "http://kb.mozillazine.org/Performance_(Thunderbird)",
    },
    {
      input: "parenthesis slash bracket: (http://localhost/)[1] etc.",
      url: "http://localhost/",
    },
    {
      input: "parenthesis bracket: (http://example.org[1]) etc.",
      url: "http://example.org",
    },
    {
      input: "ipv6 1: https://[1080::8:800:200C:417A]/foo?bar=x test",
      url: "https://[1080::8:800:200C:417A]/foo?bar=x",
    },
    {
      input: "ipv6 2: http://[::ffff:127.0.0.1]/#yay test",
      url: "http://[::ffff:127.0.0.1]/#yay",
    },
    {
      input: "ipv6 parenthesis port: (http://[2001:db8::1]:80/) test",
      url: "http://[2001:db8::1]:80/",
    },
    {
      input:
        "test http://www.map.com/map.php?t=Nova_Scotia&markers=//Not_a_survey||description=plm2 test",
      url: "http://www.map.com/map.php?t=Nova_Scotia&amp;markers=//Not_a_survey||description=plm2",
    },
    {
      input: "bug#1509493 (john@mozilla.org)john@mozilla.org test",
      url: "mailto:john@mozilla.org",
      text: "john@mozilla.org",
    },
    {
      input: "bug#1509493 {john@mozilla.org}john@mozilla.org test",
      url: "mailto:john@mozilla.org",
      text: "john@mozilla.org",
    },
  ];

  const scanTXTglyph = [
    // Some "glyph" testing (not exhaustive, the system supports 16 different
    // smiley types).
    {
      input: "this is superscript: x^2",
      results: ["<sup", "2", "</sup>"],
    },
    {
      input: "this is plus-minus: +/-",
      results: ["&plusmn;"],
    },
    {
      input: "this is a smiley :)",
      results: ["🙂"],
    },
    {
      input: "this is a smiley :-)",
      results: ["🙂"],
    },
    {
      input: "this is a smiley :-(",
      results: ["🙁"],
    },
  ];

  const scanTXTstrings = [
    "underline", // ASCII
    "äöüßáéíóúî", // Latin-1
    "a\u0301c\u0327c\u030Ce\u0309n\u0303t\u0326e\u0308d\u0323",
    // áçčẻñțëḍ Latin
    "\u016B\u00F1\u0257\u0119\u0211\u0142\u00ED\u00F1\u0119",
    // Pseudo-ese ūñɗęȑłíñę
    "\u01DDu\u0131\u0283\u0279\u01DDpun", // Upside down ǝuıʃɹǝpun
    "\u03C5\u03C0\u03BF\u03B3\u03C1\u03AC\u03BC\u03BC\u03B9\u03C3\u03B7",
    // Greek υπογράμμιση
    "\u0441\u0438\u043B\u044C\u043D\u0443\u044E", // Russian сильную
    "\u0C2C\u0C32\u0C2E\u0C46\u0C56\u0C28", // Telugu బలమైన
    "\u508D\u7DDA\u3059\u308B", // Japanese 傍線する
    "\uD841\uDF0E\uD841\uDF31\uD841\uDF79\uD843\uDC53\uD843\uDC78",
    // Chinese (supplementary plane)
    "\uD801\uDC14\uD801\uDC2F\uD801\uDC45\uD801\uDC28\uD801\uDC49\uD801\uDC2F\uD801\uDC3B",
    // Deseret 𐐔𐐯𐑅𐐨𐑉𐐯𐐻
  ];

  const scanTXTstructs = [
    {
      delimiter: "/",
      tag: "i",
      class: "moz-txt-slash",
    },
    {
      delimiter: "*",
      tag: "b",
      class: "moz-txt-star",
    },
    {
      delimiter: "_",
      tag: "span",
      class: "moz-txt-underscore",
    },
    {
      delimiter: "|",
      tag: "code",
      class: "moz-txt-verticalline",
    },
  ];

  const scanHTMLtests = [
    {
      input: "http://foo.example.com",
      shouldChange: true,
    },
    {
      input: " <a href='http://a.example.com/'>foo</a>",
      shouldChange: false,
    },
    {
      input: "<abbr>see http://abbr.example.com</abbr>",
      shouldChange: true,
    },
    {
      input: "<!-- see http://comment.example.com/ -->",
      shouldChange: false,
    },
    {
      input: "<!-- greater > -->",
      shouldChange: false,
    },
    {
      input: "<!-- lesser < -->",
      shouldChange: false,
    },
    {
      input:
        "<style id='ex'>background-image: url(http://example.com/ex.png);</style>",
      shouldChange: false,
    },
    {
      input: "<style>body > p, body > div { color:blue }</style>",
      shouldChange: false,
    },
    {
      input: "<script>window.location='http://script.example.com/';</script>",
      shouldChange: false,
    },
    {
      input: "<head><title>http://head.example.com/</title></head>",
      shouldChange: false,
    },
    {
      input: "<header>see http://header.example.com</header>",
      shouldChange: true,
    },
    {
      input: "<iframe src='http://iframe.example.com/' />",
      shouldChange: false,
    },
    {
      input: "broken end <script",
      shouldChange: false,
    },
  ];

  function hrefLink(url) {
    return ' href="' + url + '"';
  }

  function linkText(plaintext) {
    return ">" + plaintext + "</a>";
  }

  for (let i = 0; i < scanTXTtests.length; i++) {
    let t = scanTXTtests[i];
    let output = converter.scanTXT(t.input, Ci.mozITXTToHTMLConv.kURLs);
    let link = hrefLink(t.url);
    let text;
    if (t.text) {
      text = linkText(t.text);
    }
    if (!output.includes(link)) {
      do_throw(
        "Unexpected conversion by scanTXT: input=" +
          t.input +
          ", output=" +
          output +
          ", link=" +
          link
      );
    }
    if (text && !output.includes(text)) {
      do_throw(
        "Unexpected conversion by scanTXT: input=" +
          t.input +
          ", output=" +
          output +
          ", text=" +
          text
      );
    }
  }

  for (let i = 0; i < scanTXTglyph.length; i++) {
    let t = scanTXTglyph[i];
    let output = converter.scanTXT(
      t.input,
      Ci.mozITXTToHTMLConv.kGlyphSubstitution
    );
    for (let j = 0; j < t.results.length; j++) {
      if (!output.includes(t.results[j])) {
        do_throw(
          "Unexpected conversion by scanTXT: input=" +
            t.input +
            ", output=" +
            output +
            ", expected=" +
            t.results[j]
        );
      }
    }
  }

  for (let i = 0; i < scanTXTstrings.length; ++i) {
    for (let j = 0; j < scanTXTstructs.length; ++j) {
      let input =
        scanTXTstructs[j].delimiter +
        scanTXTstrings[i] +
        scanTXTstructs[j].delimiter;
      let expected =
        "<" +
        scanTXTstructs[j].tag +
        ' class="' +
        scanTXTstructs[j].class +
        '">' +
        '<span class="moz-txt-tag">' +
        scanTXTstructs[j].delimiter +
        "</span>" +
        scanTXTstrings[i] +
        '<span class="moz-txt-tag">' +
        scanTXTstructs[j].delimiter +
        "</span>" +
        "</" +
        scanTXTstructs[j].tag +
        ">";
      let actual = converter.scanTXT(input, Ci.mozITXTToHTMLConv.kStructPhrase);
      Assert.equal(encodeURIComponent(actual), encodeURIComponent(expected));
    }
  }

  for (let i = 0; i < scanHTMLtests.length; i++) {
    let t = scanHTMLtests[i];
    let output = converter.scanHTML(t.input, Ci.mozITXTToHTMLConv.kURLs);
    let changed = t.input != output;
    if (changed != t.shouldChange) {
      do_throw(
        "Unexpected change by scanHTML: changed=" +
          changed +
          ", shouldChange=" +
          t.shouldChange +
          ", \ninput=" +
          t.input +
          ", \noutput=" +
          output
      );
    }
  }
}