]> source.dussan.org Git - vaadin-framework.git/commitdiff
Sass replaces variables based on substring matches. (Tickets #10508,
authorHaijian Wang <haijian@vaadin.com>
Wed, 13 Feb 2013 12:51:18 +0000 (14:51 +0200)
committerHaijian Wang <haijian@vaadin.com>
Wed, 13 Feb 2013 12:51:18 +0000 (14:51 +0200)
#10730)

Change-Id: I7d3c5a74055e28e0e22fe6e496209d7d3c42b4af

13 files changed:
theme-compiler/src/com/vaadin/sass/internal/tree/FunctionNode.java
theme-compiler/src/com/vaadin/sass/internal/tree/MicrosoftRuleNode.java
theme-compiler/src/com/vaadin/sass/internal/tree/NestPropertiesNode.java
theme-compiler/src/com/vaadin/sass/internal/tree/RuleNode.java
theme-compiler/src/com/vaadin/sass/internal/tree/SimpleNode.java
theme-compiler/src/com/vaadin/sass/internal/tree/VariableNode.java
theme-compiler/src/com/vaadin/sass/internal/tree/controldirective/IfNode.java
theme-compiler/src/com/vaadin/sass/internal/util/StringUtil.java
theme-compiler/tests/resources/automatic/css/var-in-css-function.css [new file with mode: 0644]
theme-compiler/tests/resources/automatic/css/var-substring-match.css [new file with mode: 0644]
theme-compiler/tests/resources/automatic/scss/var-in-css-function.scss [new file with mode: 0644]
theme-compiler/tests/resources/automatic/scss/var-substring-match.scss [new file with mode: 0644]
theme-compiler/tests/src/com/vaadin/sass/internal/util/StringUtilTest.java [new file with mode: 0644]

index 919db1fdd7b0ddd2d0e9cb0104a4859c2b9bdacc..ced4671051de8c731853c6e1b9ee7faea7d5d031 100644 (file)
@@ -19,6 +19,7 @@ package com.vaadin.sass.internal.tree;
 import java.util.ArrayList;
 
 import com.vaadin.sass.internal.ScssStylesheet;
+import com.vaadin.sass.internal.util.StringUtil;
 
 public class FunctionNode extends Node implements IVariableNode {
     private static final long serialVersionUID = -5383104165955523923L;
@@ -47,8 +48,9 @@ public class FunctionNode extends Node implements IVariableNode {
     @Override
     public void replaceVariables(ArrayList<VariableNode> variables) {
         for (final VariableNode node : variables) {
-            if (args.contains(node.getName())) {
-                args.replaceAll(node.getName(), node.getExpr().toString());
+            if (StringUtil.containsVariable(args, node.getName())) {
+                args = StringUtil.replaceVariable(args, node.getName(), node
+                        .getExpr().toString());
             }
         }
     }
index 1644c1c336814148d68305bdda71717ff53d5fe6..3938188a7278e23d6616bab2e6299ab78673f8fd 100644 (file)
@@ -18,6 +18,7 @@ package com.vaadin.sass.internal.tree;
 import java.util.ArrayList;
 
 import com.vaadin.sass.internal.ScssStylesheet;
+import com.vaadin.sass.internal.util.StringUtil;
 
 public class MicrosoftRuleNode extends Node implements IVariableNode {
 
@@ -32,9 +33,9 @@ public class MicrosoftRuleNode extends Node implements IVariableNode {
     @Override
     public void replaceVariables(ArrayList<VariableNode> variables) {
         for (final VariableNode var : variables) {
-            if (value.contains("$" + var.getName())) {
-                value = value.replaceAll("$" + var.getName(), var.getExpr()
-                        .toString());
+            if (StringUtil.containsVariable(value, var.getName())) {
+                value = StringUtil.replaceVariable(value, var.getName(), var
+                        .getExpr().toString());
             }
         }
     }
index 63a42034bf13053c9583fe10ab8a2696499948af..fc50cfda61910b5af44505b7f21ae9e39b6ad4c8 100644 (file)
@@ -59,10 +59,9 @@ public class NestPropertiesNode extends Node implements IVariableNode {
 
     @Override
     public void replaceVariables(ArrayList<VariableNode> variables) {
-        for (final VariableNode node : variables) {
-            if (name.contains(node.getName())) {
-                name = name.replaceAll(node.getName(), node.getExpr()
-                        .toString());
+        for (Node child : getChildren()) {
+            if (child instanceof RuleNode) {
+                ((RuleNode) child).replaceVariables(variables);
             }
         }
     }
index 73ab31b4a1930a89ade64e240e96e5cf48f5109a..a78d9d66d24fdbd313e42f4cec939157a3aba099 100644 (file)
@@ -21,6 +21,7 @@ import java.util.regex.Pattern;
 
 import com.vaadin.sass.internal.ScssStylesheet;
 import com.vaadin.sass.internal.parser.LexicalUnitImpl;
+import com.vaadin.sass.internal.util.StringUtil;
 
 public class RuleNode extends Node implements IVariableNode {
     private static final long serialVersionUID = 6653493127869037022L;
@@ -95,28 +96,14 @@ public class RuleNode extends Node implements IVariableNode {
             if (value.getLexicalUnitType() == LexicalUnitImpl.SAC_FUNCTION) {
 
                 if (value.getParameters() != null) {
-                    if (value.getParameters().toString()
-                            .contains(node.getName())) {
-
+                    if (StringUtil.containsVariable(value.getParameters()
+                            .toString(), node.getName())) {
                         LexicalUnitImpl param = value.getParameters();
                         while (param != null) {
-                            if (param.getValue().toString()
-                                    .contains(node.getName())) {
-
-                                String value = node.getExpr().toString();
-
-                                LexicalUnitImpl prev = param
-                                        .getPreviousLexicalUnit();
-                                LexicalUnitImpl next = param
-                                        .getNextLexicalUnit();
-
-                                if (param.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE) {
-                                    param.setStringValue(value);
-                                    param.setLexicalUnitType(node.getExpr()
-                                            .getLexicalUnitType());
-                                    param.setPrevLexicalUnit(prev);
-                                    param.setNextLexicalUnit(next);
-                                }
+                            if (param.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE
+                                    && param.getValue().toString()
+                                            .equals(node.getName())) {
+                                param.replaceValue(node.getExpr());
                             }
                             param = param.getNextLexicalUnit();
                         }
index cb27498562a7a4da7bf266a7f14e74a459ad069e..796f4d8d1d25af93759bbfee94f227ceed5065d8 100644 (file)
@@ -18,6 +18,7 @@ package com.vaadin.sass.internal.tree;
 import java.util.ArrayList;
 
 import com.vaadin.sass.internal.ScssStylesheet;
+import com.vaadin.sass.internal.util.StringUtil;
 
 /**
  * A simple BlockNode where input text equals output. <b>Note : </b> ignores any
@@ -44,9 +45,9 @@ public class SimpleNode extends Node implements IVariableNode {
     @Override
     public void replaceVariables(ArrayList<VariableNode> variables) {
         for (final VariableNode node : variables) {
-            if (text.contains(node.getName())) {
-                text = text.replaceAll(node.getName(), node.getExpr()
-                        .toString());
+            if (StringUtil.containsVariable(text, node.getName())) {
+                text = StringUtil.replaceVariable(text, node.getName(), node
+                        .getExpr().toString());
             }
         }
     }
index f9b6f9dc8e7243cdb1a255b5a7769631b956084e..90be727f88c462d4ca40831977a98da490d970ff 100644 (file)
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 
 import com.vaadin.sass.internal.ScssStylesheet;
 import com.vaadin.sass.internal.parser.LexicalUnitImpl;
+import com.vaadin.sass.internal.util.StringUtil;
 import com.vaadin.sass.internal.visitor.VariableNodeHandler;
 
 public class VariableNode extends Node implements IVariableNode {
@@ -72,10 +73,11 @@ public class VariableNode extends Node implements IVariableNode {
         for (final VariableNode node : variables) {
             if (!equals(node)) {
 
-                if (expr.toString().contains("$" + node.getName())) {
+                if (StringUtil
+                        .containsVariable(expr.toString(), node.getName())) {
                     if (expr.getParameters() != null
-                            && expr.getParameters().toString()
-                                    .contains("$" + node.getName())) {
+                            && StringUtil.containsVariable(expr.getParameters()
+                                    .toString(), node.getName())) {
                         replaceValues(expr.getParameters(), node);
                     } else if (expr.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE) {
                         replaceValues(expr, node);
index de8a5a8551e5e85d6753de6003ae602099e96151..af795a58c361f3c709159ae1f60f5a8a700d23fb 100644 (file)
 package com.vaadin.sass.internal.tree.controldirective;
 
 import java.util.ArrayList;
-import java.util.regex.Pattern;
 
 import com.vaadin.sass.internal.ScssStylesheet;
 import com.vaadin.sass.internal.tree.IVariableNode;
 import com.vaadin.sass.internal.tree.Node;
 import com.vaadin.sass.internal.tree.VariableNode;
+import com.vaadin.sass.internal.util.StringUtil;
 
 public class IfNode extends Node implements IfElseNode, IVariableNode {
     private String expression;
@@ -47,10 +47,9 @@ public class IfNode extends Node implements IfElseNode, IVariableNode {
     @Override
     public void replaceVariables(ArrayList<VariableNode> variables) {
         for (final VariableNode node : variables) {
-            String variable = "$" + node.getName();
-            if (expression.contains(variable)) {
-                expression = expression.replaceAll(Pattern.quote(variable),
-                        node.getExpr().toString());
+            if (StringUtil.containsVariable(expression, node.getName())) {
+                expression = StringUtil.replaceVariable(expression,
+                        node.getName(), node.getExpr().toString());
             }
         }
     }
index dcfd3db5c6aaf95039e8853dfa348acd6c1993ca..cf227fe3a316f1364b06a5d793060c4b07b4fac4 100644 (file)
@@ -21,6 +21,8 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 public class StringUtil {
     private static final String FOLDER_SEPARATOR = "/"; // folder separator
@@ -133,4 +135,45 @@ public class StringUtil {
             String delim) {
         return collectionToDelimitedString(coll, delim, "", "");
     }
+
+    /**
+     * Check if a String contains a SCSS variable, using whole word match.
+     * 
+     * @param text
+     *            text to be checked
+     * @Param varName SCSS variable name to be checked. (Without '$' sign)
+     * @return true if the text contains the SCSS variable, false if not
+     */
+    public static boolean containsVariable(String text, String varName) {
+        StringBuilder builder = new StringBuilder();
+        // (?![\\w-]) means lookahead, the next one shouldn't be a word
+        // character nor a dash.
+        builder.append("\\$").append(Pattern.quote(varName))
+                .append("(?![\\w-])");
+        Pattern pattern = Pattern.compile(builder.toString());
+        Matcher matcher = pattern.matcher(text);
+        return matcher.find();
+    }
+
+    /**
+     * Replace the SCSS variable in a String to its corresponding value, using
+     * whole word match.
+     * 
+     * @param text
+     *            text which contains the SCSS variable
+     * @param varName
+     *            SCSS variable name
+     * @param value
+     *            the value of the SCSS variable
+     * @return the String after replacing
+     */
+    public static String replaceVariable(String text, String varName,
+            String value) {
+        StringBuilder builder = new StringBuilder();
+        // (?![\\w-]) means lookahead, the next one shouldn't be a word
+        // character nor a dash.
+        builder.append("\\$").append(Pattern.quote(varName))
+                .append("(?![\\w-])");
+        return text.replaceAll(builder.toString(), value);
+    }
 }
diff --git a/theme-compiler/tests/resources/automatic/css/var-in-css-function.css b/theme-compiler/tests/resources/automatic/css/var-in-css-function.css
new file mode 100644 (file)
index 0000000..07a9216
--- /dev/null
@@ -0,0 +1,4 @@
+.v-window-footer {
+    background: linear-gradient(bottom, hsl(110, 50%, 98%),  hsl(110, 50%, 90%));
+    abc: rgba(rgb(0, 255, 13), 0.85);
+}
\ No newline at end of file
diff --git a/theme-compiler/tests/resources/automatic/css/var-substring-match.css b/theme-compiler/tests/resources/automatic/css/var-substring-match.css
new file mode 100644 (file)
index 0000000..54c97ea
--- /dev/null
@@ -0,0 +1,3 @@
+.foo {
+  font-size: 10px;
+}
\ No newline at end of file
diff --git a/theme-compiler/tests/resources/automatic/scss/var-in-css-function.scss b/theme-compiler/tests/resources/automatic/scss/var-in-css-function.scss
new file mode 100644 (file)
index 0000000..72b371b
--- /dev/null
@@ -0,0 +1,15 @@
+$very_light: 98%;
+$light: 90%;
+
+$hue1: 110;
+$saturation1: 50%;
+$very_light1: hsl($hue1, $saturation1, $very_light); 
+$light1: hsl($hue1, $saturation1, $light); 
+
+$color: rgb(0, 255, 13);
+.v-window-footer {
+    background: linear-gradient(bottom, $very_light1, $light1);
+    abc: rgba($color, .85);
+}
+
+
diff --git a/theme-compiler/tests/resources/automatic/scss/var-substring-match.scss b/theme-compiler/tests/resources/automatic/scss/var-substring-match.scss
new file mode 100644 (file)
index 0000000..5acfb79
--- /dev/null
@@ -0,0 +1,8 @@
+@mixin mx($f: 10px) {
+  .foo {
+    font: {
+      size: $f;
+    }
+  }
+}
+@include mx;
\ No newline at end of file
diff --git a/theme-compiler/tests/src/com/vaadin/sass/internal/util/StringUtilTest.java b/theme-compiler/tests/src/com/vaadin/sass/internal/util/StringUtilTest.java
new file mode 100644 (file)
index 0000000..b05b0e9
--- /dev/null
@@ -0,0 +1,53 @@
+package com.vaadin.sass.internal.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class StringUtilTest {
+
+    @Test
+    public void testContainsVariable() {
+        String sentence = "$var1 var2";
+        String word = "var";
+        Assert.assertFalse(StringUtil.containsVariable(sentence, word));
+
+        word = "var1";
+        Assert.assertTrue(StringUtil.containsVariable(sentence, word));
+
+        String var2 = "var2";
+        Assert.assertFalse(StringUtil.containsVariable(sentence, var2));
+    }
+
+    @Test
+    public void testContainsVariableWithDash() {
+        String sentence = "$var- var2";
+        String word = "var";
+        Assert.assertFalse(StringUtil.containsVariable(sentence, word));
+    }
+
+    @Test
+    public void testReplaceVariable() {
+        String sentence = "$var1 var2";
+        String word = "var";
+        String value = "abc";
+        Assert.assertEquals(sentence,
+                StringUtil.replaceVariable(sentence, word, value));
+
+        word = "var1";
+        Assert.assertEquals("abc var2",
+                StringUtil.replaceVariable(sentence, word, value));
+
+        String var2 = "var2";
+        Assert.assertEquals(sentence,
+                StringUtil.replaceVariable(sentence, var2, value));
+    }
+
+    @Test
+    public void testReplaceVariableWithDash() {
+        String sentence = "$var- var2";
+        String word = "var";
+        String value = "abc";
+        Assert.assertEquals(sentence,
+                StringUtil.replaceVariable(sentence, word, value));
+    }
+}