single-file-core 1.3.6 → 1.3.8

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.
@@ -91,7 +91,7 @@ function getProcessorHelperClass(utilInstance) {
91
91
  }
92
92
 
93
93
  replaceStylesheets(doc, stylesheets, options, resources) {
94
- const entries = Array.from(stylesheets);
94
+ const entries = Array.from(stylesheets).reverse();
95
95
  for (const [key, stylesheetInfo] of entries) {
96
96
  if (key.urlNode) {
97
97
  const name = "stylesheet_" + resources.stylesheets.size + ".css";
@@ -100,19 +100,23 @@ function getProcessorHelperClass(utilInstance) {
100
100
  } else {
101
101
  key.urlNode.value = name;
102
102
  }
103
- resources.stylesheets.set(resources.stylesheets.size, { name, content: this.generateStylesheetContent(stylesheetInfo.stylesheet, options), url: stylesheetInfo.url });
103
+ resources.stylesheets.set(resources.stylesheets.size, { name, stylesheet: stylesheetInfo.stylesheet });
104
104
  } else {
105
105
  if (key.element.tagName.toUpperCase() == "LINK") {
106
106
  const linkElement = key.element;
107
107
  const name = "stylesheet_" + resources.stylesheets.size + ".css";
108
108
  linkElement.setAttribute("href", name);
109
- resources.stylesheets.set(resources.stylesheets.size, { name, content: this.generateStylesheetContent(stylesheetInfo.stylesheet, options), url: stylesheetInfo.url });
109
+ resources.stylesheets.set(resources.stylesheets.size, { name, stylesheet: stylesheetInfo.stylesheet });
110
110
  } else {
111
111
  const styleElement = key.element;
112
112
  styleElement.textContent = this.generateStylesheetContent(stylesheetInfo.stylesheet, options);
113
113
  }
114
114
  }
115
115
  }
116
+ for (const [, stylesheetResource] of resources.stylesheets) {
117
+ stylesheetResource.content = this.generateStylesheetContent(stylesheetResource.stylesheet, options);
118
+ stylesheetResource.stylesheet = null;
119
+ }
116
120
  }
117
121
 
