Quellcode durchsuchen

removed usage of window.Node and window.NodeFilter

Gildas vor 7 Jahren
Ursprung
Commit
a1b02c05ee
2 geänderte Dateien mit 30 neuen und 26 gelöschten Zeilen
  1. 16 14
      lib/single-file/modules/html-minifier.js
  2. 14 12
      lib/single-file/modules/html-serializer.js

+ 16 - 14
lib/single-file/modules/html-minifier.js

@@ -25,8 +25,6 @@
 
 // Derived from the work of Kirill Maltsev - https://github.com/posthtml/htmlnano
 
-/* global Node, NodeFilter */
-
 this.htmlMinifier = this.htmlMinifier || (() => {
 
 	// Source: https://github.com/kangax/html-minifier/issues/63
@@ -121,6 +119,10 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	const REGEXP_WHITESPACE = /[ \t\f\r]+/g;
 	const REGEXP_NEWLINE = /[\n]+/g;
 	const REGEXP_ENDS_WHITESPACE = /^\s+$/;
+	const NodeFilter_SHOW_ALL = 4294967295;
+	const Node_ELEMENT_NODE = 1;
+	const Node_TEXT_NODE = 3;
+	const Node_COMMENT_NODE = 8;
 
 	const modules = [
 		collapseBooleanAttributes,
@@ -136,7 +138,7 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	return {
 		process: (doc, options) => {
 			removeEmptyInlineElements(doc);
-			const nodesWalker = doc.createTreeWalker(doc.documentElement, NodeFilter.SHOW_ALL, null, false);
+			const nodesWalker = doc.createTreeWalker(doc.documentElement, NodeFilter_SHOW_ALL, null, false);
 			let node = nodesWalker.nextNode();
 			while (node) {
 				const deletedNode = modules.find(module => module(node, options));
@@ -150,7 +152,7 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	};
 
 	function collapseBooleanAttributes(node) {
-		if (node.nodeType == Node.ELEMENT_NODE) {
+		if (node.nodeType == Node_ELEMENT_NODE) {
 			Array.from(node.attributes).forEach(attribute => {
 				if (booleanAttributes.includes(attribute.name)) {
 					node.setAttribute(attribute.name, "");
@@ -160,8 +162,8 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	}
 
 	function mergeTextNodes(node) {
-		if (node.nodeType == Node.TEXT_NODE) {
-			if (node.previousSibling && node.previousSibling.nodeType == Node.TEXT_NODE) {
+		if (node.nodeType == Node_TEXT_NODE) {
+			if (node.previousSibling && node.previousSibling.nodeType == Node_TEXT_NODE) {
 				node.textContent = node.previousSibling.textContent + node.textContent;
 				node.previousSibling.remove();
 			}
@@ -169,14 +171,14 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	}
 
 	function mergeElements(node, tagName, acceptMerge) {
-		if (node.nodeType == Node.ELEMENT_NODE && node.tagName.toLowerCase() == tagName.toLowerCase()) {
+		if (node.nodeType == Node_ELEMENT_NODE && node.tagName.toLowerCase() == tagName.toLowerCase()) {
 			let previousSibling = node.previousSibling;
 			const previousSiblings = [];
-			while (previousSibling && previousSibling.nodeType == Node.TEXT_NODE && !previousSibling.textContent.trim()) {
+			while (previousSibling && previousSibling.nodeType == Node_TEXT_NODE && !previousSibling.textContent.trim()) {
 				previousSiblings.push(previousSibling);
 				previousSibling = previousSibling.previousSibling;
 			}
-			if (previousSibling && previousSibling.nodeType == Node.ELEMENT_NODE && previousSibling.tagName == node.tagName && acceptMerge(node, previousSibling)) {
+			if (previousSibling && previousSibling.nodeType == Node_ELEMENT_NODE && previousSibling.tagName == node.tagName && acceptMerge(node, previousSibling)) {
 				node.textContent = previousSibling.textContent + node.textContent;
 				previousSiblings.forEach(node => node.remove());
 				previousSibling.remove();
@@ -185,7 +187,7 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	}
 
 	function collapseWhitespace(node, options) {
-		if (node.nodeType == Node.TEXT_NODE) {
+		if (node.nodeType == Node_TEXT_NODE) {
 			let element = node.parentElement;
 			const spacePreserved = element.getAttribute(options.preservedSpaceAttributeName) == "";
 			const textContent = node.textContent;
@@ -209,13 +211,13 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	}
 
 	function removeComments(node) {
-		if (node.nodeType == Node.COMMENT_NODE) {
+		if (node.nodeType == Node_COMMENT_NODE) {
 			return !node.textContent.toLowerCase().trim().startsWith("[if");
 		}
 	}
 
 	function removeEmptyAttributes(node) {
-		if (node.nodeType == Node.ELEMENT_NODE) {
+		if (node.nodeType == Node_ELEMENT_NODE) {
 			Array.from(node.attributes).forEach(attribute => {
 				if (safeToRemoveAttrs.includes(attribute.name.toLowerCase())) {
 					const attributeValue = node.getAttribute(attribute.name);
@@ -228,7 +230,7 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	}
 
 	function removeRedundantAttributes(node) {
-		if (node.nodeType == Node.ELEMENT_NODE) {
+		if (node.nodeType == Node_ELEMENT_NODE) {
 			const tagRedundantAttributes = redundantAttributes[node.tagName.toLowerCase()];
 			if (tagRedundantAttributes) {
 				Object.keys(tagRedundantAttributes).forEach(redundantAttributeName => {
@@ -242,7 +244,7 @@ this.htmlMinifier = this.htmlMinifier || (() => {
 	}
 
 	function compressJSONLD(node) {
-		if (node.nodeType == Node.ELEMENT_NODE && node.tagName == "SCRIPT" && node.type == "application/ld+json" && node.textContent.trim()) {
+		if (node.nodeType == Node_ELEMENT_NODE && node.tagName == "SCRIPT" && node.type == "application/ld+json" && node.textContent.trim()) {
 			try {
 				node.textContent = JSON.stringify(JSON.parse(node.textContent));
 			} catch (error) {

+ 14 - 12
lib/single-file/modules/html-serializer.js

@@ -18,21 +18,23 @@
  *   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 Node_ELEMENT_NODE = 1;
+	const Node_TEXT_NODE = 3;
+	const Node_COMMENT_NODE = 8;
+
 	// 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: "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: "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, element) => (!next && element.parentElement && (element.parentElement.tagName == "UL" || element.parentElement.tagName == "OL")) || (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) },
@@ -41,8 +43,8 @@ this.serializer = this.serializer || (() => {
 		{ 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: "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 },
@@ -73,11 +75,11 @@ this.serializer = this.serializer || (() => {
 	};
 
 	function serialize(node) {
-		if (node.nodeType == Node.TEXT_NODE) {
+		if (node.nodeType == Node_TEXT_NODE) {
 			return serializeTextNode(node);
-		} else if (node.nodeType == Node.COMMENT_NODE) {
+		} else if (node.nodeType == Node_COMMENT_NODE) {
 			return serializeCommentNode(node);
-		} else if (node.nodeType == Node.ELEMENT_NODE) {
+		} else if (node.nodeType == Node_ELEMENT_NODE) {
 			return serializeElement(node);
 		}
 	}
@@ -85,7 +87,7 @@ this.serializer = this.serializer || (() => {
 	function serializeTextNode(textNode) {
 		const parentNode = textNode.parentNode;
 		let parentTagName;
-		if (parentNode && parentNode.nodeType == Node.ELEMENT_NODE) {
+		if (parentNode && parentNode.nodeType == Node_ELEMENT_NODE) {
 			parentTagName = parentNode.tagName.toLowerCase();
 		}
 		if (!parentTagName || TEXT_NODE_TAGS.includes(parentTagName)) {