/* * Copyright 2018 Gildas Lormeau * contact : gildas.lormeau gmail.com * * This file is part of SingleFile. * * SingleFile is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * SingleFile is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with SingleFile. If not, see . */ /* global Node */ this.serializer = this.serializer || (() => { const SELF_CLOSED_TAG_NAMES = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]; const OMITTED_END_TAGS = [ { tagName: "li", followings: ["li"] }, { tagName: "dt", followings: ["dt", "dd"] }, { tagName: "dd", followings: ["dt", "dd"] }, { tagName: "rt", followings: ["rt", "rp"] }, { tagName: "rp", followings: ["rt", "rp"] }, { tagName: "optgroup", followings: ["optgroup"] }, { tagName: "option", followings: ["option", "optgroup"] }, { tagName: "thead", followings: ["tbody", "tfoot"] }, { tagName: "tbody", followings: ["tbody", "tfoot"] }, { tagName: "tfoot" }, { tagName: "tr", followings: ["tr"] }, { tagName: "td", followings: ["td", "th"] }, { tagName: "th", followings: ["td", "th"] }, ]; return { process(doc, compressHTML) { return getDoctype(doc) + (compressHTML ? serialize(doc.documentElement) : doc.documentElement.outerHTML); } }; function getDoctype(doc) { const docType = doc.doctype; let docTypeString; if (docType) { docTypeString = "\n"; } return ""; } function serialize(node) { if (node.nodeType == Node.TEXT_NODE) { return serializeTextNode(node); } else if (node.nodeType == Node.COMMENT_NODE) { return serializeCommentNode(node); } else if (node.nodeType == Node.ELEMENT_NODE) { return serializeElement(node); } } function serializeTextNode(textNode) { return textNode.textContent; } function serializeCommentNode(commentNode) { return ""; } function serializeElement(element) { const tagName = element.tagName.toLowerCase(); let content = "<" + tagName; Array.from(element.attributes).forEach(attribute => { let value = attribute.value; if (attribute.name == "class") { value = element.classList.toString().trim(); } value = value.replace(/&/g, "&").replace(/\u00a0/g, " ").replace(/"/g, """); const validUnquotedValue = value.match(/^[^ \t\n\f\r"'`=<>]+$/); content += " "; if (!attribute.namespace) { content += attribute.name; } else if (attribute.namespaceURI == "http://www.w3.org/XML/1998/namespace") { content += "xml:" + attribute.name; } else if (attribute.namespaceURI == "http://www.w3.org/2000/xmlns/") { if (attribute.name !== "xmlns") { content += "xmlns:"; } content += attribute.name; } else if (attribute.namespaceURI == "http://www.w3.org/1999/xlink") { content += "xlink:" + attribute.name; } else { content += attribute.name; } if (value != "") { content += "="; if (!validUnquotedValue) { content += "\""; } content += value; if (!validUnquotedValue) { content += "\""; } } }); content += ">"; Array.from(element.childNodes).forEach(childNode => content += serialize(childNode)); const omittedEndTag = OMITTED_END_TAGS.find(omittedEndTag => { const nextSibling = element.nextSibling; return tagName == omittedEndTag.tagName && (!nextSibling || (nextSibling.nodeType == Node.ELEMENT_NODE && omittedEndTag.followings && omittedEndTag.followings.includes(nextSibling.tagName))); }); if (!omittedEndTag && !SELF_CLOSED_TAG_NAMES.includes(tagName)) { content += ""; } return content; } })();