Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for dashes/minuses in function arguments #91

Merged
merged 8 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/antlr4/cz/vutbr/web/csskit/antlr4/CSSParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ funct_argument
) S*
;
catch [RecognitionException re] {
log.error("Recognition exception | valuepart");
log.error("Recognition exception | funct_argument");
IntervalSet intervalSet = new IntervalSet(RCURLY, SEMICOLON);
getCSSErrorHandler().consumeUntil(this, intervalSet, CSSLexerState.RecoveryMode.BALANCED, null);
_localctx.addErrorNode(this.getTokenFactory().create(INVALID_STATEMENT,""));
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/cz/vutbr/web/csskit/TermFunctionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cz.vutbr.web.css.TermFloatValue;
import cz.vutbr.web.css.TermFunction;
import cz.vutbr.web.css.TermIdent;
import cz.vutbr.web.css.TermList;
import cz.vutbr.web.css.TermOperator;

/**
Expand Down Expand Up @@ -40,6 +41,50 @@ public TermFunction setFunctionName(String functionName) {
return this;
}

@Override
public TermList setValue(List<Term<?>> value) {
this.value = new ArrayList<>();

// Treat '-' as modifying the next argument, instead of as an operator
boolean prevMinus = false;

for (Term<?> term : value) {
if (term instanceof TermOperator && ((TermOperator) term).getValue() == '-') {
prevMinus = true;
} else if (prevMinus) {
if (prependMinus(term)) {
this.value.remove(this.value.size() - 1); // Remove merged minus
}

prevMinus = false;
}

this.value.add(term);
}

return this;
}

protected boolean prependMinus(Term<?> term) {
boolean merged = false;

if (term instanceof TermFloatValue) { // includes TermAngle, TermLength, etc.
TermFloatValue floatT = (TermFloatValue) term;
floatT.setValue(-1 * floatT.getValue());
merged = true;
} else if (term instanceof TermIdent) {
TermIdent ident = (TermIdent) term;
ident.setValue("-" + ident.getValue());
merged = true;
} else if (term instanceof TermFunction) {
TermFunction func = (TermFunction) term;
func.setFunctionName("-" + func.getFunctionName());
merged = true;
}

return merged;
}

@Override
public List<List<Term<?>>> getSeparatedArgs(Term<?> separator) {
List<List<Term<?>>> ret = new ArrayList<>();
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/cz/vutbr/web/csskit/antlr4/CSSParserVisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1072,53 +1072,54 @@ public Object visitFunct_argument(CSSParser.Funct_argumentContext ctx)
return null;
}
if (ctx.PLUS() != null) {
log.debug("VP - plus");
log.debug("FA - plus");
funct_args_stack.peek().term = tf.createOperator('+');
} else if (ctx.MINUS() != null) {
log.debug("VP - minus");
log.debug("FA - minus");
funct_args_stack.peek().term = tf.createOperator('-');
} else if (ctx.ASTERISK() != null) {
log.debug("VP - *");
log.debug("FA - *");
funct_args_stack.peek().term = tf.createOperator('*');
} else if (ctx.SLASH() != null) {
log.debug("VP - /");
log.debug("FA - /");
funct_args_stack.peek().term = tf.createOperator('/');
} else if (ctx.LPAREN() != null) {
log.debug("VP - (");
log.debug("FA - (");
funct_args_stack.peek().term = tf.createOperator('(');
} else if (ctx.RPAREN() != null) {
log.debug("VP - )");
log.debug("FA - )");
funct_args_stack.peek().term = tf.createOperator(')');
} else if (ctx.COMMA() != null) {
log.debug("VP - comma");
log.debug("FA - comma");
funct_args_stack.peek().term = tf.createOperator(',');
} else if (ctx.string() != null) {
log.debug("VP - string");
log.debug("FA - string");
funct_args_stack.peek().term = tf.createString(extractTextUnescaped(ctx.string().getText()));
} else if (ctx.IDENT() != null) {
log.debug("VP - ident");
log.debug("FA - ident");
funct_args_stack.peek().term = tf.createIdent(extractTextUnescaped(ctx.IDENT().getText()));
} else if (ctx.PERCENTAGE() != null) {
log.debug("VP - percentage");
log.debug("FA - percentage");
funct_args_stack.peek().term = tf.createPercent(ctx.PERCENTAGE().getText(), 1);
} else if (ctx.DIMENSION() != null) {
log.debug("VP - dimension");
log.debug("FA - dimension");
String dim = ctx.DIMENSION().getText();
funct_args_stack.peek().term = tf.createDimension(dim, 1);
if (funct_args_stack.peek().term == null) {
log.info("Unable to create dimension from {}, unary {}", dim, 1);
declaration_stack.peek().invalid = true;
}
} else if (ctx.HASH() != null) {
log.debug("VP - hash");
log.debug("FA - hash");
funct_args_stack.peek().term = tf.createColor(ctx.HASH().getText());
if (funct_args_stack.peek().term == null) {
declaration_stack.peek().invalid = true;
}
} else if (ctx.NUMBER() != null) {
log.debug("VP - number");
log.debug("FA - number");
funct_args_stack.peek().term = tf.createNumeric(ctx.NUMBER().getText(), 1);
} else if (ctx.funct() != null) {
log.debug("FA - funct");
funct_args_stack.peek().term = null;
Term<?> fnterm = (Term<?>) visitFunct(ctx.funct());
if (fnterm != null) {
Expand Down
30 changes: 21 additions & 9 deletions src/test/java/test/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,23 @@ public class FunctionsTest {
/* function name may start with minus */
public static final String TEST_DECL2A = " a:after { background-image:-moz-linear-gradient(top,#fff,#ececec); border: 1px solid red; }";

/* function arguments may include minus */
public static final String TEST_DECL3A = "img { translate: translateY(-.1em) }";

/* different rect() syntaxes */
public static final String TEST_RECT1 = "p { clip: rect(1px 10em 3rem 2ch); color: red; }";
public static final String TEST_RECT2 = "p { clip: rect(1px, 10em, 3rem, 2ch); color: red; }";
public static final String TEST_RECT3 = "p { clip: rect(1px, 10em, 3rem, auto); color: red; }";

/* calc() length expressions (all should evaluate to 60.0) */
public static final String TEST_CALC_L[];
static {
TEST_CALC_L = new String[5];
TEST_CALC_L[0] = "p { width: calc(60px); color: red; }";
TEST_CALC_L[1] = "p { width: calc(1em + 0.5em); color: red; }";
TEST_CALC_L[2] = "p { width: calc(1em + (10% * 2)); color: red; }";
TEST_CALC_L[3] = "p { width: calc(-3em + 4.5em); color: red; }";
TEST_CALC_L[4] = "p { width: calc(3em + (-1.5em)); color: red; }";
}
public static final String TEST_CALC_L[] = new String[] {
"p { width: calc(60px); color: red; }",
"p { width: calc(1em + 0.5em); color: red; }",
"p { width: calc(1em + (10% * 2)); color: red; }",
"p { width: calc(-3em + 4.5em); color: red; }",
"p { width: calc(3em + (-1.5em)); color: red; }",
"p { width: calc(3em - 1.5em); color: red; }"
};

/* calc() angle expressions (all should evaluate to 33) */
public static final String TEST_CALC_A[];
Expand Down Expand Up @@ -94,6 +96,16 @@ public void vendorSpecificFunctions() throws IOException, CSSException
char first = f.getFunctionName().charAt(0);
assertEquals("Function name starts with minus", '-', first);
}

