diff options
author | Tatu Lund <tatu@vaadin.com> | 2021-11-16 13:41:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-16 13:41:22 +0200 |
commit | 0195c63e0bde8f84b8544477507cbdd25ad22952 (patch) | |
tree | ec260ee80078aca76cead81767abfcf633cfba38 | |
parent | a243a8ad2b9bdd49e155e921f7889133c41e6c99 (diff) | |
download | vaadin-framework-0195c63e0bde8f84b8544477507cbdd25ad22952.tar.gz vaadin-framework-0195c63e0bde8f84b8544477507cbdd25ad22952.zip |
fix: Check if injected CSS String is already in the list of injected styles (#12479)
* Check if injected CSS String is already in the list of injected styles
* Change to package for unit testing
* Unit test
* Add missing imports
-rw-r--r-- | server/src/main/java/com/vaadin/server/Page.java | 29 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/server/PageTest.java | 24 |
2 files changed, 49 insertions, 4 deletions
diff --git a/server/src/main/java/com/vaadin/server/Page.java b/server/src/main/java/com/vaadin/server/Page.java index 976d86ff8e..bec3696620 100644 --- a/server/src/main/java/com/vaadin/server/Page.java +++ b/server/src/main/java/com/vaadin/server/Page.java @@ -422,6 +422,23 @@ public class Page implements Serializable { target.addText(css); target.endTag("css-string"); } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (obj instanceof InjectedStyleString) { + InjectedStyleString that = (InjectedStyleString) obj; + return css.equals(that.css); + } else { + return false; + } + } + + @Override + public int hashCode() { + return css.hashCode(); + } } private static class InjectedStyleResource implements InjectedStyle { @@ -466,9 +483,9 @@ public class Page implements Serializable { */ public static class Styles implements Serializable { - private LinkedHashSet<InjectedStyle> injectedStyles = new LinkedHashSet<>(); + LinkedHashSet<InjectedStyle> injectedStyles = new LinkedHashSet<>(); - private LinkedHashSet<InjectedStyle> pendingInjections = new LinkedHashSet<>(); + LinkedHashSet<InjectedStyle> pendingInjections = new LinkedHashSet<>(); private final UI ui; @@ -488,8 +505,12 @@ public class Page implements Serializable { "Cannot inject null CSS string"); } - pendingInjections.add(new InjectedStyleString(css)); - ui.markAsDirty(); + InjectedStyleString injectedStyleString = new InjectedStyleString( + css); + if (!injectedStyles.contains(injectedStyleString) + && pendingInjections.add(injectedStyleString)) { + ui.markAsDirty(); + } } /** diff --git a/server/src/test/java/com/vaadin/server/PageTest.java b/server/src/test/java/com/vaadin/server/PageTest.java index adb9e93227..5ac4c5be14 100644 --- a/server/src/test/java/com/vaadin/server/PageTest.java +++ b/server/src/test/java/com/vaadin/server/PageTest.java @@ -1,7 +1,10 @@ package com.vaadin.server; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import java.io.Writer; + import org.easymock.EasyMock; import org.junit.Test; @@ -45,6 +48,27 @@ public class PageTest { page.getState(false).hasResizeListeners); } + @Test + public void cssStringInjectedTwice() throws PaintException { + TestPage page = new TestPage(EasyMock.createMock(UI.class), + EasyMock.createMock(PageState.class)); + JsonPaintTarget paintTarget = new JsonPaintTarget( + EasyMock.createMock(LegacyCommunicationManager.class), + EasyMock.createMock(Writer.class), true); + + page.getStyles().add(".my-style { color: red; }"); + assertEquals(page.getStyles().pendingInjections.size(), 1); + page.paintContent(paintTarget); + assertEquals(page.getStyles().pendingInjections.size(), 0); + assertEquals(page.getStyles().injectedStyles.size(), 1); + + page.getStyles().add(".my-style { color: red; }"); + assertEquals(page.getStyles().pendingInjections.size(), 0); + page.paintContent(paintTarget); + assertEquals(page.getStyles().pendingInjections.size(), 0); + assertEquals(page.getStyles().injectedStyles.size(), 1); + } + private static class TestPage extends Page { public TestPage(UI uI, PageState state) { |