Skip to content

Commit

Permalink
fix: Handle an extra spaces in class names properly (#11861)
Browse files Browse the repository at this point in the history
Fixes #11709
  • Loading branch information
mshabarov authored and vaadin-bot committed Sep 17, 2021
1 parent fce4791 commit 4af39ea
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
22 changes: 20 additions & 2 deletions flow-server/src/main/java/com/vaadin/flow/component/HasStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,16 @@ default Style getStyle() {
*/
default void addClassNames(String... classNames) {
for (String rawClassName : classNames) {
String[] parts = rawClassName.split(" ");
if (rawClassName == null) {
throw new IllegalArgumentException(
"CSS class names cannot include a null element");
}
rawClassName = rawClassName.trim();
if (rawClassName.isEmpty()) {
throw new IllegalArgumentException(
"CSS class names cannot include an empty class name");
}
String[] parts = rawClassName.split(" +");
for (String part : parts) {
getClassNames().add(part);
}
Expand All @@ -157,7 +166,16 @@ default void addClassNames(String... classNames) {
*/
default void removeClassNames(String... classNames) {
for (String rawClassName : classNames) {
String[] parts = rawClassName.split(" ");
if (rawClassName == null) {
throw new IllegalArgumentException(
"CSS class names cannot include a null element");
}
rawClassName = rawClassName.trim();
if (rawClassName.isEmpty()) {
throw new IllegalArgumentException(
"CSS class names cannot include an empty class name");
}
String[] parts = rawClassName.split(" +");
for (String part : parts) {
getClassNames().remove(part);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,55 @@ public void testRemoveClassNames() {
assertClasses(component, "baz1", "bar1");
}

@Test
public void addClassNames_extraSpacesBetweenAndAroundClassNames_validationPasses() {
HasStyleComponent component = new HasStyleComponent();
component.addClassNames(" foo bar baz");

Assert.assertEquals(
"Unexpected component's class names count after adding 3 class names",
3, component.getClassNames().size());

Assert.assertTrue(component.getClassNames().contains("foo"));
Assert.assertTrue(component.getClassNames().contains("bar"));
Assert.assertTrue(component.getClassNames().contains("baz"));
}

@Test
public void removeClassNames_extraSpacesBetweenAndAroundClassNames_validationPasses() {
HasStyleComponent component = new HasStyleComponent();
component.addClassNames("foo", "bar", "baz");
component.removeClassNames(" foo bar baz");

Assert.assertEquals(
"Unexpected component's class names count after removing all class names",
0, component.getClassNames().size());
}

@Test(expected = IllegalArgumentException.class)
public void addClassNames_addEmptyClassName_throws() {
HasStyleComponent component = new HasStyleComponent();
component.addClassNames(" ");
}

@Test(expected = IllegalArgumentException.class)
public void addClassNames_addNullClassName_throws() {
HasStyleComponent component = new HasStyleComponent();
component.addClassNames(null, null);
}

@Test(expected = IllegalArgumentException.class)
public void removeClassNames_removeEmptyClassName_throws() {
HasStyleComponent component = new HasStyleComponent();
component.addClassNames(" ");
}

@Test(expected = IllegalArgumentException.class)
public void removeClassNames_removeNullClassName_throws() {
HasStyleComponent component = new HasStyleComponent();
component.addClassNames(null, null);
}

private void assertClasses(HasStyleComponent c, String... expectedClasses) {
Set<String> actual = c.getClassNames();
Set<String> expected = new HashSet<>(Arrays.asList(expectedClasses));
Expand Down

0 comments on commit 4af39ea

Please sign in to comment.