@Test
public void negativeArgument() throws IOException, CSSException
{
StyleSheet ss = CSSFactory.parseString(TEST_DECL3A, null);
assertEquals("One properties is accepted", 1, ss.get(0).size());
Declaration d = (Declaration) ss.get(0).get(0);
TermFunction f = (TermFunction) d.get(0);
assertEquals("The argument is -0.1em", tf.createLength(-0.1f, Unit.em), f.get(0));
}

@Test
public void rectFunctions() throws IOException, CSSException
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/test/GradientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void testGradientAngles() throws IOException, CSSException {
ss = CSSFactory.parseString(TEST_LINEAR_GRADIENT_ANGLE_NEGATIVE, null);
separatedArguments = ((TermFunction) ((RuleSet) ss.get(0)).get(0).get(0)).getSeparatedArgs(tf.createOperator(','));
assertEquals("There are 3 comma-separated arguments ", 3, separatedArguments.size());
assertEquals("The first argument is -10deg ", Arrays.asList(tf.createOperator('-'), tf.createAngle("10deg", TermNumeric.Unit.deg, 1)), separatedArguments.get(0));
assertEquals("The first argument is -10deg ", Arrays.asList(tf.createAngle("10deg", TermNumeric.Unit.deg, -1)), separatedArguments.get(0));
}

@Test
Expand Down