| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /*
- * 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"];
- // see https://www.w3.org/TR/html5/syntax.html#optional-tags
- const OMITTED_START_TAGS = [
- { tagName: "head", accept: element => !element.childNodes.length || element.childNodes[0].nodeType == Node.ELEMENT_NODE },
- { tagName: "body", accept: element => !element.childNodes.length }
- ];
- const OMITTED_END_TAGS = [
- { tagName: "html", accept: next => !next || next.nodeType != Node.COMMENT_NODE },
- { tagName: "head", accept: next => !next || (next.nodeType != Node.COMMENT_NODE && (next.nodeType != Node.TEXT_NODE || !startsWithSpaceChar(next.textContent))) },
- { tagName: "body", accept: next => !next || next.nodeType != Node.COMMENT_NODE },
- { tagName: "li", accept: next => !next || ["LI"].includes(next.tagName) },
- { tagName: "dt", accept: next => !next || ["DT", "DD"].includes(next.tagName) },
- { tagName: "p", accept: next => next && ["ADDRESS", "ARTICLE", "ASIDE", "BLOCKQUOTE", "DETAILS", "DIV", "DL", "FIELDSET", "FIGCAPTION", "FIGURE", "FOOTER", "FORM", "H1", "H2", "H3", "H4", "H5", "H6", "HEADER", "HR", "MAIN", "NAV", "OL", "P", "PRE", "SECTION", "TABLE", "UL"].includes(next.tagName) },
- { tagName: "dd", accept: next => !next || ["DT", "DD"].includes(next.tagName) },
- { tagName: "rt", accept: next => !next || ["RT", "RP"].includes(next.tagName) },
- { tagName: "rp", accept: next => !next || ["RT", "RP"].includes(next.tagName) },
- { tagName: "optgroup", accept: next => !next || ["OPTGROUP"].includes(next.tagName) },
- { tagName: "option", accept: next => !next || ["OPTION", "OPTGROUP"].includes(next.tagName) },
- { tagName: "colgroup", accept: next => !next || (next.nodeType != Node.COMMENT_NODE && (next.nodeType != Node.TEXT_NODE || !startsWithSpaceChar(next.textContent))) },
- { tagName: "caption", accept: next => !next || (next.nodeType != Node.COMMENT_NODE && (next.nodeType != Node.TEXT_NODE || !startsWithSpaceChar(next.textContent))) },
- { tagName: "thead", accept: next => !next || ["TBODY", "TFOOT"].includes(next.tagName) },
- { tagName: "tbody", accept: next => !next || ["TBODY", "TFOOT"].includes(next.tagName) },
- { tagName: "tfoot", accept: next => !next },
- { tagName: "tr", accept: next => !next || ["TR"].includes(next.tagName) },
- { tagName: "td", accept: next => !next || ["TD", "TH"].includes(next.tagName) },
- { tagName: "th", accept: next => !next || ["TD", "TH"].includes(next.tagName) }
- ];
- const TEXT_NODE_TAGS = ["style", "script", "xmp", "iframe", "noembed", "noframes", "plaintext", "noscript"];
- return {
- process(doc, compressHTML) {
- 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 + "]";
- docTypeString += "> ";
- }
- return docTypeString + (compressHTML ? serialize(doc.documentElement) : doc.documentElement.outerHTML);
- }
- };
- 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) {
- let parentNode = textNode.parentNode;
- let parentTagName;
- if (parentNode && parentNode.nodeType == Node.ELEMENT_NODE) {
- parentTagName = parentNode.tagName.toLowerCase();
- }
- if (!parentTagName || TEXT_NODE_TAGS.includes(parentTagName)) {
- return textNode.textContent;
- } else {
- return textNode.textContent.replace(/&/g, "&").replace(/\u00a0/g, " ").replace(/</g, "<").replace(/</g, ">");
- }
- }
- function serializeCommentNode(commentNode) {
- return "<!--" + commentNode.textContent + "-->";
- }
- function serializeElement(element) {
- const tagName = element.tagName.toLowerCase();
- const omittedStartTag = OMITTED_START_TAGS.find(omittedStartTag => tagName == omittedStartTag.tagName && omittedStartTag.accept(element));
- let content = "";
- if (!omittedStartTag || element.attributes.length) {
- content = "<" + tagName;
- Array.from(element.attributes).forEach(attribute => content += serializeAttribute(attribute, element));
- content += ">";
- }
- Array.from(element.childNodes).forEach(childNode => content += serialize(childNode));
- const omittedEndTag = OMITTED_END_TAGS.find(omittedEndTag => tagName == omittedEndTag.tagName && omittedEndTag.accept(element.nextSibling));
- if (!omittedEndTag && !SELF_CLOSED_TAG_NAMES.includes(tagName)) {
- content += "</" + tagName + ">";
- }
- return content;
- }
- function serializeAttribute(attribute, element) {
- const name = attribute.name;
- let content = "";
- if (!name.match(/["'>/=]/)) {
- let value = attribute.value;
- if (name == "class") {
- value = Array.from(element.classList).map(className => className.trim()).join(" ");
- }
- let simpleQuotesValue;
- value = value.replace(/&/g, "&").replace(/\u00a0/g, " ");
- if (value.includes("\"")) {
- if (value.includes("'")) {
- value = value.replace(/"/g, """);
- } else {
- simpleQuotesValue = true;
- }
- }
- const invalidUnquotedValue = !value.match(/^[^ \t\n\f\r'"`=<>]+$/);
- content += " ";
- if (!attribute.namespace) {
- content += name;
- } else if (attribute.namespaceURI == "http://www.w3.org/XML/1998/namespace") {
- content += "xml:" + name;
- } else if (attribute.namespaceURI == "http://www.w3.org/2000/xmlns/") {
- if (name !== "xmlns") {
- content += "xmlns:";
- }
- content += name;
- } else if (attribute.namespaceURI == "http://www.w3.org/1999/xlink") {
- content += "xlink:" + name;
- } else {
- content += name;
- }
- if (value != "") {
- content += "=";
- if (invalidUnquotedValue) {
- content += simpleQuotesValue ? "'" : "\"";
- }
- content += value;
- if (invalidUnquotedValue) {
- content += simpleQuotesValue ? "'" : "\"";
- }
- }
- }
- return content;
- }
- function startsWithSpaceChar(textContent) {
- return Boolean(textContent.match(/^[ \t\n\f\r]/));
- }
- })();
|