Skip to content

Commit b79c68e

Browse files
committed
introducing a special function type for calc
1 parent ab54b73 commit b79c68e

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

src/main/java/com/gargoylesoftware/css/dom/CSSValueImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ public CSSPrimitiveValueType getPrimitiveType() {
372372
case UNICODERANGE:
373373
case SUB_EXPRESSION:
374374
case FUNCTION:
375+
case FUNCTION_CALC:
375376
return CSSPrimitiveValueType.CSS_STRING;
376377
case DIMENSION:
377378
return CSSPrimitiveValueType.CSS_DIMENSION;
@@ -445,7 +446,8 @@ public String getStringValue() throws DOMException {
445446
}
446447

447448
// for rgba values we are using this type
448-
if (lu.getLexicalUnitType() == LexicalUnitType.FUNCTION) {
449+
if (lu.getLexicalUnitType() == LexicalUnitType.FUNCTION
450+
|| lu.getLexicalUnitType() == LexicalUnitType.FUNCTION_CALC) {
449451
return lu.toString();
450452
}
451453
}

src/main/java/com/gargoylesoftware/css/parser/AbstractCSSParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,9 @@ else if ("rect(".equalsIgnoreCase(funct)) {
766766
else if ("rgb(".equalsIgnoreCase(funct)) {
767767
return LexicalUnitImpl.createRgbColor(prev, params);
768768
}
769+
else if ("calc(".equalsIgnoreCase(funct)) {
770+
return LexicalUnitImpl.createCalc(prev, params);
771+
}
769772
return LexicalUnitImpl.createFunction(
770773
prev,
771774
funct.substring(0, funct.length() - 1),

src/main/java/com/gargoylesoftware/css/parser/LexicalUnit.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* Lexical unit of css values.
1919
*
20-
* @author Ronald brill
20+
* @author Ronald Brill
2121
*/
2222
public interface LexicalUnit {
2323

@@ -73,6 +73,7 @@ enum LexicalUnitType {
7373
UNICODERANGE,
7474
SUB_EXPRESSION,
7575
FUNCTION,
76+
FUNCTION_CALC,
7677
DIMENSION
7778
}
7879

src/main/java/com/gargoylesoftware/css/parser/LexicalUnitImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ public String getCssText() {
434434
}
435435
break;
436436
case FUNCTION:
437+
case FUNCTION_CALC:
437438
final String functName = getFunctionName();
438439
if (null != functName) {
439440
sb.append(functName);
@@ -698,6 +699,7 @@ public String toDebugString() {
698699
.append(")");
699700
break;
700701
case FUNCTION:
702+
case FUNCTION_CALC:
701703
sb.append("FUNCTION(")
702704
.append(getFunctionName())
703705
.append("(");
@@ -1027,6 +1029,15 @@ public static LexicalUnit createRgbColor(final LexicalUnit prev, final LexicalUn
10271029
return new LexicalUnitImpl(prev, LexicalUnitType.RGBCOLOR, "rgb", params);
10281030
}
10291031

1032+
/**
1033+
* @param prev the previous LexicalUnit
1034+
* @param params the params
1035+
* @return lexical unit with type calc
1036+
*/
1037+
public static LexicalUnit createCalc(final LexicalUnit prev, final LexicalUnit params) {
1038+
return new LexicalUnitImpl(prev, LexicalUnitType.FUNCTION_CALC, "calc", params);
1039+
}
1040+
10301041
/**
10311042
* @param prev the previous LexicalUnit
10321043
* @param name the name

src/test/java/com/gargoylesoftware/css/parser/CSS3ParserTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ public void calcPlus() throws Exception {
10531053

10541054
final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
10551055
LexicalUnitImpl unit = (LexicalUnitImpl) value.getValue();
1056-
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
1056+
Assert.assertEquals(LexicalUnitType.FUNCTION_CALC, unit.getLexicalUnitType());
10571057
Assert.assertEquals("calc", unit.getFunctionName());
10581058

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

10941094
final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
10951095
LexicalUnitImpl unit = (LexicalUnitImpl) value.getValue();
1096-
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
1096+
Assert.assertEquals(LexicalUnitType.FUNCTION_CALC, unit.getLexicalUnitType());
10971097
Assert.assertEquals("calc", unit.getFunctionName());
10981098

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

12181218
final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
12191219
LexicalUnitImpl unit = (LexicalUnitImpl) value.getValue();
1220-
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
1220+
Assert.assertEquals(LexicalUnitType.FUNCTION_CALC, unit.getLexicalUnitType());
12211221
Assert.assertEquals("calc", unit.getFunctionName());
12221222

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

12651265
final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
12661266
LexicalUnitImpl unit = (LexicalUnitImpl) value.getValue();
1267-
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
1267+
Assert.assertEquals(LexicalUnitType.FUNCTION_CALC, unit.getLexicalUnitType());
12681268
Assert.assertEquals("calc", unit.getFunctionName());
12691269

12701270
unit = (LexicalUnitImpl) unit.getParameters();

0 commit comments

Comments
 (0)