Skip to content

Commit

Permalink
Added e function. Close #65
Browse files Browse the repository at this point in the history
  • Loading branch information
meri committed Dec 23, 2012
1 parent dbd1166 commit 5ddbb9d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
import com.github.sommeri.less4j.core.ast.CssString;
import com.github.sommeri.less4j.core.ast.EscapedValue;
import com.github.sommeri.less4j.core.ast.Expression;
import com.github.sommeri.less4j.core.ast.FaultyExpression;
import com.github.sommeri.less4j.core.ast.FunctionExpression;
import com.github.sommeri.less4j.core.ast.NumberExpression;
import com.github.sommeri.less4j.core.problems.ProblemsHandler;
import com.github.sommeri.less4j.utils.InStringCssPrinter;

public class StringFunctions implements FunctionsPackage {

protected static final String ESCAPE = "escape";
protected static final String E = "e";

private static Map<String, Function> FUNCTIONS = new HashMap<String, Function>();
static {
FUNCTIONS.put(ESCAPE, new Escape());
FUNCTIONS.put(E, new E());
}

private final ProblemsHandler problemsHandler;
Expand All @@ -42,6 +47,40 @@ public Expression evaluate(FunctionExpression input, Expression parameters) {

}

class E implements Function {

@Override
public Expression evaluate(Expression parameters, ProblemsHandler problemsHandler) {
if (parameters.getType()==ASTCssNodeType.STRING_EXPRESSION)
return evaluate((CssString) parameters);

if (parameters.getType()==ASTCssNodeType.ESCAPED_VALUE)
return evaluate((EscapedValue) parameters);

if (parameters.getType()==ASTCssNodeType.NUMBER)
return evaluate((NumberExpression) parameters);

problemsHandler.warnEFunctionArgument(parameters);

return new FaultyExpression(parameters);
}

private Expression evaluate(NumberExpression parameters) {
InStringCssPrinter printer = new InStringCssPrinter();
printer.append(parameters);
return new CssString(parameters.getUnderlyingStructure(), printer.toString(), "");
}

private Expression evaluate(EscapedValue parameters) {
return parameters;
}

private CssString evaluate(CssString parameters) {
return new CssString(parameters.getUnderlyingStructure(), parameters.getValue(), "");
}

}

class Escape implements Function {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public void warnEscapeFunctionArgument(Expression errorNode) {
collector.addWarning(new CompilationWarning(errorNode, "Escape function argument should be a string."));
}

public void warnEFunctionArgument(Expression errorNode) {
collector.addError(new CompilationError(errorNode, "e function argument should be a string."));
}

public void variableAsPseudoclassParameter(PseudoClass errorNode) {
collector.addWarning(new CompilationWarning(errorNode.getParameter(), "Variables as pseudo classes parameters have been deprecated. Use selector interpolation @{variableName} instead."));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#specialCharactersTreatment {
content2: ';
content3: \";
content4: \';
content5: ";
content7: \";
content8: \';
}
h1 {
margin: 1 2 2 3;
padding: 2 2;
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/compile-basic-features/escaping/escaping.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#specialCharactersTreatment {
//content1: ~"""; syntax error
content2: ~"'";
content3: ~"\"";
content4: ~"\'";
content5: ~'"';
//content6: ~'''; syntax error
content7: ~'\"';
content8: ~'\'';
}

(~"h1") {
margin: 1 ~"2 2" 3;
padding: ~'2 2';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#e {
content1: ' \" \' " \" \';
content2: this is something {};
something1: 1+2;;
}
#e-NonString {
e1: abcdefghijklmnopqrstuvxyz ABCDEFGHIJKLMNOPQRSTUVXYZ09;
e2: ~!@#$^&*()_+}{|:> <.,';][p/\";
e3: \\;
e4: //;
e5: 11;
e5: 33;
}
#escape {
escape1: abcdefghijklmnopqrstuvxyz%20ABCDEFGHIJKLMNOPQRSTUVXYZ09;
escape2: %3B%20,%20/%20?%20%3A%20@%20&%20%3D%20+%20$%20-%20_%20.%20!%20~%20*%20'%20%28%20%29;
Expand All @@ -17,4 +30,7 @@
escape6b: undefined;
escape7: identifier;
escape8: 11;
}
#e-escape {
something1: 1+2%3B;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#e {

content1: e("'") e("\"") e("\'") e('"') e('\"') e('\'');
content2: e("this is something {}");
something1: e("1+2;");
// something2: e(`1+2`); TODO javascript is not implemented yet
}

#e-NonString {
e1: e(~"abcdefghijklmnopqrstuvxyz ABCDEFGHIJKLMNOPQRSTUVXYZ09");
e2: e(~"~!@#$^&*()_+}{|:> <.,';][p/\"");
e3: e(~"\\");
e4: e(~"//");
e5: e(11);
e5: e(11+22);
// e5: e(green); returns weird objects
// e5: e(#ff8877); returns weird objects
// e9: e(11 12 14 "test"); returns weird objects
}

#escape {
Expand All @@ -25,6 +40,10 @@
// escape9: escape(11 12 14 "test");
}

#e-escape {
something1: escape(e("1+2;"));
}

#format {
// format1: %("something");
}
Expand Down

0 comments on commit 5ddbb9d

Please sign in to comment.