]> source.dussan.org Git - vaadin-framework.git/commitdiff
Function "percentage" is implemented (#12771).
authordenisanisimov <denis@vaadin.com>
Mon, 10 Feb 2014 09:33:55 +0000 (11:33 +0200)
committerDenis Anisimov <denis@vaadin.com>
Thu, 13 Feb 2014 14:00:16 +0000 (16:00 +0200)
Change-Id: I0a2e3d31f60bd085d619b603bd229dba902ff001

theme-compiler/src/com/vaadin/sass/internal/parser/LexicalUnitImpl.java
theme-compiler/tests/resources/css/functions.css
theme-compiler/tests/resources/scss/functions.scss
theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Functions.java

index 96f841de0b0b371ce01cfc91b4914881b616a6db..09fcdee7c081656b91ec0590f0e7ac186397167b 100644 (file)
@@ -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<String, SCSSFunctionGenerator> SERIALIZERS = new HashMap<String, SCSSFunctionGenerator>();
 
     private static final SCSSFunctionGenerator DEFAULT_SERIALIZER = new DefaultFunctionGenerator();
index 45d42b34de4c84aed28377e96c9131a8e53f90c6..0a8fcbf5a81c6c741f7976a3c7e82a834fce7832 100644 (file)
@@ -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%;
+}
index 4230850b2abad81652fb2375fe7ff596191e8802..8638d0afaa86ed9d110b51fde27259c11105aa47 100644 (file)
@@ -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);
+}
index b50172ac2f600dd462b1d080a9abbca724e7e6c1..bd214986c0c4613323cc5b41bbc10e14901d4a44 100644 (file)
@@ -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