|
58 | 58 | import com.vaadin.flow.shared.ui.Transport; |
59 | 59 | import com.vaadin.flow.theme.AbstractTheme; |
60 | 60 | import com.vaadin.flow.theme.Theme; |
| 61 | +import com.vaadin.flow.component.dependency.StyleSheet; |
61 | 62 |
|
62 | 63 | import jakarta.servlet.ServletContext; |
63 | 64 | import jakarta.servlet.ServletRegistration; |
@@ -188,6 +189,41 @@ public static class AppShellExtendingComponent extends Component |
188 | 189 | implements AppShellConfigurator { |
189 | 190 | } |
190 | 191 |
|
| 192 | + @StyleSheet("./foo.css") |
| 193 | + public static class ComponentWithStylesheet extends Component { |
| 194 | + } |
| 195 | + |
| 196 | + @StyleSheet("context://my-styles.css") |
| 197 | + @StyleSheet("https://cdn.example.com/ui.css") |
| 198 | + public static class MyAppShellWithStyleSheets |
| 199 | + implements AppShellConfigurator { |
| 200 | + } |
| 201 | + |
| 202 | + @StyleSheet("context://theme-base.css") |
| 203 | + @StyleSheet("context://theme-base.css") |
| 204 | + public static class MyAppShellWithDuplicateStyles |
| 205 | + implements AppShellConfigurator { |
| 206 | + } |
| 207 | + |
| 208 | + @StyleSheet(" /trimmed.css ") |
| 209 | + @StyleSheet(" ") |
| 210 | + @StyleSheet("foo/bar.css") |
| 211 | + @StyleSheet("HTTP://cdn.Example.com/u.css") |
| 212 | + @StyleSheet("context://assets/site.css") |
| 213 | + @StyleSheet("context:///already-absolute.css") |
| 214 | + public static class MyAppShellWithVariousStyleSheets |
| 215 | + implements AppShellConfigurator { |
| 216 | + } |
| 217 | + |
| 218 | + @StyleSheet("./local.css") |
| 219 | + public static class MyAppShellWithDotSlash implements AppShellConfigurator { |
| 220 | + } |
| 221 | + |
| 222 | + @StyleSheet("../secrets.css") |
| 223 | + public static class MyAppShellWithTraversal |
| 224 | + implements AppShellConfigurator { |
| 225 | + } |
| 226 | + |
191 | 227 | @Rule |
192 | 228 | public ExpectedException exception = ExpectedException.none(); |
193 | 229 |
|
@@ -437,6 +473,34 @@ public void should_not_throw_when_appShellAnnotationsAreAllowed_and_offendingCla |
437 | 473 | assertEquals(0, elements.size()); |
438 | 474 | } |
439 | 475 |
|
| 476 | + @Test |
| 477 | + public void styleSheetOnAppShell_injectedAsLinksInOrder() throws Exception { |
| 478 | + classes.add(MyAppShellWithStyleSheets.class); |
| 479 | + initializer.process(classes, servletContext); |
| 480 | + |
| 481 | + AppShellRegistry.getInstance(context).modifyIndexHtml(document, |
| 482 | + createVaadinRequest("/")); |
| 483 | + |
| 484 | + List<Element> links = document.head().select("link[rel=stylesheet]"); |
| 485 | + assertEquals(2, links.size()); |
| 486 | + assertEquals("/my-styles.css", links.get(0).attr("href")); |
| 487 | + assertEquals("https://cdn.example.com/ui.css", |
| 488 | + links.get(1).attr("href")); |
| 489 | + } |
| 490 | + |
| 491 | + @Test |
| 492 | + public void duplicateStyleSheets_deduplicated() throws Exception { |
| 493 | + classes.add(MyAppShellWithDuplicateStyles.class); |
| 494 | + initializer.process(classes, servletContext); |
| 495 | + |
| 496 | + AppShellRegistry.getInstance(context).modifyIndexHtml(document, |
| 497 | + createVaadinRequest("/")); |
| 498 | + |
| 499 | + List<Element> links = document.head().select("link[rel=stylesheet]"); |
| 500 | + assertEquals(1, links.size()); |
| 501 | + assertEquals("/theme-base.css", links.get(0).attr("href")); |
| 502 | + } |
| 503 | + |
440 | 504 | @Test |
441 | 505 | public void should_link_to_PWA_article() throws Exception { |
442 | 506 | Mockito.when(appConfig.getBooleanProperty( |
@@ -469,6 +533,55 @@ public void should_throwException_when_appShellExtendsComponent() |
469 | 533 | initializer.process(classes, servletContext); |
470 | 534 | } |
471 | 535 |
|
| 536 | + @Test |
| 537 | + public void styleSheetOnComponent_notOffending() throws Exception { |
| 538 | + classes.add(ComponentWithStylesheet.class); |
| 539 | + // Should not throw as @StyleSheet is allowed on Components |
| 540 | + initializer.process(classes, servletContext); |
| 541 | + } |
| 542 | + |
| 543 | + @Test |
| 544 | + public void styleSheetResolution_variousScenarios() throws Exception { |
| 545 | + classes.add(MyAppShellWithVariousStyleSheets.class); |
| 546 | + initializer.process(classes, servletContext); |
| 547 | + |
| 548 | + AppShellRegistry.getInstance(context).modifyIndexHtml(document, |
| 549 | + createVaadinRequest("/", "/ctx")); |
| 550 | + |
| 551 | + List<Element> links = document.head().select("link[rel=stylesheet]"); |
| 552 | + assertEquals(5, links.size()); |
| 553 | + assertEquals("/trimmed.css", links.get(0).attr("href")); |
| 554 | + assertEquals("/foo/bar.css", links.get(1).attr("href")); |
| 555 | + assertEquals("HTTP://cdn.Example.com/u.css", links.get(2).attr("href")); |
| 556 | + assertEquals("/ctx/assets/site.css", links.get(3).attr("href")); |
| 557 | + assertEquals("/ctx/already-absolute.css", links.get(4).attr("href")); |
| 558 | + } |
| 559 | + |
| 560 | + @Test |
| 561 | + public void styleSheetResolution_handlesDotSlash() throws Exception { |
| 562 | + classes.add(MyAppShellWithDotSlash.class); |
| 563 | + initializer.process(classes, servletContext); |
| 564 | + |
| 565 | + AppShellRegistry.getInstance(context).modifyIndexHtml(document, |
| 566 | + createVaadinRequest("/", "/ctx")); |
| 567 | + |
| 568 | + List<Element> links = document.head().select("link[rel=stylesheet]"); |
| 569 | + assertEquals(1, links.size()); |
| 570 | + assertEquals("/local.css", links.get(0).attr("href")); |
| 571 | + } |
| 572 | + |
| 573 | + @Test |
| 574 | + public void styleSheetResolution_rejectsTraversal() throws Exception { |
| 575 | + classes.add(MyAppShellWithTraversal.class); |
| 576 | + initializer.process(classes, servletContext); |
| 577 | + |
| 578 | + AppShellRegistry.getInstance(context).modifyIndexHtml(document, |
| 579 | + createVaadinRequest("/", "/ctx")); |
| 580 | + |
| 581 | + List<Element> links = document.head().select("link[rel=stylesheet]"); |
| 582 | + assertEquals(0, links.size()); |
| 583 | + } |
| 584 | + |
472 | 585 | private VaadinServletRequest createVaadinRequest(String pathInfo) { |
473 | 586 | HttpServletRequest request = createRequest(pathInfo); |
474 | 587 | return new VaadinServletRequest(request, service); |
@@ -515,4 +628,21 @@ private ApplicationConfiguration mockApplicationConfiguration() { |
515 | 628 | .thenAnswer(invocation -> invocation.getArgument(1)); |
516 | 629 | return config; |
517 | 630 | } |
| 631 | + |
| 632 | + private VaadinServletRequest createVaadinRequest(String pathInfo, |
| 633 | + String contextPath) { |
| 634 | + HttpServletRequest request = createRequest(pathInfo, contextPath); |
| 635 | + return new VaadinServletRequest(request, service); |
| 636 | + } |
| 637 | + |
| 638 | + private HttpServletRequest createRequest(String pathInfo, |
| 639 | + String contextPath) { |
| 640 | + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); |
| 641 | + Mockito.when(request.getServletPath()).thenReturn(""); |
| 642 | + Mockito.when(request.getPathInfo()).thenReturn(pathInfo); |
| 643 | + Mockito.when(request.getRequestURL()) |
| 644 | + .thenReturn(new StringBuffer(pathInfo)); |
| 645 | + Mockito.when(request.getContextPath()).thenReturn(contextPath); |
| 646 | + return request; |
| 647 | + } |
518 | 648 | } |
0 commit comments