Skip to content

Commit f0890b4

Browse files
authored
fix: prevent embedded styles to leak main document (#19274)
When using an exported a themed Flow web-component, Lumo style may leak the embedding document, causing invalid CSS rules to be applied. This change prevents applying Lumo global imports when the theme is applied to a web-component. Fixes #12704 Fixes #19265
1 parent fa7d789 commit f0890b4

12 files changed

Lines changed: 139 additions & 19 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/AbstractUpdateImports.java‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ abstract class AbstractUpdateImports implements Runnable {
9696
+ " return !ae || ae.blur() || ae.focus() || true;\n" + "}";
9797
private static final String IMPORT_TEMPLATE = "import '%s';";
9898
private static final Pattern STARTING_DOT_SLASH = Pattern.compile("^\\./+");
99+
private static final Pattern VAADIN_LUMO_GLOBAL_IMPORT = Pattern
100+
.compile(".*@vaadin/vaadin-lumo-styles/.*-global.js.*");
99101
final Options options;
100102

101103
private final UnaryOperator<String> themeToLocalPathConverter;
@@ -106,7 +108,8 @@ abstract class AbstractUpdateImports implements Runnable {
106108

107109
private ClassFinder classFinder;
108110

109-
private final File generatedFlowImports;
111+
final File generatedFlowImports;
112+
final File generatedFlowWebComponentImports;
110113
private final File generatedFlowDefinitions;
111114
private File chunkFolder;
112115

@@ -131,6 +134,10 @@ abstract class AbstractUpdateImports implements Runnable {
131134
generatedFlowDefinitions = new File(
132135
generatedFlowImports.getParentFile(),
133136
FrontendUtils.IMPORTS_D_TS_NAME);
137+
138+
generatedFlowWebComponentImports = FrontendUtils
139+
.getFlowGeneratedWebComponentsImports(
140+
options.getFrontendDirectory());
134141
this.chunkFolder = new File(generatedFlowImports.getParentFile(),
135142
"chunks");
136143

@@ -146,6 +153,8 @@ public void run() {
146153

147154
Map<File, List<String>> output = process(css, javascript);
148155
writeOutput(output);
156+
writeWebComponentImports(
157+
filterWebComponentImports(output.get(generatedFlowImports)));
149158

150159
getLogger().debug("Imports and chunks update took {} ms.",
151160
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
@@ -207,6 +216,30 @@ protected void writeOutput(Map<File, List<String>> outputFiles) {
207216
}
208217
}
209218

219+
// Visible for test
220+
List<String> filterWebComponentImports(List<String> lines) {
221+
if (lines != null) {
222+
// Exclude Lumo global imports for exported web-component
223+
return lines.stream()
224+
.filter(VAADIN_LUMO_GLOBAL_IMPORT.asPredicate().negate())
225+
.collect(Collectors.toList());
226+
}
227+
return lines;
228+
}
229+
230+
private void writeWebComponentImports(List<String> lines) {
231+
if (lines != null) {
232+
try {
233+
generatedFilesSupport.writeIfChanged(
234+
generatedFlowWebComponentImports, lines);
235+
} catch (IOException e) {
236+
throw new IllegalStateException(
237+
"Failed to update the generated Flow imports for exported web component",
238+
e);
239+
}
240+
}
241+
}
242+
210243
/**
211244
* Processes what the scanner found and produces a set of files to write to
212245
* the generated folder.

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendUtils.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ public class FrontendUtils {
178178
*/
179179
public static final String IMPORTS_D_TS_NAME = "generated-flow-imports.d.ts";
180180

181+
/**
182+
* Name of the file that contains application imports, javascript, theme and
183+
* style annotations used when embedding Flow as web-component.
184+
*/
185+
public static final String IMPORTS_WEB_COMPONENT_NAME = "generated-flow-webcomponent-imports.js";
186+
181187
public static final String THEME_IMPORTS_D_TS_NAME = "theme.d.ts";
182188
public static final String THEME_IMPORTS_NAME = "theme.js";
183189

@@ -1221,6 +1227,21 @@ public static File getFlowGeneratedImports(File frontendFolder) {
12211227
return new File(getFlowGeneratedFolder(frontendFolder), IMPORTS_NAME);
12221228
}
12231229

1230+
/**
1231+
* Gets the location of the generated import file for exported web
1232+
* components.
1233+
*
1234+
* @param frontendFolder
1235+
* the project frontend folder
1236+
* @return the location of the generated import JS file for exported web
1237+
* components
1238+
*/
1239+
public static File getFlowGeneratedWebComponentsImports(
1240+
File frontendFolder) {
1241+
return new File(getFlowGeneratedFolder(frontendFolder),
1242+
IMPORTS_WEB_COMPONENT_NAME);
1243+
}
1244+
12241245
/**
12251246
* Gets the folder where exported web components are generated.
12261247
*

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskGenerateWebComponentBootstrap.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class TaskGenerateWebComponentBootstrap
5252
protected String getFileContent() {
5353
List<String> lines = new ArrayList<>();
5454

55-
lines.add(
56-
"import 'Frontend/generated/flow/generated-flow-imports.js';");
55+
lines.add("import 'Frontend/generated/flow/"
56+
+ FrontendUtils.IMPORTS_WEB_COMPONENT_NAME + "';");
5757
lines.add("import { init } from '" + FrontendUtils.JAR_RESOURCES_IMPORT
5858
+ "FlowClient.js';");
5959
lines.add("init();");

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRemoveOldFrontendGeneratedFiles.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.vaadin.flow.server.frontend;
1818

19+
import java.io.File;
1920
import java.io.IOException;
2021
import java.io.UncheckedIOException;
2122
import java.nio.file.FileVisitResult;
@@ -54,11 +55,13 @@ public class TaskRemoveOldFrontendGeneratedFiles implements FallibleCommand {
5455
private static final Logger LOGGER = LoggerFactory
5556
.getLogger(TaskRemoveOldFrontendGeneratedFiles.class);
5657
private final Path frontendGeneratedFolder;
58+
private final File frontendFolder;
5759

5860
private final Set<Path> existingFiles = new HashSet<>();
5961
private GeneratedFilesSupport generatedFilesSupport;
6062

6163
public TaskRemoveOldFrontendGeneratedFiles(Options options) {
64+
frontendFolder = options.getFrontendDirectory();
6265
frontendGeneratedFolder = options.getFrontendGeneratedFolder().toPath();
6366
if (frontendGeneratedFolder.toFile().exists()) {
6467
try (Stream<Path> files = Files.walk(frontendGeneratedFolder)) {
@@ -123,10 +126,13 @@ public FileVisitResult postVisitDirectory(Path dir,
123126

124127
private Predicate<Path> isKnownUnhandledFile() {
125128
Path flowGeneratedImports = FrontendUtils
126-
.getFlowGeneratedImports(
127-
frontendGeneratedFolder.getParent().toFile())
128-
.toPath().toAbsolutePath();
129+
.getFlowGeneratedImports(frontendFolder).toPath()
130+
.toAbsolutePath();
131+
Path flowGeneratedWebComponentImports = FrontendUtils
132+
.getFlowGeneratedWebComponentsImports(frontendFolder).toPath()
133+
.toAbsolutePath();
129134
return path -> path.equals(flowGeneratedImports)
135+
|| path.equals(flowGeneratedWebComponentImports)
130136
|| path.equals(flowGeneratedImports
131137
.resolveSibling(FrontendUtils.IMPORTS_D_TS_NAME))
132138
|| path.getFileName().toString()

‎flow-server/src/main/resources/plugins/application-theme-plugin/theme-generator.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ function writeThemeFiles(themeFolder, themeName, themeProperties, options) {
118118
imports.push(`import { ${lumoImport} } from '@vaadin/vaadin-lumo-styles/${lumoImport}.js';\n`);
119119
if (lumoImport === 'utility' || lumoImport === 'badge' || lumoImport === 'typography' || lumoImport === 'color') {
120120
// Inject into main document the same way as other Lumo styles are injected
121-
imports.push(`import '@vaadin/vaadin-lumo-styles/${lumoImport}-global.js';\n`);
121+
// Lumo imports go to the theme global imports file to prevent style leaks
122+
// when the theme is applied to an embedded component
123+
globalFileContent.push(`import '@vaadin/vaadin-lumo-styles/${lumoImport}-global.js';\n`);
122124
}
123125
});
124126

‎flow-server/src/test/java/com/vaadin/flow/server/frontend/AbstractUpdateImportsTest.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.Set;
34+
import java.util.function.Predicate;
3435
import java.util.regex.Matcher;
3536
import java.util.regex.Pattern;
3637
import java.util.stream.Collectors;
@@ -124,6 +125,7 @@ public static class FooCssImport2 extends Component {
124125
class UpdateImports extends AbstractUpdateImports {
125126

126127
private Map<File, List<String>> output;
128+
private List<String> webComponentImports;
127129

128130
UpdateImports(FrontendDependenciesScanner scanner, Options options) {
129131
super(options, scanner);
@@ -134,6 +136,12 @@ protected void writeOutput(Map<File, List<String>> output) {
134136
this.output = output;
135137
}
136138

139+
@Override
140+
List<String> filterWebComponentImports(List<String> lines) {
141+
webComponentImports = super.filterWebComponentImports(lines);
142+
return webComponentImports;
143+
}
144+
137145
public Map<File, List<String>> getOutput() {
138146
return output;
139147
}
@@ -398,13 +406,41 @@ public void generate_containsLumoThemeFiles() {
398406
updater.run();
399407

400408
assertContainsImports(true, "@vaadin/vaadin-lumo-styles/color.js",
409+
"@vaadin/vaadin-lumo-styles/color-global.js",
401410
"@vaadin/vaadin-lumo-styles/typography.js",
411+
"@vaadin/vaadin-lumo-styles/typography-global.js",
402412
"@vaadin/vaadin-lumo-styles/sizing.js",
403413
"@vaadin/vaadin-lumo-styles/spacing.js",
404414
"@vaadin/vaadin-lumo-styles/style.js",
405415
"@vaadin/vaadin-lumo-styles/icons.js");
406416
}
407417

418+
@Test
419+
public void generate_embeddedImports_doNotContainLumoGlobalThemeFiles()
420+
throws IOException {
421+
updater.run();
422+
423+
List<String> flowImports = new ArrayList<>(
424+
updater.getOutput().get(updater.generatedFlowImports));
425+
426+
Predicate<String> lumoGlobalsMatcher = Pattern
427+
.compile("@vaadin/vaadin-lumo-styles/.*-global.js")
428+
.asPredicate();
429+
assertTrue(flowImports.stream().anyMatch(lumoGlobalsMatcher));
430+
431+
assertTrue(
432+
"Import for web-components should not contain lumo global imports",
433+
updater.webComponentImports.stream()
434+
.noneMatch(lumoGlobalsMatcher));
435+
436+
// Check that imports other than lumo globals are the same
437+
flowImports.removeAll(updater.webComponentImports);
438+
assertTrue(
439+
"Flow and web-component imports must be the same, except for lumo globals",
440+
flowImports.stream().allMatch(lumoGlobalsMatcher));
441+
442+
}
443+
408444
@Test
409445
public void jsModulesOrderIsPreservedAnsAfterJsModules() {
410446
updater.run();

‎flow-server/src/test/java/com/vaadin/flow/server/frontend/NodeTestComponents.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ public static class MainView extends Component {
157157
* Lumo component theme class implementation.
158158
*/
159159
@JsModule("@vaadin/vaadin-lumo-styles/color.js")
160+
@JsModule("@vaadin/vaadin-lumo-styles/color-global.js")
160161
@JsModule("@vaadin/vaadin-lumo-styles/typography.js")
162+
@JsModule("@vaadin/vaadin-lumo-styles/typography-global.js")
161163
@JsModule("@vaadin/vaadin-lumo-styles/sizing.js")
162164
@JsModule("@vaadin/vaadin-lumo-styles/spacing.js")
163165
@JsModule("@vaadin/vaadin-lumo-styles/style.js")

‎flow-server/src/test/java/com/vaadin/flow/server/frontend/NodeUpdateTestUtil.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ List<String> getExpectedImports() {
8585
"@vaadin/vaadin-lumo-styles/icons.js",
8686
"@vaadin/vaadin-lumo-styles/style.js",
8787
"@vaadin/vaadin-lumo-styles/typography.js",
88+
"@vaadin/vaadin-lumo-styles/typography-global.js",
8889
"@vaadin/vaadin-lumo-styles/color.js",
90+
"@vaadin/vaadin-lumo-styles/color-global.js",
8991
"@vaadin/vaadin-lumo-styles/sizing.js",
9092
"@vaadin/vaadin-date-picker/theme/lumo/vaadin-date-picker.js",
9193
"@vaadin/vaadin-date-picker/src/vaadin-month-calendar.js",

‎flow-server/src/test/java/com/vaadin/flow/server/frontend/TaskGenerateWebComponentBootstrapTest.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717

1818
import java.io.File;
1919

20+
import com.vaadin.flow.di.Lookup;
21+
import com.vaadin.flow.server.ExecutionFailedException;
2022
import org.junit.Assert;
2123
import org.junit.Before;
2224
import org.junit.Rule;
2325
import org.junit.Test;
2426
import org.junit.rules.TemporaryFolder;
2527
import org.mockito.Mockito;
2628

27-
import com.vaadin.flow.di.Lookup;
28-
import com.vaadin.flow.server.ExecutionFailedException;
29-
3029
import static com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR;
3130

3231
public class TaskGenerateWebComponentBootstrapTest {
@@ -57,7 +56,7 @@ public void should_importGeneratedImports()
5756
taskGenerateWebComponentBootstrap.execute();
5857
String content = taskGenerateWebComponentBootstrap.getFileContent();
5958
Assert.assertTrue(content.contains("import 'Frontend/generated/flow/"
60-
+ FrontendUtils.IMPORTS_NAME + "'"));
59+
+ FrontendUtils.IMPORTS_WEB_COMPONENT_NAME + "'"));
6160
}
6261

6362
@Test

‎flow-server/src/test/java/com/vaadin/flow/server/frontend/scanner/FullDependenciesScannerTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public void getModules_explcitTheme_returnAllModulesExcludingNotUsedTheme_getCla
338338
return null;
339339
}, null);
340340

341-
DepsTests.assertImportCount(26, scanner.getModules());
341+
DepsTests.assertImportCount(28, scanner.getModules());
342342
List<String> modules = DepsTests.merge(scanner.getModules());
343343
assertJsModules(modules);
344344

0 commit comments

Comments
 (0)