| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- /*
- * 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 CSSRule */
- this.fontsMinifier = this.fontsMinifier || (() => {
- const REGEXP_URL_SIMPLE_QUOTES_FN = /url\s*\(\s*'(.*?)'\s*\)/i;
- const REGEXP_URL_DOUBLE_QUOTES_FN = /url\s*\(\s*"(.*?)"\s*\)/i;
- const REGEXP_URL_NO_QUOTES_FN = /url\s*\(\s*(.*?)\s*\)/i;
- const REGEXP_URL_FUNCTION = /(url|local)\(.*?\)\s*(,|$)/g;
- const REGEXP_COMMA = /\s*,\s*/;
- const REGEXP_DASH = /-/;
- const REGEXP_QUESTION_MARK = /\?/g;
- const REGEXP_STARTS_U_PLUS = /^U\+/i;
- const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
- const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
- const REGEXP_URL_FUNCTION_WOFF = /^url\(\s*["']?data:font\/(woff2?)/;
- const REGEXP_URL_FUNCTION_WOFF_ALT = /^url\(\s*["']?data:application\/x-font-(woff)/;
- const REGEXP_FONT_FORMAT = /\.([^.?#]+)((\?|#).*?)?$/;
- const REGEXP_FONT_FORMAT_VALUE = /format\((.*?)\)\s*,?$/;
- const REGEXP_FONT_SRC = /(.*?)\s*,?$/;
- const EMPTY_URL_SOURCE = "url(\"data:base64,\")";
- const PSEUDO_ELEMENTS = ["::after", "::before", "::first-line", "::first-letter", ":before", ":after", ":first-line", ":first-letter", "::placeholder", "::selection", "::marker", "::cue", "::slotted", "::spelling-error", "::grammar-error"];
- const FONT_WEIGHTS = {
- normal: "400",
- bold: "700"
- };
- const FONT_STRETCHES = {
- "ultra-condensed": "50%",
- "extra-condensed": "62.5%",
- "condensed": "75%",
- "semi-condensed": "87.5%",
- "normal": "100%",
- "semi-expanded": "112.5%",
- "expanded": "125%",
- "extra-expanded": "150%",
- "ultra-expanded": "200%"
- };
- return {
- removeUnusedFonts: (doc, options) => {
- const stats = {
- rules: {
- processed: 0,
- discarded: 0
- },
- fonts: {
- processed: 0,
- discarded: 0
- }
- };
- const fontsInfo = { declared: [], used: [] };
- let pseudoElementsContent = "";
- doc.querySelectorAll("style").forEach(style => {
- if (style.sheet) {
- stats.rules.processed += style.sheet.cssRules.length;
- stats.rules.discarded += style.sheet.cssRules.length;
- getFontsInfo(doc, style.sheet.cssRules, fontsInfo);
- pseudoElementsContent += getPseudoElementsContent(doc, style.sheet.cssRules);
- }
- });
- doc.querySelectorAll("[style]").forEach(element => {
- if (element.style && element.style.fontFamily) {
- const fontFamilyNames = element.style.fontFamily.split(",").map(fontFamilyName => removeQuotes(fontFamilyName));
- fontsInfo.used.push(fontFamilyNames);
- }
- });
- const variableFound = fontsInfo.used.find(fontNames => fontNames.find(fontName => fontName.startsWith("var(--")));
- let unusedFonts, filteredUsedFonts;
- if (variableFound) {
- unusedFonts = [];
- } else {
- filteredUsedFonts = new Map();
- fontsInfo.used.forEach(fontNames => fontNames.forEach(familyName => {
- if (fontsInfo.declared.find(fontInfo => fontInfo.familyName == familyName)) {
- const optionalData = options.usedFonts && options.usedFonts.filter(fontInfo => fontInfo.fontFamily == familyName);
- filteredUsedFonts.set(familyName, optionalData);
- }
- }));
- unusedFonts = fontsInfo.declared.filter(fontInfo => !filteredUsedFonts.has(fontInfo.familyName));
- }
- const docContent = doc.body.innerText + pseudoElementsContent;
- doc.querySelectorAll("style").forEach(style => {
- if (style.sheet) {
- style.textContent = filterUnusedFonts(doc, style.sheet.cssRules, fontsInfo.declared, unusedFonts, filteredUsedFonts, docContent);
- stats.rules.discarded -= style.sheet.cssRules.length;
- }
- });
- return stats;
- },
- removeAlternativeFonts: doc => {
- const fontsDetails = new Map();
- const stats = {
- rules: {
- processed: 0,
- discarded: 0
- },
- fonts: {
- processed: 0,
- discarded: 0
- }
- };
- doc.querySelectorAll("style").forEach(style => {
- if (style.sheet) {
- stats.rules.processed += style.sheet.cssRules.length;
- stats.rules.discarded += style.sheet.cssRules.length;
- getFontsDetails(doc, style.sheet.cssRules, fontsDetails);
- }
- });
- processFontDetails(fontsDetails);
- doc.querySelectorAll("style").forEach(style => {
- if (style.sheet) {
- style.textContent = processFontFaceRules(style.sheet.cssRules, fontsDetails, "all", stats);
- stats.rules.discarded -= style.sheet.cssRules.length;
- }
- });
- return stats;
- }
- };
- function processFontDetails(fontsDetails) {
- fontsDetails.forEach((fontInfo, fontKey) => {
- fontsDetails.set(fontKey, fontInfo.map(fontSource => {
- const fontFormatMatch = fontSource.match(REGEXP_FONT_FORMAT_VALUE);
- let fontFormat;
- const urlMatch = fontSource.match(REGEXP_URL_SIMPLE_QUOTES_FN) ||
- fontSource.match(REGEXP_URL_DOUBLE_QUOTES_FN) ||
- fontSource.match(REGEXP_URL_NO_QUOTES_FN);
- const fontUrl = urlMatch && urlMatch[1];
- if (fontFormatMatch && fontFormatMatch[1]) {
- fontFormat = fontFormatMatch[1].replace(REGEXP_SIMPLE_QUOTES_STRING, "$1").replace(REGEXP_DOUBLE_QUOTES_STRING, "$1").toLowerCase();
- }
- if (!fontFormat) {
- const fontFormatMatch = fontSource.match(REGEXP_URL_FUNCTION_WOFF);
- if (fontFormatMatch && fontFormatMatch[1]) {
- fontFormat = fontFormatMatch[1];
- } else {
- const fontFormatMatch = fontSource.match(REGEXP_URL_FUNCTION_WOFF_ALT);
- if (fontFormatMatch && fontFormatMatch[1]) {
- fontFormat = fontFormatMatch[1];
- }
- }
- }
- if (!fontFormat && fontUrl) {
- const fontFormatMatch = fontUrl.match(REGEXP_FONT_FORMAT);
- if (fontFormatMatch && fontFormatMatch[1]) {
- fontFormat = fontFormatMatch[1];
- }
- }
- return { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat };
- }));
- });
- }
- function getFontsInfo(doc, rules, fontsInfo) {
- if (rules) {
- Array.from(rules).forEach(rule => {
- if (rule.type == CSSRule.MEDIA_RULE) {
- getFontsInfo(doc, rule.cssRules, fontsInfo);
- } else if (rule.type == CSSRule.STYLE_RULE) {
- if (rule.style && rule.style.fontFamily) {
- const fontFamilyNames = rule.style.fontFamily.split(",").map(fontFamilyName => removeQuotes(fontFamilyName));
- fontsInfo.used.push(fontFamilyNames);
- }
- } else {
- if (rule.type == CSSRule.FONT_FACE_RULE && rule.style) {
- const familyName = removeQuotes(rule.style.getPropertyValue("font-family"));
- const fontWeight = getFontWeight(rule.style.getPropertyValue("font-weight")) || "400";
- const fontStyle = rule.style.getPropertyValue("font-style") || "normal";
- const fontVariant = rule.style.getPropertyValue("font-variant") || "normal";
- if (familyName) {
- fontsInfo.declared.push({ familyName, fontWeight, fontStyle, fontVariant });
- }
- }
- }
- });
- }
- }
- function getFontsDetails(doc, rules, fontsDetails) {
- if (rules) {
- Array.from(rules).forEach(rule => {
- if (rule.type == CSSRule.MEDIA_RULE) {
- getFontsDetails(doc, rule.cssRules, fontsDetails);
- } else {
- if (rule.type == CSSRule.FONT_FACE_RULE && rule.style) {
- const fontKey = getFontKey(rule.style);
- let fontInfo = fontsDetails.get(fontKey);
- if (!fontInfo) {
- fontInfo = [];
- fontsDetails.set(fontKey, fontInfo);
- }
- const src = rule.style.getPropertyValue("src");
- if (src) {
- const fontSources = src.match(REGEXP_URL_FUNCTION);
- if (fontSources) {
- fontSources.forEach(source => fontInfo.unshift(source));
- }
- }
- }
- }
- });
- }
- }
- function processFontFaceRules(rules, fontsDetails, media, stats) {
- let stylesheetContent = "";
- Array.from(rules).forEach(rule => {
- if (rule.type == CSSRule.MEDIA_RULE) {
- stylesheetContent += "@media " + Array.prototype.join.call(rule.media, ",") + "{";
- stylesheetContent += processFontFaceRules(rule.cssRules, fontsDetails, rule.media.mediaText, stats);
- stylesheetContent += "}";
- } else if (rule.type == CSSRule.FONT_FACE_RULE && (media.includes("all") || media.includes("screen"))) {
- const fontInfo = fontsDetails.get(getFontKey(rule.style));
- if (fontInfo) {
- fontsDetails.delete(getFontKey(rule.style));
- stylesheetContent += "@font-face {" + processFontFaceRule(rule, fontInfo, stats) + "}";
- }
- } else {
- stylesheetContent += rule.cssText;
- }
- });
- return stylesheetContent;
- }
- function processFontFaceRule(rule, fontInfo, stats) {
- const fontTest = (fontSource, format) => !fontSource.src.startsWith(EMPTY_URL_SOURCE) && fontSource.format == format;
- let woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff2-variations"));
- if (!woffFontFound) {
- woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff2"));
- }
- if (!woffFontFound) {
- woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff"));
- }
- stats.fonts.processed += fontInfo.length;
- stats.fonts.discarded += fontInfo.length;
- if (woffFontFound) {
- fontInfo = [woffFontFound];
- } else {
- let ttfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "truetype-variations"));
- if (!ttfFontFound) {
- ttfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "truetype"));
- }
- if (ttfFontFound) {
- fontInfo = [ttfFontFound];
- } else {
- let otfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "opentype"));
- if (!otfFontFound) {
- otfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "embedded-opentype"));
- }
- if (otfFontFound) {
- fontInfo = [otfFontFound];
- }
- }
- }
- stats.fonts.discarded -= fontInfo.length;
- let cssText = "";
- Array.from(rule.style).forEach(propertyName => {
- cssText += propertyName + ":";
- if (propertyName == "src") {
- cssText += fontInfo.map(fontSource => fontSource.src).join(",");
- } else {
- cssText += rule.style.getPropertyValue(propertyName);
- }
- cssText += ";";
- });
- return cssText;
- }
- function filterUnusedFonts(doc, rules, declaredFonts, unusedFonts, filteredUsedFonts, docContent) {
- let stylesheetContent = "";
- if (rules) {
- Array.from(rules).forEach(rule => {
- if (rule.media) {
- stylesheetContent += "@media " + Array.prototype.join.call(rule.media, ",") + "{";
- stylesheetContent += filterUnusedFonts(doc, rule.cssRules, declaredFonts, unusedFonts, filteredUsedFonts, docContent);
- stylesheetContent += "}";
- } else if (rule.type == CSSRule.FONT_FACE_RULE) {
- if (rule.style) {
- const familyName = removeQuotes(rule.style.getPropertyValue("font-family"));
- if (familyName && !unusedFonts.find(fontInfo => fontInfo.familyName == familyName)) {
- if (testUnicodeRange(docContent, rule.style.getPropertyValue("unicode-range")) && testUsedFont(rule, familyName, declaredFonts, filteredUsedFonts)) {
- stylesheetContent += rule.cssText;
- }
- }
- }
- } else {
- stylesheetContent += rule.cssText;
- }
- });
- }
- return stylesheetContent;
- }
- function testUsedFont(rule, familyName, declaredFonts, filteredUsedFonts) {
- let test;
- const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
- if (optionalUsedFonts && optionalUsedFonts.length) {
- const fontStyle = rule.style.getPropertyValue("font-style") || "normal";
- const fontWeight = getFontWeight(rule.style.getPropertyValue("font-weight")) || "400";
- const fontVariant = rule.style.getPropertyValue("font-variant") || "normal";
- const declaredFontsWeights = declaredFonts
- .filter(fontInfo => fontInfo.familyName == familyName && fontInfo.fontStyle == fontStyle && testFontVariant(fontInfo, fontVariant))
- .map(fontInfo => fontInfo.fontWeight)
- .sort((weight1, weight2) => weight1 - weight2);
- const usedFontWeights = optionalUsedFonts.map(fontInfo => findFontWeight(fontInfo.fontWeight, declaredFontsWeights));
- test = usedFontWeights.includes(fontWeight);
- } else {
- test = true;
- }
- return test;
- }
- function findFontWeight(fontWeight, fontWeights) {
- let foundWeight;
- if (fontWeight >= 400 && fontWeight <= 500) {
- foundWeight = fontWeights.find(weight => weight >= fontWeight && weight <= 500);
- if (!foundWeight) {
- foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
- }
- if (!foundWeight) {
- foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
- }
- }
- if (fontWeight < 400) {
- foundWeight = fontWeights.slice().reverse().find(weight => weight <= fontWeight);
- if (!foundWeight) {
- foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
- }
- }
- if (fontWeight > 500) {
- foundWeight = fontWeights.find(weight => weight >= fontWeight);
- if (!foundWeight) {
- foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
- }
- }
- return foundWeight;
- }
- function findDescendingFontWeight(fontWeight, fontWeights) {
- return fontWeights.slice().reverse().find(weight => weight < fontWeight);
- }
- function findAscendingFontWeight(fontWeight, fontWeights) {
- return fontWeights.find(weight => weight > fontWeight);
- }
- function getPseudoElementsContent(doc, rules) {
- if (rules) {
- return Array.from(rules).map(rule => {
- if (rule.type == CSSRule.MEDIA_RULE) {
- return getPseudoElementsContent(doc, rule.cssRules);
- } else if (rule.type == CSSRule.STYLE_RULE && testPseudoElements(rule.selectorText)) {
- let content = rule.style.getPropertyValue("content");
- content = content && removeQuotes(content);
- return content;
- }
- }).join("");
- } else {
- return "";
- }
- }
- function testFontVariant(fontInfo, fontVariant) {
- return fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures";
- }
- function testUnicodeRange(docContent, unicodeRange) {
- if (unicodeRange) {
- const unicodeRanges = unicodeRange.split(REGEXP_COMMA);
- const result = unicodeRanges.filter(rangeValue => {
- const range = rangeValue.split(REGEXP_DASH);
- if (range.length == 2) {
- range[0] = transformRange(range[0]);
- const regExpString = "[" + range[0] + "-" + transformRange("U+" + range[1]) + "]";
- return (new RegExp(regExpString, "u")).test(docContent);
- }
- if (range.length == 1) {
- if (range[0].includes("?")) {
- const firstRange = transformRange(range[0]);
- const secondRange = firstRange;
- const regExpString = "[" + firstRange.replace(REGEXP_QUESTION_MARK, "0") + "-" + secondRange.replace(REGEXP_QUESTION_MARK, "F") + "]";
- return (new RegExp(regExpString, "u")).test(docContent);
- } else {
- const regExpString = "[" + transformRange(range[0]) + "]";
- return (new RegExp(regExpString, "u")).test(docContent);
- }
- }
- return true;
- });
- return result.length;
- }
- return true;
- }
- function testPseudoElements(selectorText) {
- let indexSelector = 0, found;
- selectorText = selectorText.toLowerCase();
- while (indexSelector < PSEUDO_ELEMENTS.length && !found) {
- found = selectorText.includes(PSEUDO_ELEMENTS[indexSelector]);
- if (!found) {
- indexSelector++;
- }
- }
- return found;
- }
- function transformRange(range) {
- range = range.replace(REGEXP_STARTS_U_PLUS, "");
- while (range.length < 6) {
- range = "0" + range;
- }
- return "\\u{" + range + "}";
- }
- function getFontKey(style) {
- return JSON.stringify([
- removeQuotes(style.getPropertyValue("font-family")),
- getFontWeight(style.getPropertyValue("font-weight")),
- style.getPropertyValue("font-style"),
- style.getPropertyValue("unicode-range"),
- getFontStretch(style.getPropertyValue("font-stretch")),
- style.getPropertyValue("font-variant"),
- style.getPropertyValue("font-feature-settings"),
- style.getPropertyValue("font-variation-settings")
- ]);
- }
- function removeQuotes(string) {
- string = string.toLowerCase().trim();
- if (string.match(REGEXP_SIMPLE_QUOTES_STRING)) {
- string = string.replace(REGEXP_SIMPLE_QUOTES_STRING, "$1");
- } else {
- string = string.replace(REGEXP_DOUBLE_QUOTES_STRING, "$1");
- }
- return string.trim();
- }
- function getFontWeight(weight) {
- return FONT_WEIGHTS[weight] || weight;
- }
- function getFontStretch(stretch) {
- return FONT_STRETCHES[stretch] || stretch;
- }
- })();
|