single-file-core 1.6.37 → 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 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
- getUsedFont(getComputedStyle(win, element, "::marker"), data.usedFonts);
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);
@@ -1011,12 +1036,6 @@ function getResourcesInfo(win, doc, element, options, data, elementHidden, compu
1011
1036
  }
1012
1037
  }
1013
1038
  }
1014
- if (tagName == "IFRAME") {
1015
- if (elementHidden && options.removeHiddenElements) {
1016
- element.setAttribute(HIDDEN_FRAME_ATTRIBUTE_NAME, "");
1017
- data.markedElements.push(element);
1018
- }
1019
- }
1020
1039
  if (tagName == "INPUT") {
1021
1040
  if (element.type != "password") {
1022
1041
  element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
@@ -1082,7 +1101,7 @@ function addUsedFontCharacters(usedFontsCharacters, fontFamilyName, fontStyle, d
1082
1101
  }
1083
1102
  }
1084
1103
 
1085
- function getElementCharacters(win, element) {
1104
+ function getElementCharacters(win, element, computedStyle) {
1086
1105
  let characters = "";
1087
1106
  const tagName = element.tagName && element.tagName.toUpperCase();
1088
1107
  if (tagName == "INPUT" || tagName == "TEXTAREA" || tagName == "BUTTON") {
@@ -1099,7 +1118,72 @@ function getElementCharacters(win, element) {
1099
1118
  }
1100
1119
  });
1101
1120
  }
1102
- 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;
1103
1187
  }
1104
1188
 
1105
1189
  // getElementsInfo iterates the children of the element it is given, so the root is never visited.
