single-file-core 1.6.38 → 1.6.39
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 +99 -9
- package/core/lib/processor-helper-common.js +56 -0
- package/package.json +1 -1
package/core/helper.js
CHANGED
|
@@ -107,6 +107,27 @@ 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 FULL_WIDTH_OFFSET = 0xFEE0;
|
|
130
|
+
const FULL_WIDTH_SPACE = " ";
|
|
110
131
|
const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
|
|
111
132
|
const INFOBAR_TAGNAME = infobar.INFOBAR_TAGNAME;
|
|
112
133
|
const EMPTY_RESOURCE = "data:,";
|
|
@@ -179,6 +200,9 @@ const FileReader = globalThis.FileReader;
|
|
|
179
200
|
|
|
180
201
|
export {
|
|
181
202
|
initUserScriptHandler,
|
|
203
|
+
getElementCharacters,
|
|
204
|
+
getPseudoElementCharacters,
|
|
205
|
+
getMarkerCharacters,
|
|
182
206
|
initDoc,
|
|
183
207
|
preProcessDoc,
|
|
184
208
|
postProcessDoc,
|
|
@@ -800,16 +824,17 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
|
|
|
800
824
|
}
|
|
801
825
|
}
|
|
802
826
|
if (options.removeUnusedFonts && doc.defaultView && !headChild) {
|
|
803
|
-
const elementCharacters = getElementCharacters(win, element);
|
|
827
|
+
const elementCharacters = getElementCharacters(win, element, computedStyle);
|
|
804
828
|
getUsedFont(computedStyle, data.usedFonts, data.usedFontsCharacters, elementCharacters);
|
|
805
829
|
const beforeStyle = getComputedStyle(win, element, ":before");
|
|
806
|
-
getUsedFont(beforeStyle, data.usedFonts, data.usedFontsCharacters, getPseudoElementCharacters(beforeStyle));
|
|
830
|
+
getUsedFont(beforeStyle, data.usedFonts, data.usedFontsCharacters, getPseudoElementCharacters(beforeStyle, element));
|
|
807
831
|
const afterStyle = getComputedStyle(win, element, ":after");
|
|
808
|
-
getUsedFont(afterStyle, data.usedFonts, data.usedFontsCharacters, getPseudoElementCharacters(afterStyle));
|
|
832
|
+
getUsedFont(afterStyle, data.usedFonts, data.usedFontsCharacters, getPseudoElementCharacters(afterStyle, element));
|
|
809
833
|
const display = computedStyle ? computedStyle.getPropertyValue("display") : "";
|
|
810
834
|
const tagName = element.tagName.toUpperCase();
|
|
811
835
|
if (display == "list-item") {
|
|
812
|
-
|
|
836
|
+
const markerStyle = getComputedStyle(win, element, "::marker");
|
|
837
|
+
getUsedFont(markerStyle, data.usedFonts, data.usedFontsCharacters, getMarkerCharacters(markerStyle, computedStyle, element));
|
|
813
838
|
}
|
|
814
839
|
if (BLOCK_CONTAINER_DISPLAY_VALUES.includes(display)) {
|
|
815
840
|
getUsedFont(getComputedStyle(win, element, ":first-letter"), data.usedFonts, data.usedFontsCharacters, elementCharacters);
|
|
@@ -1076,7 +1101,7 @@ function addUsedFontCharacters(usedFontsCharacters, fontFamilyName, fontStyle, d
|
|
|
1076
1101
|
}
|
|
1077
1102
|
}
|
|
1078
1103
|
|
|
1079
|
-
function getElementCharacters(win, element) {
|
|
1104
|
+
function getElementCharacters(win, element, computedStyle) {
|
|
1080
1105
|
let characters = "";
|
|
1081
1106
|
const tagName = element.tagName && element.tagName.toUpperCase();
|
|
1082
1107
|
if (tagName == "INPUT" || tagName == "TEXTAREA" || tagName == "BUTTON") {
|
|
@@ -1093,7 +1118,72 @@ function getElementCharacters(win, element) {
|
|
|
1093
1118
|
}
|
|
1094
1119
|
});
|
|
1095
1120
|
}
|
|
1096
|
-
return { characters };
|
|
1121
|
+
return transformCharacters({ characters }, computedStyle, element);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
function getMarkerCharacters(markerStyle, computedStyle, element) {
|
|
1125
|
+
const content = markerStyle && markerStyle.getPropertyValue("content");
|
|
1126
|
+
if (content == "none") {
|
|
1127
|
+
return { characters: "" };
|
|
1128
|
+
}
|
|
1129
|
+
if (content && content != "normal") {
|
|
1130
|
+
return getPseudoElementCharacters(markerStyle, element);
|
|
1131
|
+
}
|
|
1132
|
+
const listStyleType = computedStyle ? computedStyle.getPropertyValue("list-style-type").trim() : "";
|
|
1133
|
+
if (Object.prototype.hasOwnProperty.call(COUNTER_STYLE_CHARACTERS, listStyleType)) {
|
|
1134
|
+
const characters = COUNTER_STYLE_CHARACTERS[listStyleType];
|
|
1135
|
+
return transformCharacters({ characters: characters ? characters + MARKER_SUFFIX : "" }, markerStyle || computedStyle, element);
|
|
1136
|
+
}
|
|
1137
|
+
REGEXP_QUOTED_STRING.lastIndex = 0;
|
|
1138
|
+
const match = REGEXP_QUOTED_STRING.exec(listStyleType);
|
|
1139
|
+
if (match && match[0] == listStyleType) {
|
|
1140
|
+
const characters = match[1] === undefined ? match[2] : match[1];
|
|
1141
|
+
return transformCharacters({ characters, unknown: REGEXP_BACKSLASH.test(characters) }, markerStyle || computedStyle, element);
|
|
1142
|
+
}
|
|
1143
|
+
return { characters: "", unknown: true };
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
function transformCharacters(drawnCharacters, computedStyle, element) {
|
|
1147
|
+
const textTransform = computedStyle && computedStyle.getPropertyValue("text-transform");
|
|
1148
|
+
if (!textTransform || textTransform == "none" || !drawnCharacters.characters) {
|
|
1149
|
+
return drawnCharacters;
|
|
1150
|
+
}
|
|
1151
|
+
const sourceCharacters = drawnCharacters.characters;
|
|
1152
|
+
let characters = sourceCharacters, unknown = drawnCharacters.unknown;
|
|
1153
|
+
const langElement = element && element.closest && element.closest("[lang]");
|
|
1154
|
+
const lang = langElement && langElement.getAttribute("lang");
|
|
1155
|
+
textTransform.trim().split(REGEXP_TEXT_TRANSFORM_SEPARATOR).forEach(value => {
|
|
1156
|
+
try {
|
|
1157
|
+
if (value == "uppercase" || value == "capitalize") {
|
|
1158
|
+
characters += sourceCharacters.toUpperCase() + (lang ? sourceCharacters.toLocaleUpperCase(lang) : "");
|
|
1159
|
+
} else if (value == "lowercase") {
|
|
1160
|
+
characters += sourceCharacters.toLowerCase() + (lang ? sourceCharacters.toLocaleLowerCase(lang) : "");
|
|
1161
|
+
} else if (value == "full-width") {
|
|
1162
|
+
characters += getFullWidthCharacters(sourceCharacters);
|
|
1163
|
+
} else {
|
|
1164
|
+
unknown = true;
|
|
1165
|
+
}
|
|
1166
|
+
// eslint-disable-next-line no-unused-vars
|
|
1167
|
+
} catch (error) {
|
|
1168
|
+
unknown = true;
|
|
1169
|
+
}
|
|
1170
|
+
});
|
|
1171
|
+
return { characters, unknown };
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
function getFullWidthCharacters(characters) {
|
|
1175
|
+
let result = "";
|
|
1176
|
+
for (const character of characters) {
|
|
1177
|
+
const codePoint = character.codePointAt(0);
|
|
1178
|
+
if (codePoint == 0x20) {
|
|
1179
|
+
result += FULL_WIDTH_SPACE;
|
|
1180
|
+
} else if (codePoint > 0x20 && codePoint < 0x7F) {
|
|
1181
|
+
result += String.fromCodePoint(codePoint + FULL_WIDTH_OFFSET);
|
|
1182
|
+
} else {
|
|
1183
|
+
result += character;
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
return result;
|
|
1097
1187
|
}
|
|
1098
1188
|
|
|
1099
1189
|
// getElementsInfo iterates the children of the element it is given, so the root is never visited.
|
|
@@ -1105,14 +1195,14 @@ function getElementCharacters(win, element) {
|
|
|
1105
1195
|
function getRootElementUsedFonts(win, element, data) {
|
|
1106
1196
|
ROOT_PSEUDO_ELEMENT_NAMES.forEach(pseudoElementName => {
|
|
1107
1197
|
const computedStyle = getComputedStyle(win, element, pseudoElementName);
|
|
1108
|
-
const drawnCharacters = getPseudoElementCharacters(computedStyle);
|
|
1198
|
+
const drawnCharacters = getPseudoElementCharacters(computedStyle, element);
|
|
1109
1199
|
if (drawnCharacters.characters || drawnCharacters.unknown) {
|
|
1110
1200
|
getUsedFont(computedStyle, data.usedFonts, data.usedFontsCharacters, drawnCharacters);
|
|
1111
1201
|
}
|
|
1112
1202
|
});
|
|
1113
1203
|
}
|
|
1114
1204
|
|
|
1115
|
-
function getPseudoElementCharacters(computedStyle) {
|
|
1205
|
+
function getPseudoElementCharacters(computedStyle, element) {
|
|
1116
1206
|
const content = computedStyle && computedStyle.getPropertyValue("content");
|
|
1117
1207
|
if (!content || content == "none" || content == "normal") {
|
|
1118
1208
|
return { characters: "" };
|
|
@@ -1128,7 +1218,7 @@ function getPseudoElementCharacters(computedStyle) {
|
|
|
1128
1218
|
.replace(REGEXP_QUOTED_STRING, "")
|
|
1129
1219
|
.replace(REGEXP_GENERATED_CONTENT_IMAGE, "")
|
|
1130
1220
|
.trim();
|
|
1131
|
-
return { characters, unknown: Boolean(remainder) || REGEXP_BACKSLASH.test(characters) };
|
|
1221
|
+
return transformCharacters({ characters, unknown: Boolean(remainder) || REGEXP_BACKSLASH.test(characters) }, computedStyle, element);
|
|
1132
1222
|
}
|
|
1133
1223
|
|
|
1134
1224
|
function serializeUsedFontsCharacters(usedFontsCharacters) {
|
|
@@ -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
|
}
|