summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_UrlbarInput_formatValue.js
blob: d4b73603f9954cf8968c9122c0871c1d2a6fca2a (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

// Checks that the url formatter properly recognizes the host and de-emphasizes
// the rest of the url.

/**
 * Tests a given url.
 * The de-emphasized parts must be wrapped in "<" and ">" chars.
 *
 * @param {string} aExpected The url to test.
 * @param {string} aClobbered [optional] Normally the url is de-emphasized
 *        in-place, thus it's enough to pass aExpected. Though, in some cases
 *        the formatter may decide to replace the url with a fixed one, because
 *        it can't properly guess a host. In that case aClobbered is the
 *        expected de-emphasized value.
 * @param {boolean} synthesizeInput [optional] Whether to synthesize an input
 *        event to test.
 */
function testVal(aExpected, aClobbered = null, synthesizeInput = false) {
  let str = aExpected.replace(/[<>]/g, "");
  if (synthesizeInput) {
    gURLBar.focus();
    gURLBar.select();
    EventUtils.sendString(str);
    Assert.equal(
      gURLBar.editor.rootElement.textContent,
      str,
      "Url is not highlighted"
    );
    gBrowser.selectedBrowser.focus();
  } else {
    gURLBar.value = str;
  }

  let selectionController = gURLBar.editor.selectionController;
  let selection = selectionController.getSelection(
    selectionController.SELECTION_URLSECONDARY
  );
  let value = gURLBar.editor.rootElement.textContent;
  let result = "";
  for (let i = 0; i < selection.rangeCount; i++) {
    let range = selection.getRangeAt(i).toString();
    let pos = value.indexOf(range);
    result += value.substring(0, pos) + "<" + range + ">";
    value = value.substring(pos + range.length);
  }
  result += value;
  Assert.equal(
    result,
    aClobbered || aExpected,
    "Correct part of the url is de-emphasized" +
      (synthesizeInput ? " (with input simulation)" : "")
  );

  // Now re-test synthesizing input.
  if (!synthesizeInput) {
    testVal(aExpected, aClobbered, true);
  }
}

function test() {
  const prefname = "browser.urlbar.formatting.enabled";

  registerCleanupFunction(function () {
    Services.prefs.clearUserPref(prefname);
    gURLBar.setURI();
  });

  gBrowser.selectedBrowser.focus();

  testVal("<https://>mozilla.org");
  testVal("<https://>mözilla.org");
  testVal("<https://>mozilla.imaginatory");

  testVal("<https://www.>mozilla.org");
  testVal("<https://sub.>mozilla.org");
  testVal("<https://sub1.sub2.sub3.>mozilla.org");
  testVal("<www.>mozilla.org");
  testVal("<sub.>mozilla.org");
  testVal("<sub1.sub2.sub3.>mozilla.org");
  testVal("<mozilla.com.>mozilla.com");
  testVal("<https://mozilla.com:mozilla.com@>mozilla.com");
  testVal("<mozilla.com:mozilla.com@>mozilla.com");

  testVal("<ftp.>mozilla.org");
  testVal("<ftp://ftp.>mozilla.org");

  testVal("<https://sub.>mozilla.org");
  testVal("<https://sub1.sub2.sub3.>mozilla.org");
  testVal("<https://user:pass@sub1.sub2.sub3.>mozilla.org");
  testVal("<https://user:pass@>mozilla.org");
  testVal("<user:pass@sub1.sub2.sub3.>mozilla.org");
  testVal("<user:pass@>mozilla.org");

  testVal("<https://>mozilla.org<   >");
  testVal("mozilla.org<   >");
  // RTL characters in domain change order of domain and suffix. Domain should
  // be highlighted correctly.
  testVal("<http://>اختبار.اختبار</www.mozilla.org/index.html>");

  testVal("<https://>mozilla.org</file.ext>");
  testVal("<https://>mozilla.org</sub/file.ext>");
  testVal("<https://>mozilla.org</sub/file.ext?foo>");
  testVal("<https://>mozilla.org</sub/file.ext?foo&bar>");
  testVal("<https://>mozilla.org</sub/file.ext?foo&bar#top>");
  testVal("<https://>mozilla.org</sub/file.ext?foo&bar#top>");
  testVal("foo.bar<?q=test>");
  testVal("foo.bar<#mozilla.org>");
  testVal("foo.bar<?somewhere.mozilla.org>");
  testVal("foo.bar<?@mozilla.org>");
  testVal("foo.bar<#x@mozilla.org>");
  testVal("foo.bar<#@x@mozilla.org>");
  testVal("foo.bar<?x@mozilla.org>");
  testVal("foo.bar<?@x@mozilla.org>");
  testVal("<foo.bar@x@>mozilla.org");
  testVal("<foo.bar@:baz@>mozilla.org");
  testVal("<foo.bar:@baz@>mozilla.org");
  testVal("<foo.bar@:ba:z@>mozilla.org");
  testVal("<foo.:bar:@baz@>mozilla.org");
  testVal(
    "foopy:\\blah@somewhere.com//whatever/",
    "foopy</blah@somewhere.com//whatever/>"
  );

  testVal("<https://sub.>mozilla.org<:666/file.ext>");
  testVal("<sub.>mozilla.org<:666/file.ext>");
  testVal("localhost<:666/file.ext>");

  let IPs = [
    "192.168.1.1",
    "[::]",
    "[::1]",
    "[1::]",
    "[::]",
    "[::1]",
    "[1::]",
    "[1:2:3:4:5:6:7::]",
    "[::1:2:3:4:5:6:7]",
    "[1:2:a:B:c:D:e:F]",
    "[1::8]",
    "[1:2::8]",
    "[fe80::222:19ff:fe11:8c76]",
    "[0000:0123:4567:89AB:CDEF:abcd:ef00:0000]",
    "[::192.168.1.1]",
    "[1::0.0.0.0]",
    "[1:2::255.255.255.255]",
    "[1:2:3::255.255.255.255]",
    "[1:2:3:4::255.255.255.255]",
    "[1:2:3:4:5::255.255.255.255]",
    "[1:2:3:4:5:6:255.255.255.255]",
  ];
  IPs.forEach(function (IP) {
    testVal(IP);
    testVal(IP + "</file.ext>");
    testVal(IP + "<:666/file.ext>");
    testVal("<https://>" + IP);
    testVal(`<https://>${IP}</file.ext>`);
    testVal(`<https://user:pass@>${IP}<:666/file.ext>`);
    testVal(`<user:pass@>${IP}<:666/file.ext>`);
    testVal(`user:\\pass@${IP}/`, `user</pass@${IP}/>`);
  });

  testVal("mailto:admin@mozilla.org");
  testVal("gopher://mozilla.org/");
  testVal("about:config");
  testVal("jar:http://mozilla.org/example.jar!/");
  testVal("view-source:http://mozilla.org/");
  testVal("foo9://mozilla.org/");
  testVal("foo+://mozilla.org/");
  testVal("foo.://mozilla.org/");
  testVal("foo-://mozilla.org/");

  // Disable formatting.
  Services.prefs.setBoolPref(prefname, false);

  testVal("https://mozilla.org");
}