Bug 1437269 - port bug 1436508: replace use of nsIDOMKeyEvent with KeyboardEvent. r=jorgk
authoraceman <acelists@atlas.sk>
Sat, 10 Feb 2018 05:47:00 +0100
changeset 31076 8c5c4421a947e1a513a7f51a1df9902224fb5ae9
parent 31075 38c0ac949491fa2384431ceed550722ea87812b2
child 31077 ec75a986c090c8ee53bcbf1d4d2ffda9477db5f1
push id383
push userclokep@gmail.com
push dateMon, 07 May 2018 21:52:48 +0000
reviewersjorgk
bugs1437269, 1436508
Bug 1437269 - port bug 1436508: replace use of nsIDOMKeyEvent with KeyboardEvent. r=jorgk
mail/test/mozmill/shared-modules/test-window-helpers.js
mail/test/resources/mozmill/mozmill/extension/resource/stdlib/EventUtils.js
mailnews/base/content/msgSelectOfflineFolders.js
mailnews/base/content/virtualFolderListEdit.js
--- a/mail/test/mozmill/shared-modules/test-window-helpers.js
+++ b/mail/test/mozmill/shared-modules/test-window-helpers.js
@@ -1446,28 +1446,28 @@ function _augment_helper(aController, aA
   }
 }
 
 var INPUT_PEEK_EVENTS = ["click", "keypress"];
 
 var UNIQUE_WINDOW_ID_ATTR = "__winHelper_uniqueId";
 
 var DOM_KEYCODE_TO_NAME = {};
