Bug 440075 - Location bar doesn't encode the address when using UNIX copy/paste (selecting). r=gavin
authorDão Gottwald <dao@mozilla.com>
Wed, 08 Oct 2008 16:42:39 +0200
changeset 20161 7b2a03375806ded66aca6cc10df3abf419570f94
parent 20160 e04b78ef96701c3e3ad7056d292ad93462a8c1ec
child 20162 7f66adec37acf8148214d63e84e4423f0858bcb7
push idunknown
push userunknown
push dateunknown
reviewersgavin
bugs440075
milestone1.9.1b2pre
Bug 440075 - Location bar doesn't encode the address when using UNIX copy/paste (selecting). r=gavin
browser/base/content/urlbarBindings.xml
--- a/browser/base/content/urlbarBindings.xml
+++ b/browser/base/content/urlbarBindings.xml
@@ -279,54 +279,55 @@
           // haven't seen that, so hopefully that behavior has changed.
           flavourSet.appendFlavour("text/x-moz-url");
           flavourSet.appendFlavour("text/unicode");
           flavourSet.appendFlavour("application/x-moz-file", "nsIFile");
           return flavourSet;
         ]]></body>
       </method>
 
+      <method name="_getSelectedValueForClipboard">
+        <body><![CDATA[
+          var val = this.value.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 == this.value &&
+              this.getAttribute("pageproxystate") == "valid") {
+            let uri;
+            let ioService = Cc["@mozilla.org/network/io-service;1"]
+                              .getService(Ci.nsIIOService);
+            try {
+              uri = ioService.newURI(val, null, null);
+            } catch (e) {}
+            if (uri && !uri.schemeIs("javascript") && !uri.schemeIs("data"))
+              val = uri.spec;
+          }
+
+          return val;
+        ]]></body>
+      </method>
+
       <field name="_copyCutController"><![CDATA[
         ({
           urlbar: this,
           doCommand: function(aCommand) {
             var urlbar = this.urlbar;
-            var start = urlbar.selectionStart;
-            var end = urlbar.selectionEnd;
-            if (start == end)
+            var val = urlbar._getSelectedValueForClipboard();
+            if (!val)
               return;
-            var val;
-            if (start == 0 && end == urlbar.textLength) {
-              val = urlbar.value;
 
-              // If the entire value is selected and it's a valid non-javascript,
-              // non-data URI, encode it.
-              var uri;
-              if (urlbar.getAttribute("pageproxystate") == "valid") {
-                var ioService = Cc["@mozilla.org/network/io-service;1"]
-                                  .getService(Ci.nsIIOService);
-                try {
-                  uri = ioService.newURI(val, null, null);
-                } catch (e) {}
-              }
-              if (uri && !uri.schemeIs("javascript") && !uri.schemeIs("data"))
-                 val = uri.spec;
+            if (aCommand == "cmd_cut" && this.isCommandEnabled(aCommand)) {
+              let start = urlbar.selectionStart;
+              let end = urlbar.selectionEnd;
+              urlbar.value = urlbar.value.substring(0, start) + urlbar.value.substring(end);
+              urlbar.selectionStart = urlbar.selectionEnd = start;
+              SetPageProxyState("invalid");
+            }
 
-              if (aCommand == "cmd_cut" && this.isCommandEnabled(aCommand)) {
-                urlbar.value = "";
-                SetPageProxyState("invalid");
-              }
-            } else {
-              val = urlbar.value.substring(start, end);
-              if (aCommand == "cmd_cut" && this.isCommandEnabled(aCommand)) {
-                urlbar.value = urlbar.value.substring(0, start) + urlbar.value.substring(end);
-                urlbar.selectionStart = urlbar.selectionEnd = start;
-                SetPageProxyState("invalid");
-              }
-            }
             Cc["@mozilla.org/widget/clipboardhelper;1"]
               .getService(Ci.nsIClipboardHelper)
               .copyString(val);
           },
           supportsCommand: function(aCommand) {
             switch (aCommand) {
               case "cmd_copy":
               case "cmd_cut":
@@ -408,16 +409,30 @@
         </setter>
       </property>
     </implementation>
 
     <handlers>
       <handler event="focus" phase="capturing" action="this._hideURLTooltip();"/>
       <handler event="dragover" phase="capturing" action="nsDragAndDrop.dragOver(event, this);"/>
       <handler event="dragdrop" phase="capturing" action="nsDragAndDrop.drop(event, this);"/>
+      <handler event="select"><![CDATA[
+        if (!Cc["@mozilla.org/widget/clipboard;1"]
+               .getService(Ci.nsIClipboard)
+               .supportsSelectionClipboard())
+          return;
+
+        var val = this._getSelectedValueForClipboard();
+        if (!val)
+          return;
+
+        Cc["@mozilla.org/widget/clipboardhelper;1"]
+          .getService(Ci.nsIClipboardHelper)
+          .copyStringToClipboard(val, Ci.nsIClipboard.kSelectionClipboard);
+      ]]></handler>
     </handlers>
 
   </binding>
 
   <!-- Note: this binding is applied to the autocomplete popup used in the Search bar and in web page content -->
   <binding id="browser-autocomplete-result-popup" extends="chrome://global/content/bindings/autocomplete.xml#autocomplete-result-popup">
     <implementation>
       <method name="openAutocompletePopup">