bug 454518 -allow opening URLs that are not linked from the context menu (if selected), r=gavin, ui-r=beltzner
authorMike Connor <mconnor@mozilla.com>
Wed, 09 Sep 2009 05:10:53 -0400
changeset 32348 921c0a30c2f3f8d7b7de346dad7137094febd7d9
parent 32347 8a1ce801b6a8a7a7bfdf6fa93e8f05cd38bb9cd2
child 32349 acd4bfec58138eb4c7323132e81fd2738c2ad0d3
push id9017
push usermconnor@mozilla.com
push dateWed, 09 Sep 2009 21:05:16 +0000
treeherdermozilla-central@921c0a30c2f3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin, beltzner
bugs454518
milestone1.9.3a1pre
bug 454518 -allow opening URLs that are not linked from the context menu (if selected), r=gavin, ui-r=beltzner
browser/base/content/browser-context.inc
browser/base/content/nsContextMenu.js
browser/locales/en-US/chrome/browser/browser.dtd
--- a/browser/base/content/browser-context.inc
+++ b/browser/base/content/browser-context.inc
@@ -39,16 +39,20 @@
       <menuitem id="spell-no-suggestions"
                 disabled="true"
                 label="&spellNoSuggestions.label;"/>
       <menuitem id="spell-add-to-dictionary"
                 label="&spellAddToDictionary.label;"
                 accesskey="&spellAddToDictionary.accesskey;"
                 oncommand="InlineSpellCheckerUI.addToDictionary();"/>
       <menuseparator id="spell-suggestions-separator"/>
+      <menuitem id="context-openlinkincurrent"
+                label="&openLinkCmdInCurrent.label;"
+                accesskey="&openLinkCmdInCurrent.accesskey;"
+                oncommand="gContextMenu.openLinkInCurrent();"/>
       <menuitem id="context-openlink"
                 label="&openLinkCmd.label;"
                 accesskey="&openLinkCmd.accesskey;"
                 oncommand="gContextMenu.openLink();"/>
       <menuitem id="context-openlinkintab"
                 label="&openLinkCmdInTab.label;"
                 accesskey="&openLinkCmdInTab.accesskey;"
                 oncommand="gContextMenu.openLinkInTab();"/>
--- a/browser/base/content/nsContextMenu.js
+++ b/browser/base/content/nsContextMenu.js
@@ -138,19 +138,39 @@ nsContextMenu.prototype = {
     if (this.onMailtoLink) {
       var mailtoHandler = Cc["@mozilla.org/uriloader/external-protocol-service;1"].
                           getService(Ci.nsIExternalProtocolService).
                           getProtocolHandlerInfo("mailto");
       isMailtoInternal = (!mailtoHandler.alwaysAskBeforeHandling &&
                           mailtoHandler.preferredAction == Ci.nsIHandlerInfo.useHelperApp &&
                           (mailtoHandler.preferredApplicationHandler instanceof Ci.nsIWebHandlerApp));
     }
-    var shouldShow = this.onSaveableLink || isMailtoInternal;
+
+    // time to do some bad things and see if we've highlighted a URL that isn't actually linked
+    if (this.isTextSelected) {
+      // ok, we have some text, let's figure out if it looks like a URL
+      var someText = document.commandDispatcher.focusedWindow
+                             .getSelection().toString();
+      try {
+       var uri = makeURI(someText);
+      }
+      catch (ex) { }
+ 
+      var onPlainTextLink = false;
+      if (uri && /^(https?|ftp)/i.test(uri.scheme) && uri.host) {
+        this.linkURI = uri;
+        this.linkURL = this.linkURI.spec;
+        onPlainTextLink = true;
+      }
+    }
+ 
+    var shouldShow = this.onSaveableLink || isMailtoInternal || onPlainTextLink;
     this.showItem("context-openlink", shouldShow);
     this.showItem("context-openlinkintab", shouldShow);
+    this.showItem("context-openlinkincurrent", onPlainTextLink);
     this.showItem("context-sep-open", shouldShow);
   },
 
   initNavigationItems: function CM_initNavigationItems() {
     var shouldShow = !(this.isContentSelected || this.onLink || this.onImage ||
                        this.onCanvas || this.onVideo || this.onAudio ||
                        this.onTextInput);
     this.showItem("context-back", shouldShow);
@@ -650,16 +670,22 @@ nsContextMenu.prototype = {
     openNewWindowWith(this.linkURL, this.target.ownerDocument, null, false);
   },
 
   // Open linked-to URL in a new tab.
   openLinkInTab: function() {
     openNewTabWith(this.linkURL, this.target.ownerDocument, null, null, false);
   },
 
+  // open URL in current tab
+  openLinkInCurrent: function() {
+    openUILinkIn(this.linkURL, "current", null, null, 
+                 this.target.ownerDocument.documentURIObject);
+  },
+
   // Open frame in a new tab.
   openFrameInTab: function() {
     var doc = this.target.ownerDocument;
     var frameURL = doc.location.href;
     var referrer = doc.referrer;
 
     return openNewTabWith(frameURL, null, null, null, false,
                           referrer ? makeURI(referrer) : null);
--- a/browser/locales/en-US/chrome/browser/browser.dtd
+++ b/browser/locales/en-US/chrome/browser/browser.dtd
@@ -230,16 +230,18 @@
   
  -->
 <!ENTITY searchFocus.commandkey       "k">
 <!ENTITY searchFocus.commandkey2      "e">
 <!ENTITY searchFocusUnix.commandkey   "j">
 
 <!ENTITY openLinkCmd.label            "Open Link in New Window">
 <!ENTITY openLinkCmd.accesskey        "W">
+<!ENTITY openLinkCmdInCurrent.label     "Open Link">
+<!ENTITY openLinkCmdInCurrent.accesskey "O">
 <!ENTITY openLinkCmdInTab.label       "Open Link in New Tab">
 <!ENTITY openLinkCmdInTab.accesskey   "T">
 <!ENTITY openFrameCmd.label           "Open Frame in New Window">
 <!ENTITY openFrameCmd.accesskey       "W">
 <!ENTITY openFrameCmdInTab.label      "Open Frame in New Tab">
 <!ENTITY openFrameCmdInTab.accesskey  "T">
 <!ENTITY showOnlyThisFrameCmd.label     "Show Only This Frame">
 <!ENTITY showOnlyThisFrameCmd.accesskey "S">