Skip to content

Commit 93a5b95

Browse files
authored
fix: STRF-11740 Rewrote logic for retrieving stylesheets (#94)
1 parent 8980b41 commit 93a5b95

2 files changed

Lines changed: 35 additions & 30 deletions

File tree

lib/StylesheetsFileResolver.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,44 @@ class StylesheetsFileResolver {
1818
const cssFiles = [];
1919
for await (const file of files) {
2020
const content = await fs.promises.readFile(file, { encoding: 'utf-8' });
21-
const result = content.matchAll(STYLESHEET_REGEXP);
22-
if (result) {
23-
for (const item of result) {
24-
// remove quotes
25-
const filePath = item[1].slice(1, -1);
26-
const fileName = this.tryToResolveCssFileLocation(filePath);
27-
if (
28-
fileName &&
29-
!this.isStyleSheetAComment(content, filePath) &&
30-
!cssFiles.includes(fileName)
31-
) {
32-
cssFiles.push(fileName);
33-
}
34-
}
35-
}
21+
const currentCssFiles = this.getStylesheets(content);
22+
cssFiles.push(...currentCssFiles);
3623
}
3724

3825
return cssFiles;
3926
}
4027

41-
isStyleSheetAComment(content, cssFilePath) {
28+
extractFileNameFromMatchedResult(item) {
29+
// remove quotes
30+
const filePath = item[1].slice(1, -1);
31+
const fileName = this.tryToResolveCssFileLocation(filePath);
32+
return fileName;
33+
}
34+
35+
getStylesheets(content) {
36+
const self = this;
37+
const stylesheets = [];
4238
const $ = cheerio.load(content);
43-
const comments = $('*')
39+
// ignore non-text nodes (like comments, etc.)
40+
const stylesheetTexts = $('*')
4441
.contents()
4542
.filter(function () {
46-
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node.comment_node
47-
return this.nodeType === 8;
43+
return this.type === "text" && this.data && this.data.includes('stylesheet');
44+
})
45+
.map(function() {
46+
return $(this).text();
4847
});
49-
for (const comment of comments) {
50-
const { data } = comment;
51-
if (data && data.includes('stylesheet') && data.includes(cssFilePath) && !this.alreadyExlcudedComments.includes(data)) {
52-
this.alreadyExlcudedComments.push(data);
53-
return true;
48+
// extract stylesheet paths from the text nodes and add them to the array
49+
for (const node of stylesheetTexts) {
50+
const result = node.matchAll(STYLESHEET_REGEXP);
51+
if (result) {
52+
for (const item of result) {
53+
const extracted = self.extractFileNameFromMatchedResult(item);
54+
stylesheets.push(extracted);
55+
}
5456
}
5557
}
56-
57-
return false;
58+
return stylesheets;
5859
}
5960

6061
// returns relative path starting from root scss/css folder

test/mocks/themes/invalid/templates/pages/page2.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
<head>
44
<title>page2.html</title>
55
{{head.scripts}}
6+
<!--{{{stylesheet '/assets/css/test.css' rel="stylesheet"}}}-->
7+
<!--{{stylesheet '/assets/css/test.css' }}-->
8+
<!--{{ stylesheet "assets/css/commented.css" }} -->
9+
610
{{ stylesheet "assets/css/test.css" }}
7-
/* {{ stylesheet "assets/css/test.css" }} */
8-
/* {{ stylesheet "assets/css/test.css" }} */
9-
/* {{ stylesheet "assets/css/test.css" }} */
10-
/* {{ stylesheet "assets/css/commented.css" }} */
11+
<!-- {{ stylesheet "assets/css/test3.css" }}
12+
<style>
13+
14+
</style> -->
1115
</head>
1216
<body>
1317
<h1>{{theme_settings.customizable_title}}</h1>

0 commit comments

Comments
 (0)