From 399f91fc0b49d695bf00df7eaf80f3f185d8d908 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Thu, 23 Sep 2021 10:49:19 +0300 Subject: [PATCH] feat: add WhiteSpace setter and getter methods fixes #4577 --- .../com/vaadin/flow/component/HasText.java | 47 +++++++++++++ .../vaadin/flow/component/HasTextTest.java | 67 +++++++++++++++++++ .../vaadin/flow/component/WhiteSpaceTest.java | 37 ++++++++++ 3 files changed, 151 insertions(+) create mode 100644 flow-server/src/test/java/com/vaadin/flow/component/HasTextTest.java create mode 100644 flow-server/src/test/java/com/vaadin/flow/component/WhiteSpaceTest.java diff --git a/flow-server/src/main/java/com/vaadin/flow/component/HasText.java b/flow-server/src/main/java/com/vaadin/flow/component/HasText.java index 9ea3b381e59..74de1fa0c5a 100644 --- a/flow-server/src/main/java/com/vaadin/flow/component/HasText.java +++ b/flow-server/src/main/java/com/vaadin/flow/component/HasText.java @@ -15,6 +15,10 @@ */ package com.vaadin.flow.component; +import java.util.Locale; +import java.util.Objects; +import java.util.stream.Stream; + /** * A component that supports text content. *

@@ -28,6 +32,21 @@ */ public interface HasText extends HasElement { + enum WhiteSpace { + NORMAL, NOWRAP, PRE, PRE_WRAP, PRE_LINE, BREAK_SPACES, INHERIT, INITIAL; + + @Override + public String toString() { + return name().toLowerCase(Locale.ENGLISH).replace('_', '-'); + } + + public static WhiteSpace forString(String value) { + return Stream.of(values()) + .filter(whiteSpace -> whiteSpace.toString().equals(value)) + .findFirst().orElse(null); + } + } + /** * Sets the given string as the content of this component. This removes any * existing child components and child elements. To mix text and child @@ -52,4 +71,32 @@ default void setText(String text) { default String getText() { return getElement().getText(); } + + /** + * Sets the given {@code value} as {@code "white-space"} style value. + * + * @param value + * the {@code "white-space"} style value, not {@code null} + */ + default void setWhiteSpace(WhiteSpace value) { + getElement().getStyle().set("white-space", + Objects.requireNonNull(value).toString()); + } + + /** + * Gets the {@code "white-space"} style value. + *

+ * The default value is {@literal WhiteSpace#NORMAL}. If the + * {@code "white-space"} style value is non standard then {@code null} is + * returned. + * + * @return the {@code "white-space"} style value, may be {@code null} + */ + default WhiteSpace getWhiteSpace() { + String value = getElement().getStyle().get("white-space"); + if (value == null) { + return WhiteSpace.NORMAL; + } + return WhiteSpace.forString(value); + } } diff --git a/flow-server/src/test/java/com/vaadin/flow/component/HasTextTest.java b/flow-server/src/test/java/com/vaadin/flow/component/HasTextTest.java new file mode 100644 index 00000000000..33d799af19f --- /dev/null +++ b/flow-server/src/test/java/com/vaadin/flow/component/HasTextTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2000-2021 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import com.vaadin.flow.component.HasText.WhiteSpace; +import com.vaadin.flow.dom.Element; +import com.vaadin.flow.dom.ElementFactory; + +public class HasTextTest { + + private HasText hasText = Mockito.mock(HasText.class); + + @Before + public void setUp() { + Element element = ElementFactory.createDiv(); + Mockito.when(hasText.getElement()).thenReturn(element); + + Mockito.doCallRealMethod().when(hasText).setWhiteSpace(Mockito.any()); + Mockito.doCallRealMethod().when(hasText).getWhiteSpace(); + } + + @Test + public void setWhiteSpace_styleIsSet() { + hasText.setWhiteSpace(WhiteSpace.NOWRAP); + + Assert.assertEquals("nowrap", + hasText.getElement().getStyle().get("white-space")); + } + + @Test + public void getWhiteSpace_getStyleValue() { + hasText.getElement().getStyle().set("white-space", "inherit"); + + Assert.assertEquals(WhiteSpace.INHERIT, hasText.getWhiteSpace()); + } + + @Test + public void getWhiteSpace_noStyleIsSet_normalIsReturned() { + Assert.assertEquals(WhiteSpace.NORMAL, hasText.getWhiteSpace()); + } + + @Test + public void getWhiteSpace_notStandardValue_nullIsReturned() { + hasText.getElement().getStyle().set("white-space", "foo"); + + Assert.assertEquals(null, hasText.getWhiteSpace()); + } + +} diff --git a/flow-server/src/test/java/com/vaadin/flow/component/WhiteSpaceTest.java b/flow-server/src/test/java/com/vaadin/flow/component/WhiteSpaceTest.java new file mode 100644 index 00000000000..27c85d53141 --- /dev/null +++ b/flow-server/src/test/java/com/vaadin/flow/component/WhiteSpaceTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2021 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.flow.component.HasText.WhiteSpace; + +public class WhiteSpaceTest { + + @Test + public void toString_styleValueIsReturned() { + Assert.assertEquals("nowrap", WhiteSpace.NOWRAP.toString()); + Assert.assertEquals("pre-line", WhiteSpace.PRE_LINE.toString()); + } + + @Test + public void forString_enumIsReturned() { + Assert.assertEquals(WhiteSpace.NORMAL, WhiteSpace.forString("normal")); + Assert.assertEquals(WhiteSpace.PRE_WRAP, + WhiteSpace.forString("pre-wrap")); + } +}