Skip to content

Commit

Permalink
Merge minus operators with subsequent terms
Browse files Browse the repository at this point in the history
This happens for all non-calc and non-url functions.
  • Loading branch information
Mangara committed Mar 24, 2018
1 parent b96265e commit b0212aa
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 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,63 @@ 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
int minusCount = 0;

for (Term<?> term : value) {
if (term instanceof TermOperator && ((TermOperator) term).getValue() == '-') {
minusCount++;
} else if (minusCount > 0) {
if (prependMinuses(term, minusCount)) {
// Remove merged minuses
for (int i = 0; i < minusCount; i++) {
this.value.remove(this.value.size() - 1);
}
}

minusCount = 0;
}

this.value.add(term);
}

return this;
}

protected boolean prependMinuses(Term<?> term, int minusCount) {
boolean merged = false;

if (term instanceof TermFloatValue) { // includes TermAngle, TermLength, etc.
TermFloatValue floatT = (TermFloatValue) term;
if (minusCount % 2 == 1) {
floatT.setValue(-1 * floatT.getValue());
}
merged = true;
} else if (term instanceof TermIdent) {
TermIdent ident = (TermIdent) term;
StringBuilder minuses = new StringBuilder();
for (int i = 0; i < minusCount; i++) {
minuses.append('-');
}
ident.setValue(minuses + ident.getValue());
merged = true;
} else if (term instanceof TermFunction) {
TermFunction func = (TermFunction) term;
StringBuilder minuses = new StringBuilder();
for (int i = 0; i < minusCount; i++) {
minuses.append('-');
}
func.setFunctionName(minuses + func.getFunctionName());
merged = true;
}

return merged;
}

@Override
public List<List<Term<?>>> getSeparatedArgs(Term<?> separator) {
List<List<Term<?>>> ret = new ArrayList<>();
Expand Down

2 comments on commit b0212aa

@radkovo
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this seems fine for some range of functions. Is there any reason for supporting more than a single '-'? It seems to be syntactically incorrect at least for the numeric values. For the same reason, I have some doubts regarding the combination of '-' and an identifier. Actually, when the arguments are incorrect, the function should not be accepted at all and the declaration should be ignored.

@Mangara
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I assumed double negation would be valid, but it's not.

The only use case for double-minus I see is for supporting the var function, although actual support for that would require a lot of other changes, so it can probably be treated as a special case (like calc).

Regarding single-minus + identifier, there don't seem to be many use cases for these, but I did find a family of functions that takes arbitrary user-defined identifiers that are allowed to start with a dash (but not two). As long as there is no full function argument validation, rejecting these seems premature.

Please sign in to comment.