@@ -1111,14 +1195,14 @@ function getElementCharacters(win, element) {
1111
1195
  function getRootElementUsedFonts(win, element, data) {
1112
1196
  ROOT_PSEUDO_ELEMENT_NAMES.forEach(pseudoElementName => {
1113
1197
  const computedStyle = getComputedStyle(win, element, pseudoElementName);
1114
- const drawnCharacters = getPseudoElementCharacters(computedStyle);
1198
+ const drawnCharacters = getPseudoElementCharacters(computedStyle, element);
1115
1199
  if (drawnCharacters.characters || drawnCharacters.unknown) {
1116
1200
  getUsedFont(computedStyle, data.usedFonts, data.usedFontsCharacters, drawnCharacters);
1117
1201
  }
1118
1202
  });
1119
1203
  }
1120
1204
 
1121
- function getPseudoElementCharacters(computedStyle) {
1205
+ function getPseudoElementCharacters(computedStyle, element) {
1122
1206
  const content = computedStyle && computedStyle.getPropertyValue("content");
1123
1207
  if (!content || content == "none" || content == "normal") {
1124
1208
  return { characters: "" };
@@ -1134,7 +1218,7 @@ function getPseudoElementCharacters(computedStyle) {
1134
1218
  .replace(REGEXP_QUOTED_STRING, "")
1135
1219
  .replace(REGEXP_GENERATED_CONTENT_IMAGE, "")
1136
1220
  .trim();
1137
- return { characters, unknown: Boolean(remainder) || REGEXP_BACKSLASH.test(characters) };
1221
+ return transformCharacters({ characters, unknown: Boolean(remainder) || REGEXP_BACKSLASH.test(characters) }, computedStyle, element);
1138
1222
  }
1139
1223
 
1140
1224
  function serializeUsedFontsCharacters(usedFontsCharacters) {
@@ -1198,8 +1282,7 @@ function testHiddenElement(element, computedStyle) {
1198
1282
  const display = computedStyle.getPropertyValue("display");
1199
1283
  const opacity = computedStyle.getPropertyValue("opacity");
1200
1284
  const visibility = computedStyle.getPropertyValue("visibility");
1201
- const tagName = element.tagName && element.tagName.toUpperCase();
1202
- hidden = display == "none" || (visibility == "hidden" && tagName == "IFRAME");
1285
+ hidden = display == "none";
1203
1286
  if (!hidden && (opacity == "0" || visibility == "hidden") && element.getBoundingClientRect) {
1204
1287
  const boundingRect = element.getBoundingClientRect();
1205
1288
  hidden = !boundingRect.width && !boundingRect.height;
package/core/index.js CHANGED
@@ -1770,6 +1770,8 @@ class Processor {
1770
1770
  this.stats.add("discarded", "frames", 1);
1771
1771
  }
1772
1772
  }
1773
+ } else if (frameElement.getAttribute(util.HIDDEN_FRAME_ATTRIBUTE_NAME) == "") {
1774
+ this.stats.add("discarded", "frames", 1);
1773
1775
  }
1774
1776
  }));
1775
1777
  capturedFrames.forEach(capturedFrame => {
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.6.37",
3
+ "version": "1.6.39",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -26,6 +26,7 @@ import {
26
26
  ON_BEFORE_CAPTURE_EVENT_NAME,
27
27
  ON_AFTER_CAPTURE_EVENT_NAME,
28
28
  WIN_ID_ATTRIBUTE_NAME,
29
+ HIDDEN_FRAME_ATTRIBUTE_NAME,
29
30
  WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
30
31
  MESSAGE_PREFIX,
31
32
  preProcessDoc,
@@ -39,6 +40,7 @@ const helper = {
39
40
  ON_BEFORE_CAPTURE_EVENT_NAME,
40
41
  ON_AFTER_CAPTURE_EVENT_NAME,
41
42
  WIN_ID_ATTRIBUTE_NAME,
43
+ HIDDEN_FRAME_ATTRIBUTE_NAME,
42
44
  WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
43
45
  preProcessDoc,
44
46
  serialize,
@@ -303,6 +305,7 @@ function initResponse(message) {
303
305
  }
304
306
  function processFrames(doc, options, parentWindowId, sessionId, waitForFrames) {
305
307
  const frameElements = getFrames(doc);
308
+ markHiddenFrames(frameElements, options);
306
309
  processFramesAsync(doc, frameElements, options, parentWindowId, sessionId, waitForFrames);
307
310
  if (frameElements.length) {
308
311
  processFramesSync(doc, frameElements, options, parentWindowId, sessionId, waitForFrames);
@@ -319,12 +322,18 @@ function processFramesAsync(doc, frameElements, options, parentWindowId, session
319
322
  sessions.set(sessionId, { requestTimeouts });
320
323
  }
321
324
  frameElements.forEach((frameElement, frameIndex) => {
325
+ if (isHiddenFrame(frameElement)) {
326
+ return;
327
+ }
322
328
  const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
323
329
  frameElement.setAttribute(helper.WIN_ID_ATTRIBUTE_NAME, windowId);
324
330
  frames.push({ windowId });
325
331
  });
326
332
  sendInitResponse({ frames, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
327
333
  frameElements.forEach((frameElement, frameIndex) => {
334
+ if (isHiddenFrame(frameElement)) {
335
+ return;
336
+ }
328
337
  const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
329
338
  try {
330
339
  sendMessage(frameElement.contentWindow, { method: INIT_REQUEST_MESSAGE, windowId, sessionId, options: RUNTIME_MESSAGING ? undefined : options, scrolling: frameElement.scrolling, waitForFrames });
@@ -341,6 +350,9 @@ function processFramesAsync(doc, frameElements, options, parentWindowId, session
341
350
  function processFramesSync(doc, frameElements, options, parentWindowId, sessionId, waitForFrames) {
342
351
  const frames = [];
343
352
  frameElements.forEach((frameElement, frameIndex) => {
353
+ if (isHiddenFrame(frameElement)) {
354
+ return;
355
+ }
344
356
  const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
345
357
  let frameDoc, frameWindow;
346
358
  try {
@@ -430,6 +442,7 @@ function cleanupFrames(frameElements, parentWindowId, sessionId) {
430
442
  frameElements.forEach((frameElement, frameIndex) => {
431
443
  const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
432
444
  frameElement.removeAttribute(helper.WIN_ID_ATTRIBUTE_NAME);
445
+ frameElement.removeAttribute(helper.HIDDEN_FRAME_ATTRIBUTE_NAME);
433
446
  try {
434
447
  sendMessage(frameElement.contentWindow, { method: CLEANUP_REQUEST_MESSAGE, windowId, sessionId });
435
448
  // eslint-disable-next-line no-unused-vars
@@ -519,4 +532,39 @@ function getFrames(document) {
519
532
  }
520
533
  });
521
534
  return frames;
535
+ }
536
+
537
+ // hidden frames are marked and left out of the tree: their content is dropped anyway when hidden
538
+ // elements are removed, and each one would otherwise cost up to the ack timeout; the frames keep
539
+ // their index in the list, which is what the cleanup numbers them by
540
+ function markHiddenFrames(frameElements, options) {
541
+ frameElements.forEach(frameElement => {
542
+ if (options.removeHiddenElements && testHiddenFrame(frameElement)) {
543
+ frameElement.setAttribute(helper.HIDDEN_FRAME_ATTRIBUTE_NAME, "");
544
+ } else {
545
+ frameElement.removeAttribute(helper.HIDDEN_FRAME_ATTRIBUTE_NAME);
546
+ }
547
+ });
548
+ }
549
+
550
+ function isHiddenFrame(frameElement) {
551
+ return frameElement.getAttribute(helper.HIDDEN_FRAME_ATTRIBUTE_NAME) == "";
552
+ }
553
+
554
+ // a frame is a replaced element: nothing in it can overflow its box, and its document cannot undo
555
+ // the visibility set on the element, so a frame with no box or not painted shows nothing, whatever it
556
+ // holds; an opacity of 0 is left alone, it is how an embed fades in once loaded
557
+ function testHiddenFrame(frameElement) {
558
+ try {
559
+ const win = frameElement.ownerDocument.defaultView;
560
+ const computedStyle = win.getComputedStyle(frameElement);
561
+ if (computedStyle.getPropertyValue("display") == "none" || computedStyle.getPropertyValue("visibility") == "hidden") {
562
+ return true;
563
+ }
564
+ const boundingRect = frameElement.getBoundingClientRect();
565
+ return !boundingRect.width && !boundingRect.height;
566
+ // eslint-disable-next-line no-unused-vars
567
+ } catch (error) {
568
+ return false;
569
+ }
522
570
  }