single-file-core 1.2.32 → 1.2.34

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/index.js CHANGED
@@ -837,7 +837,13 @@ class Processor {
837
837
  const originalElement = templateElement.content.firstChild;
838
838
  if (originalElement) {
839
839
  if (originalElement.hasAttributes()) {
840
- Array.from(originalElement.attributes).forEach(attribute => placeHolderElement.setAttribute(attribute.name, attribute.value));
840
+ Array.from(originalElement.attributes).forEach(attribute => {
841
+ try {
842
+ placeHolderElement.setAttribute(attribute.name, attribute.value);
843
+ } catch (error) {
844
+ // ignored
845
+ }
846
+ });
841
847
  }
842
848
  originalElement.childNodes.forEach(childNode => placeHolderElement.appendChild(childNode.cloneNode(true)));
843
849
  }
@@ -60,12 +60,12 @@ function process(doc, stylesheets, styles, options) {
60
60
  if (cssRules) {
61
61
  stats.processed += cssRules.size;
62
62
  stats.discarded += cssRules.size;
63
- getFontsInfo(cssRules, fontsInfo);
63
+ getFontsInfo(cssRules, fontsInfo, options);
64
64
  docContent = getRulesTextContent(doc, cssRules, workStyleElement, docContent);
65
65
  }
66
66
  });
67
67
  styles.forEach(declarations => {
68
- const fontFamilyNames = getFontFamilyNames(declarations);
68
+ const fontFamilyNames = getFontFamilyNames(declarations, options);
69
69
  if (fontFamilyNames.length) {
70
70
  fontsInfo.used.push(fontFamilyNames);
71
71
  }
@@ -110,12 +110,12 @@ function process(doc, stylesheets, styles, options) {
110
110
  return stats;
111
111
  }
112
112
 
113
- function getFontsInfo(cssRules, fontsInfo) {
113
+ function getFontsInfo(cssRules, fontsInfo, options) {
114
114
  cssRules.forEach(ruleData => {
115
- if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports") && ruleData.block && ruleData.block.children) {
116
- getFontsInfo(ruleData.block.children, fontsInfo);
115
+ if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports" || ruleData.name == "layer") && ruleData.block && ruleData.block.children) {
116
+ getFontsInfo(ruleData.block.children, fontsInfo, options);
117
117
  } else if (ruleData.type == "Rule") {
118
- const fontFamilyNames = getFontFamilyNames(ruleData.block);
118
+ const fontFamilyNames = getFontFamilyNames(ruleData.block, options);
119
119
  if (fontFamilyNames.length) {
120
120
  fontsInfo.used.push(fontFamilyNames);
121
121
  }
@@ -138,7 +138,7 @@ function filterUnusedFonts(cssRules, declaredFonts, unusedFonts, filteredUsedFon
138
138
  const removedRules = [];
139
139
  for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
140
140
  const ruleData = cssRule.data;
141
- if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports") && ruleData.block && ruleData.block.children) {
141
+ if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports" || ruleData.name == "layer") && ruleData.block && ruleData.block.children) {
142
142
  filterUnusedFonts(ruleData.block.children, declaredFonts, unusedFonts, filteredUsedFonts, docContent);
143
143
  } else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
144
144
  const fontFamily = helper.normalizeFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
@@ -171,8 +171,8 @@ function testUsedFont(ruleData, familyName, declaredFonts, filteredUsedFonts) {
171
171
  const fontWeight = helper.getFontWeight(getDeclarationValue(ruleData.block.children, "font-weight") || "400");
172
172
  const declaredFontsWeights = declaredFonts
173
173
  .filter(fontInfo => fontInfo.fontFamily == familyName && fontInfo.fontStyle == fontStyle)
174
- .map(fontInfo => fontInfo.fontWeight)
175
- .sort((weight1, weight2) => Number.parseInt(weight1, 10) - Number.parseInt(weight2, 10));
174
+ .map(fontInfo => fontInfo.fontWeight.split(" "))
175
+ .sort((weight1, weight2) => Number.parseInt(weight1[0], 10) - Number.parseInt(weight2[0], 10));
176
176
  let usedFontWeights = optionalUsedFonts.map(fontInfo => getUsedFontWeight(fontInfo, fontStyle, declaredFontsWeights));
177
177
  test = testFontweight(fontWeight, usedFontWeights);
178
178
  if (!test) {
@@ -202,8 +202,8 @@ function testUsedFont(ruleData, familyName, declaredFonts, filteredUsedFonts) {
202
202
 
203
203
  function testFontweight(fontWeight, usedFontWeights) {
204
204
  let test;
205
- for (const value of fontWeight.split(/[ ,]/)) {
206
- test = test || usedFontWeights.includes(helper.getFontWeight(helper.removeQuotes(value)));
205
+ for (const value of fontWeight.split(",")) {
206
+ test = test || usedFontWeights.includes(helper.getFontWeight(helper.removeQuotes(value.trim())));
207
207
  }
208
208
  return test;
209
209
  }
@@ -222,7 +222,7 @@ function getDeclarationValue(declarations, propertyName) {
222
222
  }
223
223
  }
224
224
 
225
- function getFontFamilyNames(declarations) {
225
+ function getFontFamilyNames(declarations, options) {
226
226
  let fontFamilyName = declarations.children.filter(node => node.property == "font-family").tail;
227
227
  let fontFamilyNames = [];
228
228
  if (fontFamilyName) {
@@ -238,7 +238,13 @@ function getFontFamilyNames(declarations) {
238
238
  const font = declarations.children.filter(node => node.property == "font").tail;
239
239
  if (font && font.data && font.data.value) {
240
240
  try {
241
- const parsedFont = fontPropertyParser.parse(font.data.value);
241
+ let value = font.data.value;
242
+ let fontFamilyName = cssTree.generate(value);
243
+ const matchedVar = fontFamilyName.match(/^var\((--.*)\)$/);
244
+ if (matchedVar && matchedVar[1]) {
245
+ value = cssTree.parse(globalThis.getComputedStyle(options.doc.body).getPropertyValue(matchedVar[1]), { context: "value" });
246
+ }
247
+ const parsedFont = fontPropertyParser.parse(value);
242
248
  parsedFont.family.forEach(familyName => fontFamilyNames.push(helper.normalizeFontFamily(familyName)));
243
249
  } catch (error) {
244
250
  // ignored
@@ -289,12 +295,12 @@ function parseFamilyNames(fontFamilyNameTokenData, fontFamilyNames) {
289
295
 
290
296
  function getUsedFontWeight(fontInfo, fontStyle, fontWeights) {
291
297
  let foundWeight;
292
- fontWeights = fontWeights.map(weight => String(Number.parseInt(weight, 10)));
298
+ fontWeights = fontWeights.map(weights => weights.map(value => String(Number.parseInt(value, 10))));
293
299
  if (fontInfo[2] == fontStyle) {
294
300
  let fontWeight = Number(fontInfo[1]);
295
301
  if (fontWeights.length > 1) {
296
302
  if (fontWeight >= 400 && fontWeight <= 500) {
297
- foundWeight = fontWeights.find(weight => weight >= fontWeight && weight <= 500);
303
+ foundWeight = fontWeights.find(weights => weights[0] >= fontWeight && weights[weights.length - 1] <= 500);
298
304
  if (!foundWeight) {
299
305
  foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
300
306
  }
@@ -303,13 +309,13 @@ function getUsedFontWeight(fontInfo, fontStyle, fontWeights) {
303
309
  }
304
310
  }
305
311
  if (fontWeight < 400) {
306
- foundWeight = fontWeights.slice().reverse().find(weight => weight <= fontWeight);
312
+ foundWeight = fontWeights.slice().reverse().find(weights => weights[weights.length - 1] <= fontWeight);
307
313
  if (!foundWeight) {
308
314
  foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
309
315
  }
310
316
  }
311
317
  if (fontWeight > 500) {
312
- foundWeight = fontWeights.find(weight => weight >= fontWeight);
318
+ foundWeight = fontWeights.find(weights => weights[0] >= fontWeight);
313
319
  if (!foundWeight) {
314
320
  foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
315
321
  }
@@ -318,21 +324,21 @@ function getUsedFontWeight(fontInfo, fontStyle, fontWeights) {
318
324
  foundWeight = fontWeights[0];
319
325
  }
320
326
  }
321
- return foundWeight;
327
+ return foundWeight ? foundWeight.join(" ") : undefined;
322
328
  }
323
329
 
324
330
  function findDescendingFontWeight(fontWeight, fontWeights) {
325
- return fontWeights.slice().reverse().find(weight => weight < fontWeight);
331
+ return fontWeights.slice().reverse().find(weights => weights[weights.length - 1] < fontWeight);
326
332
  }
327
333
 
328
334
  function findAscendingFontWeight(fontWeight, fontWeights) {
329
- return fontWeights.find(weight => weight > fontWeight);
335
+ return fontWeights.find(weights => weights[0] > fontWeight);
330
336
  }
331
337
 
332
338
  function getRulesTextContent(doc, cssRules, workStylesheet, content) {
333
339
  cssRules.forEach(ruleData => {
334
340
  if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
335
- if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports")) {
341
+ if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports" || ruleData.name == "layer")) {
336
342
  content = getRulesTextContent(doc, ruleData.block.children, workStylesheet, content);
337
343
  } else if (ruleData.type == "Rule") {
338
344
  content = getDeclarationsTextContent(ruleData.block.children, workStylesheet, content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.2.32",
3
+ "version": "1.2.34",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -164,11 +164,6 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
164
164
  }
165
165
  }
166
166
  }));
167
- if (filename.match(REGEXP_MATCH_INDEX) || filename.match(REGEXP_MATCH_FRAMES) || noBlobURL) {
168
- resource.content = await getDataURI(textContent, mimeType);
169
- } else {
170
- resource.content = URL.createObjectURL(new Blob([textContent], { type: mimeType }));
171
- }
172
167
  resource.textContent = textContent;
173
168
  }
174
169
  if (filename.match(REGEXP_MATCH_INDEX)) {
@@ -176,6 +171,11 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
176
171
  resource.textContent = textContent.replace(/<script data-template-shadow-root.*<\/script>/g, "<script data-template-shadow-root src=" + shadowRootScriptURL + "></" + "script>");
177
172
  }
178
173
  }
174
+ if (filename.match(REGEXP_MATCH_INDEX) || filename.match(REGEXP_MATCH_FRAMES) || noBlobURL) {
175
+ resource.content = await getDataURI(textContent, mimeType);
176
+ } else {
177
+ resource.content = URL.createObjectURL(new Blob([textContent], { type: mimeType }));
178
+ }
179
179
  if (filename.match(REGEXP_MATCH_ROOT_INDEX)) {
180
180
  docContent = textContent;
181
181
  url = resource.url;