Quellcode durchsuchen

moved from single-file-browser.js into doc-util-core.js all the code not dependent on DOM

Gildas vor 7 Jahren
Ursprung
Commit
3bf013f0a2
4 geänderte Dateien mit 216 neuen und 165 gelöschten Zeilen
  1. 1 0
      extension/core/bg/core.js
  2. 36 165
      lib/single-file/single-file-browser.js
  3. 177 0
      lib/single-file/util/doc-util-core.js
  4. 2 0
      manifest.json

+ 1 - 0
extension/core/bg/core.js

@@ -29,6 +29,7 @@ singlefile.core = (() => {
 		"/lib/single-file/vendor/html-srcset-parser.js",
 		"/lib/single-file/util/timeout.js",
 		"/lib/single-file/util/doc-helper.js",
+		"/lib/single-file/util/doc-util-core.js",
 		"/lib/fetch/content/fetch.js",
 		"/lib/single-file/single-file-core.js",
 		"/lib/single-file/single-file-browser.js",

+ 36 - 165
lib/single-file/single-file-browser.js

@@ -19,21 +19,12 @@
  */
 
 /* global 
+	DocUtilCore,
 	crypto, 
-	cssMinifier, 
-	cssRulesMinifier, 
 	cssTree,
 	docHelper, 
 	fetch, 
-	fontsAltMinifier, 
-	fontsMinifier, 
-	htmlMinifier, 
-	imagesAltMinifier, 
-	matchedRules, 
-	mediasMinifier, 
-	serializer, 
 	setTimeout, 
-	srcsetParser, 
 	superFetch, 
 	Blob, 
 	DOMParser, 
@@ -41,8 +32,7 @@
 	FontFace
 	SingleFileCore, 
 	TextDecoder, 
-	TextEncoder, 
-	URL */
+	TextEncoder */
 
 this.SingleFileBrowser = this.SingleFileBrowser || (() => {
 
@@ -51,33 +41,13 @@ this.SingleFileBrowser = this.SingleFileBrowser || (() => {
 	const PREFIX_CONTENT_TYPE_TEXT = "text/";
 	const FONT_FACE_TEST_MAX_DELAY = 1000;
 
-	// --------
-	// Download
-	// --------	
 	let fetchResource;
-
-	if (this.serializer === undefined) {
-		this.serializer = {
-			process(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 + "]";
-					docTypeString += "> ";
-				}
-				return docTypeString + doc.documentElement.outerHTML;
-			}
-		};
-	}
-
+	return {
+		getClass: () => {
+			const DocUtil = DocUtilCore.getClass(getContent, parseDocContent, parseSVGContent, isValidFontUrl, getContentSize, digestText);
+			return SingleFileCore.getClass(DocUtil, cssTree);
+		}
+	};
 
 	async function getContent(resourceURL, options) {
 		let resourceContent, startTime;
@@ -182,137 +152,40 @@ this.SingleFileBrowser = this.SingleFileBrowser || (() => {
 		return hexCodes.join("");
 	}
 
-	// ---
-	// DocUtil
-	// ---
-	class DocUtil {
-		static async getContent(resourceURL, options) {
-			return getContent(resourceURL, options);
-		}
-
-		static parseURL(resourceURL, baseURI) {
-			return new URL(resourceURL, baseURI);
-		}
-
-		static resolveURL(resourceURL, baseURI) {
-			return this.parseURL(resourceURL, baseURI).href;
-		}
-
-		static parseDocContent(content, baseURI) {
-			const doc = (new DOMParser()).parseFromString(content, "text/html");
-			let baseElement = doc.querySelector("base");
-			if (!baseElement || !baseElement.getAttribute("href")) {
-				if (baseElement) {
-					baseElement.remove();
-				}
-				baseElement = doc.createElement("base");
-				baseElement.setAttribute("href", baseURI);
-				doc.head.insertBefore(baseElement, doc.head.firstChild);
+	function parseDocContent(content, baseURI) {
+		const doc = (new DOMParser()).parseFromString(content, "text/html");
+		let baseElement = doc.querySelector("base");
+		if (!baseElement || !baseElement.getAttribute("href")) {
+			if (baseElement) {
+				baseElement.remove();
 			}
-			return doc;
-		}
-
-		static parseSVGContent(content) {
-			return (new DOMParser()).parseFromString(content, "image/svg+xml");
-		}
-
-		static async digest(algo, text) {
-			const hash = await crypto.subtle.digest(algo, new TextEncoder("utf-8").encode(text));
-			return (hex(hash));
-		}
-
-		static getContentSize(content) {
-			return new Blob([content]).size;
-		}
-
-		static async validFont(urlFunction) {
-			try {
-				const font = new FontFace("font-test", urlFunction);
-				await Promise.race([font.load(), new Promise(resolve => setTimeout(() => resolve(true), FONT_FACE_TEST_MAX_DELAY))]);
-				return true;
-			} catch (error) {
-				return false;
-			}
-		}
-
-		static minifyHTML(doc, options) {
-			return htmlMinifier.process(doc, options);
-		}
-
-		static postMinifyHTML(doc) {
-			return htmlMinifier.postProcess(doc);
-		}
-
-		static minifyCSSRules(stylesheets, styles, mediaAllInfo) {
-			return cssRulesMinifier.process(stylesheets, styles, mediaAllInfo);
-		}
-
-		static removeUnusedFonts(doc, stylesheets, styles, options) {
-			return fontsMinifier.process(doc, stylesheets, styles, options);
-		}
-
-		static removeAlternativeFonts(doc, stylesheets) {
-			return fontsAltMinifier.process(doc, stylesheets);
-		}
-
-		static getMediaAllInfo(doc, stylesheets, styles) {
-			return matchedRules.getMediaAllInfo(doc, stylesheets, styles);
-		}
-
-		static compressCSS(content, options) {
-			return cssMinifier.processString(content, options);
-		}
-
-		static minifyMedias(stylesheets) {
-			return mediasMinifier.process(stylesheets);
-		}
-
-		static removeAlternativeImages(doc, options) {
-			return imagesAltMinifier.process(doc, options);
-		}
-
-		static parseSrcset(srcset) {
-			return srcsetParser.process(srcset);
-		}
-
-		static preProcessDoc(doc, win, options) {
-			return docHelper.preProcessDoc(doc, win, options);
-		}
-
-		static postProcessDoc(doc, options) {
-			docHelper.postProcessDoc(doc, options);
-		}
-
-		static serialize(doc, compressHTML) {
-			return serializer.process(doc, compressHTML);
-		}
-
-		static removeQuotes(string) {
-			return docHelper.removeQuotes(string);
-		}
-
-		static windowIdAttributeName(sessionId) {
-			return docHelper.windowIdAttributeName(sessionId);
-		}
-
-		static preservedSpaceAttributeName(sessionId) {
-			return docHelper.preservedSpaceAttributeName(sessionId);
+			baseElement = doc.createElement("base");
+			baseElement.setAttribute("href", baseURI);
+			doc.head.insertBefore(baseElement, doc.head.firstChild);
 		}
+		return doc;
+	}
 
-		static removedContentAttributeName(sessionId) {
-			return docHelper.removedContentAttributeName(sessionId);
-		}
+	function parseSVGContent(content) {
+		return (new DOMParser()).parseFromString(content, "image/svg+xml");
+	}
 
-		static imagesAttributeName(sessionId) {
-			return docHelper.imagesAttributeName(sessionId);
-		}
+	async function digestText(algo, text) {
+		const hash = await crypto.subtle.digest(algo, new TextEncoder("utf-8").encode(text));
+		return (hex(hash));
+	}
 
-		static inputValueAttributeName(sessionId) {
-			return docHelper.inputValueAttributeName(sessionId);
-		}
+	function getContentSize(content) {
+		return new Blob([content]).size;
+	}
 
-		static shadowRootAttributeName(sessionId) {
-			return docHelper.shadowRootAttributeName(sessionId);
+	async function isValidFontUrl(urlFunction) {
+		try {
+			const font = new FontFace("font-test", urlFunction);
+			await Promise.race([font.load(), new Promise(resolve => setTimeout(() => resolve(true), FONT_FACE_TEST_MAX_DELAY))]);
+			return true;
+		} catch (error) {
+			return false;
 		}
 	}
 
@@ -320,6 +193,4 @@ this.SingleFileBrowser = this.SingleFileBrowser || (() => {
 		console.log("S-File <browser>", ...args); // eslint-disable-line no-console
 	}
 
-	return { getClass: () => SingleFileCore.getClass(DocUtil, cssTree) };
-
 })();

+ 177 - 0
lib/single-file/util/doc-util-core.js

@@ -0,0 +1,177 @@
+/*
+ * Copyright 2010-2019 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 
+	cssMinifier, 
+	cssRulesMinifier, 
+	docHelper, 
+	fontsAltMinifier, 
+	fontsMinifier, 
+	htmlMinifier, 
+	imagesAltMinifier, 
+	matchedRules, 
+	mediasMinifier, 
+	serializer, 
+	srcsetParser, 
+	URL */
+
+this.DocUtilCore = this.DocUtilCore || (() => {
+
+	if (this.serializer === undefined) {
+		this.serializer = {
+			process(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 + "]";
+					docTypeString += "> ";
+				}
+				return docTypeString + doc.documentElement.outerHTML;
+			}
+		};
+	}
+
+	return {
+		getClass: (getContent, parseDocContent, parseSVGContent, isValidFontUrl, getContentSize, digestText) => {
+			return class DocUtl {
+				static async getContent(resourceURL, options) {
+					return getContent(resourceURL, options);
+				}
+
+				static parseURL(resourceURL, baseURI) {
+					return new URL(resourceURL, baseURI);
+				}
+
+				static resolveURL(resourceURL, baseURI) {
+					return this.parseURL(resourceURL, baseURI).href;
+				}
+
+				static parseDocContent(content, baseURI) {
+					return parseDocContent(content, baseURI);
+				}
+
+				static parseSVGContent(content) {
+					return parseSVGContent(content);
+				}
+
+				static async digest(algo, text) {
+					return digestText(algo, text);
+				}
+
+				static getContentSize(content) {
+					return getContentSize(content);
+				}
+
+				static async validFont(urlFunction) {
+					return isValidFontUrl(urlFunction);
+				}
+
+				static minifyHTML(doc, options) {
+					return htmlMinifier.process(doc, options);
+				}
+
+				static postMinifyHTML(doc) {
+					return htmlMinifier.postProcess(doc);
+				}
+
+				static minifyCSSRules(stylesheets, styles, mediaAllInfo) {
+					return cssRulesMinifier.process(stylesheets, styles, mediaAllInfo);
+				}
+
+				static removeUnusedFonts(doc, stylesheets, styles, options) {
+					return fontsMinifier.process(doc, stylesheets, styles, options);
+				}
+
+				static removeAlternativeFonts(doc, stylesheets) {
+					return fontsAltMinifier.process(doc, stylesheets);
+				}
+
+				static getMediaAllInfo(doc, stylesheets, styles) {
+					return matchedRules.getMediaAllInfo(doc, stylesheets, styles);
+				}
+
+				static compressCSS(content, options) {
+					return cssMinifier.processString(content, options);
+				}
+
+				static minifyMedias(stylesheets) {
+					return mediasMinifier.process(stylesheets);
+				}
+
+				static removeAlternativeImages(doc, options) {
+					return imagesAltMinifier.process(doc, options);
+				}
+
+				static parseSrcset(srcset) {
+					return srcsetParser.process(srcset);
+				}
+
+				static preProcessDoc(doc, win, options) {
+					return docHelper.preProcessDoc(doc, win, options);
+				}
+
+				static postProcessDoc(doc, options) {
+					docHelper.postProcessDoc(doc, options);
+				}
+
+				static serialize(doc, compressHTML) {
+					return serializer.process(doc, compressHTML);
+				}
+
+				static removeQuotes(string) {
+					return docHelper.removeQuotes(string);
+				}
+
+				static windowIdAttributeName(sessionId) {
+					return docHelper.windowIdAttributeName(sessionId);
+				}
+
+				static preservedSpaceAttributeName(sessionId) {
+					return docHelper.preservedSpaceAttributeName(sessionId);
+				}
+
+				static removedContentAttributeName(sessionId) {
+					return docHelper.removedContentAttributeName(sessionId);
+				}
+
+				static imagesAttributeName(sessionId) {
+					return docHelper.imagesAttributeName(sessionId);
+				}
+
+				static inputValueAttributeName(sessionId) {
+					return docHelper.inputValueAttributeName(sessionId);
+				}
+
+				static shadowRootAttributeName(sessionId) {
+					return docHelper.shadowRootAttributeName(sessionId);
+				}
+			};
+		}
+	};
+
+})();

+ 2 - 0
manifest.json

@@ -19,6 +19,7 @@
 				"lib/hooks/hooks-frame.js",
 				"lib/browser-polyfill/chrome-browser-polyfill.js",
 				"lib/single-file/util/doc-helper.js",
+				"lib/single-file/util/doc-util-core.js",
 				"lib/single-file/util/timeout.js",
 				"lib/frame-tree/frame-tree.js",
 				"extension/index.js",
@@ -72,6 +73,7 @@
 			"lib/single-file/vendor/html-srcset-parser.js",
 			"lib/single-file/vendor/css-font-property-parser.js",
 			"lib/single-file/util/doc-helper.js",
+			"lib/single-file/util/doc-util-core.js",
 			"lib/single-file/modules/css-fonts-minifier.js",
 			"lib/single-file/modules/css-fonts-alt-minifier.js",
 			"lib/single-file/modules/css-medias-alt-minifier.js",