single-file-core 1.6.40 → 1.7.0
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/filename.js +11 -2
- package/core/helper.js +16 -3
- package/core/index.js +13 -2
- package/core/lib/processor-helper-common.js +38 -0
- package/core/lib/processor-helper-inline.js +25 -0
- package/core/lib/processor-helper.js +25 -0
- package/core/util.js +1 -0
- package/modules/css-fonts-minifier.js +4 -1
- package/modules/font-glyphs-minifier.js +1570 -0
- package/modules/template-formatter.js +2 -0
- package/package.json +1 -1
- package/processors/compression/compression-packager.js +2 -8
- package/processors/compression/compression.js +16 -7
- package/processors/frame-tree/content/content-frame-tree.js +20 -8
package/core/filename.js
CHANGED
|
@@ -38,12 +38,12 @@ function getValidFilename(filename, replacedCharacters = DEFAULT_REPLACED_CHARAC
|
|
|
38
38
|
if (indexReplacementCharacter && replacedCharacters[index] !== undefined && indexReplacementCharacter != replacedCharacters[index]) {
|
|
39
39
|
// no "+" here, unlike the fallback below: a lookalike replaces its character one for
|
|
40
40
|
// one, so collapsing a run would drop characters the name needs ("C++" -> "C+")
|
|
41
|
-
filename = filename.replace(
|
|
41
|
+
filename = filename.replace(getCharacterClassRegExp(replacedCharacters[index], ""), indexReplacementCharacter);
|
|
42
42
|
}
|
|
43
43
|
});
|
|
44
44
|
replacedCharacters.forEach((replacedCharacter, index) => {
|
|
45
45
|
if (!replacementCharacters[index]) {
|
|
46
|
-
filename = filename.replace(
|
|
46
|
+
filename = filename.replace(getCharacterClassRegExp(replacedCharacter, "+"), replacementCharacter);
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
49
|
filename = filename
|
|
@@ -57,6 +57,15 @@ function getValidFilename(filename, replacedCharacters = DEFAULT_REPLACED_CHARAC
|
|
|
57
57
|
return filename;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
function getCharacterClassRegExp(characters, quantifier) {
|
|
61
|
+
const pattern = "[" + getCharacterClassContent(characters) + "]" + quantifier;
|
|
62
|
+
try {
|
|
63
|
+
return new RegExp(pattern, "gu");
|
|
64
|
+
} catch {
|
|
65
|
+
return new RegExp(pattern, "g");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
60
69
|
function getCharacterClassContent(characters) {
|
|
61
70
|
return characters.length == 1 && CHARACTER_CLASS_SPECIAL_CHARACTERS.includes(characters) ? "\\" + characters : characters;
|
|
62
71
|
}
|
package/core/helper.js
CHANGED
|
@@ -777,12 +777,25 @@ function fixInvalidNesting(document, NESTING_TRACK_ID_ATTRIBUTE_NAME, preventCle
|
|
|
777
777
|
const parentId = idParts.slice(0, -1).join(".");
|
|
778
778
|
const expectedParent = trackIds[parentId];
|
|
779
779
|
if (expectedParent && element.parentElement !== expectedParent && !element.contains(expectedParent)) {
|
|
780
|
+
const followingNodes = [];
|
|
781
|
+
let sibling = element.nextSibling;
|
|
782
|
+
while (sibling && sibling.nodeType != 1) {
|
|
783
|
+
followingNodes.push(sibling);
|
|
784
|
+
sibling = sibling.nextSibling;
|
|
785
|
+
}
|
|
780
786
|
expectedParent.appendChild(element);
|
|
787
|
+
if (sibling && trackIds[sibling.getAttribute(NESTING_TRACK_ID_ATTRIBUTE_NAME)] === sibling && getParentTrackId(sibling) == parentId) {
|
|
788
|
+
expectedParent.append(...followingNodes);
|
|
789
|
+
}
|
|
781
790
|
}
|
|
782
791
|
}
|
|
783
792
|
}
|
|
784
793
|
});
|
|
785
794
|
|
|
795
|
+
function getParentTrackId(element) {
|
|
796
|
+
return element.getAttribute(NESTING_TRACK_ID_ATTRIBUTE_NAME).split(".").slice(0, -1).join(".");
|
|
797
|
+
}
|
|
798
|
+
|
|
786
799
|
function buildTrackIdMap(element) {
|
|
787
800
|
const id = element.getAttribute(NESTING_TRACK_ID_ATTRIBUTE_NAME);
|
|
788
801
|
if (id) {
|
|
@@ -801,9 +814,9 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
|
|
|
801
814
|
const elements = Array.from(element.childNodes).filter(node => (node instanceof win.HTMLElement) || (node instanceof win.SVGElement) || (node instanceof globalThis.HTMLElement) || (node instanceof globalThis.SVGElement));
|
|
802
815
|
elements.forEach(element => {
|
|
803
816
|
let elementHidden, elementKept, computedStyle, headChild;
|
|
804
|
-
if (!options.autoSaveExternalSave && (options.removeHiddenElements || options.removeUnusedFonts || options.compressHTML)) {
|
|
817
|
+
if (!options.autoSaveExternalSave && (options.removeHiddenElements || options.removeUnusedFonts || options.removeUnusedGlyphs || options.compressHTML)) {
|
|
805
818
|
computedStyle = getComputedStyle(win, element);
|
|
806
|
-
if (options.removeHiddenElements || options.removeUnusedFonts) {
|
|
819
|
+
if (options.removeHiddenElements || options.removeUnusedFonts || options.removeUnusedGlyphs) {
|
|
807
820
|
headChild = Boolean(element.closest("html > head"));
|
|
808
821
|
}
|
|
809
822
|
if ((element instanceof win.HTMLElement) || (element instanceof globalThis.HTMLElement)) {
|
|
@@ -826,7 +839,7 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
|
|
|
826
839
|
data.markedElements.push(element);
|
|
827
840
|
}
|
|
828
841
|
}
|
|
829
|
-
if (options.removeUnusedFonts && doc.defaultView && !headChild) {
|
|
842
|
+
if ((options.removeUnusedFonts || options.removeUnusedGlyphs) && doc.defaultView && !headChild) {
|
|
830
843
|
const elementCharacters = getElementCharacters(win, element, computedStyle);
|
|
831
844
|
getUsedFont(computedStyle, data.usedFonts, data.usedFontsCharacters, elementCharacters);
|
|
832
845
|
const beforeStyle = getComputedStyle(win, element, ":before");
|
package/core/index.js
CHANGED
|
@@ -98,8 +98,9 @@ class ProgressEvent {
|
|
|
98
98
|
const RESOLVE_URLS_STAGE = 0;
|
|
99
99
|
const REPLACE_DATA_STAGE = 1;
|
|
100
100
|
const REPLACE_DOCS_STAGE = 2;
|
|
101
|
-
const
|
|
102
|
-
const
|
|
101
|
+
const REMOVE_GLYPHS_STAGE = 3;
|
|
102
|
+
const POST_PROCESS_STAGE = 4;
|
|
103
|
+
const FINALIZE_STAGE = 5;
|
|
103
104
|
const STAGES = [{
|
|
104
105
|
sequential: [
|
|
105
106
|
{ action: "preProcessPage" },
|
|
@@ -153,6 +154,11 @@ const STAGES = [{
|
|
|
153
154
|
{ option: "removeAlternativeFonts", action: "removeAlternativeFonts" },
|
|
154
155
|
{ option: "!removeFrames", action: "processFrames" }
|
|
155
156
|
]
|
|
157
|
+
}, {
|
|
158
|
+
sequential: [],
|
|
159
|
+
parallel: [
|
|
160
|
+
{ option: "removeUnusedGlyphs", action: "removeUnusedGlyphs" }
|
|
161
|
+
]
|
|
156
162
|
}, {
|
|
157
163
|
sequential: [
|
|
158
164
|
{ action: "replaceStylesheets" },
|
|
@@ -267,6 +273,7 @@ class Runner {
|
|
|
267
273
|
this.options.doc = null;
|
|
268
274
|
this.options.win = null;
|
|
269
275
|
await this.executeStage(REPLACE_DOCS_STAGE);
|
|
276
|
+
await this.executeStage(REMOVE_GLYPHS_STAGE);
|
|
270
277
|
await this.executeStage(POST_PROCESS_STAGE);
|
|
271
278
|
await this.executeStage(FINALIZE_STAGE);
|
|
272
279
|
this.processor.finalize();
|
|
@@ -1738,6 +1745,10 @@ class Processor {
|
|
|
1738
1745
|
util.removeAlternativeImages(this.doc);
|
|
1739
1746
|
}
|
|
1740
1747
|
|
|
1748
|
+
async removeUnusedGlyphs() {
|
|
1749
|
+
await this.processorHelper.removeUnusedGlyphs(this.stylesheets, this.resources.fonts, this.options);
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1741
1752
|
groupDuplicateFonts() {
|
|
1742
1753
|
this.processorHelper.groupDuplicateFonts(this.stylesheets, this.resources.fonts, this.options);
|
|
1743
1754
|
}
|
|
@@ -588,6 +588,30 @@ class ProcessorHelperCommon {
|
|
|
588
588
|
};
|
|
589
589
|
}
|
|
590
590
|
|
|
591
|
+
getFontFacesGlyphRanges(stylesheets, options) {
|
|
592
|
+
const familiesRanges = new Map();
|
|
593
|
+
(options.usedFontsCharacters || []).forEach(([fontFamily, , ranges, unknown]) => {
|
|
594
|
+
let familyRanges = familiesRanges.get(fontFamily);
|
|
595
|
+
if (!familyRanges) {
|
|
596
|
+
familyRanges = { ranges: [], unknown: false };
|
|
597
|
+
familiesRanges.set(fontFamily, familyRanges);
|
|
598
|
+
}
|
|
599
|
+
familyRanges.ranges.push(...(ranges || []));
|
|
600
|
+
familyRanges.unknown = familyRanges.unknown || Boolean(unknown);
|
|
601
|
+
});
|
|
602
|
+
const fontFaces = [];
|
|
603
|
+
stylesheets.forEach(stylesheetInfo => {
|
|
604
|
+
if (stylesheetInfo.stylesheet && stylesheetInfo.stylesheet.children) {
|
|
605
|
+
forEachFontFaceRule(stylesheetInfo.stylesheet.children, ruleData => {
|
|
606
|
+
const fontFamily = normalizeFontFamily(this.getPropertyValue(ruleData, "font-family"));
|
|
607
|
+
const familyRanges = fontFamily && familiesRanges.get(fontFamily);
|
|
608
|
+
fontFaces.push({ urlNodes: getUrlFunctions(ruleData), ranges: familyRanges && !familyRanges.unknown ? familyRanges.ranges : null });
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
return fontFaces;
|
|
613
|
+
}
|
|
614
|
+
|
|
591
615
|
getFontKey(ruleData) {
|
|
592
616
|
return JSON.stringify([
|
|
593
617
|
normalizeFontFamily(this.getPropertyValue(ruleData, "font-family")),
|
|
@@ -786,6 +810,20 @@ function getUrlFunctions(declarationList) {
|
|
|
786
810
|
return cssTree.findAll(declarationList, node => node.type == "Url");
|
|
787
811
|
}
|
|
788
812
|
|
|
813
|
+
function forEachFontFaceRule(cssRules, callback) {
|
|
814
|
+
cssRules.forEach(ruleData => {
|
|
815
|
+
if (ruleData.type == "Atrule" && ruleData.name == "import" && ruleData.prelude && ruleData.prelude.children && ruleData.prelude.children.head.data.importedChildren) {
|
|
816
|
+
forEachFontFaceRule(ruleData.prelude.children.head.data.importedChildren, callback);
|
|
817
|
+
} else if (ruleData.type == "Atrule" && decodeName(ruleData.name) == "font-face") {
|
|
818
|
+
if (ruleData.block && ruleData.block.children) {
|
|
819
|
+
callback(ruleData);
|
|
820
|
+
}
|
|
821
|
+
} else if (ruleData.type == "Atrule" && ruleData.block && ruleData.block.children) {
|
|
822
|
+
forEachFontFaceRule(ruleData.block.children, callback);
|
|
823
|
+
}
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
|
|
789
827
|
function recoverFontFaceSources(stylesheet) {
|
|
790
828
|
cssTree.walk(stylesheet, {
|
|
791
829
|
visit: "Atrule",
|
|
@@ -23,8 +23,10 @@
|
|
|
23
23
|
|
|
24
24
|
import * as cssTree from "./../../vendor/css-tree.js";
|
|
25
25
|
import { serialize as serializeSrcset } from "./../../vendor/html-srcset-parser.js";
|
|
26
|
+
import * as fontGlyphsMinifier from "./../../modules/font-glyphs-minifier.js";
|
|
26
27
|
|
|
27
28
|
const JSON = globalThis.JSON;
|
|
29
|
+
const atob = globalThis.atob;
|
|
28
30
|
const FontFace = globalThis.FontFace;
|
|
29
31
|
const Set = globalThis.Set;
|
|
30
32
|
const setTimeout = globalThis.setTimeout;
|
|
@@ -45,6 +47,7 @@ const PREFIXES_FORBIDDEN_DATA_URI = ["data:text/"];
|
|
|
45
47
|
const SCRIPT_TAG_FOUND = /<script/gi;
|
|
46
48
|
const NOSCRIPT_TAG_FOUND = /<noscript/gi;
|
|
47
49
|
const CANVAS_TAG_FOUND = /<canvas/gi;
|
|
50
|
+
const REGEXP_BASE64_DATA_URI = /^(.* )?data:([^;,]*);base64,(.*)$/;
|
|
48
51
|
const SINGLE_FILE_VARIABLE_NAME_PREFIX = "--sf-img-";
|
|
49
52
|
const SINGLE_FILE_VARIABLE_MAX_SIZE = 512 * 1024;
|
|
50
53
|
const EMPTY_URL_SOURCE = /^url\(["']?data:[^,]*,?["']?\)/;
|
|
@@ -581,6 +584,28 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
581
584
|
doc.querySelectorAll("link[rel*=stylesheet][rel*=alternate][title]").forEach(element => element.remove());
|
|
582
585
|
}
|
|
583
586
|
|
|
587
|
+
async removeUnusedGlyphs(stylesheets, fonts, options) {
|
|
588
|
+
await Promise.all(this.getFontFacesGlyphRanges(stylesheets, options).map(async ({ urlNodes, ranges }) => {
|
|
589
|
+
if (ranges) {
|
|
590
|
+
await Promise.all(urlNodes.map(async urlNode => {
|
|
591
|
+
const match = urlNode.value.match(REGEXP_BASE64_DATA_URI);
|
|
592
|
+
if (match) {
|
|
593
|
+
const [, prefix = "", contentType, data] = match;
|
|
594
|
+
const binaryString = atob(data);
|
|
595
|
+
const content = new Uint8Array(binaryString.length);
|
|
596
|
+
for (let index = 0; index < content.length; index++) {
|
|
597
|
+
content[index] = binaryString.charCodeAt(index);
|
|
598
|
+
}
|
|
599
|
+
const result = await fontGlyphsMinifier.process(content, ranges, { brotli: util.brotli });
|
|
600
|
+
if (result) {
|
|
601
|
+
urlNode.value = prefix + await toDataURI(result, contentType);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}));
|
|
605
|
+
}
|
|
606
|
+
}));
|
|
607
|
+
}
|
|
608
|
+
|
|
584
609
|
groupDuplicateFonts(stylesheets, fonts, options) {
|
|
585
610
|
if (options.usedFonts && options.usedFonts.length) {
|
|
586
611
|
const fontFaces = [];
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
import * as cssTree from "./../../vendor/css-tree.js";
|
|
25
25
|
import { serialize as serializeSrcset } from "./../../vendor/html-srcset-parser.js";
|
|
26
|
+
import * as fontGlyphsMinifier from "./../../modules/font-glyphs-minifier.js";
|
|
26
27
|
|
|
27
28
|
const JSON = globalThis.JSON;
|
|
28
29
|
const FontFace = globalThis.FontFace;
|
|
@@ -343,6 +344,25 @@ function getProcessorHelperClass(utilInstance) {
|
|
|
343
344
|
}
|
|
344
345
|
}
|
|
345
346
|
|
|
347
|
+
async removeUnusedGlyphs(stylesheets, fonts, options) {
|
|
348
|
+
const rangesByName = new Map();
|
|
349
|
+
this.getFontFacesGlyphRanges(stylesheets, options).forEach(({ urlNodes, ranges }) => urlNodes.forEach(urlNode => {
|
|
350
|
+
const name = getResourceName(urlNode.value);
|
|
351
|
+
if (!rangesByName.has(name) || rangesByName.get(name)) {
|
|
352
|
+
rangesByName.set(name, ranges ? (rangesByName.get(name) || []).concat(ranges) : null);
|
|
353
|
+
}
|
|
354
|
+
}));
|
|
355
|
+
await Promise.all(Array.from(fonts.values()).map(async resource => {
|
|
356
|
+
const ranges = rangesByName.get(resource.name);
|
|
357
|
+
if (ranges && resource.content) {
|
|
358
|
+
const content = await fontGlyphsMinifier.process(resource.content, ranges, { brotli: util.brotli });
|
|
359
|
+
if (content) {
|
|
360
|
+
resource.content = content;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}));
|
|
364
|
+
}
|
|
365
|
+
|
|
346
366
|
groupDuplicateFonts(stylesheets, fonts) {
|
|
347
367
|
const canonicalNames = new Map();
|
|
348
368
|
const fontResources = [...fonts]
|
|
@@ -751,6 +771,11 @@ function testSameContent(content, otherContent) {
|
|
|
751
771
|
return true;
|
|
752
772
|
}
|
|
753
773
|
|
|
774
|
+
function getResourceName(value) {
|
|
775
|
+
const index = value.lastIndexOf(" ");
|
|
776
|
+
return index == -1 ? value : value.substring(index + 1);
|
|
777
|
+
}
|
|
778
|
+
|
|
754
779
|
function replaceResourceName(urlNode, canonicalNames) {
|
|
755
780
|
canonicalNames.forEach((canonicalName, name) => {
|
|
756
781
|
if (urlNode.value == name) {
|
package/core/util.js
CHANGED
|
@@ -100,6 +100,7 @@ function getInstance(utilOptions) {
|
|
|
100
100
|
utilOptions.frameFetch = utilOptions.frameFetch || utilOptions.fetch;
|
|
101
101
|
return {
|
|
102
102
|
getDoctypeString,
|
|
103
|
+
brotli: utilOptions.brotli,
|
|
103
104
|
getFilenameExtension(resourceURL, replacedCharacters, replacementCharacter, replacementCharacters) {
|
|
104
105
|
let matchExtension;
|
|
105
106
|
try {
|
|
@@ -154,7 +154,10 @@ function process(doc, stylesheets, styles, options) {
|
|
|
154
154
|
selectedFonts = getSelectedFonts(fontsInfo.declared, filteredUsedFonts);
|
|
155
155
|
}
|
|
156
156
|
const usedFontsCharacters = getUsedFontsCharacters(options);
|
|
157
|
-
const docCharCodes = new Set(
|
|
157
|
+
const docCharCodes = new Set();
|
|
158
|
+
for (const char of docContent) {
|
|
159
|
+
docCharCodes.add(char.codePointAt(0));
|
|
160
|
+
}
|
|
158
161
|
usedFontsCharacters.forEach(buckets => buckets.forEach(bucket => bucket.ranges.forEach(([fromCharCode, toCharCode]) => {
|
|
159
162
|
for (let charCode = fromCharCode; charCode <= toCharCode; charCode++) {
|
|
160
163
|
if (!NON_GLYPH_CHAR_CODES.includes(charCode)) {
|