118
122
  async resolveImportURLs(stylesheetInfo, baseURI, options, workStylesheet, resources, stylesheets) {
@@ -40,16 +40,16 @@ class MatchedRules {
40
40
  doc.body.appendChild(workStyleSheet);
41
41
  const workStyleElement = doc.createElement("span");
42
42
  doc.body.appendChild(workStyleElement);
43
- stylesheets.forEach(stylesheetInfo => {
44
- if (!stylesheetInfo.scoped) {
43
+ stylesheets.forEach((stylesheetInfo, key) => {
44
+ if (!stylesheetInfo.scoped && !key.urlNode) {
45
45
  const cssRules = stylesheetInfo.stylesheet.children;
46
46
  if (cssRules) {
47
47
  if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != MEDIA_ALL) {
48
48
  const mediaInfo = createMediaInfo(stylesheetInfo.mediaText);
49
49
  this.mediaAllInfo.medias.set("style-" + sheetIndex + "-" + stylesheetInfo.mediaText, mediaInfo);
50
- getMatchedElementsRules(doc, cssRules, mediaInfo, sheetIndex, styles, matchedElementsCache, workStyleSheet);
50
+ getMatchedElementsRules(doc, cssRules, stylesheets, mediaInfo, sheetIndex, styles, matchedElementsCache, workStyleSheet);
51
51
  } else {
52
- getMatchedElementsRules(doc, cssRules, this.mediaAllInfo, sheetIndex, styles, matchedElementsCache, workStyleSheet);
52
+ getMatchedElementsRules(doc, cssRules, stylesheets, this.mediaAllInfo, sheetIndex, styles, matchedElementsCache, workStyleSheet);
53
53
  }
54
54
  }
55
55
  }
@@ -88,33 +88,44 @@ function getMediaAllInfo(doc, stylesheets, styles) {
88
88
  }
89
89
 
90
90
  function createMediaInfo(media) {
91
- const mediaInfo = { media: media, elements: new Map(), medias: new Map(), rules: new Map(), pseudoRules: new Map() };
91
+ const mediaInfo = {
92
+ media: media,
93
+ elements: new Map(),
94
+ medias: new Map(),
95
+ rules: new Map(),
96
+ pseudoRules: new Map()
97
+ };
92
98
  if (media == MEDIA_ALL) {
93
99
  mediaInfo.matchedStyles = new Map();
94
100
  }
95
101
  return mediaInfo;
96
102
  }
97
103
 
98
- function getMatchedElementsRules(doc, cssRules, mediaInfo, sheetIndex, styles, matchedElementsCache, workStylesheet) {
99
- let mediaIndex = 0;
100
- let ruleIndex = 0;
104
+ function getMatchedElementsRules(doc, cssRules, stylesheets, mediaInfo, sheetIndex, styles, matchedElementsCache, workStylesheet, indexes = {
105
+ mediaIndex: 0, ruleIndex: 0
106
+ }) {
101
107
  let startTime;
102
108
  if (DEBUG && cssRules.length > 1) {
103
109
  startTime = Date.now();
104
110
  log(" -- STARTED getMatchedElementsRules", " index =", sheetIndex, "rules.length =", cssRules.length);
105
111
  }
106
112
  cssRules.forEach(ruleData => {
107
- if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
113
+ if (ruleData.type == "Atrule" && ruleData.name == "import") {
114
+ const stylesheetEntry = Array.from(stylesheets.entries()).find(([key]) => key.urlNode == ruleData.prelude.children.head.data);
115
+ const stylesheetInfo = stylesheetEntry[1];
116
+ ruleData.importedChildren = stylesheetInfo.stylesheet.children;
117
+ getMatchedElementsRules(doc, ruleData.importedChildren, stylesheets, mediaInfo, sheetIndex, styles, matchedElementsCache, workStylesheet, indexes);
118
+ } else if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
108
119
  if (ruleData.type == "Atrule" && ruleData.name == "media") {
109
120
  const mediaText = cssTree.generate(ruleData.prelude);
110
121
  const ruleMediaInfo = createMediaInfo(mediaText);
111
- mediaInfo.medias.set("rule-" + sheetIndex + "-" + mediaIndex + "-" + mediaText, ruleMediaInfo);
112
- mediaIndex++;
113
- getMatchedElementsRules(doc, ruleData.block.children, ruleMediaInfo, sheetIndex, styles, matchedElementsCache, workStylesheet);
122
+ mediaInfo.medias.set("rule-" + sheetIndex + "-" + indexes.mediaIndex + "-" + mediaText, ruleMediaInfo);
123
+ getMatchedElementsRules(doc, ruleData.block.children, stylesheets, ruleMediaInfo, sheetIndex, styles, matchedElementsCache, workStylesheet);
124
+ indexes.mediaIndex++;
114
125
  } else if (ruleData.type == "Rule") {
115
126
  const selectors = ruleData.prelude.children.toArray();
116
127
  const selectorsText = ruleData.prelude.children.toArray().map(selector => cssTree.generate(selector));
117
- const ruleInfo = { ruleData, mediaInfo, ruleIndex, sheetIndex, matchedSelectors: new Set(), declarations: new Set(), selectors, selectorsText };
128
+ const ruleInfo = { ruleData, mediaInfo, ruleIndex: indexes.ruleIndex, sheetIndex, matchedSelectors: new Set(), declarations: new Set(), selectors, selectorsText };
118
129
  if (!invalidSelector(selectorsText.join(","), workStylesheet) || selectorsText.find(selectorText => selectorText.includes("|"))) {
119
130
  for (let selector = ruleData.prelude.children.head, selectorIndex = 0; selector; selector = selector.next, selectorIndex++) {
120
131
  const selectorText = selectorsText[selectorIndex];
@@ -122,7 +133,7 @@ function getMatchedElementsRules(doc, cssRules, mediaInfo, sheetIndex, styles, m
122
133
  getMatchedElementsSelector(doc, selectorInfo, styles, matchedElementsCache);
123
134
  }
124
135
  }
125
- ruleIndex++;
136
+ indexes.ruleIndex++;
126
137
  }
127
138
  }
128
139
  });
@@ -178,7 +189,7 @@ function getFilteredSelector(selector, selectorText) {
178
189
  let namespaceFound;
179
190
  selector = { data: cssTree.parse(cssTree.generate(selector.data), { context: "selector" }) };
180
191
  filterNamespace(selector);
181
- if (namespaceFound) {
192
+ if (namespaceFound) {
182
193
  selectorText = cssTree.generate(selector.data).trim();
183
194
  }
184
195
  filterPseudoClasses(selector);
@@ -32,8 +32,8 @@ export {
32
32
  function process(stylesheets, styles, mediaAllInfo) {
33
33
  const stats = { processed: 0, discarded: 0 };
34
34
  let sheetIndex = 0;
35
- stylesheets.forEach(stylesheetInfo => {
36
- if (!stylesheetInfo.scoped) {
35
+ stylesheets.forEach((stylesheetInfo, key) => {
36
+ if (!stylesheetInfo.scoped && !key.urlNode) {
37
37
  const cssRules = stylesheetInfo.stylesheet.children;
38
38
  if (cssRules) {
39
39
  stats.processed += cssRules.size;
@@ -62,8 +62,8 @@ function process(stylesheets, styles, mediaAllInfo) {
62
62
  return stats;
63
63
  }
64
64
 
65
- function processRules(cssRules, sheetIndex, mediaInfo) {
66
- let mediaRuleIndex = 0, startTime;
65
+ function processRules(cssRules, sheetIndex, mediaInfo, indexes = { mediaRuleIndex: 0 }) {
66
+ let startTime;
67
67
  if (DEBUG && cssRules.size > 1) {
68
68
  startTime = Date.now();
69
69
  log(" -- STARTED processRules", "rules.length =", cssRules.size);
@@ -71,14 +71,15 @@ function processRules(cssRules, sheetIndex, mediaInfo) {
71
71
  const removedCssRules = [];
72
72
  for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
73
73
  const ruleData = cssRule.data;
74
- if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
74
+ if (ruleData.type == "Atrule" && ruleData.name == "import") {
75
+ if (ruleData.importedChildren) {
76
+ processRules(ruleData.importedChildren, sheetIndex, mediaInfo, indexes);
77
+ }
78
+ } else if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
75
79
  if (ruleData.type == "Atrule" && ruleData.name == "media") {
76
80
  const mediaText = cssTree.generate(ruleData.prelude);
77
- processRules(ruleData.block.children, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + mediaText));
78
- if (!ruleData.prelude.children.size || !ruleData.block.children.size) {
79
- removedCssRules.push(cssRule);
80
- }
81
- mediaRuleIndex++;
81
+ processRules(ruleData.block.children, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + indexes.mediaRuleIndex + "-" + mediaText));
82
+ indexes.mediaRuleIndex++;
82
83
  } else if (ruleData.type == "Rule") {
83
84
  const ruleInfo = mediaInfo.rules.get(ruleData);
84
85
  const pseudoSelectors = mediaInfo.pseudoRules.get(ruleData);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.3.6",
3
+ "version": "1.3.8",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -383,7 +383,7 @@
383
383
  event.stopPropagation();
384
384
  if (shadowRoot) {
385
385
  shadowRoot.addEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener, {});
386
- shadowRoot.addEventListener(UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, () => shadowRoot.removeEventListener(getAdoptedStylesheetsListener), { once: true });
386
+ shadowRoot.addEventListener(UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, () => shadowRoot.removeEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener), { once: true });
387
387
  const adoptedStyleSheets = Array.from(shadowRoot.adoptedStyleSheets).map(stylesheet => Array.from(stylesheet.cssRules).map(cssRule => cssRule.cssText).join("\n"));
388
388
  if (adoptedStyleSheets.length) {
389
389
  event.target.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, { detail: { adoptedStyleSheets } }));