|
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* Any copyright is dedicated to the Public Domain. |
|
4 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
5 |
|
6 "use strict"; |
|
7 |
|
8 const kHighlightPref = "browser.urlbar.formatting.enabled"; |
|
9 var gHighlightPrefValue; |
|
10 |
|
11 function test() { |
|
12 runTests(); |
|
13 } |
|
14 |
|
15 function setUp() { |
|
16 gHighlightPrefValue = SpecialPowers.getBoolPref(kHighlightPref); |
|
17 SpecialPowers.setBoolPref(kHighlightPref, true); |
|
18 yield addTab("about:blank"); |
|
19 } |
|
20 |
|
21 function tearDown() { |
|
22 SpecialPowers.setBoolPref(kHighlightPref, gHighlightPrefValue); |
|
23 Browser.closeTab(Browser.selectedTab, { forceClose: true }); |
|
24 } |
|
25 |
|
26 function testHighlight(aExpected) { |
|
27 let urlbar = BrowserUI._edit; |
|
28 urlbar.value = aExpected.replace(/[<>]/g, ""); |
|
29 |
|
30 let selectionController = urlbar.editor.selectionController; |
|
31 let selection = selectionController.getSelection(selectionController.SELECTION_URLSECONDARY); |
|
32 let value = urlbar.editor.rootElement.textContent; |
|
33 |
|
34 let result = ""; |
|
35 for (let i = 0; i < selection.rangeCount; i++) { |
|
36 let range = selection.getRangeAt(i).toString(); |
|
37 let pos = value.indexOf(range); |
|
38 result += value.substring(0, pos) + "<" + range + ">"; |
|
39 value = value.substring(pos + range.length); |
|
40 } |
|
41 result += value; |
|
42 is(result, aExpected, "test highight"); |
|
43 } |
|
44 |
|
45 gTests.push({ |
|
46 desc: "Domain-based URIs (not in editing mode)", |
|
47 setUp: setUp, |
|
48 tearDown: tearDown, |
|
49 run: function () { |
|
50 let testcases = [ |
|
51 "<https://>mozilla.org", |
|
52 "<https://>mözilla.org", |
|
53 "<https://>mozilla.imaginatory", |
|
54 |
|
55 "<https://www.>mozilla.org", |
|
56 "<https://sub.>mozilla.org", |
|
57 "<https://sub1.sub2.sub3.>mozilla.org", |
|
58 "<www.>mozilla.org", |
|
59 "<sub.>mozilla.org", |
|
60 "<sub1.sub2.sub3.>mozilla.org", |
|
61 |
|
62 "<http://ftp.>mozilla.org", |
|
63 "<ftp://ftp.>mozilla.org", |
|
64 |
|
65 "<https://sub.>mozilla.org", |
|
66 "<https://sub1.sub2.sub3.>mozilla.org", |
|
67 "<https://user:pass@sub1.sub2.sub3.>mozilla.org", |
|
68 "<https://user:pass@>mozilla.org", |
|
69 |
|
70 "<https://>mozilla.org</file.ext>", |
|
71 "<https://>mozilla.org</sub/file.ext>", |
|
72 "<https://>mozilla.org</sub/file.ext?foo>", |
|
73 "<https://>mozilla.org</sub/file.ext?foo&bar>", |
|
74 "<https://>mozilla.org</sub/file.ext?foo&bar#top>", |
|
75 "<https://>mozilla.org</sub/file.ext?foo&bar#top>", |
|
76 |
|
77 "<https://sub.>mozilla.org<:666/file.ext>", |
|
78 "<sub.>mozilla.org<:666/file.ext>", |
|
79 "localhost<:666/file.ext>", |
|
80 |
|
81 "mailto:admin@mozilla.org", |
|
82 "gopher://mozilla.org/", |
|
83 "about:config", |
|
84 "jar:http://mozilla.org/example.jar!/", |
|
85 "view-source:http://mozilla.org/", |
|
86 "foo9://mozilla.org/", |
|
87 "foo+://mozilla.org/", |
|
88 "foo.://mozilla.org/", |
|
89 "foo-://mozilla.org/" |
|
90 ]; |
|
91 |
|
92 testcases.forEach(testHighlight); |
|
93 } |
|
94 }); |
|
95 |
|
96 gTests.push({ |
|
97 desc: "IP-based URIs (not in editing mode)", |
|
98 setUp: setUp, |
|
99 tearDown: tearDown, |
|
100 run: function () { |
|
101 let ips = [ |
|
102 "192.168.1.1", |
|
103 "[::]", |
|
104 "[::1]", |
|
105 "[1::]", |
|
106 "[::]", |
|
107 "[::1]", |
|
108 "[1::]", |
|
109 "[1:2:3:4:5:6:7::]", |
|
110 "[::1:2:3:4:5:6:7]", |
|
111 "[1:2:a:B:c:D:e:F]", |
|
112 "[1::8]", |
|
113 "[1:2::8]", |
|
114 "[fe80::222:19ff:fe11:8c76]", |
|
115 "[0000:0123:4567:89AB:CDEF:abcd:ef00:0000]", |
|
116 "[::192.168.1.1]", |
|
117 "[1::0.0.0.0]", |
|
118 "[1:2::255.255.255.255]", |
|
119 "[1:2:3::255.255.255.255]", |
|
120 "[1:2:3:4::255.255.255.255]", |
|
121 "[1:2:3:4:5::255.255.255.255]", |
|
122 "[1:2:3:4:5:6:255.255.255.255]" |
|
123 ]; |
|
124 |
|
125 let formats = [ |
|
126 "{ip}</file.ext>", |
|
127 "{ip}<:666/file.ext>", |
|
128 "<https://>{ip}", |
|
129 "<https://>{ip}</file.ext>", |
|
130 "<https://user:pass@>{ip}<:666/file.ext>", |
|
131 "<http://user:pass@>{ip}<:666/file.ext>" |
|
132 ]; |
|
133 |
|
134 function testHighlightAllFormats(aIP) { |
|
135 formats.forEach((aFormat) => testHighlight(aFormat.replace("{ip}", aIP))); |
|
136 } |
|
137 |
|
138 ips.forEach(testHighlightAllFormats); |
|
139 } |
|
140 }); |
|
141 |
|
142 gTests.push({ |
|
143 desc: "no highlighting (in editing mode)", |
|
144 setUp: setUp, |
|
145 tearDown: tearDown, |
|
146 run: function () { |
|
147 testHighlight("<https://>mozilla.org"); |
|
148 |
|
149 BrowserUI._edit.focus(); |
|
150 testHighlight("https://mozilla.org"); |
|
151 |
|
152 Browser.selectedBrowser.focus(); |
|
153 testHighlight("<https://>mozilla.org"); |
|
154 } |
|
155 }); |
|
156 |
|
157 gTests.push({ |
|
158 desc: "no higlighting (pref disabled)", |
|
159 setUp: setUp, |
|
160 tearDown: tearDown, |
|
161 run: function () { |
|
162 testHighlight("<https://>mozilla.org"); |
|
163 |
|
164 SpecialPowers.setBoolPref(kHighlightPref, false); |
|
165 testHighlight("https://mozilla.org"); |
|
166 |
|
167 SpecialPowers.setBoolPref(kHighlightPref, true); |
|
168 testHighlight("<https://>mozilla.org"); |
|
169 } |
|
170 }); |
|
171 |