mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-10-08 16:27:31 +09:00
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.
This commit is contained in:
parent
338ef48fcf
commit
89ae9a0b36
1 changed files with 42 additions and 0 deletions
|
|
@ -346,6 +346,11 @@ this.ReaderMode = {
|
||||||
pathBase: Services.io.newURI(".", null, doc.baseURIObject).spec
|
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 = {
|
let langAttributes = {
|
||||||
charset: doc.characterSet,
|
charset: doc.characterSet,
|
||||||
lang: doc.documentElement.lang
|
lang: doc.documentElement.lang
|
||||||
|
|
@ -540,6 +545,43 @@ this.ReaderMode = {
|
||||||
|
|
||||||
return readingSpeed.get(lang) || readingSpeed.get("en");
|
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
|
// Don't try to parse the page if it has too many elements (for memory and
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue