single-file-core 1.0.58 → 1.0.60

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.
@@ -174,12 +174,13 @@ function getMatchedElementsSelector(doc, selectorInfo, styles, matchedElementsCa
174
174
  }
175
175
 
176
176
  function getFilteredSelector(selector, selectorText) {
177
- const namespaceSeparatorIndex = selectorText.lastIndexOf("|");
178
- if (namespaceSeparatorIndex > -1) {
179
- return selectorText.substring(namespaceSeparatorIndex + 1);
180
- }
181
177
  const removedSelectors = [];
178
+ let namespaceFound;
182
179
  selector = { data: cssTree.parse(cssTree.generate(selector.data), { context: "selector" }) };
180
+ filterNamespace(selector);
181
+ if (namespaceFound) {
182
+ selectorText = cssTree.generate(selector.data).trim();
183
+ }
183
184
  filterPseudoClasses(selector);
184
185
  if (removedSelectors.length) {
185
186
  removedSelectors.forEach(({ parentSelector, selector }) => {
@@ -205,6 +206,18 @@ function getFilteredSelector(selector, selectorText) {
205
206
  }
206
207
  }
207
208
 
209
+ function filterNamespace(selector) {
210
+ if (selector.data.children) {
211
+ for (let childSelector = selector.data.children.head; childSelector; childSelector = childSelector.next) {
212
+ filterNamespace(childSelector);
213
+ }
214
+ }
215
+ if (selector.data.type == "TypeSelector" && selector.data.name.includes("|")) {
216
+ namespaceFound = true;
217
+ selector.data.name = selector.data.name.substring(selector.data.name.lastIndexOf("|") + 1);
218
+ }
219
+ }
220
+
208
221
  function testVendorPseudo(selector) {
209
222
  const name = selector.data.name;
210
223
  return name.startsWith("-") || name.startsWith("\\-");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.0.58",
3
+ "version": "1.0.60",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -1075,6 +1075,7 @@ class Processor {
1075
1075
  if (options.compressCSS) {
1076
1076
  ProcessorHelper.removeSingleLineCssComments(stylesheet);
1077
1077
  }
1078
+ ProcessorHelper.replacePseudoClassDefined(stylesheet);
1078
1079
  stylesheetInfo.stylesheet = stylesheet;
1079
1080
  } else {
1080
1081
  stylesheets.delete(element);
@@ -1534,6 +1535,40 @@ class ProcessorHelper {
1534
1535
  }
1535
1536
  }
1536
1537
 
1538
+ static replacePseudoClassDefined(stylesheet) {
1539
+ const removedSelectors = [];
1540
+ if (stylesheet.children) {
1541
+ for (let cssRule = stylesheet.children.head; cssRule; cssRule = cssRule.next) {
1542
+ const ruleData = cssRule.data;
1543
+ if (ruleData.type == "Rule" && ruleData.prelude && ruleData.prelude.children) {
1544
+ for (let selector = ruleData.prelude.children.head; selector; selector = selector.next) {
1545
+ replacePseudoDefinedSelector(selector, ruleData.prelude);
1546
+ }
1547
+ }
1548
+ }
1549
+ }
1550
+ if (removedSelectors.length) {
1551
+ removedSelectors.forEach(({ parentSelector, selector }) => {
1552
+ if (parentSelector.data.children.size == 0 || !selector.prev || selector.prev.data.type == "Combinator" || selector.prev.data.type == "WhiteSpace") {
1553
+ parentSelector.data.children.replace(selector, cssTree.parse("*", { context: "selector" }).children.head);
1554
+ } else {
1555
+ parentSelector.data.children.remove(selector);
1556
+ }
1557
+ });
1558
+ }
1559
+
1560
+ function replacePseudoDefinedSelector(selector, parentSelector) {
1561
+ if (selector.data.children) {
1562
+ for (let childSelector = selector.data.children.head; childSelector; childSelector = childSelector.next) {
1563
+ replacePseudoDefinedSelector(childSelector, selector);
1564
+ }
1565
+ }
1566
+ if (selector.data.type == "PseudoClassSelector" && selector.data.name == "defined") {
1567
+ removedSelectors.push({ parentSelector, selector });
1568
+ }
1569
+ }
1570
+ }
1571
+
1537
1572
  static async resolveImportURLs(stylesheet, baseURI, options, workStylesheet, importedStyleSheets = new Set()) {
1538
1573
  let importFound;
1539
1574
  ProcessorHelper.resolveStylesheetURLs(stylesheet, baseURI, workStylesheet);
@@ -1560,6 +1595,9 @@ class ProcessorHelper {
1560
1595
  if (mediaQueryListNode) {
1561
1596
  content.data = wrapMediaQuery(content.data, cssTree.generate(mediaQueryListNode));
1562
1597
  }
1598
+
1599
+ content.data = content.data.replace(/:defined/gi, "*");
1600
+
1563
1601
  const importedStylesheet = cssTree.parse(content.data, { context: "stylesheet", parseCustomProperty: true });
1564
1602
  const ancestorStyleSheets = new Set(importedStyleSheets);
1565
1603
  ancestorStyleSheets.add(resourceURL);
@@ -1665,6 +1703,9 @@ class ProcessorHelper {
1665
1703
  if (content.data && content.data.match(/^<!doctype /i)) {
1666
1704
  content.data = "";
1667
1705
  }
1706
+
1707
+ content.data = content.data.replace(/:defined/gi, "*");
1708
+
1668
1709
  let stylesheet = cssTree.parse(content.data, { context: "stylesheet", parseCustomProperty: true });
1669
1710
  const importFound = await ProcessorHelper.resolveImportURLs(stylesheet, resourceURL, options, workStylesheet);
1670
1711
  if (importFound) {