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;
@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());
}
}
}
import java.util.ArrayList;
import com.vaadin.sass.internal.ScssStylesheet;
+import com.vaadin.sass.internal.util.StringUtil;
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());
}
}
}
@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);
}
}
}
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;
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();
}
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
@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());
}
}
}
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 {
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);
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;
@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());
}
}
}
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
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);
+ }
}
--- /dev/null
+.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
--- /dev/null
+.foo {
+ font-size: 10px;
+}
\ No newline at end of file
--- /dev/null
+$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);
+}
+
+
--- /dev/null
+@mixin mx($f: 10px) {
+ .foo {
+ font: {
+ size: $f;
+ }
+ }
+}
+@include mx;
\ No newline at end of file
--- /dev/null
+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));
+ }
+}