diff options
author | Trushita <trushita@gmail.com> | 2020-05-23 22:22:53 -0400 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2020-06-01 11:47:01 +0000 |
commit | 83ac4391b64b67b22fada4f60a15a7ac185e6435 (patch) | |
tree | fb4c7b9bd111694f83da7c340a04fb0b7aa688e9 /toolkit | |
parent | 027a4cac9cd2f89ac73c35d9cf8da7cfa2ab3e60 (diff) | |
download | uxp-83ac4391b64b67b22fada4f60a15a7ac185e6435.tar.gz |
Bug 1154295 - use normal formatting for plaintext documents in reader mode
Before this change the plain text document in reader mode was not formatted/styled properly.
Tag #361.
Diffstat (limited to 'toolkit')
-rw-r--r-- | toolkit/components/reader/ReaderMode.jsm | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/toolkit/components/reader/ReaderMode.jsm b/toolkit/components/reader/ReaderMode.jsm index d9889e19e5..a1d2c5643a 100644 --- a/toolkit/components/reader/ReaderMode.jsm +++ b/toolkit/components/reader/ReaderMode.jsm @@ -346,6 +346,11 @@ this.ReaderMode = { 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 langAttributes = { charset: doc.characterSet, lang: doc.documentElement.lang @@ -540,6 +545,43 @@ this.ReaderMode = { 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; + }, }; // Don't try to parse the page if it has too many elements (for memory and |