-function populateDomKeycodeMap() {
-  let nsIDOMKeyEvent = Ci.nsIDOMKeyEvent;
-
-  for (let key in nsIDOMKeyEvent) {
-
-    if (key.startsWith("DOM_VK_")) {
-      let val = nsIDOMKeyEvent[key];
-      DOM_KEYCODE_TO_NAME[val] = key;
+function keycodeToName(aKeyValue, aWindow) {
+  if (!(aKeyValue in DOM_KEYCODE_TO_NAME)) {
+    for (let key in aWindow.KeyboardEvent) {
+      if (key.startsWith("DOM_VK_")) {
+        let val = aWindow.KeyboardEvent[key];
+        DOM_KEYCODE_TO_NAME[val] = key;
+      }
     }
   }
+
+  return DOM_KEYCODE_TO_NAME[aKeyValue];
 }
-populateDomKeycodeMap();
 
 /**
  * Given something you would find on event.target (should be a DOM node /
  *  DOM window), attempt to describe the hierarchy of that thing all the way
  *  to the outermost enclosing window.  This is intended to solve the problem
  *  where our event target can be the "Window" of an iframe, which is not
  *  very enlightening.  We really want to know the frameElement and what
  *  window it lives in.
@@ -1620,17 +1620,17 @@ function __bubbled_click_handler(event) 
                "in", getWindowDescribeyFromEvent(event),
                "original target:", normalize_for_json(event.originalTarget)]);
   return true;
 }
 
 function describeKeyEvent(event) {
   let s;
   if (event.keyCode) {
-    s = DOM_KEYCODE_TO_NAME[event.keyCode];
+    s = keycodeToName(event.keyCode, event.window);
   }
   else if (event.charCode) {
     s = "'" + String.fromCharCode(event.charCode) + "'";
   }
   else {
     s = "no keyCode/charCode?";
   }
 
--- a/mail/test/resources/mozmill/mozmill/extension/resource/stdlib/EventUtils.js
+++ b/mail/test/resources/mozmill/mozmill/extension/resource/stdlib/EventUtils.js
@@ -5,24 +5,16 @@ var EXPORTED_SYMBOLS = ["sendMouseEvent"
                         "synthesizeDragStart", "synthesizeDrop", "synthesizeText",
                         "disableNonTestMouseEvents", "synthesizeComposition",
                         "synthesizeQuerySelectedText", "synthesizeQueryTextContent",
                         "synthesizeQueryCaretRect", "synthesizeQueryTextRect",
                         "synthesizeQueryEditorRect", "synthesizeCharAtPoint",
                         "synthesizeSelectionSet"];
 
 /**
- * Get the array with available key events
- */
-function getKeyEvent(aWindow) {
-  var win = aWindow.wrappedJSObject ? aWindow.wrappedJSObject : aWindow;
-  return win.KeyEvent;
-}
-
-/**
  * EventUtils provides some utility methods for creating and sending DOM events.
  * Current methods:
  *  sendMouseEvent
  *  sendChar
  *  sendString
  *  sendKey
  */
 
@@ -125,21 +117,21 @@ function sendString(aStr, aTarget) {
  * or anything like that), false otherwise.
  */
 function sendKey(aKey, aTarget, aWindow) {
   if (!aWindow)
     aWindow = window;
 
   var keyName = "DOM_VK_" + aKey.toUpperCase();
 
-  if (!getKeyEvent(aWindow)[keyName]) {
+  if (!_getKeyboardEvent(aWindow)[keyName]) {
     throw "Unknown key: " + keyName;
   }
 
-  return __doEventDispatch(aTarget, 0, getKeyEvent(aWindow)[keyName], false);
+  return __doEventDispatch(aTarget, 0, _getKeyboardEvent(aWindow)[keyName], false);
 }
 
 /**
  * Actually perform event dispatch given a charCode, keyCode, and boolean for
  * whether "shift" was pressed.  Send the event to the node with id aTarget.  If
  * aTarget is not provided, use "target".
  *
  * Returns true if the keypress event was accepted (no calls to preventDefault
@@ -335,17 +327,17 @@ function synthesizeMouseScroll(aTarget, 
  *        Note that if some of these values are false, they are ignored (i.e.,
  *        not inactivated with this function).
  *  - keyCode: Must be 0 - 255 (0xFF). If this is specified explicitly,
  *             .keyCode value is initialized with this value.
  *
  * aWindow is optional, and defaults to the current window object.
  * aCallback is optional, use the callback for receiving notifications of TIP.
  */
-function synthesizeKey(aKey, aEvent, aWindow = window, aCallback)
+function synthesizeKey(aKey, aEvent, aWindow, aCallback)
 {
   var TIP = _getTIP(aWindow, aCallback);
   if (!TIP) {
     return;
   }
   var KeyboardEvent = _getKeyboardEvent(aWindow);
   var modifiers = _emulateToActivateModifiers(TIP, aEvent, aWindow);
   var keyEventDict = _createKeyboardEventDictionary(aKey, aEvent, aWindow);
@@ -878,41 +870,41 @@ function _getTIP(aWindow, aCallback)
   }
   if (!tip.beginInputTransactionForTests(aWindow, aCallback)) {
     tip = null;
     TIPMap.delete(aWindow);
   }
   return tip;
 }
 
-function _getKeyboardEvent(aWindow = window)
+function _getKeyboardEvent(aWindow)
 {
   if (typeof KeyboardEvent != "undefined") {
     try {
       // See if the object can be instantiated; sometimes this yields
       // 'TypeError: can't access dead object' or 'KeyboardEvent is not a constructor'.
       new KeyboardEvent("", {});
       return KeyboardEvent;
     } catch (ex) {}
   }
   if (typeof content != "undefined" && ("KeyboardEvent" in content)) {
     return content.KeyboardEvent;
   }
   return aWindow.KeyboardEvent;
 }
 
-function _getNavigator(aWindow = window)
+function _getNavigator(aWindow)
 {
   if (typeof navigator != "undefined") {
     return navigator;
   }
   return aWindow.navigator;
 }
 
-function _guessKeyNameFromKeyCode(aKeyCode, aWindow = window)
+function _guessKeyNameFromKeyCode(aKeyCode, aWindow)
 {
   var KeyboardEvent = _getKeyboardEvent(aWindow);
   switch (aKeyCode) {
     case KeyboardEvent.DOM_VK_CANCEL:
       return "Cancel";
     case KeyboardEvent.DOM_VK_HELP:
       return "Help";
     case KeyboardEvent.DOM_VK_BACK_SPACE:
@@ -1049,37 +1041,37 @@ function _guessKeyNameFromKeyCode(aKeyCo
       return "EraseEof";
     case KeyboardEvent.DOM_VK_PLAY:
       return "Play";
     default:
       return "Unidentified";
   }
 }
 
-function _createKeyboardEventDictionary(aKey, aKeyEvent, aWindow = window) {
+function _createKeyboardEventDictionary(aKey, aKeyEvent, aWindow) {
   var result = { dictionary: null, flags: 0 };
   var keyCodeIsDefined = "keyCode" in aKeyEvent;
   var keyCode =
     (keyCodeIsDefined && aKeyEvent.keyCode >= 0 && aKeyEvent.keyCode <= 255) ?
       aKeyEvent.keyCode : 0;
   var keyName = "Unidentified";
   if (aKey.indexOf("KEY_") == 0) {
     keyName = aKey.substr("KEY_".length);
     result.flags |= Components.interfaces.nsITextInputProcessor.KEY_NON_PRINTABLE_KEY;
   } else if (aKey.indexOf("VK_") == 0) {
-    keyCode = Components.interfaces.nsIDOMKeyEvent["DOM_" + aKey];
+    keyCode = _getKeyboardEvent(aWindow)["DOM_" + aKey];
     if (!keyCode) {
       throw "Unknown key: " + aKey;
     }
     keyName = _guessKeyNameFromKeyCode(keyCode, aWindow);
     result.flags |= Components.interfaces.nsITextInputProcessor.KEY_NON_PRINTABLE_KEY;
   } else if (aKey != "") {
     keyName = aKey;
     if (!keyCodeIsDefined) {
-      keyCode = _computeKeyCodeFromChar(aKey.charAt(0));
+      keyCode = _computeKeyCodeFromChar(aKey.charAt(0), aWindow);
     }
     if (!keyCode) {
       result.flags |= Components.interfaces.nsITextInputProcessor.KEY_KEEP_KEYCODE_ZERO;
     }
     result.flags |= Components.interfaces.nsITextInputProcessor.KEY_FORCE_PRINTABLE_KEY;
   }
   var locationIsDefined = "location" in aKeyEvent;
   if (locationIsDefined && aKeyEvent.location === 0) {
@@ -1090,17 +1082,17 @@ function _createKeyboardEventDictionary(
     code: "code" in aKeyEvent ? aKeyEvent.code : "",
     location: locationIsDefined ? aKeyEvent.location : 0,
     repeat: "repeat" in aKeyEvent ? aKeyEvent.repeat === true : false,
     keyCode: keyCode,
   };
   return result;
 }
 
-function _emulateToActivateModifiers(aTIP, aKeyEvent, aWindow = window)
+function _emulateToActivateModifiers(aTIP, aKeyEvent, aWindow)
 {
   if (!aKeyEvent) {
     return null;
   }
   var KeyboardEvent = _getKeyboardEvent(aWindow);
   var navigator = _getNavigator(aWindow);
 
   var modifiers = {
@@ -1149,17 +1141,17 @@ function _emulateToActivateModifiers(aTI
       aTIP.KEY_NON_PRINTABLE_KEY | aTIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT);
     aTIP.keyup(event,
       aTIP.KEY_NON_PRINTABLE_KEY | aTIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT);
     modifiers.lockable[i].activated = true;
   }
   return modifiers;
 }
 
-function _emulateToInactivateModifiers(aTIP, aModifiers, aWindow = window)
+function _emulateToInactivateModifiers(aTIP, aModifiers, aWindow)
 {
   if (!aModifiers) {
     return;
   }
   var KeyboardEvent = _getKeyboardEvent(aWindow);
   for (var i = 0; i < aModifiers.normal.length; i++) {
     if (!aModifiers.normal[i].activated) {
       continue;
@@ -1178,22 +1170,22 @@ function _emulateToInactivateModifiers(a
     var event = new KeyboardEvent("", { key: aModifiers.lockable[i].key });
     aTIP.keydown(event,
       aTIP.KEY_NON_PRINTABLE_KEY | aTIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT);
     aTIP.keyup(event,
       aTIP.KEY_NON_PRINTABLE_KEY | aTIP.KEY_DONT_DISPATCH_MODIFIER_KEY_EVENT);
   }
 }
 
-function _computeKeyCodeFromChar(aChar)
+function _computeKeyCodeFromChar(aChar, aWindow)
 {
   if (aChar.length != 1) {
     return 0;
   }
-  var KeyEvent = Components.interfaces.nsIDOMKeyEvent;
+  var KeyEvent = _getKeyboardEvent(aWindow);
   if (aChar >= 'a' && aChar <= 'z') {
     return KeyEvent.DOM_VK_A + aChar.charCodeAt(0) - 'a'.charCodeAt(0);
   }
   if (aChar >= 'A' && aChar <= 'Z') {
     return KeyEvent.DOM_VK_A + aChar.charCodeAt(0) - 'A'.charCodeAt(0);
   }
   if (aChar >= '0' && aChar <= '9') {
     return KeyEvent.DOM_VK_0 + aChar.charCodeAt(0) - '0'.charCodeAt(0);
--- a/mailnews/base/content/msgSelectOfflineFolders.js
+++ b/mailnews/base/content/msgSelectOfflineFolders.js
@@ -42,17 +42,17 @@ var gSelectOffline = {
     gFolderTreeView.registerFolderTreeMode(this._treeElement.getAttribute("mode"),
                                            modeOffline, "Offline Folders");
     gFolderTreeView.load(this._treeElement);
 
   },
 
   onKeyPress: function(aEvent) {
     // For now, only do something on space key.
-    if (aEvent.charCode != Components.interfaces.nsIDOMKeyEvent.DOM_VK_SPACE)
+    if (aEvent.charCode != aEvent.DOM_VK_SPACE)
       return;
 
     let selection = this._treeElement.view.selection;
     let start = {};
     let end = {};
     let numRanges = selection.getRangeCount();
 
     for (let range = 0; range < numRanges; range++) {
--- a/mailnews/base/content/virtualFolderListEdit.js
+++ b/mailnews/base/content/virtualFolderListEdit.js
@@ -47,17 +47,17 @@ var gSelectVirtual = {
 
     gFolderTreeView.registerFolderTreeMode(this._treeElement.getAttribute("mode"),
                                            modeVirtual, "Virtual Folders");
     gFolderTreeView.load(this._treeElement);
   },
 
   onKeyPress: function(aEvent) {
     // For now, only do something on space key.
-    if (aEvent.charCode != Components.interfaces.nsIDOMKeyEvent.DOM_VK_SPACE)
+    if (aEvent.charCode != aEvent.DOM_VK_SPACE)
       return;
 
     let selection = this._treeElement.view.selection;
     let start = {};
     let end = {};
     let numRanges = selection.getRangeCount();
 
     for (let range = 0; range < numRanges; range++) {