Skip to content

Commit

Permalink
feat: add WhiteSpace setter and getter methods (#11919)
Browse files Browse the repository at this point in the history
feat: add WhiteSpace setter and getter methods

fixes #4577
  • Loading branch information
Denis authored Oct 5, 2021
1 parent b92cc98 commit 7a540e2
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.junit.Test;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasText;
import com.vaadin.flow.component.HtmlComponent;
import com.vaadin.flow.component.HtmlContainer;
import com.vaadin.flow.component.Tag;
Expand Down Expand Up @@ -71,6 +72,7 @@ public class HtmlComponentSmokeTest {
new IFrame.SandboxType[] { IFrame.SandboxType.ALLOW_POPUPS,
IFrame.SandboxType.ALLOW_MODALS });
testValues.put(Component.class, new Paragraph("Component"));
testValues.put(HasText.WhiteSpace.class, HasText.WhiteSpace.PRE_LINE);
}

private static final Map<Class<?>, Map<Class<?>, Object>> specialTestValues = new HashMap<>();
Expand Down
101 changes: 101 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/component/HasText.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand All @@ -28,6 +32,75 @@
*/
public interface HasText extends HasElement {

/**
* Represents <code>"white-space"</code> style values.
*
* @author Vaadin Ltd
* @since
*
*/
enum WhiteSpace {
/**
* Sequences of white space are collapsed. Newline characters in the
* source are handled the same as other white space. Lines are broken as
* necessary to fill line boxes.
*/
NORMAL,
/**
* Collapses white space as for normal, but suppresses line breaks (text
* wrapping) within the source.
*/
NOWRAP,
/**
* Sequences of white space are preserved. Lines are only broken at
* newline characters in the source and at &lt;br&gt; elements.
*/
PRE,
/**
* Sequences of white space are preserved. Lines are broken at newline
* characters, at &lt;br&gt;, and as necessary to fill line boxes.
*/
PRE_WRAP,
/**
* Sequences of white space are collapsed. Lines are broken at newline
* characters, at &lt;br&gt;, and as necessary to fill line boxes.
*/
PRE_LINE,
/**
* The behavior is identical to that of pre-wrap, except that:
*
* <ul>
* <li>Any sequence of preserved white space always takes up space,
* including at the end of the line.
* <li>A line breaking opportunity exists after every preserved white
* space character, including between white space characters.
* <li>Such preserved spaces take up space and do not hang, and thus
* affect the box’s intrinsic sizes (min-content size and max-content
* size).
* </ul>
*/
BREAK_SPACES,
/**
* Inherits this property from its parent element.
*/
INHERIT,
/**
* Sets this property to its default value.
*/
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
Expand All @@ -52,4 +125,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.
* <p>
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 7a540e2

Please sign in to comment.