From 853d42a08bfdbad030eb728b296c930176bbe2bd Mon Sep 17 00:00:00 2001 From: denisanisimov Date: Mon, 10 Feb 2014 11:33:55 +0200 Subject: [PATCH] Function "percentage" is implemented (#12771). Change-Id: I0a2e3d31f60bd085d619b603bd229dba902ff001 --- .../sass/internal/parser/LexicalUnitImpl.java | 40 +++++++++++++++++++ .../tests/resources/css/functions.css | 4 +- .../tests/resources/scss/functions.scss | 4 +- .../vaadin/sass/testcases/scss/Functions.java | 2 +- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java b/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java index 96f841de0b..09fcdee7c0 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java +++ b/theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java @@ -52,6 +52,9 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, Serializable { private static final long serialVersionUID = -6649833716809789399L; + private static int PRECISION = 100000; + private static int PERC_PRECISION_FACTOR = 100 * PRECISION; + LexicalUnitImpl prev; LexicalUnitImpl next; @@ -788,9 +791,46 @@ public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit, list.add(new FloorFunctionGenerator()); list.add(new LightenFunctionGenerator()); list.add(new RoundFunctionGenerator()); + list.add(new PercentageFunctionGenerator()); return list; } + private static class PercentageFunctionGenerator implements + SCSSFunctionGenerator { + + @Override + public String getFunctionName() { + return "percentage"; + } + + @Override + public String printState(LexicalUnitImpl function) { + StringBuilder builder = new StringBuilder(); + LexicalUnitImpl firstParam = function.getParameters(); + float value = firstParam.getFloatValue(); + value *= PERC_PRECISION_FACTOR; + int intValue = Math.round(value); + value = ((float) intValue) / PRECISION; + + int resultIntValue = (int) value; + + firstParam.type = SAC_PERCENTAGE; + + if (intValue == resultIntValue * PRECISION) { + builder.append(resultIntValue); + firstParam.setIntegerValue(resultIntValue); + } else { + builder.append(value); + firstParam.setFloatValue(value); + } + + firstParam.setStringValue(builder.append('%').toString()); + + return firstParam.toString(); + } + + } + private static final Map SERIALIZERS = new HashMap(); private static final SCSSFunctionGenerator DEFAULT_SERIALIZER = new DefaultFunctionGenerator(); diff --git a/theme-compiler/tests/resources/css/functions.css b/theme-compiler/tests/resources/css/functions.css index 45d42b34de..0a8fcbf5a8 100644 --- a/theme-compiler/tests/resources/css/functions.css +++ b/theme-compiler/tests/resources/css/functions.css @@ -14,4 +14,6 @@ color: hsl(33, 7%, 89%); color: hsl(33, 7%, 95%); color: rgb(1, 2, 3); -} \ No newline at end of file + percents: -20%; + percents: 33.33%; +} diff --git a/theme-compiler/tests/resources/scss/functions.scss b/theme-compiler/tests/resources/scss/functions.scss index 4230850b2a..8638d0afaa 100644 --- a/theme-compiler/tests/resources/scss/functions.scss +++ b/theme-compiler/tests/resources/scss/functions.scss @@ -19,4 +19,6 @@ $blue:3; color : $base-color; color : $app-bg-color; color: rgb($red, $green, $blue); -} \ No newline at end of file + percents: percentage(-0.2); + percents: percentage(0.3333); +} diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Functions.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Functions.java index b50172ac2f..bd214986c0 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Functions.java +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Functions.java @@ -43,7 +43,7 @@ public class Functions extends AbstractTestBase { ScssStylesheet root = handler.getStyleSheet(); Assert.assertEquals(6, root.getChildren().size()); BlockNode blockNode = (BlockNode) root.getChildren().get(5); - Assert.assertEquals(15, blockNode.getChildren().size()); + Assert.assertEquals(17, blockNode.getChildren().size()); } @Test -- 2.39.5