Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getters for used Unit to HasSize #9824

Merged
merged 9 commits into from
Feb 18, 2021
22 changes: 22 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/component/HasSize.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.vaadin.flow.component;

import java.util.Optional;

import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.ElementConstants;

Expand Down Expand Up @@ -164,6 +166,16 @@ default String getMaxWidth() {
return getElement().getStyle().get(ElementConstants.STYLE_MAX_WIDTH);
}

/**
* Gets the width unit of the component, if defined.
*
* @return an optional width unit for the component, or an empty
* optional if no width unit has been set
*/
default Optional<Unit> getWidthUnit() {
return Unit.getUnit(getWidth());
}

/**
* Sets the height of the component.
* <p>
Expand Down Expand Up @@ -296,6 +308,16 @@ default String getMaxHeight() {
return getElement().getStyle().get(ElementConstants.STYLE_MAX_HEIGHT);
}

/**
* Gets the height unit of the component, if defined.
*
* @return an optional height unit for the component, or an empty
* optional if no height unit has been set
*/
default Optional<Unit> getHeightUnit() {
return Unit.getUnit(getHeight());
}

/**
* Sets the width and the height of the component to "100%".
* <p>
Expand Down
60 changes: 43 additions & 17 deletions flow-server/src/main/java/com/vaadin/flow/component/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@
*/
public enum Unit {
/**
* Unit code representing pixels.
*/
PIXELS("px"),
/**
* Unit code representing points (1/72nd of an inch).
* Unit code representing in percentage of the containing element
* defined by terminal.
*/
POINTS("pt"),
PERCENTAGE("%"),
/**
* Unit code representing picas (12 points).
* Unit code representing pixels.
*/
PICAS("pc"),
PIXELS("px"),
/**
* Unit code representing the font-size of the root font.
*/
Expand All @@ -42,6 +39,30 @@ public enum Unit {
* Unit code representing the font-size of the relevant font.
*/
EM("em"),
/**
* Unit code representing the viewport's width.
*/
VW("vw"),
/**
* Unit code representing the viewport's height.
*/
VH("vh"),
/**
* Unit code representing the viewport's smaller dimension.
*/
VMIN("vmin"),
/**
* Unit code representing the viewport's larger dimension.
*/
VMAX("vmax"),
/**
* Unit code representing points (1/72nd of an inch).
*/
POINTS("pt"),
/**
* Unit code representing picas (12 points).
*/
PICAS("pc"),
/**
* Unit code representing the x-height of the relevant font.
*/
Expand All @@ -50,19 +71,18 @@ public enum Unit {
* Unit code representing millimeters.
*/
MM("mm"),
/**
* Unit code representing the width of the "0" (zero).
*/
CH("ch"),
/**
* Unit code representing centimeters.
*/
CM("cm"),
/**
* Unit code representing inches.
*/
INCH("in"),
/**
* Unit code representing in percentage of the containing element
* defined by terminal.
*/
PERCENTAGE("%");
INCH("in");

private final String symbol;

Expand Down Expand Up @@ -91,10 +111,16 @@ static Stream<Unit> getUnits() {
* @return A Optional unit.
*/
public static Optional<Unit> getUnit(String cssSize) {
if (cssSize == null) {
throw new IllegalArgumentException("The parameter can't be null");
if (cssSize == null || cssSize.isEmpty()) {
taefi marked this conversation as resolved.
Show resolved Hide resolved
return Optional.empty();
}

for (Unit unit : values()) {
if (cssSize.endsWith(unit.getSymbol())) {
return Optional.of(unit);
}
}
return getUnits().filter(unit -> cssSize.endsWith(unit.toString())).findFirst();
return Optional.empty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,29 @@ public void setSizeUndefined() {
Assert.assertNull(component.getHeight());
}

@Test
public void getWidthUnit() {
HasSizeComponent component = new HasSizeComponent();
Assert.assertFalse(component.getWidthUnit().isPresent());

component.setWidth("10px");
Assert.assertTrue(component.getWidthUnit().isPresent());
Assert.assertEquals(Unit.PIXELS, component.getWidthUnit().get());
taefi marked this conversation as resolved.
Show resolved Hide resolved

component.setSizeUndefined();
Assert.assertFalse(component.getWidthUnit().isPresent());
}

@Test
public void getHeightUnit() {
HasSizeComponent component = new HasSizeComponent();
Assert.assertFalse(component.getHeightUnit().isPresent());

component.setHeight("10%");
Assert.assertTrue(component.getHeightUnit().isPresent());
Assert.assertEquals(Unit.PERCENTAGE, component.getHeightUnit().get());
taefi marked this conversation as resolved.
Show resolved Hide resolved

component.setSizeUndefined();
Assert.assertFalse(component.getHeightUnit().isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,4 @@ public void getSizeNoValidUnit() {
float size = Unit.getSize(cssSize);
}

@Test(expected = IllegalArgumentException.class)
public void getUnitException() {
String cssSize = null;
Optional<Unit> size = Unit.getUnit(cssSize);
}

}
32 changes: 32 additions & 0 deletions flow-server/src/test/java/com/vaadin/flow/component/UnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2000-2020 Vaadin Ltd.
taefi marked this conversation as resolved.
Show resolved Hide resolved
*
* 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;

public class UnitTest {

@Test
public void getUnit() {
Assert.assertFalse(Unit.getUnit(null).isPresent());
Assert.assertFalse(Unit.getUnit("").isPresent());
Assert.assertFalse(Unit.getUnit("10unknown").isPresent());

Assert.assertEquals(Unit.PERCENTAGE, Unit.getUnit("100%").get());
Assert.assertEquals(Unit.PIXELS, Unit.getUnit("100px").get());
}
}