single-file-core 1.6.38 → 1.6.40
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 +112 -11
- package/core/lib/processor-helper-common.js +56 -0
- package/package.json +1 -1
package/core/helper.js
CHANGED
|
@@ -107,6 +107,29 @@ const TEXT_NODE_TYPE = 3;
|
|
|
107
107
|
const REGEXP_QUOTED_STRING = /"((?:[^"\\]|\\.)*)"|'((?:[^'\\]|\\.)*)'/g;
|
|
108
108
|
const REGEXP_GENERATED_CONTENT_IMAGE = /(url|image-set|-webkit-image-set|linear-gradient|radial-gradient|conic-gradient)\([^)]*\)/g;
|
|
109
109
|
const REGEXP_BACKSLASH = /\\/;
|
|
110
|
+
const REGEXP_TEXT_TRANSFORM_SEPARATOR = /\s+/;
|
|
111
|
+
const MARKER_SUFFIX = ". ";
|
|
112
|
+
const COUNTER_STYLE_CHARACTERS = {
|
|
113
|
+
"none": "",
|
|
114
|
+
"disc": "",
|
|
115
|
+
"circle": "",
|
|
116
|
+
"square": "",
|
|
117
|
+
"disclosure-open": "",
|
|
118
|
+
"disclosure-closed": "",
|
|
119
|
+
"decimal": "0123456789",
|
|
120
|
+
"decimal-leading-zero": "0123456789",
|
|
121
|
+
"lower-alpha": "abcdefghijklmnopqrstuvwxyz",
|
|
122
|
+
"lower-latin": "abcdefghijklmnopqrstuvwxyz",
|
|
123
|
+
"upper-alpha": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
124
|
+
"upper-latin": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
125
|
+
"lower-roman": "ivxlcdm",
|
|
126
|
+
"upper-roman": "IVXLCDM",
|
|
127
|
+
"lower-greek": "αβγδεζηθικλμνξοπρστυφχψω"
|
|
128
|
+
};
|
|
129
|
+
const REGEXP_INFINITE_FEATURE_VALUE = /(\(\s*[a-z-]+\s*(?::|<=?|>=?|=)\s*)(-?)infinity\s*\*\s*1([a-z%]+)/gi;
|
|
130
|
+
const MAX_FINITE_FEATURE_VALUE = "3.40282e38";
|
|
131
|
+
const FULL_WIDTH_OFFSET = 0xFEE0;
|
|
132
|
+
const FULL_WIDTH_SPACE = " ";
|
|
110
133
|
const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
|
|
111
134
|
const INFOBAR_TAGNAME = infobar.INFOBAR_TAGNAME;
|
|
112
135
|
const EMPTY_RESOURCE = "data:,";
|
|
@@ -179,6 +202,10 @@ const FileReader = globalThis.FileReader;
|
|
|
179
202
|
|
|
180
203
|
export {
|
|
181
204
|
initUserScriptHandler,
|
|
205
|
+
getElementCharacters,
|
|
206
|
+
getPseudoElementCharacters,
|
|
207
|
+
getMarkerCharacters,
|
|
208
|
+
normalizeSerializedCSSText,
|
|
182
209
|
initDoc,
|
|
183
210
|
preProcessDoc,
|
|
184
211
|
postProcessDoc,
|
|
@@ -800,16 +827,17 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
|
|
|
800
827
|
}
|
|
801
828
|
}
|
|
802
829
|
if (options.removeUnusedFonts && doc.defaultView && !headChild) {
|
|
803
|
-
const elementCharacters = getElementCharacters(win, element);
|
|
830
|
+
const elementCharacters = getElementCharacters(win, element, computedStyle);
|
|
804
831
|
getUsedFont(computedStyle, data.usedFonts, data.usedFontsCharacters, elementCharacters);
|
|
805
832
|
const beforeStyle = getComputedStyle(win, element, ":before");
|
|
806
|
-
getUsedFont(beforeStyle, data.usedFonts, data.usedFontsCharacters, getPseudoElementCharacters(beforeStyle));
|
|
833
|
+
getUsedFont(beforeStyle, data.usedFonts, data.usedFontsCharacters, getPseudoElementCharacters(beforeStyle, element));
|
|
807
834
|
const afterStyle = getComputedStyle(win, element, ":after");
|
|
808
|
-
getUsedFont(afterStyle, data.usedFonts, data.usedFontsCharacters, getPseudoElementCharacters(afterStyle));
|
|
835
|
+
getUsedFont(afterStyle, data.usedFonts, data.usedFontsCharacters, getPseudoElementCharacters(afterStyle, element));
|
|
809
836
|
const display = computedStyle ? computedStyle.getPropertyValue("display") : "";
|
|
810
837
|
const tagName = element.tagName.toUpperCase();
|
|
811
838
|
if (display == "list-item") {
|
|
812
|
-
|
|
839
|
+
const markerStyle = getComputedStyle(win, element, "::marker");
|
|
840
|
+
getUsedFont(markerStyle, data.usedFonts, data.usedFontsCharacters, getMarkerCharacters(markerStyle, computedStyle, element));
|
|
813
841
|
}
|
|
814
842
|
if (BLOCK_CONTAINER_DISPLAY_VALUES.includes(display)) {
|
|
815
843
|
getUsedFont(getComputedStyle(win, element, ":first-letter"), data.usedFonts, data.usedFontsCharacters, elementCharacters);
|
|
@@ -901,7 +929,7 @@ function getStylesheetsContent(styleSheets, adoptedStyleSheetsCache = new Map())
|
|
|
901
929
|
let cssText = "";
|
|
902
930
|
if (styleSheet && styleSheet.cssRules) {
|
|
903
931
|
for (const cssRule of styleSheet.cssRules) {
|
|
904
|
-
cssText += cssRule
|
|
932
|
+
cssText += getSerializedCSSText(cssRule) + "\n";
|
|
905
933
|
}
|
|
906
934
|
}
|
|
907
935
|
adoptedStyleSheetsCache.set(styleSheet, cssText);
|
|
@@ -1076,7 +1104,7 @@ function addUsedFontCharacters(usedFontsCharacters, fontFamilyName, fontStyle, d
|
|
|
1076
1104
|
}
|
|
1077
1105
|
}
|
|
1078
1106
|
|
|
1079
|
-
function getElementCharacters(win, element) {
|
|
1107
|
+
function getElementCharacters(win, element, computedStyle) {
|
|
1080
1108
|
let characters = "";
|
|
1081
1109
|
const tagName = element.tagName && element.tagName.toUpperCase();
|
|
1082
1110
|
if (tagName == "INPUT" || tagName == "TEXTAREA" || tagName == "BUTTON") {
|
|
@@ -1093,7 +1121,72 @@ function getElementCharacters(win, element) {
|
|
|
1093
1121
|
}
|
|
1094
1122
|
});
|
|
1095
1123
|
}
|
|
1096
|
-
return { characters };
|
|
1124
|
+
return transformCharacters({ characters }, computedStyle, element);
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
function getMarkerCharacters(markerStyle, computedStyle, element) {
|
|
1128
|
+
const content = markerStyle && markerStyle.getPropertyValue("content");
|
|
1129
|
+
if (content == "none") {
|
|
1130
|
+
return { characters: "" };
|
|
1131
|
+
}
|
|
1132
|
+
if (content && content != "normal") {
|
|
1133
|
+
return getPseudoElementCharacters(markerStyle, element);
|
|
1134
|
+
}
|
|
1135
|
+
const listStyleType = computedStyle ? computedStyle.getPropertyValue("list-style-type").trim() : "";
|
|
1136
|
+
if (Object.prototype.hasOwnProperty.call(COUNTER_STYLE_CHARACTERS, listStyleType)) {
|
|
1137
|
+
const characters = COUNTER_STYLE_CHARACTERS[listStyleType];
|
|
1138
|
+
return transformCharacters({ characters: characters ? characters + MARKER_SUFFIX : "" }, markerStyle || computedStyle, element);
|
|
1139
|
+
}
|
|
1140
|
+
REGEXP_QUOTED_STRING.lastIndex = 0;
|
|
1141
|
+
const match = REGEXP_QUOTED_STRING.exec(listStyleType);
|
|
1142
|
+
if (match && match[0] == listStyleType) {
|
|
1143
|
+
const characters = match[1] === undefined ? match[2] : match[1];
|
|
1144
|
+
return transformCharacters({ characters, unknown: REGEXP_BACKSLASH.test(characters) }, markerStyle || computedStyle, element);
|
|
1145
|
+
}
|
|
1146
|
+
return { characters: "", unknown: true };
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
function transformCharacters(drawnCharacters, computedStyle, element) {
|
|
1150
|
+
const textTransform = computedStyle && computedStyle.getPropertyValue("text-transform");
|
|
1151
|
+
if (!textTransform || textTransform == "none" || !drawnCharacters.characters) {
|
|
1152
|
+
return drawnCharacters;
|
|
1153
|
+
}
|
|
1154
|
+
const sourceCharacters = drawnCharacters.characters;
|
|
1155
|
+
let characters = sourceCharacters, unknown = drawnCharacters.unknown;
|
|
1156
|
+
const langElement = element && element.closest && element.closest("[lang]");
|
|
1157
|
+
const lang = langElement && langElement.getAttribute("lang");
|
|
1158
|
+
textTransform.trim().split(REGEXP_TEXT_TRANSFORM_SEPARATOR).forEach(value => {
|
|
1159
|
+
try {
|
|
1160
|
+
if (value == "uppercase" || value == "capitalize") {
|
|
1161
|
+
characters += sourceCharacters.toUpperCase() + (lang ? sourceCharacters.toLocaleUpperCase(lang) : "");
|
|
1162
|
+
} else if (value == "lowercase") {
|
|
1163
|
+
characters += sourceCharacters.toLowerCase() + (lang ? sourceCharacters.toLocaleLowerCase(lang) : "");
|
|
1164
|
+
} else if (value == "full-width") {
|
|
1165
|
+
characters += getFullWidthCharacters(sourceCharacters);
|
|
1166
|
+
} else {
|
|
1167
|
+
unknown = true;
|
|
1168
|
+
}
|
|
1169
|
+
// eslint-disable-next-line no-unused-vars
|
|
1170
|
+
} catch (error) {
|
|
1171
|
+
unknown = true;
|
|
1172
|
+
}
|
|
1173
|
+
});
|
|
1174
|
+
return { characters, unknown };
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
function getFullWidthCharacters(characters) {
|
|
1178
|
+
let result = "";
|
|
1179
|
+
for (const character of characters) {
|
|
1180
|
+
const codePoint = character.codePointAt(0);
|
|
1181
|
+
if (codePoint == 0x20) {
|
|
1182
|
+
result += FULL_WIDTH_SPACE;
|
|
1183
|
+
} else if (codePoint > 0x20 && codePoint < 0x7F) {
|
|
1184
|
+
result += String.fromCodePoint(codePoint + FULL_WIDTH_OFFSET);
|
|
1185
|
+
} else {
|
|
1186
|
+
result += character;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
return result;
|
|
1097
1190
|
}
|
|
1098
1191
|
|
|
1099
1192
|
// getElementsInfo iterates the children of the element it is given, so the root is never visited.
|
|
@@ -1105,14 +1198,14 @@ function getElementCharacters(win, element) {
|
|
|
1105
1198
|
function getRootElementUsedFonts(win, element, data) {
|
|
1106
1199
|
ROOT_PSEUDO_ELEMENT_NAMES.forEach(pseudoElementName => {
|
|
1107
1200
|
const computedStyle = getComputedStyle(win, element, pseudoElementName);
|
|
1108
|
-
const drawnCharacters = getPseudoElementCharacters(computedStyle);
|
|
1201
|
+
const drawnCharacters = getPseudoElementCharacters(computedStyle, element);
|
|
1109
1202
|
if (drawnCharacters.characters || drawnCharacters.unknown) {
|
|
1110
1203
|
getUsedFont(computedStyle, data.usedFonts, data.usedFontsCharacters, drawnCharacters);
|
|
1111
1204
|
}
|
|
1112
1205
|
});
|
|
1113
1206
|
}
|
|
1114
1207
|
|
|
1115
|
-
function getPseudoElementCharacters(computedStyle) {
|
|
1208
|
+
function getPseudoElementCharacters(computedStyle, element) {
|
|
1116
1209
|
const content = computedStyle && computedStyle.getPropertyValue("content");
|
|
1117
1210
|
if (!content || content == "none" || content == "normal") {
|
|
1118
1211
|
return { characters: "" };
|
|
@@ -1128,7 +1221,7 @@ function getPseudoElementCharacters(computedStyle) {
|
|
|
1128
1221
|
.replace(REGEXP_QUOTED_STRING, "")
|
|
1129
1222
|
.replace(REGEXP_GENERATED_CONTENT_IMAGE, "")
|
|
1130
1223
|
.trim();
|
|
1131
|
-
return { characters, unknown: Boolean(remainder) || REGEXP_BACKSLASH.test(characters) };
|
|
1224
|
+
return transformCharacters({ characters, unknown: Boolean(remainder) || REGEXP_BACKSLASH.test(characters) }, computedStyle, element);
|
|
1132
1225
|
}
|
|
1133
1226
|
|
|
1134
1227
|
function serializeUsedFontsCharacters(usedFontsCharacters) {
|
|
@@ -1253,6 +1346,14 @@ function markLinkStylesheets(doc, markedElements) {
|
|
|
1253
1346
|
return linkElements;
|
|
1254
1347
|
}
|
|
1255
1348
|
|
|
1349
|
+
function getSerializedCSSText(cssRule) {
|
|
1350
|
+
return normalizeSerializedCSSText(cssRule.cssText);
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
function normalizeSerializedCSSText(cssText) {
|
|
1354
|
+
return cssText.replace(REGEXP_INFINITE_FEATURE_VALUE, "$1$2" + MAX_FINITE_FEATURE_VALUE + "$3");
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1256
1357
|
function getStylesheetsData(doc, markedElements) {
|
|
1257
1358
|
if (doc) {
|
|
1258
1359
|
const contents = [];
|
|
@@ -1269,7 +1370,7 @@ function getStylesheetsData(doc, markedElements) {
|
|
|
1269
1370
|
if (!stylesheet || textContentStylesheet != sheetStylesheet) {
|
|
1270
1371
|
styleElement.setAttribute(STYLESHEET_ATTRIBUTE_NAME, styleIndex);
|
|
1271
1372
|
markedElements.push(styleElement);
|
|
1272
|
-
contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(
|
|
1373
|
+
contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(getSerializedCSSText).join("\n");
|
|
1273
1374
|
}
|
|
1274
1375
|
}
|
|
1275
1376
|
// eslint-disable-next-line no-unused-vars
|
|
@@ -399,6 +399,7 @@ class ProcessorHelperCommon {
|
|
|
399
399
|
}
|
|
400
400
|
|
|
401
401
|
resolveStylesheetURLs(stylesheet, baseURI, workStylesheet, stylesheetReferrer, stylesheetReferrers) {
|
|
402
|
+
recoverFontFaceSources(stylesheet);
|
|
402
403
|
const urls = getUrlFunctions(stylesheet);
|
|
403
404
|
urls.map(urlNode => {
|
|
404
405
|
const originalResourceURL = urlNode.value;
|
|
@@ -785,6 +786,61 @@ function getUrlFunctions(declarationList) {
|
|
|
785
786
|
return cssTree.findAll(declarationList, node => node.type == "Url");
|
|
786
787
|
}
|
|
787
788
|
|
|
789
|
+
function recoverFontFaceSources(stylesheet) {
|
|
790
|
+
cssTree.walk(stylesheet, {
|
|
791
|
+
visit: "Atrule",
|
|
792
|
+
enter(ruleData) {
|
|
793
|
+
if (decodeName(ruleData.name) == "font-face" && ruleData.block && ruleData.block.children) {
|
|
794
|
+
ruleData.block.children.forEach(declaration => {
|
|
795
|
+
if (declaration.type == "Declaration" && declaration.property == "src" && declaration.value && declaration.value.type == "Raw") {
|
|
796
|
+
const sources = splitFontSources(declaration.value.value).map(parseFontSource).filter(Boolean);
|
|
797
|
+
if (sources.length) {
|
|
798
|
+
const children = new cssTree.List();
|
|
799
|
+
sources.forEach((source, index) => {
|
|
800
|
+
if (index) {
|
|
801
|
+
children.push({ type: "Operator", value: "," });
|
|
802
|
+
}
|
|
803
|
+
source.children.forEach(child => children.push(child));
|
|
804
|
+
});
|
|
805
|
+
declaration.value = { type: "Value", children };
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
function splitFontSources(value) {
|
|
815
|
+
const { Function, LeftParenthesis, LeftSquareBracket, LeftCurlyBracket, RightParenthesis, RightSquareBracket, RightCurlyBracket, Comma } = cssTree.tokenTypes;
|
|
816
|
+
const sources = [];
|
|
817
|
+
let depth = 0, start = 0;
|
|
818
|
+
cssTree.tokenize(value, (type, tokenStart, tokenEnd) => {
|
|
819
|
+
if (type == Function || type == LeftParenthesis || type == LeftSquareBracket || type == LeftCurlyBracket) {
|
|
820
|
+
depth++;
|
|
821
|
+
} else if (type == RightParenthesis || type == RightSquareBracket || type == RightCurlyBracket) {
|
|
822
|
+
depth = Math.max(0, depth - 1);
|
|
823
|
+
} else if (type == Comma && !depth) {
|
|
824
|
+
sources.push(value.substring(start, tokenStart));
|
|
825
|
+
start = tokenEnd;
|
|
826
|
+
}
|
|
827
|
+
});
|
|
828
|
+
sources.push(value.substring(start));
|
|
829
|
+
return sources;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
function parseFontSource(source) {
|
|
833
|
+
try {
|
|
834
|
+
const value = cssTree.parse(source, { context: "value" });
|
|
835
|
+
if (cssTree.find(value, node => node.type == "Url" || (node.type == "Function" && node.name.toLowerCase() == "local"))) {
|
|
836
|
+
return value;
|
|
837
|
+
}
|
|
838
|
+
// eslint-disable-next-line no-unused-vars
|
|
839
|
+
} catch (error) {
|
|
840
|
+
// ignored
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
788
844
|
function getImportFunctions(declarationList) {
|
|
789
845
|
return cssTree.findAll(declarationList, node => node.type == "Atrule" && node.name == "import");
|
|
790
846
|
}
|