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