Bug 613199 URLBar FixUps. Part1. Followup to PasteAndGo, implement copy and cut, and fix Bug 480537. r=Neil.
authorPhilip Chee <philip.chee@gmail.com>
Sun, 21 Nov 2010 23:34:33 +0800
changeset 6729 2ceaa8229eb1aca1e56b8a91be232ca4f7ee00cd
parent 6728 39b78698b329ce48e38528e54792da9cda87ed15
child 6730 46dd15659816143494ee3c3e669ebea75916a601
push idunknown
push userunknown
push dateunknown
reviewersNeil
bugs613199, 480537
Bug 613199 URLBar FixUps. Part1. Followup to PasteAndGo, implement copy and cut, and fix Bug 480537. r=Neil.
suite/browser/urlbarBindings.xml
--- a/suite/browser/urlbarBindings.xml
+++ b/suite/browser/urlbarBindings.xml
@@ -96,40 +96,88 @@
                 this.mDefaultMatchFilled = true;
               }
               this.mNeedToComplete = true;
             }
           }
         ]]></body>
       </method>
 
+      <method name="_getSelectedValueForClipboard">
+        <body>
+          <![CDATA[
+            var inputVal = this.inputField.value;
+            var val = inputVal.substring(this.selectionStart, this.selectionEnd);
+
+            /* If the entire value is selected and it's a valid non-javascript,
+               non-data URI, encode it. */
+            if (val == inputVal &&
+                gProxyButton.getAttribute("pageproxystate") == "valid") {
+              var uri;
+              try {
+                uri = makeURI(val);
+              } catch (e) {}
+
+              if (uri && !uri.schemeIs("javascript") && !uri.schemeIs("data")) {
+                val = uri.spec;
+
+                // Parentheses are known to confuse third-party applications (bug 458565).
+                val = val.replace(/[()]/g, function (c) escape(c));
+              }
+            }
+
+            return val;
+          ]]>
+        </body>
+      </method>
+
       <field name="_editItemsController"><![CDATA[
         ({
-          editor: this.editor,
-          _fireEvent: this._fireEvent.bind(this),
           supportsCommand: function(aCommand) {
             switch (aCommand) {
+              case "cmd_copy":
+              case "cmd_cut":
               case "cmd_pasteAndGo":
                 return true;
             }
             return false;
           },
           isCommandEnabled: function(aCommand) {
+            var hasSelection = this.selectionStart < this.selectionEnd;
             switch (aCommand) {
+              case "cmd_copy":
+                return hasSelection;
+              case "cmd_cut":
+                return !this.readOnly && hasSelection;
               case "cmd_pasteAndGo":
                 return document.commandDispatcher
                                .getControllerForCommand("cmd_paste")
                                .isCommandEnabled("cmd_paste");
             }
             return false;
-          },
+          }.bind(this),
           doCommand: function(aCommand) {
             switch (aCommand) {
+              case "cmd_copy":
+              case "cmd_cut":
+                var val = this._getSelectedValueForClipboard();
+                var controller = this._editItemsController;
+                if (!val || !controller.isCommandEnabled(aCommand))
+                  return;
+
+                Components.classes["@mozilla.org/widget/clipboardhelper;1"]
+                          .getService(Components.interfaces.nsIClipboardHelper)
+                          .copyString(val);
+
+                if (aCommand == "cmd_cut")
+                  goDoCommand("cmd_delete");
+                break;
+
               case "cmd_pasteAndGo":
-                this.value = "";
+                this.select();
                 goDoCommand("cmd_paste");
                 this._fireEvent("textentered", "pasting");
                 break;
             }
           }.bind(this),
           onEvent: function(aEventName) {}
         })
       ]]></field>