Bug 608791 - onBeforeLinkTraversal should compare host strings instead of top level domains. r=gavin, a=blocking
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Mon, 29 Nov 2010 13:22:25 -0800
changeset 58343 50913ed5b7775fb0abe89b565aad0aaf0a3c8c0c
parent 58342 9a0741efda5ce4ce5541f75d8ed330a383f87543
child 58344 eaecf12fbf6edd451ef2fbdd8ae431f5ccd94e63
push id17254
push usermargaret.leibovic@gmail.com
push dateMon, 29 Nov 2010 21:29:47 +0000
treeherdermozilla-central@650ad3cd7e52 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin, blocking
bugs608791
milestone2.0b8pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 608791 - onBeforeLinkTraversal should compare host strings instead of top level domains. r=gavin, a=blocking
browser/base/content/browser.js
browser/base/content/test/app_bug575561.html
browser/base/content/test/browser_bug575561.js
build/pgo/server-locations.txt
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -3990,28 +3990,39 @@ var XULBrowserWindow = {
 
   // Called before links are navigated to to allow us to retarget them if needed.
   onBeforeLinkTraversal: function(originalTarget, linkURI, linkNode, isAppTab) {
     // Don't modify non-default targets or targets that aren't in top-level app
     // tab docshells (isAppTab will be false for app tab subframes).
     if (originalTarget != "" || !isAppTab)
       return originalTarget;
 
-    let docURI = linkNode.ownerDocument.documentURIObject;
+    // External links from within app tabs should always open in new tabs
+    // instead of replacing the app tab's page (Bug 575561)
+    let linkHost;
+    let docHost;
     try {
-      let docURIDomain = Services.eTLD.getBaseDomain(docURI, 0);
-      let linkURIDomain = Services.eTLD.getBaseDomain(linkURI, 0);
-      // External links from within app tabs should always open in new tabs
-      // instead of replacing the app tab's page (Bug 575561)
-      if (docURIDomain != linkURIDomain)
-        return "_blank";
+      linkHost = linkURI.host;
+      docHost = linkNode.ownerDocument.documentURIObject.host;
     } catch(e) {
-      // If getBaseDomain fails, we return originalTarget below.
-    }
-    return originalTarget;
+      // nsIURI.host can throw for non-nsStandardURL nsIURIs.
+      // If we fail to get either host, just return originalTarget.
+      return originalTarget;
+    }
+
+    if (docHost == linkHost)
+      return originalTarget;
+
+    // Special case: ignore "www" prefix if it is part of host string
+    let [longHost, shortHost] =
+      linkHost.length > docHost.length ? [linkHost, docHost] : [docHost, linkHost];
+    if (longHost == "www." + shortHost)
+      return originalTarget;
+
+    return "_blank";
   },
 
   onLinkIconAvailable: function (aIconURL) {
     if (gProxyFavIcon && gBrowser.userTypedValue === null)
       PageProxySetIcon(aIconURL); // update the favicon in the URL bar
   },
 
   onProgressChange: function (aWebProgress, aRequest,
--- a/browser/base/content/test/app_bug575561.html
+++ b/browser/base/content/test/app_bug575561.html
@@ -6,11 +6,14 @@ https://bugzilla.mozilla.org/show_bug.cg
   <head>
     <title>Test for links in app tabs</title>
   </head>
   <body>
     <a href="http://example.com/browser/browser/base/content/test/dummy_page.html">same domain</a>
     <a href="http://test1.example.com/browser/browser/base/content/test/dummy_page.html">same domain (different subdomain)</a>
     <a href="http://example.org/browser/browser/base/content/test/dummy_page.html">different domain</a>
     <a href="http://example.org/browser/browser/base/content/test/dummy_page.html" target="foo">different domain (with target)</a>
+    <a href="http://www.example.com/browser/browser/base/content/test/dummy_page.html">same domain (www prefix)</a>
+    <a href="data:text/html,<!DOCTYPE html><html><body>Another Page</body></html>">data: URI</a>
+    <a href="about:mozilla">about: URI</a>
     <iframe src="app_subframe_bug575561.html"></iframe>
   </body>
 </html>
--- a/browser/base/content/test/browser_bug575561.js
+++ b/browser/base/content/test/browser_bug575561.js
@@ -1,29 +1,41 @@
 function test() {
   waitForExplicitFinish();
 
   // Pinned: Link to the same domain should not open a new tab
   // Tests link to http://example.com/browser/browser/base/content/test/dummy_page.html  
   testLink(0, true, false, function() {
-    // Pinned: Link to the same domain should not open a new tab
+    // Pinned: Link to a different subdomain should open a new tab
     // Tests link to http://test1.example.com/browser/browser/base/content/test/dummy_page.html
-    testLink(1, true, false, function() {
+    testLink(1, true, true, function() {
       // Pinned: Link to a different domain should open a new tab
       // Tests link to http://example.org/browser/browser/base/content/test/dummy_page.html
       testLink(2, true, true, function() {
         // Not Pinned: Link to a different domain should not open a new tab
         // Tests link to http://example.org/browser/browser/base/content/test/dummy_page.html
         testLink(2, false, false, function() {
           // Pinned: Targetted link should open a new tab
           // Tests link to http://example.org/browser/browser/base/content/test/dummy_page.html with target="foo"
           testLink(3, true, true, function() {
             // Pinned: Link in a subframe should not open a new tab
             // Tests link to http://example.org/browser/browser/base/content/test/dummy_page.html in subframe
-            testLink(0, true, false, finish, true);
+            testLink(0, true, false, function() {
+              // Pinned: Link to the same domain (with www prefix) should not open a new tab
+              // Tests link to http://www.example.com/browser/browser/base/content/test/dummy_page.html      
+              testLink(4, true, false, function() {
+                // Pinned: Link to a data: URI should not open a new tab
+                // Tests link to data:text/html,<!DOCTYPE html><html><body>Another Page</body></html>
+                testLink(5, true, false, function() {
+                  // Pinned: Link to an about: URI should not open a new tab
+                  // Tests link to about:mozilla
+                  testLink(6, true, false, finish);
+                });
+              });
+            }, true);
           });
         });
       });
     });
   });
 }
 
 function testLink(aLinkIndex, pinTab, expectNewTab, nextTest, testSubFrame) {
--- a/build/pgo/server-locations.txt
+++ b/build/pgo/server-locations.txt
@@ -95,16 +95,17 @@ http://sub2.test2.example.org:80     pri
 http://example.org:8000              privileged
 http://test1.example.org:8000        privileged
 http://test2.example.org:8000        privileged
 http://sub1.test1.example.org:8000   privileged
 http://sub1.test2.example.org:8000   privileged
 http://sub2.test1.example.org:8000   privileged
 http://sub2.test2.example.org:8000   privileged
 http://example.com:80                privileged
+http://www.example.com:80            privileged
 http://test1.example.com:80          privileged
 http://test2.example.com:80          privileged
 http://sub1.test1.example.com:80     privileged
 http://sub1.test2.example.com:80     privileged
 http://sub2.test1.example.com:80     privileged
 http://sub2.test2.example.com:80     privileged
 http://noxul.example.com:80          privileged,noxul