Procházet zdrojové kódy

fix #317 (unescape font family names)

Former-commit-id: 6d5cdaad313b4c9a86068b555ddb3c729712aff6
Gildas před 6 roky
rodič
revize
3ff26100fb

+ 1 - 0
cli/back-ends/common/scripts.js

@@ -36,6 +36,7 @@ const SCRIPTS = [
 	"lib/single-file/vendor/html-srcset-parser.js",
 	"lib/single-file/vendor/css-minifier.js",
 	"lib/single-file/vendor/css-font-property-parser.js",
+	"lib/single-file/vendor/css-unescape.js",
 	"lib/single-file/vendor/css-media-query-parser.js",
 	"lib/single-file/modules/html-minifier.js",
 	"lib/single-file/modules/css-fonts-minifier.js",

+ 1 - 0
extension/lib/single-file/core/bg/scripts.js

@@ -30,6 +30,7 @@ singlefile.extension.lib.core.bg.scripts = (() => {
 	const contentScriptFiles = [
 		"lib/single-file/index.js",
 		"lib/single-file/vendor/css-font-property-parser.js",
+		"lib/single-file/vendor/css-unescape.js",
 		"lib/single-file/vendor/css-media-query-parser.js",
 		"lib/single-file/vendor/css-tree.js",
 		"lib/single-file/vendor/html-srcset-parser.js",

+ 1 - 1
lib/single-file/single-file-helper.js

@@ -300,7 +300,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 	}
 
 	function normalizeFontFamily(fontFamilyName) {
-		return removeQuotes(fontFamilyName.trim()).toLowerCase();
+		return removeQuotes(singlefile.lib.vendor.cssUnescape.process(fontFamilyName.trim())).toLowerCase();
 	}
 
 	function testHiddenElement(element, computedStyle, markedElements) {

+ 74 - 0
lib/single-file/vendor/css-unescape.js

@@ -0,0 +1,74 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Author: Gildas Lormeau
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+// derived from https://github.com/postcss/postcss-selector-parser/blob/master/src/util/unesc.js
+
+/*
+ * The MIT License (MIT)
+ * Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://beneb.info)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+this.singlefile.lib.vendor.cssUnescape = this.singlefile.lib.vendor.cssUnescape || (() => {
+
+	const whitespace = "[\\x20\\t\\r\\n\\f]";
+	const unescapeRegExp = new RegExp("\\\\([\\da-f]{1,6}" + whitespace + "?|(" + whitespace + ")|.)", "ig");
+
+	return {
+		process: function (str) {
+			return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
+				const high = "0x" + escaped - 0x10000;
+
+				// NaN means non-codepoint
+				// Workaround erroneous numeric interpretation of +"0x"
+				// eslint-disable-next-line no-self-compare
+				return high !== high || escapedWhitespace
+					? escaped
+					: high < 0
+						? // BMP codepoint
+						String.fromCharCode(high + 0x10000)
+						: // Supplemental Plane codepoint (surrogate pair)
+						String.fromCharCode((high >> 10) | 0xd800, (high & 0x3ff) | 0xdc00);
+			});
+		}
+	};
+
+})();

+ 1 - 0
manifest.json

@@ -63,6 +63,7 @@
 			"lib/single-file/vendor/css-media-query-parser.js",
 			"lib/single-file/vendor/html-srcset-parser.js",
 			"lib/single-file/vendor/css-font-property-parser.js",
+			"lib/single-file/vendor/css-unescape.js",
 			"lib/single-file/single-file-util.js",
 			"lib/single-file/single-file-helper.js",
 			"lib/single-file/modules/css-fonts-minifier.js",