single-file-core 1.5.26 → 1.5.27

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
@@ -326,7 +326,7 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
326
326
  function getStylesheetsContent(styleSheets, adoptedStyleSheetsCache = new Map()) {
327
327
  if (styleSheets) {
328
328
  const result = [];
329
- for (const styleSheet of styleSheets) {
329
+ for (const styleSheet of Array.from(styleSheets)) {
330
330
  if (adoptedStyleSheetsCache.has(styleSheet)) {
331
331
  result.push(adoptedStyleSheetsCache.get(styleSheet));
332
332
  } else {
@@ -113,21 +113,21 @@ function getProcessorHelperClass(utilInstance) {
113
113
  const stylesheetInfo = stylesheets.get(styleElement);
114
114
  if (stylesheetInfo) {
115
115
  stylesheets.delete(styleElement);
116
- const styleSheetRefIndex = options.inlineStylesheetsRefs.get(styleElement);
117
- if (styleSheetRefIndex === undefined) {
116
+ const stylesheetRefIndex = options.inlineStylesheetsRefs.get(styleElement);
117
+ if (stylesheetRefIndex === undefined) {
118
118
  styleElement.textContent = this.generateStylesheetContent(stylesheetInfo.stylesheet, options);
119
119
  } else if (options.groupDuplicateStylesheets) {
120
- if (!doc.querySelector("style[" + DUPLICATE_STYLESHEET_ATTRIBUTE_NAME + "=\"" + styleSheetRefIndex + "\"]")) {
120
+ if (!doc.querySelector("style[" + DUPLICATE_STYLESHEET_ATTRIBUTE_NAME + "=\"" + stylesheetRefIndex + "\"]")) {
121
121
  const styleElement = doc.createElement("style");
122
- styleElement.textContent = options.inlineStylesheets.get(styleSheetRefIndex);
122
+ styleElement.textContent = options.inlineStylesheets.get(stylesheetRefIndex);
123
123
  styleElement.setAttribute("media", "not all");
124
- styleElement.setAttribute(DUPLICATE_STYLESHEET_ATTRIBUTE_NAME, styleSheetRefIndex);
124
+ styleElement.setAttribute(DUPLICATE_STYLESHEET_ATTRIBUTE_NAME, stylesheetRefIndex);
125
125
  doc.head.appendChild(styleElement);
126
126
  }
127
127
  styleElement.textContent = "/* */";
128
- styleElement.setAttribute("onload", "this.textContent=document.querySelector('style[" + DUPLICATE_STYLESHEET_ATTRIBUTE_NAME + "=\"" + styleSheetRefIndex + "\"]').textContent;this.removeAttribute(\"onload\")");
128
+ styleElement.setAttribute("onload", "this.textContent=document.querySelector('style[" + DUPLICATE_STYLESHEET_ATTRIBUTE_NAME + "=\"" + stylesheetRefIndex + "\"]').textContent;this.removeAttribute(\"onload\")");
129
129
  } else {
130
- styleElement.textContent = options.inlineStylesheets.get(styleSheetRefIndex);
130
+ styleElement.textContent = options.inlineStylesheets.get(stylesheetRefIndex);
131
131
  }
132
132
  if (stylesheetInfo.mediaText) {
133
133
  styleElement.media = stylesheetInfo.mediaText;
@@ -76,27 +76,38 @@ function getProcessorHelperClass(utilInstance) {
76
76
  }
77
77
 
78
78
  async resolveStylesheetElement(element, stylesheetInfo, stylesheets, baseURI, options, workStyleElement, resources) {
79
- if (!options.inlineStylesheetsRefs.has(element)) {
80
- if (!options.blockStylesheets) {
81
- stylesheets.set({ element }, stylesheetInfo);
79
+ if (!options.blockStylesheets) {
80
+ stylesheets.set({ element }, stylesheetInfo);
81
+ if (!options.inlineStylesheetsRefs.has(element)) {
82
82
  if (element.tagName.toUpperCase() == "LINK") {
83
83
  await this.resolveLinkStylesheetURLs(stylesheetInfo, element, element.href, baseURI, options, workStyleElement, resources, stylesheets);
84
84
  } else {
85
85
  stylesheetInfo.stylesheet = cssTree.parse(element.textContent, { context: "stylesheet", parseCustomProperty: true });
86
86
  await this.resolveImportURLs(stylesheetInfo, baseURI, options, workStyleElement, resources, stylesheets);
87
87
  }
88
+ }
89
+ } else {
90
+ if (element.tagName.toUpperCase() == "LINK") {
91
+ element.href = util.EMPTY_RESOURCE;
88
92
  } else {
89
- if (element.tagName.toUpperCase() == "LINK") {
90
- element.href = util.EMPTY_RESOURCE;
91
- } else {
92
- element.textContent = "";
93
- }
93
+ element.textContent = "";
94
94
  }
95
95
  }
96
96
  }
97
97
 
98
98
  replaceStylesheets(doc, stylesheets, options, resources) {
99
99
  const entries = Array.from(stylesheets).reverse();
100
+ const linkElements = new Map();
101
+ Array.from(new Set(options.inlineStylesheetsRefs.values())).forEach(stylesheetRefIndex => {
102
+ const linkElement = doc.createElement("link");
103
+ linkElement.setAttribute("rel", "stylesheet");
104
+ linkElement.setAttribute("type", "text/css");
105
+ const name = "stylesheet_" + resources.stylesheets.size + ".css";
106
+ linkElement.setAttribute("href", name);
107
+ const content = options.inlineStylesheets.get(stylesheetRefIndex);
108
+ resources.stylesheets.set(resources.stylesheets.size, { name, content });
109
+ linkElements.set(stylesheetRefIndex, linkElement);
110
+ });
100
111
  for (const [key, stylesheetInfo] of entries) {
101
112
  if (key.urlNode) {
102
113
  const name = "stylesheet_" + resources.stylesheets.size + ".css";
@@ -105,27 +116,29 @@ function getProcessorHelperClass(utilInstance) {
105
116
  } else {
106
117
  key.urlNode.value = name;
107
118
  }
108
- resources.stylesheets.set(resources.stylesheets.size, { name, stylesheet: stylesheetInfo.stylesheet });
119
+ resources.stylesheets.set(resources.stylesheets.size, { name, stylesheet: stylesheetInfo.stylesheet, url: stylesheetInfo.url });
120
+ } else if (key.element.tagName.toUpperCase() == "LINK") {
121
+ const linkElement = key.element;
122
+ const name = "stylesheet_" + resources.stylesheets.size + ".css";
123
+ linkElement.setAttribute("href", name);
124
+ resources.stylesheets.set(resources.stylesheets.size, { name, stylesheet: stylesheetInfo.stylesheet, url: stylesheetInfo.url });
109
125
  } else {
110
- if (key.element.tagName.toUpperCase() == "LINK") {
111
- const linkElement = key.element;
112
- const name = "stylesheet_" + resources.stylesheets.size + ".css";
113
- linkElement.setAttribute("href", name);
114
- resources.stylesheets.set(resources.stylesheets.size, { name, stylesheet: stylesheetInfo.stylesheet });
126
+ const styleElement = key.element;
127
+ const stylesheetRefIndex = options.inlineStylesheetsRefs.get(styleElement);
128
+ if (stylesheetRefIndex === undefined) {
129
+ styleElement.textContent = this.generateStylesheetContent(stylesheetInfo.stylesheet, options);
115
130
  } else {
116
- const styleElement = key.element;
117
- const styleSheetRefIndex = options.inlineStylesheetsRefs.get(styleElement);
118
- if (styleSheetRefIndex === undefined) {
119
- styleElement.textContent = this.generateStylesheetContent(stylesheetInfo.stylesheet, options);
120
- } else {
121
- styleElement.textContent = options.inlineStylesheets.get(styleSheetRefIndex);
122
- }
131
+ const linkElement = linkElements.get(stylesheetRefIndex).cloneNode(true);
132
+ styleElement.replaceWith(linkElement);
133
+ key.element = linkElement;
123
134
  }
124
135
  }
125
136
  }
126
137
  for (const [, stylesheetResource] of resources.stylesheets) {
127
- stylesheetResource.content = this.generateStylesheetContent(stylesheetResource.stylesheet, options);
128
- stylesheetResource.stylesheet = null;
138
+ if (stylesheetResource.stylesheet) {
139
+ stylesheetResource.content = this.generateStylesheetContent(stylesheetResource.stylesheet, options);
140
+ stylesheetResource.stylesheet = null;
141
+ }
129
142
  }
130
143
  }
131
144
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.5.26",
3
+ "version": "1.5.27",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -51,6 +51,7 @@
51
51
  const DELETE_FONT_EVENT = "single-file-delete-font";
52
52
  const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
53
53
  const NEW_WORKLET_EVENT = "single-file-new-worklet";
54
+ const BOOTSTRAP_EVENT = "single-file-bootstrap";
54
55
  const FONT_STYLE_PROPERTIES = {
55
56
  family: "font-family",
56
57
  style: "font-style",
@@ -149,6 +150,11 @@
149
150
  document.dispatchEvent(new CustomEvent(FETCH_RESPONSE_EVENT, { detail }));
150
151
  });
151
152
  document.addEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener);
153
+ document.addEventListener(BOOTSTRAP_EVENT, event => {
154
+ if (globalThis.bootstrap && event.detail.data) {
155
+ globalThis.bootstrap(event.detail.data);
156
+ }
157
+ });
152
158
  }
153
159
 
154
160
  function loadDeferredImagesStart(keepZoomLevel) {