Skip to content

Commit b63ab55

Browse files
authored
fix: Check assets for url handling (#10746)
If a resource is not found in themes folder check also any assets that are copied for the theme. Fixes #10426
1 parent f1f8046 commit b63ab55

4 files changed

Lines changed: 72 additions & 3 deletions

File tree

‎flow-server/src/main/resources/plugins/theme-loader/theme-loader.js‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ module.exports = function (source, map) {
3434

3535
source = source.replace(urlMatcher, function (match, url, quoteMark, replace, fileUrl, endString) {
3636
let absolutePath = path.resolve(handledResourceFolder, replace, fileUrl);
37-
if (fs.existsSync(absolutePath) && absolutePath.startsWith(themeFolder)) {
38-
const frontendThemeFolder = "themes/" + path.basename(themeFolder);
37+
if ((fs.existsSync(absolutePath) && absolutePath.startsWith(themeFolder)) || assetsContains(fileUrl, themeFolder, logger)) {
38+
// Adding ./ will skip css-loader, which should be done for asset files
39+
const skipLoader = (fs.existsSync(absolutePath) && absolutePath.startsWith(themeFolder)) ? "": "./";
40+
const frontendThemeFolder = skipLoader + "themes/" + path.basename(themeFolder);
3941
logger.debug("Updating url for file", "'" + replace + fileUrl + "'", "to use", "'" + frontendThemeFolder + "/" + fileUrl + "'");
4042
const pathResolved = absolutePath.substring(themeFolder.length).replace(/\\/g, '/');
4143

@@ -52,3 +54,46 @@ module.exports = function (source, map) {
5254

5355
this.callback(null, source, map);
5456
}
57+
58+
function assetsContains(fileUrl, themeFolder, logger) {
59+
const themeProperties = getThemeProperties(themeFolder);
60+
if (!themeProperties) {
61+
logger.debug("No theme properties found.");
62+
return false;
63+
}
64+
const assets = themeProperties['assets'];
65+
if (!assets) {
66+
logger.debug("No defined assets in theme properties");
67+
return false;
68+
}
69+
// Go through each asset module
70+
for (let module of Object.keys(assets)) {
71+
const copyRules = assets[module];
72+
// Go through each copy rule
73+
for (let copyRule of Object.keys(copyRules)) {
74+
// if file starts with copyRule target check if file with path after copy target can be found
75+
if (fileUrl.startsWith(copyRules[copyRule])) {
76+
const targetFile = fileUrl.replace(copyRules[copyRule], "");
77+
const files = require('glob').sync(path.resolve('node_modules/', module, copyRule), {nodir: true});
78+
79+
for (let file of files) {
80+
if (file.endsWith(targetFile))
81+
return true;
82+
}
83+
}
84+
}
85+
}
86+
return false;
87+
}
88+
89+
function getThemeProperties(themeFolder) {
90+
const themePropertyFile = path.resolve(themeFolder, 'theme.json');
91+
if (!fs.existsSync(themePropertyFile)) {
92+
return {};
93+
}
94+
const themePropertyFileAsString = fs.readFileSync(themePropertyFile);
95+
if (themePropertyFileAsString.length === 0) {
96+
return {};
97+
}
98+
return JSON.parse(themePropertyFileAsString);
99+
}

‎flow-tests/test-themes/frontend/themes/app-theme/styles.css‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ body {
6161
display: block;
6262
}
6363

64+
#icon-snowflake {
65+
background-image: url('./fortawesome/icons/snowflake.svg');
66+
height: 1em;
67+
width: 1em;
68+
}
69+
6470
p.global {
6571
color: rgba(255, 255, 0, 1); /* yellow */
6672
font-size: 20px;

‎flow-tests/test-themes/src/main/java/com/vaadin/flow/uitest/ui/theme/ThemeView.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ThemeView extends Div {
3333
public static final String FONTAWESOME_ID = "font-awesome";
3434
public static final String SUB_COMPONENT_ID = "sub-component";
3535
public static final String DICE_ID = "dice";
36+
public static final String CSS_SNOWFLAKE = "icon-snowflake";
3637

3738
public ThemeView() {
3839
final Span textSpan = new Span("This is the theme test view");
@@ -47,6 +48,9 @@ public ThemeView() {
4748
Span octopuss = new Span();
4849
octopuss.setId(OCTOPUSS_ID);
4950

51+
Span cssSnowflake = new Span();
52+
cssSnowflake.setId(CSS_SNOWFLAKE);
53+
5054
Span faText = new Span("This test is FontAwesome.");
5155
faText.setClassName("fas fa-coffee");
5256
faText.setId(FONTAWESOME_ID);
@@ -62,7 +66,7 @@ public ThemeView() {
6266
"url('themes/app-theme/img/dice.jpg')");
6367
diceImageSpan.setId(DICE_ID);
6468

65-
add(textSpan, snowFlake, subCss, butterfly, octopuss, faText,
69+
add(textSpan, snowFlake, subCss, butterfly, octopuss, cssSnowflake, faText,
6670
diceImageSpan);
6771

6872
add(new Div());

‎flow-tests/test-themes/src/test/java/com/vaadin/flow/uitest/ui/theme/ThemeIT.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.vaadin.testbench.TestBenchElement;
2929

3030
import static com.vaadin.flow.uitest.ui.theme.ThemeView.BUTTERFLY_ID;
31+
import static com.vaadin.flow.uitest.ui.theme.ThemeView.CSS_SNOWFLAKE;
3132
import static com.vaadin.flow.uitest.ui.theme.ThemeView.DICE_ID;
3233
import static com.vaadin.flow.uitest.ui.theme.ThemeView.FONTAWESOME_ID;
3334
import static com.vaadin.flow.uitest.ui.theme.ThemeView.MY_LIT_ID;
@@ -71,6 +72,19 @@ public void referenceResourcesOnJavaSideForStyling_stylesAreApplied() {
7172
driver.getPageSource().contains("HTTP ERROR 404 Not Found"));
7273
}
7374

75+
@Test
76+
public void nodeAssetInCss_pathIsSetCorrectly() {
77+
open();
78+
final String resourceUrl = getRootURL()
79+
+ "/path/themes/app-theme/fortawesome/icons/snowflake.svg";
80+
WebElement cssNodeSnowflake = findElement(By.id(CSS_SNOWFLAKE));
81+
final String expectedImgUrl = "url(\"" + resourceUrl + "\")";
82+
Assert.assertEquals(
83+
"Background image has been referenced in styles.css and "
84+
+ "expected to be applied",
85+
expectedImgUrl, cssNodeSnowflake.getCssValue("background-image"));
86+
}
87+
7488
@Test
7589
public void secondTheme_staticFilesNotCopied() {
7690
getDriver().get(getRootURL() + "/path/themes/app-theme/img/bg.jpg");

0 commit comments

Comments
 (0)