Skip to content

Commit

Permalink
introducing a special function type for calc
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jul 24, 2021
1 parent ab54b73 commit b79c68e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/gargoylesoftware/css/dom/CSSValueImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ public CSSPrimitiveValueType getPrimitiveType() {
case UNICODERANGE:
case SUB_EXPRESSION:
case FUNCTION:
case FUNCTION_CALC:
return CSSPrimitiveValueType.CSS_STRING;
case DIMENSION:
return CSSPrimitiveValueType.CSS_DIMENSION;
Expand Down Expand Up @@ -445,7 +446,8 @@ public String getStringValue() throws DOMException {
}

// for rgba values we are using this type
if (lu.getLexicalUnitType() == LexicalUnitType.FUNCTION) {
if (lu.getLexicalUnitType() == LexicalUnitType.FUNCTION
|| lu.getLexicalUnitType() == LexicalUnitType.FUNCTION_CALC) {
return lu.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,9 @@ else if ("rect(".equalsIgnoreCase(funct)) {
else if ("rgb(".equalsIgnoreCase(funct)) {
return LexicalUnitImpl.createRgbColor(prev, params);
}
else if ("calc(".equalsIgnoreCase(funct)) {
return LexicalUnitImpl.createCalc(prev, params);
}
return LexicalUnitImpl.createFunction(
prev,
funct.substring(0, funct.length() - 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Lexical unit of css values.
*
* @author Ronald brill
* @author Ronald Brill
*/
public interface LexicalUnit {

Expand Down Expand Up @@ -73,6 +73,7 @@ enum LexicalUnitType {
UNICODERANGE,
SUB_EXPRESSION,
FUNCTION,
FUNCTION_CALC,
DIMENSION
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/gargoylesoftware/css/parser/LexicalUnitImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ public String getCssText() {
}
break;
case FUNCTION:
case FUNCTION_CALC:
final String functName = getFunctionName();
if (null != functName) {
sb.append(functName);
Expand Down Expand Up @@ -698,6 +699,7 @@ public String toDebugString() {
.append(")");
break;
case FUNCTION:
case FUNCTION_CALC:
sb.append("FUNCTION(")
.append(getFunctionName())
.append("(");
Expand Down Expand Up @@ -1027,6 +1029,15 @@ public static LexicalUnit createRgbColor(final LexicalUnit prev, final LexicalUn
return new LexicalUnitImpl(prev, LexicalUnitType.RGBCOLOR, "rgb", params);
}

/**
* @param prev the previous LexicalUnit
* @param params the params
* @return lexical unit with type calc
*/
public static LexicalUnit createCalc(final LexicalUnit prev, final LexicalUnit params) {
return new LexicalUnitImpl(prev, LexicalUnitType.FUNCTION_CALC, "calc", params);
}

/**
* @param prev the previous LexicalUnit
* @param name the name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ public void calcPlus() throws Exception {

final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
LexicalUnitImpl unit = (LexicalUnitImpl) value.getValue();
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
Assert.assertEquals(LexicalUnitType.FUNCTION_CALC, unit.getLexicalUnitType());
Assert.assertEquals("calc", unit.getFunctionName());

unit = (LexicalUnitImpl) unit.getParameters();
Expand Down Expand Up @@ -1093,7 +1093,7 @@ public void calcSum() throws Exception {

final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
LexicalUnitImpl unit = (LexicalUnitImpl) value.getValue();
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
Assert.assertEquals(LexicalUnitType.FUNCTION_CALC, unit.getLexicalUnitType());
Assert.assertEquals("calc", unit.getFunctionName());

unit = (LexicalUnitImpl) unit.getParameters();
Expand Down Expand Up @@ -1217,7 +1217,7 @@ public void calcComplex() throws Exception {

final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
LexicalUnitImpl unit = (LexicalUnitImpl) value.getValue();
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
Assert.assertEquals(LexicalUnitType.FUNCTION_CALC, unit.getLexicalUnitType());
Assert.assertEquals("calc", unit.getFunctionName());

unit = (LexicalUnitImpl) unit.getParameters();
Expand Down Expand Up @@ -1264,7 +1264,7 @@ public void calcCalc() throws Exception {

final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
LexicalUnitImpl unit = (LexicalUnitImpl) value.getValue();
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
Assert.assertEquals(LexicalUnitType.FUNCTION_CALC, unit.getLexicalUnitType());
Assert.assertEquals("calc", unit.getFunctionName());

unit = (LexicalUnitImpl) unit.getParameters();
Expand Down

0 comments on commit b79c68e

Please sign in to comment.