css-fonts-minifier.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global cssTree, docHelper */
  21. this.fontsMinifier = this.fontsMinifier || (() => {
  22. const REGEXP_URL_SIMPLE_QUOTES_FN = /url\s*\(\s*'(.*?)'\s*\)/i;
  23. const REGEXP_URL_DOUBLE_QUOTES_FN = /url\s*\(\s*"(.*?)"\s*\)/i;
  24. const REGEXP_URL_NO_QUOTES_FN = /url\s*\(\s*(.*?)\s*\)/i;
  25. const REGEXP_URL_FUNCTION = /(url|local)\(.*?\)\s*(,|$)/g;
  26. const REGEXP_COMMA = /\s*,\s*/;
  27. const REGEXP_DASH = /-/;
  28. const REGEXP_QUESTION_MARK = /\?/g;
  29. const REGEXP_STARTS_U_PLUS = /^U\+/i;
  30. const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
  31. const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
  32. const REGEXP_URL_FUNCTION_WOFF = /^url\(\s*["']?data:font\/(woff2?)/;
  33. const REGEXP_URL_FUNCTION_WOFF_ALT = /^url\(\s*["']?data:application\/x-font-(woff)/;
  34. const REGEXP_FONT_FORMAT = /\.([^.?#]+)((\?|#).*?)?$/;
  35. const REGEXP_FONT_FORMAT_VALUE = /format\((.*?)\)\s*,?$/;
  36. const REGEXP_FONT_SRC = /(.*?)\s*,?$/;
  37. const EMPTY_URL_SOURCE = "url(\"data:base64,\")";
  38. const LOCAL_SOURCE = "local(";
  39. const PSEUDO_ELEMENTS = ["::after", "::before", "::first-line", "::first-letter", ":before", ":after", ":first-line", ":first-letter", "::placeholder", "::selection", "::marker", "::cue", "::slotted", "::spelling-error", "::grammar-error"];
  40. const FONT_WEIGHTS = {
  41. normal: "400",
  42. bold: "700"
  43. };
  44. const FONT_STRETCHES = {
  45. "ultra-condensed": "50%",
  46. "extra-condensed": "62.5%",
  47. "condensed": "75%",
  48. "semi-condensed": "87.5%",
  49. "normal": "100%",
  50. "semi-expanded": "112.5%",
  51. "expanded": "125%",
  52. "extra-expanded": "150%",
  53. "ultra-expanded": "200%"
  54. };
  55. return {
  56. removeUnusedFonts: (doc, stylesheets, styles, options) => {
  57. const stats = { rules: { processed: 0, discarded: 0 }, fonts: { processed: 0, discarded: 0 } };
  58. const fontsInfo = { declared: [], used: [] };
  59. let pseudoElementsContent = "";
  60. stylesheets.forEach(stylesheetInfo => {
  61. const cssRules = stylesheetInfo.stylesheet.children;
  62. stats.processed += cssRules.getSize();
  63. stats.discarded += cssRules.getSize();
  64. getFontsInfo(cssRules, fontsInfo);
  65. pseudoElementsContent += getPseudoElementsContent(doc, cssRules);
  66. });
  67. styles.forEach(style => {
  68. const fontFamilyNames = getFontFamilyNames(style);
  69. if (fontFamilyNames.length) {
  70. fontsInfo.used.push(fontFamilyNames);
  71. }
  72. });
  73. const variableFound = fontsInfo.used.find(fontNames => fontNames.find(fontName => fontName.startsWith("var(--")));
  74. let unusedFonts, filteredUsedFonts;
  75. if (variableFound) {
  76. unusedFonts = [];
  77. } else {
  78. filteredUsedFonts = new Map();
  79. fontsInfo.used.forEach(fontNames => fontNames.forEach(familyName => {
  80. if (fontsInfo.declared.find(fontInfo => fontInfo.fontFamily == familyName)) {
  81. const optionalData = options.usedFonts && options.usedFonts.filter(fontInfo => fontInfo.fontFamily == familyName);
  82. filteredUsedFonts.set(familyName, optionalData);
  83. }
  84. }));
  85. unusedFonts = fontsInfo.declared.filter(fontInfo => !filteredUsedFonts.has(fontInfo.fontFamily));
  86. }
  87. const docContent = doc.body.innerText + pseudoElementsContent;
  88. stylesheets.forEach(stylesheetInfo => {
  89. const cssRules = stylesheetInfo.stylesheet.children;
  90. filterUnusedFonts(cssRules, fontsInfo.declared, unusedFonts, filteredUsedFonts, docContent);
  91. stats.rules.discarded -= cssRules.getSize();
  92. });
  93. return stats;
  94. },
  95. removeAlternativeFonts: (doc, stylesheets) => {
  96. const fontsDetails = new Map();
  97. const stats = { rules: { processed: 0, discarded: 0 }, fonts: { processed: 0, discarded: 0 } };
  98. stylesheets.forEach(stylesheetInfo => {
  99. const cssRules = stylesheetInfo.stylesheet.children;
  100. stats.rules.processed += cssRules.getSize();
  101. stats.rules.discarded += cssRules.getSize();
  102. getFontsDetails(doc, cssRules, fontsDetails);
  103. });
  104. processFontDetails(fontsDetails);
  105. stylesheets.forEach(stylesheetInfo => {
  106. const cssRules = stylesheetInfo.stylesheet.children;
  107. processFontFaceRules(cssRules, fontsDetails, "all", stats);
  108. stats.rules.discarded -= cssRules.getSize();
  109. });
  110. return stats;
  111. }
  112. };
  113. function processFontDetails(fontsDetails) {
  114. fontsDetails.forEach((fontInfo, fontKey) => {
  115. fontsDetails.set(fontKey, fontInfo.map(fontSource => {
  116. const fontFormatMatch = fontSource.match(REGEXP_FONT_FORMAT_VALUE);
  117. let fontFormat;
  118. const urlMatch = fontSource.match(REGEXP_URL_SIMPLE_QUOTES_FN) ||
  119. fontSource.match(REGEXP_URL_DOUBLE_QUOTES_FN) ||
  120. fontSource.match(REGEXP_URL_NO_QUOTES_FN);
  121. const fontUrl = urlMatch && urlMatch[1];
  122. if (fontFormatMatch && fontFormatMatch[1]) {
  123. fontFormat = fontFormatMatch[1].replace(REGEXP_SIMPLE_QUOTES_STRING, "$1").replace(REGEXP_DOUBLE_QUOTES_STRING, "$1").toLowerCase();
  124. }
  125. if (!fontFormat) {
  126. const fontFormatMatch = fontSource.match(REGEXP_URL_FUNCTION_WOFF);
  127. if (fontFormatMatch && fontFormatMatch[1]) {
  128. fontFormat = fontFormatMatch[1];
  129. } else {
  130. const fontFormatMatch = fontSource.match(REGEXP_URL_FUNCTION_WOFF_ALT);
  131. if (fontFormatMatch && fontFormatMatch[1]) {
  132. fontFormat = fontFormatMatch[1];
  133. }
  134. }
  135. }
  136. if (!fontFormat && fontUrl) {
  137. const fontFormatMatch = fontUrl.match(REGEXP_FONT_FORMAT);
  138. if (fontFormatMatch && fontFormatMatch[1]) {
  139. fontFormat = fontFormatMatch[1];
  140. }
  141. }
  142. return { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat };
  143. }));
  144. });
  145. }
  146. function getFontsInfo(cssRules, fontsInfo) {
  147. cssRules.forEach(cssRule => {
  148. if (cssRule.type == "Atrule" && cssRule.name == "media" && cssRule.block) {
  149. getFontsInfo(cssRule.block.children, fontsInfo);
  150. } else if (cssRule.type == "Rule") {
  151. const fontFamilyNames = getFontFamilyNames(cssRule.block);
  152. if (fontFamilyNames.length) {
  153. fontsInfo.used.push(fontFamilyNames);
  154. }
  155. } else {
  156. if (cssRule.type == "Atrule" && cssRule.name == "font-face") {
  157. const fontFamily = getFontFamily(getPropertyValue(cssRule, "font-family"));
  158. if (fontFamily) {
  159. const fontWeight = getFontWeight(getPropertyValue(cssRule, "font-weight") || "400");
  160. const fontStyle = getPropertyValue(cssRule, "font-style") || "normal";
  161. const fontVariant = getPropertyValue(cssRule, "font-variant") || "normal";
  162. fontsInfo.declared.push({ fontFamily, fontWeight, fontStyle, fontVariant });
  163. }
  164. }
  165. }
  166. });
  167. }
  168. function filterUnusedFonts(cssRules, declaredFonts, unusedFonts, filteredUsedFonts, docContent) {
  169. const removedRules = [];
  170. for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
  171. const ruleData = cssRule.data;
  172. if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block) {
  173. filterUnusedFonts(ruleData.block.children, declaredFonts, unusedFonts, filteredUsedFonts, docContent);
  174. } else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
  175. const fontFamily = getFontFamily(getPropertyValue(ruleData, "font-family"));
  176. if (fontFamily) {
  177. const unicodeRange = getPropertyValue(ruleData, "unicode-range");
  178. if (unusedFonts.find(fontInfo => fontInfo.fontFamily == fontFamily) || !testUnicodeRange(docContent, unicodeRange) || !testUsedFont(ruleData, fontFamily, declaredFonts, filteredUsedFonts)) {
  179. removedRules.push(cssRule);
  180. }
  181. }
  182. }
  183. }
  184. removedRules.forEach(cssRule => cssRules.remove(cssRule));
  185. }
  186. function testUsedFont(cssRule, familyName, declaredFonts, filteredUsedFonts) {
  187. let test;
  188. const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
  189. if (optionalUsedFonts && optionalUsedFonts.length) {
  190. const fontStyle = getPropertyValue(cssRule, "font-style") || "normal";
  191. const fontWeight = getFontWeight(getPropertyValue(cssRule, "font-weight") || "400");
  192. const fontVariant = getPropertyValue(cssRule, "font-variant") || "normal";
  193. const declaredFontsWeights = declaredFonts
  194. .filter(fontInfo => fontInfo.fontFamily == familyName && fontInfo.fontStyle == fontStyle && testFontVariant(fontInfo, fontVariant))
  195. .map(fontInfo => fontInfo.fontWeight)
  196. .sort((weight1, weight2) => weight1 - weight2);
  197. const usedFontWeights = optionalUsedFonts.map(fontInfo => findFontWeight(fontInfo.fontWeight, declaredFontsWeights));
  198. test = usedFontWeights.includes(fontWeight);
  199. } else {
  200. test = true;
  201. }
  202. return test;
  203. }
  204. function processFontFaceRules(cssRules, fontsDetails, media, stats) {
  205. const removedRules = [];
  206. for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
  207. const ruleData = cssRule.data;
  208. if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.prelude && ruleData.prelude.children) {
  209. const mediaText = cssTree.generate(ruleData.prelude);
  210. processFontFaceRules(ruleData.block.children, fontsDetails, mediaText, stats);
  211. } else if (ruleData.type == "Atrule" && ruleData.name == "font-face" && (media.includes("all") || media.includes("screen"))) {
  212. const fontInfo = fontsDetails.get(getFontKey(ruleData));
  213. if (fontInfo) {
  214. fontsDetails.delete(getFontKey(ruleData));
  215. processFontFaceRule(ruleData, fontInfo, stats);
  216. } else {
  217. removedRules.push(cssRule);
  218. }
  219. }
  220. }
  221. removedRules.forEach(cssRule => cssRules.remove(cssRule));
  222. }
  223. function processFontFaceRule(cssRule, fontInfo, stats) {
  224. const findSource = fontFormat => fontInfo.find(source => source.src != EMPTY_URL_SOURCE && source.format == fontFormat);
  225. const filterSource = fontSource => fontInfo.filter(source => source == fontSource || source.src.startsWith(LOCAL_SOURCE));
  226. stats.fonts.processed += fontInfo.length;
  227. stats.fonts.discarded += fontInfo.length;
  228. const woffFontFound = findSource("woff2-variations") || findSource("woff2") || findSource("woff");
  229. if (woffFontFound) {
  230. fontInfo = filterSource(woffFontFound);
  231. } else {
  232. const ttfFontFound = findSource("truetype-variations") || findSource("truetype");
  233. if (ttfFontFound) {
  234. fontInfo = filterSource(ttfFontFound);
  235. } else {
  236. const otfFontFound = findSource("opentype") || findSource("embedded-opentype");
  237. if (otfFontFound) {
  238. fontInfo = filterSource(otfFontFound);
  239. }
  240. }
  241. }
  242. stats.fonts.discarded -= fontInfo.length;
  243. const removedNodes = [];
  244. for (let node = cssRule.block.children.head; node; node = node.next) {
  245. if (node.data.property == "src") {
  246. removedNodes.push(node);
  247. }
  248. }
  249. removedNodes.pop();
  250. removedNodes.forEach(node => cssRule.block.children.remove(node));
  251. const srcDeclaration = cssRule.block.children.filter(node => node.property == "src").tail;
  252. if (srcDeclaration) {
  253. fontInfo.reverse();
  254. srcDeclaration.data.value = cssTree.parse(fontInfo.map(fontSource => fontSource.src).join(","), { context: "value" });
  255. }
  256. }
  257. function getPropertyValue(cssRule, propertyName) {
  258. const property = cssRule.block.children.filter(node => node.property == propertyName).tail;
  259. if (property) {
  260. try {
  261. return cssTree.generate(property.data.value);
  262. } catch (error) {
  263. // ignored
  264. }
  265. }
  266. }
  267. function getFontFamilyNames(declarations) {
  268. let fontFamilyName = declarations.children.filter(node => node.property == "font-family").tail;
  269. let fontFamilyNames = [];
  270. if (fontFamilyName) {
  271. let familyName = "";
  272. if (fontFamilyName.data.value.children) {
  273. fontFamilyName.data.value.children.forEach(node => {
  274. if (node.type == "Operator" && node.value == "," && familyName) {
  275. fontFamilyNames.push(getFontFamily(familyName));
  276. familyName = "";
  277. } else {
  278. familyName += cssTree.generate(node);
  279. }
  280. });
  281. } else {
  282. fontFamilyName = cssTree.generate(fontFamilyName.data.value);
  283. }
  284. if (familyName) {
  285. fontFamilyNames.push(getFontFamily(familyName));
  286. }
  287. }
  288. const font = declarations.children.filter(node => node.property == "font").tail;
  289. if (font) {
  290. let familyName = "";
  291. const findPreviousComma = node => {
  292. for (; node && !(node.data.type == "Operator" && node.data.value == ","); node = node.prev);
  293. return node;
  294. };
  295. for (let node = font.data.value.children.tail; node && (node.data.type != "WhiteSpace" || findPreviousComma(node)); node = node.prev) {
  296. if (node.data.type == "Operator" && node.data.value == "," && familyName) {
  297. fontFamilyNames.push(getFontFamily(familyName));
  298. familyName = "";
  299. } else {
  300. familyName = cssTree.generate(node.data) + familyName;
  301. }
  302. }
  303. if (familyName) {
  304. fontFamilyNames.push(getFontFamily(familyName));
  305. }
  306. }
  307. return fontFamilyNames;
  308. }
  309. function getFontsDetails(doc, cssRules, fontsDetails) {
  310. cssRules.forEach(cssRule => {
  311. if (cssRule.type == "Atrule" && cssRule.name == "media" && cssRule.block) {
  312. getFontsDetails(doc, cssRule.block.children, fontsDetails);
  313. } else {
  314. if (cssRule.type == "Atrule" && cssRule.name == "font-face") {
  315. const fontKey = getFontKey(cssRule);
  316. let fontInfo = fontsDetails.get(fontKey);
  317. if (!fontInfo) {
  318. fontInfo = [];
  319. fontsDetails.set(fontKey, fontInfo);
  320. }
  321. const src = getPropertyValue(cssRule, "src");
  322. if (src) {
  323. const fontSources = src.match(REGEXP_URL_FUNCTION);
  324. if (fontSources) {
  325. fontSources.forEach(source => fontInfo.unshift(source));
  326. }
  327. }
  328. }
  329. }
  330. });
  331. }
  332. function findFontWeight(fontWeight, fontWeights) {
  333. let foundWeight;
  334. if (fontWeight >= 400 && fontWeight <= 500) {
  335. foundWeight = fontWeights.find(weight => weight >= fontWeight && weight <= 500);
  336. if (!foundWeight) {
  337. foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
  338. }
  339. if (!foundWeight) {
  340. foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
  341. }
  342. }
  343. if (fontWeight < 400) {
  344. foundWeight = fontWeights.slice().reverse().find(weight => weight <= fontWeight);
  345. if (!foundWeight) {
  346. foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
  347. }
  348. }
  349. if (fontWeight > 500) {
  350. foundWeight = fontWeights.find(weight => weight >= fontWeight);
  351. if (!foundWeight) {
  352. foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
  353. }
  354. }
  355. return foundWeight;
  356. }
  357. function findDescendingFontWeight(fontWeight, fontWeights) {
  358. return fontWeights.slice().reverse().find(weight => weight < fontWeight);
  359. }
  360. function findAscendingFontWeight(fontWeight, fontWeights) {
  361. return fontWeights.find(weight => weight > fontWeight);
  362. }
  363. function getPseudoElementsContent(doc, cssRules) {
  364. return cssRules.toArray().map(cssRule => {
  365. if (cssRule.block && cssRule.block.children && cssRule.prelude && cssRule.prelude.children) {
  366. if (cssRule.type == "Atrule" && cssRule.name == "media") {
  367. return getPseudoElementsContent(doc, cssRule.block.children);
  368. } else if (cssRule.type == "Rule") {
  369. const selector = cssTree.generate(cssRule.prelude); // TODO use OM
  370. if (testPseudoElements(selector)) {
  371. const value = docHelper.removeQuotes(getPropertyValue(cssRule, "content") || "");
  372. if (value) {
  373. const styleElement = doc.createElement("style");
  374. styleElement.textContent = "tmp { content:\"" + value + "\"}";
  375. doc.documentElement.appendChild(styleElement);
  376. let content = docHelper.removeQuotes(styleElement.sheet.cssRules[0].style.getPropertyValue("content"));
  377. styleElement.remove();
  378. return content;
  379. }
  380. }
  381. }
  382. }
  383. }).join("");
  384. }
  385. function testFontVariant(fontInfo, fontVariant) {
  386. return fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures";
  387. }
  388. function testUnicodeRange(docContent, unicodeRange) {
  389. if (unicodeRange) {
  390. const unicodeRanges = unicodeRange.split(REGEXP_COMMA);
  391. let invalid;
  392. const result = unicodeRanges.filter(rangeValue => {
  393. const range = rangeValue.split(REGEXP_DASH);
  394. let regExpString;
  395. if (range.length == 2) {
  396. range[0] = transformRange(range[0]);
  397. regExpString = "[" + range[0] + "-" + transformRange("U+" + range[1]) + "]";
  398. }
  399. if (range.length == 1) {
  400. if (range[0].includes("?")) {
  401. const firstRange = transformRange(range[0]);
  402. const secondRange = firstRange;
  403. regExpString = "[" + firstRange.replace(REGEXP_QUESTION_MARK, "0") + "-" + secondRange.replace(REGEXP_QUESTION_MARK, "F") + "]";
  404. } else {
  405. regExpString = "[" + transformRange(range[0]) + "]";
  406. }
  407. }
  408. if (regExpString) {
  409. try {
  410. return (new RegExp(regExpString, "u")).test(docContent);
  411. } catch (error) {
  412. invalid = true;
  413. return false;
  414. }
  415. }
  416. return true;
  417. });
  418. return !invalid && (!unicodeRanges.length || result.length);
  419. }
  420. return true;
  421. }
  422. function testPseudoElements(selectorText) {
  423. let indexSelector = 0, found;
  424. selectorText = selectorText.toLowerCase();
  425. while (indexSelector < PSEUDO_ELEMENTS.length && !found) {
  426. found = selectorText.includes(PSEUDO_ELEMENTS[indexSelector]);
  427. if (!found) {
  428. indexSelector++;
  429. }
  430. }
  431. return found;
  432. }
  433. function transformRange(range) {
  434. range = range.replace(REGEXP_STARTS_U_PLUS, "");
  435. while (range.length < 6) {
  436. range = "0" + range;
  437. }
  438. return "\\u{" + range + "}";
  439. }
  440. function getFontKey(cssRule) {
  441. return JSON.stringify([
  442. getFontFamily(getPropertyValue(cssRule, "font-family")),
  443. getFontWeight(getPropertyValue(cssRule, "font-weight") || "400"),
  444. getPropertyValue(cssRule, "font-style") || "normal",
  445. getPropertyValue(cssRule, "unicode-range"),
  446. getFontStretch(getPropertyValue(cssRule, "font-stretch")),
  447. getPropertyValue(cssRule, "font-variant") || "normal",
  448. getPropertyValue(cssRule, "font-feature-settings"),
  449. getPropertyValue(cssRule, "font-variation-settings")
  450. ]);
  451. }
  452. function getFontFamily(string = "") {
  453. string = string.toLowerCase().trim();
  454. if (string.match(REGEXP_SIMPLE_QUOTES_STRING)) {
  455. string = string.replace(REGEXP_SIMPLE_QUOTES_STRING, "$1");
  456. } else {
  457. string = string.replace(REGEXP_DOUBLE_QUOTES_STRING, "$1");
  458. }
  459. return string.trim();
  460. }
  461. function getFontWeight(weight) {
  462. return FONT_WEIGHTS[weight] || weight;
  463. }
  464. function getFontStretch(stretch) {
  465. return FONT_STRETCHES[stretch] || stretch;
  466. }
  467. })();