Bug 1280888 - make hash links in about:reader absolute to avoid kicking people out of reader mode unnecessarily, r?jaws draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Wed, 24 Aug 2016 16:49:13 +0100
changeset 405002 94f03c3e29b478b6f3bed16241c53b74a16e3373
parent 404560 8df08a4648cd4432db3fd6ef2885672335cdc4ef
child 529331 a1da0c0df44e78fd8cb9adb189e706e062c29352
push id27367
push usergijskruitbosch@gmail.com
push dateWed, 24 Aug 2016 15:49:50 +0000
reviewersjaws
bugs1280888
milestone51.0a1
Bug 1280888 - make hash links in about:reader absolute to avoid kicking people out of reader mode unnecessarily, r?jaws MozReview-Commit-ID: IwrPJZZXxEW
toolkit/components/reader/AboutReader.jsm
--- a/toolkit/components/reader/AboutReader.jsm
+++ b/toolkit/components/reader/AboutReader.jsm
@@ -716,16 +716,31 @@ AboutReader.prototype = {
     if (!article.dir)
       return;
 
     //Set "dir" attribute on content
     this._contentElement.setAttribute("dir", article.dir);
     this._headerElement.setAttribute("dir", article.dir);
   },
 
+  _fixLocalLinks() {
+    // We need to do this because preprocessing the content through nsIParserUtils
+    // gives back a DOM with a <base> element. That influences how these URLs get
+    // resolved, making them no longer match the document URI (which is
+    // about:reader?url=...). To fix this, make all the hash URIs absolute. This
+    // is hacky, but the alternative of removing the base element has potential
+    // security implications if Readability has not successfully made all the URLs
+    // absolute, so we pick just fixing these in-document links explicitly.
+    let localLinks = this._contentElement.querySelectorAll("a[href^='#']");
+    for (let localLink of localLinks) {
+      // Have to get the attribute because .href provides an absolute URI.
+      localLink.href = this._doc.documentURI + localLink.getAttribute("href");
+    }
+  },
+
   _showError: function() {
     this._headerElement.style.display = "none";
     this._contentElement.style.display = "none";
 
     let errorMessage = gStrings.GetStringFromName("aboutReader.loadError");
     this._messageElement.textContent = errorMessage;
     this._messageElement.style.display = "block";
 
@@ -767,16 +782,17 @@ AboutReader.prototype = {
     this._headerElement.style.display = "block";
 
     let parserUtils = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils);
     let contentFragment = parserUtils.parseFragment(article.content,
       Ci.nsIParserUtils.SanitizerDropForms | Ci.nsIParserUtils.SanitizerAllowStyle,
       false, articleUri, this._contentElement);
     this._contentElement.innerHTML = "";
     this._contentElement.appendChild(contentFragment);
+    this._fixLocalLinks();
     this._maybeSetTextDirection(article);
 
     this._contentElement.style.display = "block";
     this._updateImageMargins();
 
     this._requestFavicon();
     this._doc.body.classList.add("loaded");