Bug 1245153 - Make EventUtils.js use aWindow argument for sub-calls consistently; r=jmaher

EventUtils.js previously allowed you to override the Window object
reference through passing it as an optional argument to its functions.
This change fixes certain uses of implicit globals that reside on Window.

MozReview-Commit-ID: EJT8iIs85ej
This commit is contained in:
Andreas Tolfsen 2016-02-08 13:59:40 +00:00
parent 528ee737bc
commit fd79378c2b

View File

@ -114,7 +114,7 @@ function sendMouseEvent(aEvent, aTarget, aWindow) {
* object with the properties set that the real drag event object should
* have. This includes the type of the drag event.
*/
function sendDragEvent(aEvent, aTarget, aWindow=window) {
function sendDragEvent(aEvent, aTarget, aWindow = window) {
if (['drag', 'dragstart', 'dragend', 'dragover', 'dragenter', 'dragleave', 'drop'].indexOf(aEvent.type) == -1) {
throw new Error("sendDragEvent doesn't know about event type '" + aEvent.type + "'");
}
@ -219,9 +219,10 @@ function sendKey(aKey, aWindow) {
* Parse the key modifier flags from aEvent. Used to share code between
* synthesizeMouse and synthesizeKey.
*/
function _parseModifiers(aEvent)
function _parseModifiers(aEvent, aWindow = window)
{
const nsIDOMWindowUtils = _EU_Ci.nsIDOMWindowUtils;
var navigator = _getNavigator(aWindow);
var nsIDOMWindowUtils = _EU_Ci.nsIDOMWindowUtils;
var mval = 0;
if (aEvent.shiftKey) {
mval |= nsIDOMWindowUtils.MODIFIER_SHIFT;
@ -315,7 +316,7 @@ function synthesizePointer(aTarget, aOffsetX, aOffsetY, aEvent, aWindow)
*
* aWindow is optional, and defaults to the current window object.
*/
function synthesizeMouseAtPoint(left, top, aEvent, aWindow)
function synthesizeMouseAtPoint(left, top, aEvent, aWindow = window)
{
var utils = _getDOMWindowUtils(aWindow);
var defaultPrevented = false;
@ -323,7 +324,7 @@ function synthesizeMouseAtPoint(left, top, aEvent, aWindow)
if (utils) {
var button = computeButton(aEvent);
var clickCount = aEvent.clickCount || 1;
var modifiers = _parseModifiers(aEvent);
var modifiers = _parseModifiers(aEvent, aWindow);
var pressure = ("pressure" in aEvent) ? aEvent.pressure : 0;
var inputSource = ("inputSource" in aEvent) ? aEvent.inputSource : 0;
var synthesized = ("isSynthesized" in aEvent) ? aEvent.isSynthesized : true;
@ -342,7 +343,8 @@ function synthesizeMouseAtPoint(left, top, aEvent, aWindow)
return defaultPrevented;
}
function synthesizeTouchAtPoint(left, top, aEvent, aWindow)
function synthesizeTouchAtPoint(left, top, aEvent, aWindow = window)
{
var utils = _getDOMWindowUtils(aWindow);
@ -352,7 +354,7 @@ function synthesizeTouchAtPoint(left, top, aEvent, aWindow)
var ry = aEvent.rx || 1;
var angle = aEvent.angle || 0;
var force = aEvent.force || 1;
var modifiers = _parseModifiers(aEvent);
var modifiers = _parseModifiers(aEvent, aWindow);
if (("type" in aEvent) && aEvent.type) {
utils.sendTouchEvent(aEvent.type, [id], [left], [top], [rx], [ry], [angle], [force], 1, modifiers);
@ -363,7 +365,8 @@ function synthesizeTouchAtPoint(left, top, aEvent, aWindow)
}
}
}
function synthesizePointerAtPoint(left, top, aEvent, aWindow)
function synthesizePointerAtPoint(left, top, aEvent, aWindow = window)
{
var utils = _getDOMWindowUtils(aWindow);
var defaultPrevented = false;
@ -371,7 +374,7 @@ function synthesizePointerAtPoint(left, top, aEvent, aWindow)
if (utils) {
var button = computeButton(aEvent);
var clickCount = aEvent.clickCount || 1;
var modifiers = _parseModifiers(aEvent);
var modifiers = _parseModifiers(aEvent, aWindow);
var pressure = ("pressure" in aEvent) ? aEvent.pressure : 0;
var inputSource = ("inputSource" in aEvent) ? aEvent.inputSource : 0;
var synthesized = ("isSynthesized" in aEvent) ? aEvent.isSynthesized : true;
@ -423,14 +426,14 @@ function synthesizeTouchAtCenter(aTarget, aEvent, aWindow)
*
* aWindow is optional, and defaults to the current window object.
*/
function synthesizeWheelAtPoint(aLeft, aTop, aEvent, aWindow)
function synthesizeWheelAtPoint(aLeft, aTop, aEvent, aWindow = window)
{
var utils = _getDOMWindowUtils(aWindow);
if (!utils) {
return;
}
var modifiers = _parseModifiers(aEvent);
var modifiers = _parseModifiers(aEvent, aWindow);
var options = 0;
if (aEvent.isNoLineOrPageDelta) {
options |= utils.WHEEL_EVENT_CAUSED_BY_NO_LINE_OR_PAGE_DELTA_DEVICE;
@ -523,9 +526,7 @@ function synthesizeWheel(aTarget, aOffsetX, aOffsetY, aEvent, aWindow)
* determining scroll completion and the refresh driver is not automatically
* restored.
*/
function sendWheelAndPaint(aTarget, aOffsetX, aOffsetY, aEvent, aCallback, aWindow) {
aWindow = aWindow || window;
function sendWheelAndPaint(aTarget, aOffsetX, aOffsetY, aEvent, aCallback, aWindow = window) {
var utils = _getDOMWindowUtils(aWindow);
if (!utils)
return;
@ -573,9 +574,7 @@ function sendWheelAndPaint(aTarget, aOffsetX, aOffsetY, aEvent, aCallback, aWind
synthesizeWheel(aTarget, aOffsetX, aOffsetY, aEvent, aWindow);
}
function synthesizeNativeMouseMove(aTarget, aOffsetX, aOffsetY, aCallback, aWindow) {
aWindow = aWindow || window;
function synthesizeNativeMouseMove(aTarget, aOffsetX, aOffsetY, aCallback, aWindow = window) {
var utils = _getDOMWindowUtils(aWindow);
if (!utils)
return;
@ -600,75 +599,75 @@ function _computeKeyCodeFromChar(aChar)
if (aChar.length != 1) {
return 0;
}
const nsIDOMKeyEvent = _EU_Ci.nsIDOMKeyEvent;
var KeyEvent = _EU_Ci.nsIDOMKeyEvent;
if (aChar >= 'a' && aChar <= 'z') {
return nsIDOMKeyEvent.DOM_VK_A + aChar.charCodeAt(0) - 'a'.charCodeAt(0);
return KeyEvent.DOM_VK_A + aChar.charCodeAt(0) - 'a'.charCodeAt(0);
}
if (aChar >= 'A' && aChar <= 'Z') {
return nsIDOMKeyEvent.DOM_VK_A + aChar.charCodeAt(0) - 'A'.charCodeAt(0);
return KeyEvent.DOM_VK_A + aChar.charCodeAt(0) - 'A'.charCodeAt(0);
}
if (aChar >= '0' && aChar <= '9') {
return nsIDOMKeyEvent.DOM_VK_0 + aChar.charCodeAt(0) - '0'.charCodeAt(0);
return KeyEvent.DOM_VK_0 + aChar.charCodeAt(0) - '0'.charCodeAt(0);
}
// returns US keyboard layout's keycode
switch (aChar) {
case '~':
case '`':
return nsIDOMKeyEvent.DOM_VK_BACK_QUOTE;
return KeyEvent.DOM_VK_BACK_QUOTE;
case '!':
return nsIDOMKeyEvent.DOM_VK_1;
return KeyEvent.DOM_VK_1;
case '@':
return nsIDOMKeyEvent.DOM_VK_2;
return KeyEvent.DOM_VK_2;
case '#':
return nsIDOMKeyEvent.DOM_VK_3;
return KeyEvent.DOM_VK_3;
case '$':
return nsIDOMKeyEvent.DOM_VK_4;
return KeyEvent.DOM_VK_4;
case '%':
return nsIDOMKeyEvent.DOM_VK_5;
return KeyEvent.DOM_VK_5;
case '^':
return nsIDOMKeyEvent.DOM_VK_6;
return KeyEvent.DOM_VK_6;
case '&':
return nsIDOMKeyEvent.DOM_VK_7;
return KeyEvent.DOM_VK_7;
case '*':
return nsIDOMKeyEvent.DOM_VK_8;
return KeyEvent.DOM_VK_8;
case '(':
return nsIDOMKeyEvent.DOM_VK_9;
return KeyEvent.DOM_VK_9;
case ')':
return nsIDOMKeyEvent.DOM_VK_0;
return KeyEvent.DOM_VK_0;
case '-':
case '_':
return nsIDOMKeyEvent.DOM_VK_SUBTRACT;
return KeyEvent.DOM_VK_SUBTRACT;
case '+':
case '=':
return nsIDOMKeyEvent.DOM_VK_EQUALS;
return KeyEvent.DOM_VK_EQUALS;
case '{':
case '[':
return nsIDOMKeyEvent.DOM_VK_OPEN_BRACKET;
return KeyEvent.DOM_VK_OPEN_BRACKET;
case '}':
case ']':
return nsIDOMKeyEvent.DOM_VK_CLOSE_BRACKET;
return KeyEvent.DOM_VK_CLOSE_BRACKET;
case '|':
case '\\':
return nsIDOMKeyEvent.DOM_VK_BACK_SLASH;
return KeyEvent.DOM_VK_BACK_SLASH;
case ':':
case ';':
return nsIDOMKeyEvent.DOM_VK_SEMICOLON;
return KeyEvent.DOM_VK_SEMICOLON;
case '\'':
case '"':
return nsIDOMKeyEvent.DOM_VK_QUOTE;
return KeyEvent.DOM_VK_QUOTE;
case '<':
case ',':
return nsIDOMKeyEvent.DOM_VK_COMMA;
return KeyEvent.DOM_VK_COMMA;
case '>':
case '.':
return nsIDOMKeyEvent.DOM_VK_PERIOD;
return KeyEvent.DOM_VK_PERIOD;
case '?':
case '/':
return nsIDOMKeyEvent.DOM_VK_SLASH;
return KeyEvent.DOM_VK_SLASH;
case '\n':
return nsIDOMKeyEvent.DOM_VK_RETURN;
return KeyEvent.DOM_VK_RETURN;
case ' ':
return nsIDOMKeyEvent.DOM_VK_SPACE;
return KeyEvent.DOM_VK_SPACE;
default:
return 0;
}
@ -711,14 +710,15 @@ function _computeKeyCodeFromChar(aChar)
*
* aWindow is optional, and defaults to the current window object.
*/
function synthesizeKey(aKey, aEvent, aWindow)
function synthesizeKey(aKey, aEvent, aWindow = window)
{
var TIP = _getTIP(aWindow);
if (!TIP) {
return;
}
var modifiers = _emulateToActivateModifiers(TIP, aEvent);
var keyEventDict = _createKeyboardEventDictionary(aKey, aEvent);
var KeyboardEvent = _getKeyboardEvent(aWindow);
var modifiers = _emulateToActivateModifiers(TIP, aEvent, aWindow);
var keyEventDict = _createKeyboardEventDictionary(aKey, aEvent, aWindow);
var keyEvent = new KeyboardEvent("", keyEventDict.dictionary);
var dispatchKeydown =
!("type" in aEvent) || aEvent.type === "keydown" || !aEvent.type;
@ -740,12 +740,13 @@ function synthesizeKey(aKey, aEvent, aWindow)
TIP.keyup(keyEvent, keyEventDict.flags);
}
} finally {
_emulateToInactivateModifiers(TIP, modifiers);
_emulateToInactivateModifiers(TIP, modifiers, aWindow);
}
}
function _parseNativeModifiers(aModifiers)
function _parseNativeModifiers(aModifiers, aWindow = window)
{
var navigator = _getNavigator(aWindow);
var modifiers;
if (aModifiers.capsLockKey) {
modifiers |= 0x00000001;
@ -864,12 +865,13 @@ const KEYBOARD_LAYOUT_THAI =
*/
function synthesizeNativeKey(aKeyboardLayout, aNativeKeyCode, aModifiers,
aChars, aUnmodifiedChars, aCallback)
aChars, aUnmodifiedChars, aCallback, aWindow = window)
{
var utils = _getDOMWindowUtils(window);
var utils = _getDOMWindowUtils(aWindow);
if (!utils) {
return false;
}
var navigator = _getNavigator(aWindow);
var nativeKeyboardLayout = null;
if (navigator.platform.indexOf("Mac") == 0) {
nativeKeyboardLayout = aKeyboardLayout.Mac;
@ -888,7 +890,7 @@ function synthesizeNativeKey(aKeyboardLayout, aNativeKeyCode, aModifiers,
}
};
utils.sendNativeKeyEvent(nativeKeyboardLayout, aNativeKeyCode,
_parseNativeModifiers(aModifiers),
_parseNativeModifiers(aModifiers, aWindow),
aChars, aUnmodifiedChars, observer);
return true;
}
@ -989,8 +991,10 @@ function disableNonTestMouseEvents(aDisable)
domutils.disableNonTestMouseEvents(aDisable);
}
function _getDOMWindowUtils(aWindow)
function _getDOMWindowUtils(aWindow = window)
{
// Leave this here as something, somewhere, passes a falsy argument
// to this, causing the |window| default argument not to get picked up.
if (!aWindow) {
aWindow = window;
}
@ -1006,9 +1010,10 @@ function _getDOMWindowUtils(aWindow)
return parent.SpecialPowers.getDOMWindowUtils(aWindow);
}
//TODO: this is assuming we are in chrome space
return aWindow.QueryInterface(_EU_Ci.nsIInterfaceRequestor).
getInterface(_EU_Ci.nsIDOMWindowUtils);
// TODO: this is assuming we are in chrome space
return aWindow
.QueryInterface(_EU_Ci.nsIInterfaceRequestor)
.getInterface(_EU_Ci.nsIDOMWindowUtils);
}
function _defineConstant(name, value) {
@ -1055,8 +1060,25 @@ function _getTIP(aWindow, aCallback)
return tip;
}
function _guessKeyNameFromKeyCode(aKeyCode)
function _getKeyboardEvent(aWindow = window)
{
if (typeof KeyboardEvent != "undefined") {
return KeyboardEvent;
}
return aWindow.KeyboardEvent;
}
function _getNavigator(aWindow = window)
{
if (typeof navigator != "undefined") {
return navigator;
}
return aWindow.navigator;
}
function _guessKeyNameFromKeyCode(aKeyCode, aWindow = window)
{
var KeyboardEvent = _getKeyboardEvent(aWindow);
switch (aKeyCode) {
case KeyboardEvent.DOM_VK_CANCEL:
return "Cancel";
@ -1201,10 +1223,8 @@ function _guessKeyNameFromKeyCode(aKeyCode)
}
}
function _createKeyboardEventDictionary(aKey, aKeyEvent)
{
function _createKeyboardEventDictionary(aKey, aKeyEvent, aWindow = window) {
var result = { dictionary: null, flags: 0 };
var keyCodeIsDefined = "keyCode" in aKeyEvent;
var keyCode =
(keyCodeIsDefined && aKeyEvent.keyCode >= 0 && aKeyEvent.keyCode <= 255) ?
@ -1214,11 +1234,11 @@ function _createKeyboardEventDictionary(aKey, aKeyEvent)
keyName = aKey.substr("KEY_".length);
result.flags |= _EU_Ci.nsITextInputProcessor.KEY_NON_PRINTABLE_KEY;
} else if (aKey.indexOf("VK_") == 0) {
keyCode = KeyEvent["DOM_" + aKey];
keyCode = _EU_Ci.nsIDOMKeyEvent["DOM_" + aKey];
if (!keyCode) {
throw "Unknown key: " + aKey;
}
keyName = _guessKeyNameFromKeyCode(keyCode);
keyName = _guessKeyNameFromKeyCode(keyCode, aWindow);
result.flags |= _EU_Ci.nsITextInputProcessor.KEY_NON_PRINTABLE_KEY;
} else if (aKey != "") {
keyName = aKey;
@ -1244,11 +1264,14 @@ function _createKeyboardEventDictionary(aKey, aKeyEvent)
return result;
}
function _emulateToActivateModifiers(aTIP, aKeyEvent)
function _emulateToActivateModifiers(aTIP, aKeyEvent, aWindow = window)
{
if (!aKeyEvent) {
return null;
}
var KeyboardEvent = _getKeyboardEvent(aWindow);
var navigator = _getNavigator(aWindow);
var modifiers = {
normal: [
{ key: "Alt", attr: "altKey" },
@ -1300,11 +1323,12 @@ function _emulateToActivateModifiers(aTIP, aKeyEvent)
return modifiers;
}
function _emulateToInactivateModifiers(aTIP, aModifiers)
function _emulateToInactivateModifiers(aTIP, aModifiers, aWindow = window)
{
if (!aModifiers) {
return;
}
var KeyboardEvent = _getKeyboardEvent(aWindow);
for (var i = 0; i < aModifiers.normal.length; i++) {
if (!aModifiers.normal[i].activated) {
continue;
@ -1349,17 +1373,18 @@ function _emulateToInactivateModifiers(aTIP, aModifiers)
* @param aCallback Optional (If non-null, use the callback for
* receiving notifications to IME)
*/
function synthesizeComposition(aEvent, aWindow, aCallback)
function synthesizeComposition(aEvent, aWindow = window, aCallback)
{
var TIP = _getTIP(aWindow, aCallback);
if (!TIP) {
return false;
}
var modifiers = _emulateToActivateModifiers(TIP, aEvent.key);
var KeyboardEvent = _getKeyboardEvent(aWindow);
var modifiers = _emulateToActivateModifiers(TIP, aEvent.key, aWindow);
var ret = false;
var keyEventDict =
"key" in aEvent ?
_createKeyboardEventDictionary(aEvent.key.key, aEvent.key) :
_createKeyboardEventDictionary(aEvent.key.key, aEvent.key, aWindow) :
{ dictionary: null, flags: 0 };
var keyEvent =
"key" in aEvent ?
@ -1380,7 +1405,7 @@ function synthesizeComposition(aEvent, aWindow, aCallback)
break;
}
} finally {
_emulateToInactivateModifiers(TIP, modifiers);
_emulateToInactivateModifiers(TIP, modifiers, aWindow);
}
}
/**
@ -1432,12 +1457,13 @@ function synthesizeComposition(aEvent, aWindow, aCallback)
* @param aCallback Optional (If non-null, use the callback for receiving
* notifications to IME)
*/
function synthesizeCompositionChange(aEvent, aWindow, aCallback)
function synthesizeCompositionChange(aEvent, aWindow = window, aCallback)
{
var TIP = _getTIP(aWindow, aCallback);
if (!TIP) {
return;
}
var KeyboardEvent = _getKeyboardEvent(aWindow);
if (!aEvent.composition || !aEvent.composition.clauses ||
!aEvent.composition.clauses[0]) {
@ -1470,11 +1496,11 @@ function synthesizeCompositionChange(aEvent, aWindow, aCallback)
TIP.setCaretInPendingComposition(aEvent.caret.start);
}
var modifiers = _emulateToActivateModifiers(TIP, aEvent.key);
var modifiers = _emulateToActivateModifiers(TIP, aEvent.key, aWindow);
try {
var keyEventDict =
"key" in aEvent ?
_createKeyboardEventDictionary(aEvent.key.key, aEvent.key) :
_createKeyboardEventDictionary(aEvent.key.key, aEvent.key, aWindow) :
{ dictionary: null, flags: 0 };
var keyEvent =
"key" in aEvent ?
@ -1483,7 +1509,7 @@ function synthesizeCompositionChange(aEvent, aWindow, aCallback)
null;
TIP.flushPendingComposition(keyEvent, keyEventDict.flags);
} finally {
_emulateToInactivateModifiers(TIP, modifiers);
_emulateToInactivateModifiers(TIP, modifiers, aWindow);
}
}