michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
michael@0: /* vim: set ts=2 et sw=2 tw=80: */
michael@0: /* Any copyright is dedicated to the Public Domain.
michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0:
michael@0: "use strict";
michael@0:
michael@0: const kHighlightPref = "browser.urlbar.formatting.enabled";
michael@0: var gHighlightPrefValue;
michael@0:
michael@0: function test() {
michael@0: runTests();
michael@0: }
michael@0:
michael@0: function setUp() {
michael@0: gHighlightPrefValue = SpecialPowers.getBoolPref(kHighlightPref);
michael@0: SpecialPowers.setBoolPref(kHighlightPref, true);
michael@0: yield addTab("about:blank");
michael@0: }
michael@0:
michael@0: function tearDown() {
michael@0: SpecialPowers.setBoolPref(kHighlightPref, gHighlightPrefValue);
michael@0: Browser.closeTab(Browser.selectedTab, { forceClose: true });
michael@0: }
michael@0:
michael@0: function testHighlight(aExpected) {
michael@0: let urlbar = BrowserUI._edit;
michael@0: urlbar.value = aExpected.replace(/[<>]/g, "");
michael@0:
michael@0: let selectionController = urlbar.editor.selectionController;
michael@0: let selection = selectionController.getSelection(selectionController.SELECTION_URLSECONDARY);
michael@0: let value = urlbar.editor.rootElement.textContent;
michael@0:
michael@0: let result = "";
michael@0: for (let i = 0; i < selection.rangeCount; i++) {
michael@0: let range = selection.getRangeAt(i).toString();
michael@0: let pos = value.indexOf(range);
michael@0: result += value.substring(0, pos) + "<" + range + ">";
michael@0: value = value.substring(pos + range.length);
michael@0: }
michael@0: result += value;
michael@0: is(result, aExpected, "test highight");
michael@0: }
michael@0:
michael@0: gTests.push({
michael@0: desc: "Domain-based URIs (not in editing mode)",
michael@0: setUp: setUp,
michael@0: tearDown: tearDown,
michael@0: run: function () {
michael@0: let testcases = [
michael@0: "mozilla.org",
michael@0: "mözilla.org",
michael@0: "mozilla.imaginatory",
michael@0:
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0:
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0:
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0:
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0: "mozilla.org",
michael@0:
michael@0: "mozilla.org<:666/file.ext>",
michael@0: "mozilla.org<:666/file.ext>",
michael@0: "localhost<:666/file.ext>",
michael@0:
michael@0: "mailto:admin@mozilla.org",
michael@0: "gopher://mozilla.org/",
michael@0: "about:config",
michael@0: "jar:http://mozilla.org/example.jar!/",
michael@0: "view-source:http://mozilla.org/",
michael@0: "foo9://mozilla.org/",
michael@0: "foo+://mozilla.org/",
michael@0: "foo.://mozilla.org/",
michael@0: "foo-://mozilla.org/"
michael@0: ];
michael@0:
michael@0: testcases.forEach(testHighlight);
michael@0: }
michael@0: });
michael@0:
michael@0: gTests.push({
michael@0: desc: "IP-based URIs (not in editing mode)",
michael@0: setUp: setUp,
michael@0: tearDown: tearDown,
michael@0: run: function () {
michael@0: let ips = [
michael@0: "192.168.1.1",
michael@0: "[::]",
michael@0: "[::1]",
michael@0: "[1::]",
michael@0: "[::]",
michael@0: "[::1]",
michael@0: "[1::]",
michael@0: "[1:2:3:4:5:6:7::]",
michael@0: "[::1:2:3:4:5:6:7]",
michael@0: "[1:2:a:B:c:D:e:F]",
michael@0: "[1::8]",
michael@0: "[1:2::8]",
michael@0: "[fe80::222:19ff:fe11:8c76]",
michael@0: "[0000:0123:4567:89AB:CDEF:abcd:ef00:0000]",
michael@0: "[::192.168.1.1]",
michael@0: "[1::0.0.0.0]",
michael@0: "[1:2::255.255.255.255]",
michael@0: "[1:2:3::255.255.255.255]",
michael@0: "[1:2:3:4::255.255.255.255]",
michael@0: "[1:2:3:4:5::255.255.255.255]",
michael@0: "[1:2:3:4:5:6:255.255.255.255]"
michael@0: ];
michael@0:
michael@0: let formats = [
michael@0: "{ip}",
michael@0: "{ip}<:666/file.ext>",
michael@0: "{ip}",
michael@0: "{ip}",
michael@0: "{ip}<:666/file.ext>",
michael@0: "{ip}<:666/file.ext>"
michael@0: ];
michael@0:
michael@0: function testHighlightAllFormats(aIP) {
michael@0: formats.forEach((aFormat) => testHighlight(aFormat.replace("{ip}", aIP)));
michael@0: }
michael@0:
michael@0: ips.forEach(testHighlightAllFormats);
michael@0: }
michael@0: });
michael@0:
michael@0: gTests.push({
michael@0: desc: "no highlighting (in editing mode)",
michael@0: setUp: setUp,
michael@0: tearDown: tearDown,
michael@0: run: function () {
michael@0: testHighlight("mozilla.org");
michael@0:
michael@0: BrowserUI._edit.focus();
michael@0: testHighlight("https://mozilla.org");
michael@0:
michael@0: Browser.selectedBrowser.focus();
michael@0: testHighlight("mozilla.org");
michael@0: }
michael@0: });
michael@0:
michael@0: gTests.push({
michael@0: desc: "no higlighting (pref disabled)",
michael@0: setUp: setUp,
michael@0: tearDown: tearDown,
michael@0: run: function () {
michael@0: testHighlight("mozilla.org");
michael@0:
michael@0: SpecialPowers.setBoolPref(kHighlightPref, false);
michael@0: testHighlight("https://mozilla.org");
michael@0:
michael@0: SpecialPowers.setBoolPref(kHighlightPref, true);
michael@0: testHighlight("mozilla.org");
michael@0: }
michael@0: });
michael@0: