Bug 1263560 - Accept whitespace in command API key names. r=kmag draft
authorMatthew Wein <mwein@mozilla.com>
Thu, 14 Apr 2016 22:39:50 -0400
changeset 352209 9278465a2c0a48285c15b62838427f982fd0bb5a
parent 351754 e595db332894e0d6a2bacd793f792d56ce3960d1
child 518609 5b8dccb884161f049234a32831437155bf473eff
push id15647
push usermwein@mozilla.com
push dateFri, 15 Apr 2016 22:11:31 +0000
reviewerskmag
bugs1263560
milestone48.0a1
Bug 1263560 - Accept whitespace in command API key names. r=kmag MozReview-Commit-ID: IGQDs1RuE7w
browser/components/extensions/ext-commands.js
browser/components/extensions/schemas/commands.json
browser/components/extensions/test/browser/browser_ext_commands_onCommand.js
--- a/browser/components/extensions/ext-commands.js
+++ b/browser/components/extensions/ext-commands.js
@@ -69,19 +69,20 @@ CommandList.prototype = {
    */
   loadCommandsFromManifest(manifest) {
     let commands = new Map();
     // For Windows, chrome.runtime expects 'win' while chrome.commands
     // expects 'windows'.  We can special case this for now.
     let os = PlatformInfo.os == "win" ? "windows" : PlatformInfo.os;
     for (let name of Object.keys(manifest.commands)) {
       let command = manifest.commands[name];
+      let shortcut = command.suggested_key[os] || command.suggested_key.default;
       commands.set(name, {
         description: command.description,
-        shortcut: command.suggested_key[os] || command.suggested_key.default,
+        shortcut: shortcut.replace(/\s+/g, ""),
       });
     }
     return commands;
   },
 
   /**
    * Registers the commands to a document.
    * @param {ChromeWindow} window The XUL window to insert the Keyset.
--- a/browser/components/extensions/schemas/commands.json
+++ b/browser/components/extensions/schemas/commands.json
@@ -6,17 +6,17 @@
   {
     "namespace": "manifest",
     "types": [
      {
         "id": "KeyName",
         "choices": [
           {
             "type": "string",
-            "pattern": "^(Alt|Ctrl|Command|MacCtr)\\+(Shift\\+)?([A-Z0-9]|Comma|Period|Home|End|PageUp|PageDown|Space|Insert|Delete|Up|Down|Left|Right)$"
+            "pattern": "^\\s*(Alt|Ctrl|Command|MacCtr)\\s*\\+\\s*(Shift\\s*\\+\\s*)?([A-Z0-9]|Comma|Period|Home|End|PageUp|PageDown|Space|Insert|Delete|Up|Down|Left|Right)\\s*$"
           },
           {
             "type": "string",
             "pattern": "^(MediaNextTrack|MediaPlayPause|MediaPrevTrack|MediaStop)$"
           }
         ]
       },
       {
--- a/browser/components/extensions/test/browser/browser_ext_commands_onCommand.js
+++ b/browser/components/extensions/test/browser/browser_ext_commands_onCommand.js
@@ -17,21 +17,26 @@ add_task(function* test_user_defined_com
           },
         },
         "toggle-feature-using-alt-shift-comma": {
           "suggested_key": {
             "default": "Alt+Shift+Comma",
           },
           "unrecognized_property": "with-a-random-value",
         },
+        "toggle-feature-with-whitespace-in-suggested-key": {
+          "suggested_key": {
+            "default": "  Alt + Shift + 2  ",
+          },
+        },
       },
     },
 
     background: function() {
-      browser.commands.onCommand.addListener((commandName) => {
+      browser.commands.onCommand.addListener(commandName => {
         browser.test.sendMessage("oncommand", commandName);
       });
       browser.test.sendMessage("ready");
     },
   });
 
 
   SimpleTest.waitForExplicitFinish();
@@ -48,33 +53,37 @@ add_task(function* test_user_defined_com
   let win2 = yield BrowserTestUtils.openNewBrowserWindow();
   yield BrowserTestUtils.loadURI(win2.gBrowser.selectedBrowser, "about:config");
   yield BrowserTestUtils.browserLoaded(win2.gBrowser.selectedBrowser);
 
   // Confirm the keysets have been added to both windows.
   let keysetID = `ext-keyset-id-${makeWidgetId(extension.id)}`;
   let keyset = win1.document.getElementById(keysetID);
   ok(keyset != null, "Expected keyset to exist");
-  is(keyset.childNodes.length, 2, "Expected keyset to have 2 children");
+  is(keyset.childNodes.length, 3, "Expected keyset to have 3 children");
 
   keyset = win2.document.getElementById(keysetID);
   ok(keyset != null, "Expected keyset to exist");
-  is(keyset.childNodes.length, 2, "Expected keyset to have 2 children");
+  is(keyset.childNodes.length, 3, "Expected keyset to have 3 children");
 
   // Confirm that the commands are registered to both windows.
   yield focusWindow(win1);
   EventUtils.synthesizeKey("3", {altKey: true, shiftKey: true});
   let message = yield extension.awaitMessage("oncommand");
   is(message, "toggle-feature-using-alt-shift-3", "Expected onCommand listener to fire with correct message");
 
   yield focusWindow(win2);
   EventUtils.synthesizeKey("VK_COMMA", {altKey: true, shiftKey: true});
   message = yield extension.awaitMessage("oncommand");
   is(message, "toggle-feature-using-alt-shift-comma", "Expected onCommand listener to fire with correct message");
 
+  EventUtils.synthesizeKey("2", {altKey: true, shiftKey: true});
+  message = yield extension.awaitMessage("oncommand");
+  is(message, "toggle-feature-with-whitespace-in-suggested-key", "Expected onCommand listener to fire with correct message");
+
   yield extension.unload();
 
   // Confirm that the keysets have been removed from both windows after the extension is unloaded.
   keyset = win1.document.getElementById(keysetID);
   is(keyset, null, "Expected keyset to be removed from the window");
 
   keyset = win2.document.getElementById(keysetID);
   is(keyset, null, "Expected keyset to be removed from the window");