Clean up better

This commit is contained in:
Domenic Denicola 2015-05-08 00:15:43 +02:00
commit 64de4a27e5

View file

@ -68,16 +68,45 @@ function getChapterString(rawChapterDoc) {
}
function cleanContentEl(el) {
// Remove Next Chapter and Previous Chapter <p>s
// Remove initial Next Chapter and Previous Chapter <p>
el.removeChild(el.firstElementChild);
el.removeChild(el.lastElementChild);
// Remove redundant dir="ltr"
// Remove everything after the last <p> (e.g. analytics <div>s)
const lastP = el.querySelector("p:last-of-type");
while (el.lastElementChild !== lastP) {
el.removeChild(el.lastElementChild);
}
// Remove empty <p>s or Last Chapter/Next Chapter <p>s
while (isEmptyOrGarbage(el.lastElementChild)) {
el.removeChild(el.lastElementChild);
}
// Remove redundant dir="ltr" and align="LEFT" and style="text-align: left;"
Array.prototype.forEach.call(el.children, function (child) {
if (child.getAttribute("dir") === "ltr") {
child.removeAttribute("dir");
}
if ((child.getAttribute("align") || "").toLowerCase() === "left") {
child.removeAttribute("align");
}
if (child.getAttribute("style") === "text-align:left;") {
child.removeAttribute("style");
}
});
// Remove empty <em>s and <i>s
const ems = el.querySelectorAll("em, i");
Array.prototype.forEach.call(ems, function (em) {
if (em.textContent.trim() === "") {
em.parentNode.removeChild(em);
}
});
return el;
}
function isEmptyOrGarbage(el) {
const text = el.textContent.trim();
return text === "" || text.startsWith("Last Chapter") || text.startsWith("Next Chapter");
}