Bug 1280888 - make hash links in about:reader absolute to avoid kicking people out of reader mode unnecessarily, r?jaws
MozReview-Commit-ID: IwrPJZZXxEW
--- 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");