single-file-core 1.6.5 → 1.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/helper.js +174 -37
- package/core/index.js +56 -32
- package/core/lib/processor-helper-common.js +26 -5
- package/core/lib/processor-helper-inline.js +115 -0
- package/core/lib/processor-helper.js +86 -60
- package/modules/css-fonts-minifier.js +139 -7
- package/modules/css-rules-minifier.js +32 -6
- package/modules/css-selector-sanitizer.js +12 -0
- package/package.json +1 -1
- package/processors/compression/compression-packager.js +8 -0
- package/processors/compression/compression.js +33 -11
- package/processors/frame-tree/content/content-frame-tree.js +2 -0
- package/processors/hooks/content/content-hooks-frames-web.js +3 -1
|
@@ -414,6 +414,7 @@ class ProcessorHelperCommon {
|
|
|
414
414
|
sheetIndex++;
|
|
415
415
|
});
|
|
416
416
|
processFontDetails(fontsDetails, fonts);
|
|
417
|
+
this.markRepeatedFontFaceRules(fontsDetails);
|
|
417
418
|
await Promise.all([...stylesheets].map(async ([, stylesheetInfo], sheetIndex) => {
|
|
418
419
|
if (stylesheetInfo.stylesheet) {
|
|
419
420
|
const cssRules = stylesheetInfo.stylesheet.children;
|
|
@@ -431,6 +432,21 @@ class ProcessorHelperCommon {
|
|
|
431
432
|
return stats;
|
|
432
433
|
}
|
|
433
434
|
|
|
435
|
+
markRepeatedFontFaceRules(fontsDetails) {
|
|
436
|
+
const lastRules = new Map();
|
|
437
|
+
fontsDetails.fonts.forEach((fontInfo, ruleData) => {
|
|
438
|
+
const ruleKey = this.getFontKey(ruleData) + " " + this.getPropertyValue(ruleData, "src");
|
|
439
|
+
const previousRuleData = lastRules.get(ruleKey);
|
|
440
|
+
if (previousRuleData) {
|
|
441
|
+
fontsDetails.repeatedFonts.add(previousRuleData);
|
|
442
|
+
}
|
|
443
|
+
lastRules.set(ruleKey, ruleData);
|
|
444
|
+
});
|
|
445
|
+
fontsDetails.medias.forEach(mediaFontsDetails => this.markRepeatedFontFaceRules(mediaFontsDetails));
|
|
446
|
+
fontsDetails.supports.forEach(supportsFontsDetails => this.markRepeatedFontFaceRules(supportsFontsDetails));
|
|
447
|
+
fontsDetails.layers.forEach(layerFontsDetails => this.markRepeatedFontFaceRules(layerFontsDetails));
|
|
448
|
+
}
|
|
449
|
+
|
|
434
450
|
async processFontFaceRules(cssRules, sheetIndex, fontsDetails, fonts, fontTests, stats) {
|
|
435
451
|
const removedRules = [];
|
|
436
452
|
let mediaIndex = 0, supportsIndex = 0, layerIndex = 0;
|
|
@@ -451,11 +467,9 @@ class ProcessorHelperCommon {
|
|
|
451
467
|
} else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
|
|
452
468
|
const fontInfo = fontsDetails.fonts.get(ruleData);
|
|
453
469
|
if (fontInfo) {
|
|
454
|
-
|
|
455
|
-
if (fontsDetails.emittedFonts.has(ruleKey)) {
|
|
470
|
+
if (fontsDetails.repeatedFonts.has(ruleData)) {
|
|
456
471
|
removedRules.push(cssRule);
|
|
457
472
|
} else {
|
|
458
|
-
fontsDetails.emittedFonts.add(ruleKey);
|
|
459
473
|
const keptRule = await this.processFontFaceRule(ruleData, fontInfo, fonts, fontTests, stats);
|
|
460
474
|
if (!keptRule) {
|
|
461
475
|
removedRules.push(cssRule);
|
|
@@ -469,6 +483,9 @@ class ProcessorHelperCommon {
|
|
|
469
483
|
removedRules.forEach(cssRule => cssRules.remove(cssRule));
|
|
470
484
|
}
|
|
471
485
|
|
|
486
|
+
groupDuplicateFonts() {
|
|
487
|
+
}
|
|
488
|
+
|
|
472
489
|
getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails) {
|
|
473
490
|
let mediaIndex = 0, supportsIndex = 0, layerIndex = 0;
|
|
474
491
|
cssRules.forEach(ruleData => {
|
|
@@ -516,7 +533,7 @@ class ProcessorHelperCommon {
|
|
|
516
533
|
medias: new Map(),
|
|
517
534
|
supports: new Map(),
|
|
518
535
|
layers: new Map(),
|
|
519
|
-
|
|
536
|
+
repeatedFonts: new Set()
|
|
520
537
|
};
|
|
521
538
|
}
|
|
522
539
|
|
|
@@ -529,7 +546,11 @@ class ProcessorHelperCommon {
|
|
|
529
546
|
getFontStretch(this.getPropertyValue(ruleData, "font-stretch")),
|
|
530
547
|
this.getPropertyValue(ruleData, "font-variant") || "normal",
|
|
531
548
|
this.getPropertyValue(ruleData, "font-feature-settings"),
|
|
532
|
-
this.getPropertyValue(ruleData, "font-variation-settings")
|
|
549
|
+
this.getPropertyValue(ruleData, "font-variation-settings"),
|
|
550
|
+
this.getPropertyValue(ruleData, "ascent-override"),
|
|
551
|
+
this.getPropertyValue(ruleData, "descent-override"),
|
|
552
|
+
this.getPropertyValue(ruleData, "line-gap-override"),
|
|
553
|
+
this.getPropertyValue(ruleData, "size-adjust")
|
|
533
554
|
]);
|
|
534
555
|
}
|
|
535
556
|
|
|
@@ -33,6 +33,13 @@ const Image = globalThis.Image;
|
|
|
33
33
|
|
|
34
34
|
const ABOUT_BLANK_URI = "about:blank";
|
|
35
35
|
const UTF8_CHARSET = "utf-8";
|
|
36
|
+
const DATA_URI_PREFIX = "data:";
|
|
37
|
+
const DEFAULT_FONT_WEIGHT = 400;
|
|
38
|
+
const FONT_KEY_FAMILY_INDEX = 0;
|
|
39
|
+
const FONT_KEY_WEIGHT_INDEX = 1;
|
|
40
|
+
const FONT_KEY_STYLE_INDEX = 2;
|
|
41
|
+
const FONT_KEY_STRETCH_INDEX = 4;
|
|
42
|
+
const NESTED_AT_RULE_NAMES = ["media", "supports", "layer", "container"];
|
|
36
43
|
const PREFIX_DATA_URI_IMAGE_SVG = "data:image/svg+xml";
|
|
37
44
|
const PREFIXES_FORBIDDEN_DATA_URI = ["data:text/"];
|
|
38
45
|
const SCRIPT_TAG_FOUND = /<script/gi;
|
|
@@ -65,6 +72,7 @@ import {
|
|
|
65
72
|
resizeImage,
|
|
66
73
|
toDataURI
|
|
67
74
|
} from "./processor-helper-common.js";
|
|
75
|
+
import { getFontWeight } from "./../helper.js";
|
|
68
76
|
|
|
69
77
|
export {
|
|
70
78
|
getProcessorHelperClass,
|
|
@@ -569,6 +577,97 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
569
577
|
doc.querySelectorAll("link[rel*=stylesheet][rel*=alternate][title]").forEach(element => element.remove());
|
|
570
578
|
}
|
|
571
579
|
|
|
580
|
+
groupDuplicateFonts(stylesheets, fonts, options) {
|
|
581
|
+
if (options.usedFonts && options.usedFonts.length) {
|
|
582
|
+
const fontFaces = [];
|
|
583
|
+
stylesheets.forEach(stylesheetInfo => {
|
|
584
|
+
if (stylesheetInfo.stylesheet && stylesheetInfo.stylesheet.children) {
|
|
585
|
+
getFontFaces(stylesheetInfo.stylesheet.children, "", fontFaces);
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
const families = new Map();
|
|
589
|
+
fontFaces.forEach(fontFace => {
|
|
590
|
+
const fontKey = JSON.parse(this.getFontKey(fontFace.ruleData));
|
|
591
|
+
const familyKey = fontFace.scope + JSON.stringify([fontKey[FONT_KEY_FAMILY_INDEX],
|
|
592
|
+
fontKey[FONT_KEY_STYLE_INDEX], fontKey[FONT_KEY_STRETCH_INDEX]]);
|
|
593
|
+
let family = families.get(familyKey);
|
|
594
|
+
if (!family) {
|
|
595
|
+
family = { name: fontKey[FONT_KEY_FAMILY_INDEX], groups: new Map(), mergeable: true };
|
|
596
|
+
families.set(familyKey, family);
|
|
597
|
+
}
|
|
598
|
+
const source = this.getPropertyValue(fontFace.ruleData, "src");
|
|
599
|
+
const range = this.getFontWeightRange(fontFace.ruleData);
|
|
600
|
+
if (source && source.includes(DATA_URI_PREFIX) && range) {
|
|
601
|
+
fontKey[FONT_KEY_WEIGHT_INDEX] = null;
|
|
602
|
+
const groupKey = JSON.stringify(fontKey) + " " + source;
|
|
603
|
+
const group = family.groups.get(groupKey);
|
|
604
|
+
if (group) {
|
|
605
|
+
group.push({ fontFace, range });
|
|
606
|
+
} else {
|
|
607
|
+
family.groups.set(groupKey, [{ fontFace, range }]);
|
|
608
|
+
}
|
|
609
|
+
} else {
|
|
610
|
+
family.mergeable = false;
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
families.forEach(family => this.mergeFontFaceWeights(family, options.usedFonts));
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Every group of the family has to be merged or none of it: two groups that differ only in
|
|
618
|
+
// unicode-range are one composite face, and merging one of them alone would leave a weight
|
|
619
|
+
// matched by two faces that are no longer a composite, which CSS Fonts 4 §5.2 resolves in a way
|
|
620
|
+
// that "can differ between multiple user agents". Requiring every group to span the same set of
|
|
621
|
+
// weights is what keeps the merged faces a composite, and it also blocks the case where some
|
|
622
|
+
// other face of the family sits inside the interval, since that face is a group of its own.
|
|
623
|
+
mergeFontFaceWeights(family, usedFonts) {
|
|
624
|
+
const groups = Array.from(family.groups.values());
|
|
625
|
+
const weightKey = group => group.map(({ range }) => range.join("-")).sort().join(",");
|
|
626
|
+
if (family.mergeable && groups.some(group => group.length > 1) &&
|
|
627
|
+
groups.every(group => weightKey(group) == weightKey(groups[0]))) {
|
|
628
|
+
const ranges = groups[0].map(({ range }) => range);
|
|
629
|
+
const minWeight = Math.min(...ranges.map(([min]) => min));
|
|
630
|
+
const maxWeight = Math.max(...ranges.map(([, max]) => max));
|
|
631
|
+
const declared = weight => ranges.some(([min, max]) => weight >= min && weight <= max);
|
|
632
|
+
const usedWeightInRange = usedFonts.some(([usedFamily, usedWeight]) => {
|
|
633
|
+
const weight = Number(usedWeight);
|
|
634
|
+
return usedFamily == family.name && weight > minWeight && weight < maxWeight && !declared(weight);
|
|
635
|
+
});
|
|
636
|
+
if (!usedWeightInRange) {
|
|
637
|
+
groups.forEach(group => {
|
|
638
|
+
const kept = group[group.length - 1];
|
|
639
|
+
this.setFontWeightRange(kept.fontFace.ruleData, minWeight, maxWeight);
|
|
640
|
+
group.forEach(({ fontFace }) => {
|
|
641
|
+
if (fontFace != kept.fontFace) {
|
|
642
|
+
fontFace.cssRules.remove(fontFace.cssRule);
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
getFontWeightRange(ruleData) {
|
|
651
|
+
const value = this.getPropertyValue(ruleData, "font-weight");
|
|
652
|
+
if (value === undefined) {
|
|
653
|
+
return [DEFAULT_FONT_WEIGHT, DEFAULT_FONT_WEIGHT];
|
|
654
|
+
}
|
|
655
|
+
const weights = value.trim().split(/\s+/).map(weight => Number(getFontWeight(util.removeQuotes(weight))));
|
|
656
|
+
if (weights.length <= 2 && weights.every(weight => Number.isFinite(weight))) {
|
|
657
|
+
return [Math.min(...weights), Math.max(...weights)];
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
setFontWeightRange(ruleData, minWeight, maxWeight) {
|
|
662
|
+
const value = cssTree.parse(minWeight == maxWeight ? String(minWeight) : minWeight + " " + maxWeight, { context: "value" });
|
|
663
|
+
const declaration = ruleData.block.children.filter(node => node.property == "font-weight").tail;
|
|
664
|
+
if (declaration) {
|
|
665
|
+
declaration.data.value = value;
|
|
666
|
+
} else {
|
|
667
|
+
ruleData.block.children.appendData({ type: "Declaration", property: "font-weight", important: false, value });
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
572
671
|
async processFontFaceRule(ruleData, fontInfo, fontDeclarations, fontTests, stats) {
|
|
573
672
|
const removedNodes = [];
|
|
574
673
|
for (let node = ruleData.block.children.head; node; node = node.next) {
|
|
@@ -662,4 +761,20 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
662
761
|
return true;
|
|
663
762
|
}
|
|
664
763
|
};
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function getFontFaces(cssRules, scope, fontFaces) {
|
|
767
|
+
for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
|
|
768
|
+
const ruleData = cssRule.data;
|
|
769
|
+
if (ruleData.type == "Atrule" && ruleData.name == "import" && ruleData.prelude && ruleData.prelude.children &&
|
|
770
|
+
ruleData.prelude.children.head.data.importedChildren) {
|
|
771
|
+
getFontFaces(ruleData.prelude.children.head.data.importedChildren,
|
|
772
|
+
scope + "|import " + cssTree.generate(ruleData.prelude), fontFaces);
|
|
773
|
+
} else if (ruleData.type == "Atrule" && NESTED_AT_RULE_NAMES.includes(ruleData.name) && ruleData.block && ruleData.block.children) {
|
|
774
|
+
getFontFaces(ruleData.block.children,
|
|
775
|
+
scope + "|" + ruleData.name + " " + (ruleData.prelude ? cssTree.generate(ruleData.prelude) : ""), fontFaces);
|
|
776
|
+
} else if (ruleData.type == "Atrule" && ruleData.name == "font-face" && ruleData.block && ruleData.block.children) {
|
|
777
|
+
fontFaces.push({ ruleData, cssRule, cssRules, scope });
|
|
778
|
+
}
|
|
779
|
+
}
|
|
665
780
|
}
|
|
@@ -114,7 +114,7 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
114
114
|
const stylesheet = sharedEntry
|
|
115
115
|
? sharedEntry[1].stylesheet
|
|
116
116
|
: cssTree.parse(content, { context: "stylesheet", parseCustomProperty: true });
|
|
117
|
-
resources.stylesheets.set(resources.stylesheets.size, { name,
|
|
117
|
+
resources.stylesheets.set(resources.stylesheets.size, { name, stylesheet });
|
|
118
118
|
linkElements.set(stylesheetRefIndex, linkElement);
|
|
119
119
|
sharedStyleElements.set(styleElement, stylesheetRefIndex);
|
|
120
120
|
});
|
|
@@ -194,35 +194,23 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
194
194
|
if (supportsNode) {
|
|
195
195
|
supportsCondition = cssTree.generate(supportsNode);
|
|
196
196
|
}
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const content = await this.getStylesheetContent(resourceURL, options);
|
|
215
|
-
stylesheetInfo.url = resourceURL = content.resourceURL;
|
|
216
|
-
content.data = getUpdatedResourceContent(resourceURL, options) || content.data;
|
|
217
|
-
stylesheetInfo.stylesheet = cssTree.parse(content.data, { context: "stylesheet", parseCustomProperty: true });
|
|
218
|
-
stylesheet = stylesheetInfo.stylesheet;
|
|
219
|
-
const ancestorStyleSheets = new Set(importedStyleSheets);
|
|
220
|
-
ancestorStyleSheets.add(requestedURL);
|
|
221
|
-
ancestorStyleSheets.add(resourceURL);
|
|
222
|
-
await this.resolveImportURLs(stylesheetInfo, resourceURL, options, workStylesheet, resources, stylesheets, ancestorStyleSheets);
|
|
223
|
-
stylesheets.set({ urlNode }, stylesheetInfo);
|
|
224
|
-
}
|
|
225
|
-
urlNode.importedChildren = stylesheet.children;
|
|
197
|
+
const stylesheetInfo = {
|
|
198
|
+
scoped,
|
|
199
|
+
mediaText,
|
|
200
|
+
layerName,
|
|
201
|
+
supportsCondition
|
|
202
|
+
};
|
|
203
|
+
stylesheets.set({ urlNode }, stylesheetInfo);
|
|
204
|
+
const requestedURL = resourceURL;
|
|
205
|
+
const content = await this.getStylesheetContent(resourceURL, options);
|
|
206
|
+
stylesheetInfo.url = resourceURL = content.resourceURL;
|
|
207
|
+
content.data = getUpdatedResourceContent(resourceURL, options) || content.data;
|
|
208
|
+
stylesheetInfo.stylesheet = cssTree.parse(content.data, { context: "stylesheet", parseCustomProperty: true });
|
|
209
|
+
const ancestorStyleSheets = new Set(importedStyleSheets);
|
|
210
|
+
ancestorStyleSheets.add(requestedURL);
|
|
211
|
+
ancestorStyleSheets.add(resourceURL);
|
|
212
|
+
await this.resolveImportURLs(stylesheetInfo, resourceURL, options, workStylesheet, resources, stylesheets, ancestorStyleSheets);
|
|
213
|
+
urlNode.importedChildren = stylesheetInfo.stylesheet.children;
|
|
226
214
|
urlNode.importedMediaText = mediaText;
|
|
227
215
|
urlNode.importedLayerName = layerName;
|
|
228
216
|
urlNode.importedSupportsCondition = supportsCondition;
|
|
@@ -235,36 +223,27 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
235
223
|
async resolveLinkStylesheetURLs(stylesheetInfo, element, resourceURL, baseURI, options, workStylesheet, resources, stylesheets) {
|
|
236
224
|
resourceURL = normalizeURL(resourceURL);
|
|
237
225
|
if (resourceURL && resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
226
|
+
const content = await util.getContent(resourceURL, {
|
|
227
|
+
maxResourceSize: options.maxResourceSize,
|
|
228
|
+
maxResourceSizeEnabled: options.maxResourceSizeEnabled,
|
|
229
|
+
charset: options.charset,
|
|
230
|
+
frameId: options.frameId,
|
|
231
|
+
resourceReferrer: options.resourceReferrer,
|
|
232
|
+
validateTextContentType: true,
|
|
233
|
+
baseURI: baseURI,
|
|
234
|
+
blockMixedContent: options.blockMixedContent,
|
|
235
|
+
expectedType: "stylesheet",
|
|
236
|
+
acceptHeaders: options.acceptHeaders,
|
|
237
|
+
networkTimeout: options.networkTimeout
|
|
238
|
+
});
|
|
239
|
+
if (!(matchCharsetEquals(content.data, content.charset) || matchCharsetEquals(content.data, options.charset))) {
|
|
240
|
+
options = Object.assign({}, options, { charset: getCharset(content.data) });
|
|
241
|
+
await this.resolveLinkStylesheetURLs(stylesheetInfo, element, resourceURL, baseURI, options, workStylesheet, resources, stylesheets);
|
|
245
242
|
} else {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
frameId: options.frameId,
|
|
251
|
-
resourceReferrer: options.resourceReferrer,
|
|
252
|
-
validateTextContentType: true,
|
|
253
|
-
baseURI: baseURI,
|
|
254
|
-
blockMixedContent: options.blockMixedContent,
|
|
255
|
-
expectedType: "stylesheet",
|
|
256
|
-
acceptHeaders: options.acceptHeaders,
|
|
257
|
-
networkTimeout: options.networkTimeout
|
|
258
|
-
});
|
|
259
|
-
if (!(matchCharsetEquals(content.data, content.charset) || matchCharsetEquals(content.data, options.charset))) {
|
|
260
|
-
options = Object.assign({}, options, { charset: getCharset(content.data) });
|
|
261
|
-
await this.resolveLinkStylesheetURLs(stylesheetInfo, element, resourceURL, baseURI, options, workStylesheet, resources, stylesheets);
|
|
262
|
-
} else {
|
|
263
|
-
resourceURL = content.resourceURL;
|
|
264
|
-
content.data = getUpdatedResourceContent(content.resourceURL, options) || content.data;
|
|
265
|
-
stylesheetInfo.stylesheet = cssTree.parse(content.data, { context: "stylesheet", parseCustomProperty: true });
|
|
266
|
-
await this.resolveImportURLs(stylesheetInfo, resourceURL, options, workStylesheet, resources, stylesheets);
|
|
267
|
-
}
|
|
243
|
+
resourceURL = content.resourceURL;
|
|
244
|
+
content.data = getUpdatedResourceContent(content.resourceURL, options) || content.data;
|
|
245
|
+
stylesheetInfo.stylesheet = cssTree.parse(content.data, { context: "stylesheet", parseCustomProperty: true });
|
|
246
|
+
await this.resolveImportURLs(stylesheetInfo, resourceURL, options, workStylesheet, resources, stylesheets);
|
|
268
247
|
}
|
|
269
248
|
}
|
|
270
249
|
}
|
|
@@ -300,6 +279,27 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
300
279
|
resources.fonts.set(indexResource, { name, content, extension, contentType, url: resourceURL });
|
|
301
280
|
}
|
|
302
281
|
|
|
282
|
+
groupDuplicateFonts(stylesheets, fonts) {
|
|
283
|
+
const canonicalNames = new Map();
|
|
284
|
+
const fontResources = [...fonts]
|
|
285
|
+
.filter(([, resource]) => resource.content)
|
|
286
|
+
.sort(([indexResource], [otherIndexResource]) => indexResource - otherIndexResource);
|
|
287
|
+
fontResources.forEach(([indexResource, resource], index) => {
|
|
288
|
+
const original = fontResources.slice(0, index).find(([, previousResource]) => testSameContent(previousResource.content, resource.content));
|
|
289
|
+
if (original) {
|
|
290
|
+
canonicalNames.set(resource.name, original[1].name);
|
|
291
|
+
fonts.delete(indexResource);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
if (canonicalNames.size) {
|
|
295
|
+
stylesheets.forEach(stylesheetInfo => {
|
|
296
|
+
if (stylesheetInfo.stylesheet) {
|
|
297
|
+
getUrlFunctions(stylesheetInfo.stylesheet).forEach(urlNode => replaceFontName(urlNode, canonicalNames));
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
303
|
async processStyle(ruleData, options, resources, batchRequest) {
|
|
304
304
|
const urls = getUrlFunctions(ruleData);
|
|
305
305
|
await Promise.all(urls.map(async urlNode => {
|
|
@@ -428,7 +428,11 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
428
428
|
Object.keys(pageResources).forEach(resourceType => {
|
|
429
429
|
const unusedResources = Array.from(pageResources[resourceType]).filter(([, value]) => !textContent.includes(value.name));
|
|
430
430
|
unusedResources.forEach(([indexResource]) => pageResources[resourceType].delete(indexResource));
|
|
431
|
-
|
|
431
|
+
const orderedResources = Array.from(pageResources[resourceType]);
|
|
432
|
+
if (orderedResources.every(([indexResource]) => typeof indexResource == "number")) {
|
|
433
|
+
orderedResources.sort(([indexResource], [otherIndexResource]) => indexResource - otherIndexResource);
|
|
434
|
+
}
|
|
435
|
+
resources[resourceType] = orderedResources.map(([, resource]) => resource);
|
|
432
436
|
});
|
|
433
437
|
const viewportElement = doc.head.querySelector("meta[name=viewport]");
|
|
434
438
|
const viewport = viewportElement ? viewportElement.content : null;
|
|
@@ -588,4 +592,26 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
588
592
|
return true;
|
|
589
593
|
}
|
|
590
594
|
};
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function testSameContent(content, otherContent) {
|
|
598
|
+
if (content.length != otherContent.length) {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
for (let index = 0; index < content.length; index++) {
|
|
602
|
+
if (content[index] != otherContent[index]) {
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return true;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function replaceFontName(urlNode, canonicalNames) {
|
|
610
|
+
canonicalNames.forEach((canonicalName, name) => {
|
|
611
|
+
if (urlNode.value == name) {
|
|
612
|
+
urlNode.value = canonicalName;
|
|
613
|
+
} else if (urlNode.value.endsWith(" " + name)) {
|
|
614
|
+
urlNode.value = urlNode.value.substring(0, urlNode.value.length - name.length) + canonicalName;
|
|
615
|
+
}
|
|
616
|
+
});
|
|
591
617
|
}
|
|
@@ -48,6 +48,8 @@ const REGEXP_CUSTOM_PROPERTY = /var\(\s*(--[^\s,)]+)\s*(?:,[^)]*)?\)/g;
|
|
|
48
48
|
const REGEXP_CUSTOM_PROPERTY_FAMILY = /^var\(\s*(--[^\s,)]+)\s*(?:,(.*))?\)$/;
|
|
49
49
|
const REGEXP_CUSTOM_PROPERTY_NAME = /^--/;
|
|
50
50
|
const VALID_FONT_STYLES = [/^normal$/, /^italic$/, /^oblique$/, /^oblique\s+/];
|
|
51
|
+
const NON_GLYPH_CHAR_CODES = [9, 10, 12, 13];
|
|
52
|
+
const MAX_NON_GLYPH_CHAR_CODE = 13;
|
|
51
53
|
// a family name kept when the "font" shorthand cannot be read: it resolves to nothing, so it
|
|
52
54
|
// survives the substitution below and marks the fonts as undetermined instead of unused
|
|
53
55
|
const UNRESOLVED_CUSTOM_PROPERTY_FAMILY = "var(--)";
|
|
@@ -125,11 +127,12 @@ function process(doc, stylesheets, styles, options) {
|
|
|
125
127
|
unusedFonts = fontsInfo.declared.filter(fontInfo => !filteredUsedFonts.has(fontInfo.fontFamily));
|
|
126
128
|
}
|
|
127
129
|
const docChars = Array.from(new Set(docContent)).map(char => char.charCodeAt(0)).sort((value1, value2) => value1 - value2);
|
|
130
|
+
const usedFontsCharacters = getUsedFontsCharacters(options);
|
|
128
131
|
stylesheets.forEach(stylesheetInfo => {
|
|
129
132
|
if (stylesheetInfo.stylesheet) {
|
|
130
133
|
const cssRules = stylesheetInfo.stylesheet.children;
|
|
131
134
|
if (cssRules) {
|
|
132
|
-
filterUnusedFonts(cssRules, fontsInfo.declared, unusedFonts, filteredUsedFonts, docChars);
|
|
135
|
+
filterUnusedFonts(cssRules, fontsInfo.declared, unusedFonts, filteredUsedFonts, docChars, usedFontsCharacters);
|
|
133
136
|
stats.rules.discarded -= cssRules.size;
|
|
134
137
|
}
|
|
135
138
|
}
|
|
@@ -164,8 +167,9 @@ function getFontsInfo(cssRules, fontsInfo, options) {
|
|
|
164
167
|
const fontWeight = getDeclarationValue(ruleData.block.children, "font-weight") || "400";
|
|
165
168
|
const fontStyle = getDeclarationValue(ruleData.block.children, "font-style") || "normal";
|
|
166
169
|
const fontVariant = getDeclarationValue(ruleData.block.children, "font-variant") || "normal";
|
|
170
|
+
const unicodeRange = getDeclarationValue(ruleData.block.children, "unicode-range");
|
|
167
171
|
fontWeight.split(",").forEach(weightValue =>
|
|
168
|
-
fontsInfo.declared.push({ fontFamily, fontWeight: helper.getFontWeight(helper.removeQuotes(weightValue)), fontStyle, fontVariant }));
|
|
172
|
+
fontsInfo.declared.push({ fontFamily, fontWeight: helper.getFontWeight(helper.removeQuotes(weightValue)), fontStyle, fontVariant, unicodeRange, ruleData }));
|
|
169
173
|
}
|
|
170
174
|
}
|
|
171
175
|
}
|
|
@@ -292,19 +296,24 @@ function splitValues(value) {
|
|
|
292
296
|
return values;
|
|
293
297
|
}
|
|
294
298
|
|
|
295
|
-
function filterUnusedFonts(cssRules, declaredFonts, unusedFonts, filteredUsedFonts, docChars) {
|
|
299
|
+
function filterUnusedFonts(cssRules, declaredFonts, unusedFonts, filteredUsedFonts, docChars, usedFontsCharacters) {
|
|
296
300
|
const removedRules = [];
|
|
297
301
|
for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
|
|
298
302
|
const ruleData = cssRule.data;
|
|
299
303
|
if (ruleData.type == "Atrule" && ruleData.name == "import" && ruleData.prelude && ruleData.prelude.children && ruleData.prelude.children.head.data.importedChildren) {
|
|
300
|
-
filterUnusedFonts(ruleData.prelude.children.head.data.importedChildren, declaredFonts, unusedFonts, filteredUsedFonts, docChars);
|
|
304
|
+
filterUnusedFonts(ruleData.prelude.children.head.data.importedChildren, declaredFonts, unusedFonts, filteredUsedFonts, docChars, usedFontsCharacters);
|
|
301
305
|
} else if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports" || ruleData.name == "layer" || ruleData.name == "container") && ruleData.block && ruleData.block.children) {
|
|
302
|
-
filterUnusedFonts(ruleData.block.children, declaredFonts, unusedFonts, filteredUsedFonts, docChars);
|
|
306
|
+
filterUnusedFonts(ruleData.block.children, declaredFonts, unusedFonts, filteredUsedFonts, docChars, usedFontsCharacters);
|
|
303
307
|
} else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
|
|
304
308
|
const fontFamily = helper.normalizeFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
|
|
305
309
|
if (fontFamily) {
|
|
306
310
|
const unicodeRange = getDeclarationValue(ruleData.block.children, "unicode-range");
|
|
307
|
-
|
|
311
|
+
const laterUnicodeRanges = getLaterUnicodeRanges(declaredFonts, ruleData);
|
|
312
|
+
if (unusedFonts.find(fontInfo => fontInfo.fontFamily == fontFamily) ||
|
|
313
|
+
!testUnicodeRange(docChars, unicodeRange) ||
|
|
314
|
+
!testReachableUnicodeRange(docChars, unicodeRange, laterUnicodeRanges) ||
|
|
315
|
+
!testDrawnUnicodeRange(ruleData, fontFamily, unicodeRange, usedFontsCharacters) ||
|
|
316
|
+
!testUsedFont(ruleData, fontFamily, declaredFonts, filteredUsedFonts)) {
|
|
308
317
|
removedRules.push(cssRule);
|
|
309
318
|
}
|
|
310
319
|
}
|
|
@@ -322,6 +331,71 @@ function filterUnusedFonts(cssRules, declaredFonts, unusedFonts, filteredUsedFon
|
|
|
322
331
|
removedRules.forEach(cssRule => cssRules.remove(cssRule));
|
|
323
332
|
}
|
|
324
333
|
|
|
334
|
+
function getUsedFontsCharacters(options) {
|
|
335
|
+
const usedFontsCharacters = new Map();
|
|
336
|
+
if (options.usedFontsCharacters && options.usedFontsCharacters.length) {
|
|
337
|
+
options.usedFontsCharacters.forEach(([fontFamily, fontStyle, ranges, unknown]) => {
|
|
338
|
+
let buckets = usedFontsCharacters.get(fontFamily);
|
|
339
|
+
if (!buckets) {
|
|
340
|
+
buckets = [];
|
|
341
|
+
usedFontsCharacters.set(fontFamily, buckets);
|
|
342
|
+
}
|
|
343
|
+
buckets.push({ fontStyle, ranges: ranges || [], unknown: Boolean(unknown) });
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
return usedFontsCharacters;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function testDrawnUnicodeRange(ruleData, familyName, unicodeRange, usedFontsCharacters) {
|
|
350
|
+
if (!unicodeRange || !usedFontsCharacters || !usedFontsCharacters.size) {
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
const buckets = usedFontsCharacters.get(familyName);
|
|
354
|
+
if (!buckets || !buckets.length) {
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
const fontStyle = getDeclarationValue(ruleData.block.children, "font-style") || "normal";
|
|
358
|
+
if (!VALID_FONT_STYLES.find(rule => fontStyle.trim().match(rule))) {
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
const matchedBuckets = buckets.filter(bucket => testFontStyle(bucket.fontStyle, fontStyle));
|
|
362
|
+
if (!matchedBuckets.length || matchedBuckets.find(bucket => bucket.unknown)) {
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
if (!matchedBuckets.find(bucket => bucket.ranges.length)) {
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
const ranges = parseUnicodeRanges(unicodeRange);
|
|
369
|
+
if (!ranges.length) {
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
return Boolean(matchedBuckets.find(bucket =>
|
|
373
|
+
bucket.ranges.find(drawnRange =>
|
|
374
|
+
ranges.find(range => testDrawnGlyph(drawnRange, range)))));
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// A tab or a newline is recorded as drawn like any other character, but neither can ever select a
|
|
378
|
+
// glyph, so a face whose unicode-range meets the page in nothing else is dead however the page is
|
|
379
|
+
// laid out. Google Fonts symbol subsets are exactly that shape: measured on a capture of
|
|
380
|
+
// techcrunch.com, two Roboto faces worth 23,505 bytes matched the document in U+0009 and U+000A and
|
|
381
|
+
// nothing more.
|
|
382
|
+
function testDrawnGlyph(drawnRange, range) {
|
|
383
|
+
const fromCharCode = Math.max(drawnRange[0], range[0]);
|
|
384
|
+
const toCharCode = Math.min(drawnRange[1], range[1]);
|
|
385
|
+
if (fromCharCode > toCharCode) {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
if (fromCharCode > MAX_NON_GLYPH_CHAR_CODE || toCharCode > MAX_NON_GLYPH_CHAR_CODE) {
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
for (let charCode = fromCharCode; charCode <= toCharCode; charCode++) {
|
|
392
|
+
if (!NON_GLYPH_CHAR_CODES.includes(charCode)) {
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
|
|
325
399
|
function testUsedFont(ruleData, familyName, declaredFonts, filteredUsedFonts) {
|
|
326
400
|
let test;
|
|
327
401
|
const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
|
|
@@ -389,7 +463,7 @@ function testFontweight(fontWeight, usedFontWeights) {
|
|
|
389
463
|
for (const fontWeightValue of fontWeight.split(",")) {
|
|
390
464
|
let { min: fontWeightMin, max: fontWeightMax } = parseFontWeight(fontWeightValue);
|
|
391
465
|
if (!fontWeightMax) {
|
|
392
|
-
fontWeightMax =
|
|
466
|
+
fontWeightMax = fontWeightMin;
|
|
393
467
|
}
|
|
394
468
|
test = test || usedFontWeights.find(usedFontWeight => {
|
|
395
469
|
let { min: usedFontWeightMin, max: usedFontWeightMax } = parseFontWeight(usedFontWeight);
|
|
@@ -631,6 +705,64 @@ function testUnicodeRange(docCharCodes, unicodeRange) {
|
|
|
631
705
|
return true;
|
|
632
706
|
}
|
|
633
707
|
|
|
708
|
+
function getLaterUnicodeRanges(declaredFonts, ruleData) {
|
|
709
|
+
const index = declaredFonts.findIndex(fontInfo => fontInfo.ruleData == ruleData);
|
|
710
|
+
if (index == -1) {
|
|
711
|
+
return [];
|
|
712
|
+
}
|
|
713
|
+
const { fontFamily, fontWeight, fontStyle } = declaredFonts[index];
|
|
714
|
+
return declaredFonts
|
|
715
|
+
.slice(index + 1)
|
|
716
|
+
.filter(fontInfo => fontInfo.ruleData != ruleData &&
|
|
717
|
+
fontInfo.fontFamily == fontFamily &&
|
|
718
|
+
fontInfo.fontWeight == fontWeight &&
|
|
719
|
+
fontInfo.fontStyle == fontStyle)
|
|
720
|
+
.map(fontInfo => fontInfo.unicodeRange);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
function testReachableUnicodeRange(docCharCodes, unicodeRange, laterUnicodeRanges) {
|
|
724
|
+
if (!laterUnicodeRanges.length) {
|
|
725
|
+
return true;
|
|
726
|
+
}
|
|
727
|
+
const laterRanges = helper.flatten(laterUnicodeRanges.map(laterUnicodeRange => parseUnicodeRanges(laterUnicodeRange)));
|
|
728
|
+
if (!laterRanges.length) {
|
|
729
|
+
return true;
|
|
730
|
+
}
|
|
731
|
+
const ranges = parseUnicodeRanges(unicodeRange);
|
|
732
|
+
return Boolean(docCharCodes.find(charCode =>
|
|
733
|
+
(!ranges.length || ranges.find(range => testCharCodeInRange(charCode, range))) &&
|
|
734
|
+
!laterRanges.find(range => testCharCodeInRange(charCode, range))));
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function testCharCodeInRange(charCode, range) {
|
|
738
|
+
return charCode >= range[0] && charCode <= range[1];
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function parseUnicodeRanges(unicodeRange) {
|
|
742
|
+
const ranges = [];
|
|
743
|
+
if (unicodeRange) {
|
|
744
|
+
unicodeRange.split(REGEXP_COMMA).forEach(rangeValue => {
|
|
745
|
+
const range = rangeValue.split(REGEXP_DASH);
|
|
746
|
+
let min, max;
|
|
747
|
+
if (range.length == 2) {
|
|
748
|
+
min = transformRange(range[0]);
|
|
749
|
+
max = transformRange(range[1]);
|
|
750
|
+
} else if (range.length == 1 && range[0]) {
|
|
751
|
+
if (range[0].includes("?")) {
|
|
752
|
+
min = transformRange(range[0].replace(REGEXP_QUESTION_MARK, "0"));
|
|
753
|
+
max = transformRange(range[0].replace(REGEXP_QUESTION_MARK, "F"));
|
|
754
|
+
} else {
|
|
755
|
+
min = max = transformRange(range[0]);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
if (Number.isInteger(min) && Number.isInteger(max)) {
|
|
759
|
+
ranges.push([min, max]);
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
return ranges;
|
|
764
|
+
}
|
|
765
|
+
|
|
634
766
|
function transformRange(range) {
|
|
635
767
|
range = range.replace(REGEXP_STARTS_U_PLUS, "");
|
|
636
768
|
return parseInt(range, 16);
|