Bug 1154295 - use normal formatting for plaintext documents in reader mode r=Gijs
authorTrushita <trushita@gmail.com>
Fri, 22 Mar 2019 20:08:56 +0000
changeset 465774 0539b9847dd53c4cdb75108b4e580bc518642690
parent 465773 67cb542606682bfa8cdf359a48d54b5d461a010c
child 465775 565644972ec23f4a8e54b60efc793b0e23e6d84a
push id35746
push usershindli@mozilla.com
push dateSat, 23 Mar 2019 09:46:24 +0000
treeherdermozilla-central@02b7484f316b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1154295
milestone68.0a1
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 1154295 - use normal formatting for plaintext documents in reader mode r=Gijs Before this change the plain text document in reader mode was not formatted/styled properly. Differential Revision: https://phabricator.services.mozilla.com/D24541
toolkit/components/reader/ReaderMode.jsm
--- a/toolkit/components/reader/ReaderMode.jsm
+++ b/toolkit/components/reader/ReaderMode.jsm
@@ -377,16 +377,21 @@ var ReaderMode = {
     let uriParam = {
       spec: doc.baseURIObject.spec,
       host: doc.baseURIObject.host,
       prePath: doc.baseURIObject.prePath,
       scheme: doc.baseURIObject.scheme,
       pathBase: Services.io.newURI(".", null, doc.baseURIObject).spec,
     };
 
+    // convert text/plain document, if any, to XHTML format
+    if (this._isDocumentPlainText(doc)) {
+      doc = this._convertPlainTextDocument(doc);
+    }
+
     let serializer = new XMLSerializer();
     let serializedDoc = serializer.serializeToString(doc);
 
     let options = {
       classesToPreserve: CLASSES_TO_PRESERVE,
     };
 
     let article = null;
@@ -535,12 +540,47 @@ var ReaderMode = {
       [ "sk", {cpm: 885,  variance: 145 } ],
       [ "sv", {cpm: 917,  variance: 156 } ],
       [ "tr", {cpm: 1054, variance: 156 } ],
       [ "zh", {cpm: 255,  variance: 29 } ],
     ]);
 
     return readingSpeed.get(lang) || readingSpeed.get("en");
   },
+  /**
+   *
+   * Check if the document to be parsed is text document.
+   * @param doc the doc object to be parsed.
+   * @return boolean
+   *
+   */
+  _isDocumentPlainText(doc) {
+    return doc.contentType == "text/plain";
+  },
+  /**
+   *
+   * The document to be parsed is text document and is converted to HTML format.
+   * @param doc the doc object to be parsed.
+   * @return doc
+   *
+   */
+  _convertPlainTextDocument(doc) {
+    let preTag = doc.querySelector("pre");
+    let docFrag = doc.createDocumentFragment();
+    let content = preTag.textContent;
+    let paragraphs = content.split(/\r?\n\r?\n/);
+    for (let para of paragraphs) {
+      let pElem = doc.createElement("p");
+      let lines = para.split(/\n/);
+      for (let line of lines) {
+        pElem.append(line);
+        let brElem = doc.createElement("br");
+        pElem.append(brElem);
+      }
+      docFrag.append(pElem);
+    }
+    preTag.parentNode.replaceChild(docFrag, preTag);
+    return doc;
+  },
 };
 
 XPCOMUtils.defineLazyPreferenceGetter(
   ReaderMode, "maxElemsToParse", "reader.parse-node-limit", 0);