| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /*
- * Copyright 2018 Gildas Lormeau
- * contact : gildas.lormeau <at> 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 <http://www.gnu.org/licenses/>.
- */
- /* 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 = "<!DOCTYPE " + docType.nodeName;
- if (docType.publicId) {
- docTypeString += " PUBLIC \"" + docType.publicId + "\"";
- if (docType.systemId)
- docTypeString += " \"" + docType.systemId + "\"";
- } else if (docType.systemId)
- docTypeString += " SYSTEM \"" + docType.systemId + "\"";
- if (docType.internalSubset)
- docTypeString += " [" + docType.internalSubset + "]";
- return 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 "<!--" + commentNode.textContent + "-->";
- }
- 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;
- }
- 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 += "</" + tagName + ">";
- }
- return content;
- }
- })();
|