فهرست منبع

validate fonts

Gildas 7 سال پیش
والد
کامیت
0803bb1744
2فایلهای تغییر یافته به همراه8 افزوده شده و 6 حذف شده
  1. 4 6
      lib/single-file/single-file-browser.js
  2. 4 0
      lib/single-file/single-file-core.js

+ 4 - 6
lib/single-file/single-file-browser.js

@@ -25,6 +25,7 @@ this.SingleFile = this.SingleFile || (() => {
 	const ONE_MB = 1024 * 1024;
 	const DEBUG = false;
 	const PREFIX_CONTENT_TYPE_TEXT = "text/";
+	const FONT_FACE_TEST_MAX_DELAY = 1000;
 
 	// --------
 	// Download
@@ -168,8 +169,6 @@ this.SingleFile = this.SingleFile || (() => {
 		return hexCodes.join("");
 	}
 
-	let fontIndex = 0;
-
 	// ---
 	// DOM
 	// ---
@@ -212,11 +211,10 @@ this.SingleFile = this.SingleFile || (() => {
 			return new Blob([content]).size;
 		}
 
-		static async validFont(url) {
+		static async validFont(urlFunction) {
 			try {
-				const font = new FontFace("font-test-" + fontIndex, "url(\"" + url + "\")");
-				fontIndex++;
-				await font.load();
+				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;

+ 4 - 0
lib/single-file/single-file-core.js

@@ -1084,6 +1084,10 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				if (!DomUtil.testIgnoredPath(resourceURL)) {
 					if (DomUtil.testValidURL(resourceURL, baseURI, options.url)) {
 						let { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL);
+						const validFont = !processFont || content == EMPTY_DATA_URI || content.startsWith(PREFIX_DATA_URI_VND) || (content.startsWith(PREFIX_DATA_URI_IMAGE_SVG)) || await DOM.validFont(urlFunction);
+						if (!validFont) {
+							content = EMPTY_DATA_URI;
+						}
 						let regExpUrlFunction = DomUtil.getRegExp(urlFunction);
 						if (!stylesheetContent.match(regExpUrlFunction)) {
 							urlFunction = "url(" + originalResourceURL + ")";