/usr/share/xul-ext/adblock-plus-element-hiding-helper/keySelector.js is in xul-ext-adblock-plus-element-hiding-helper 1.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
Cu.import("resource://gre/modules/Services.jsm");
let validModifiers =
{
ACCEL: null,
CTRL: "control",
CONTROL: "control",
SHIFT: "shift",
ALT: "alt",
META: "meta",
__proto__: null
};
/**
* Sets the correct value of validModifiers.ACCEL.
*/
function initAccelKey()
{
try
{
let accelKey = Services.prefs.getIntPref("ui.key.accelKey");
if (accelKey == Ci.nsIDOMKeyEvent.DOM_VK_CONTROL)
validModifiers.ACCEL = "control";
else if (accelKey == Ci.nsIDOMKeyEvent.DOM_VK_ALT)
validModifiers.ACCEL = "alt";
else if (accelKey == Ci.nsIDOMKeyEvent.DOM_VK_META)
validModifiers.ACCEL = "meta";
}
catch(e)
{
validModifiers.ACCEL = "control";
Cu.reportError(e);
}
}
exports.KeySelector = KeySelector;
/**
* This class provides capabilities to find and use available keyboard shortcut
* keys.
* @param {ChromeWindow} window the window where to look up existing shortcut
* keys
* @constructor
*/
function KeySelector(window)
{
this._initExistingShortcuts(window);
}
KeySelector.prototype =
{
/**
* Map listing existing shortcut keys as its keys.
* @type Object
*/
_existingShortcuts: null,
/**
* Sets up _existingShortcuts property for a window.
*/
_initExistingShortcuts: function(/**ChromeWindow*/ window)
{
if (!validModifiers.ACCEL)
initAccelKey();
this._existingShortcuts = {__proto__: null};
let keys = window.document.getElementsByTagName("key");
for (let i = 0; i < keys.length; i++)
{
let key = keys[i];
let keyData =
{
shift: false,
meta: false,
alt: false,
control: false,
char: null,
code: null
};
let keyChar = key.getAttribute("key");
if (keyChar && keyChar.length == 1)
keyData.char = keyChar.toUpperCase();
let keyCode = key.getAttribute("keycode");
if (keyCode && "DOM_" + keyCode.toUpperCase() in Ci.nsIDOMKeyEvent)
keyData.code = Ci.nsIDOMKeyEvent["DOM_" + keyCode.toUpperCase()];
if (!keyData.char && !keyData.code)
continue;
let keyModifiers = key.getAttribute("modifiers");
if (keyModifiers)
for each (let modifier in keyModifiers.toUpperCase().match(/\w+/g))
if (modifier in validModifiers)
keyData[validModifiers[modifier]] = true;
let canonical = [keyData.shift, keyData.meta, keyData.alt, keyData.control, keyData.char || keyData.code].join(" ");
this._existingShortcuts[canonical] = true;
}
},
/**
* Selects a keyboard shortcut variant that isn't already taken,
* parses it into an object.
*/
selectKey: function(/**String*/ variants) /**Object*/
{
for each (let variant in variants.split(/\s*,\s*/))
{
if (!variant)
continue;
let keyData =
{
shift: false,
meta: false,
alt: false,
control: false,
char: null,
code: null,
codeName: null
};
for each (let part in variant.toUpperCase().split(/\s+/))
{
if (part in validModifiers)
keyData[validModifiers[part]] = true;
else if (part.length == 1)
keyData.char = part;
else if ("DOM_VK_" + part in Ci.nsIDOMKeyEvent)
{
keyData.code = Ci.nsIDOMKeyEvent["DOM_VK_" + part];
keyData.codeName = "VK_" + part;
}
}
if (!keyData.char && !keyData.code)
continue;
let canonical = [keyData.shift, keyData.meta, keyData.alt, keyData.control, keyData.char || keyData.code].join(" ");
if (canonical in this._existingShortcuts)
continue;
return keyData;
}
return null;
}
};
/**
* Creates the text representation for a key.
* @static
*/
KeySelector.getTextForKey = function (/**Object*/ key) /**String*/
{
if (!key)
return null;
if (!("text" in key))
{
key.text = null;
try
{
let stringBundle = Services.strings.createBundle("chrome://global-platform/locale/platformKeys.properties");
let parts = [];
if (key.control)
parts.push(stringBundle.GetStringFromName("VK_CONTROL"));
if (key.alt)
parts.push(stringBundle.GetStringFromName("VK_ALT"));
if (key.meta)
parts.push(stringBundle.GetStringFromName("VK_META"));
if (key.shift)
parts.push(stringBundle.GetStringFromName("VK_SHIFT"));
if (key.char)
parts.push(key.char.toUpperCase());
else
{
let stringBundle2 = Services.strings.createBundle("chrome://global/locale/keys.properties");
parts.push(stringBundle2.GetStringFromName(key.codeName));
}
key.text = parts.join(stringBundle.GetStringFromName("MODIFIER_SEPARATOR"));
}
catch (e)
{
Cu.reportError(e);
return null;
}
}
return key.text;
};
/**
* Tests whether a keypress event matches the given key.
* @static
*/
KeySelector.matchesKey = function(/**Event*/ event, /**Object*/ key) /**Boolean*/
{
if (event.defaultPrevented || !key)
return false;
if (key.shift != event.shiftKey || key.alt != event.altKey)
return false;
if (key.meta != event.metaKey || key.control != event.ctrlKey)
return false;
if (key.char && event.charCode && String.fromCharCode(event.charCode).toUpperCase() == key.char)
return true;
if (key.code && event.keyCode && event.keyCode == key.code)
return true;
return false;
};
|