Skip to content

Commit 1900d1d

Browse files
committed
trig functions + e and pi
1 parent b824567 commit 1900d1d

File tree

3 files changed

+126
-9
lines changed

3 files changed

+126
-9
lines changed

org/w3c/css/parser/analyzer/CssParser.jj

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,8 @@ TOKEN [IGNORE_CASE] :
775775
| <FUNCTIONNOT : "not(" >
776776
| <FUNCTIONCALC : "calc(" | "-moz-calc(" | "-webkit-calc(" >
777777
| <FUNCTIONMATHN : "min(" | "max(" | "hypot(" >
778-
| <FUNCTIONMATH1 : "sin(" | "cos(" | "tan(" | "asin(" | "acos(" | "atan(" | "sqrt(" | "log(" | "exp(" | "sign(" >
778+
| <FUNCTIONMATH1 : "sin(" | "cos(" | "tan(" | "asin(" | "acos(" | "atan(" | "sqrt(" | "exp(" | "abs(" | "sign(" >
779+
| <FUNCTIONMATH2 : "mod(" | "rem(" | "atan2(" | "pow(" | "log(" >
779780
| <FUNCTIONCLAMP : "clamp(" >
780781
| <FUNCTIONATTR : "attr(" >
781782
| <FUNCTIONVAR : "var(" >
@@ -3300,18 +3301,25 @@ void mediaterm(CssExpression exp) :
33003301
}
33013302

33023303
CssCheckableValue mathfunction() :
3304+
{
3305+
CssCheckableValue v;
3306+
}
3307+
{
3308+
( v=mathfunction1() | v=mathfunction2() | v=mathfunctionn() ) {
3309+
return v;
3310+
}
3311+
}
3312+
3313+
3314+
CssCheckableValue mathfunctionn() :
33033315
{
33043316
CssCheckableValue v;
33053317
CssMathFunction c;
33063318
Token n;
33073319
}
33083320
{
33093321
( n=<FUNCTIONMATHN> ( <S> )* v=mathsum() {
3310-
// if (v.getRawType() != CssTypes.CSS_MATH_FUNCTION) {
33113322
c = new CssMathFunction(ac, n.image, v);
3312-
// } else {
3313-
// c = (CssMathFunction) v;
3314-
// }
33153323
}
33163324
( <S> )* ( <COMMA> ( <S> )* v=mathsum() {
33173325
c.addValue(v);
@@ -3321,6 +3329,42 @@ CssCheckableValue mathfunction() :
33213329
}
33223330
}
33233331

3332+
CssCheckableValue mathfunction1() :
3333+
{
3334+
CssCheckableValue v;
3335+
CssMathFunction c;
3336+
Token n;
3337+
}
3338+
{
3339+
( n=<FUNCTIONMATH1> ( <S> )* v=mathsum() {
3340+
c = new CssMathFunction(ac, n.image, v);
3341+
}
3342+
( <S> )* <RPAREN> ) {
3343+
c.validate();
3344+
return c;
3345+
}
3346+
}
3347+
3348+
CssCheckableValue mathfunction2() :
3349+
{
3350+
CssCheckableValue v;
3351+
CssMathFunction c;
3352+
Token n;
3353+
}
3354+
{
3355+
( n=<FUNCTIONMATH2> ( <S> )* v=mathsum() {
3356+
c = new CssMathFunction(ac, n.image, v);
3357+
}
3358+
( <S> )* ( <COMMA> ( <S> )* v=mathsum() {
3359+
c.addValue(v);
3360+
} ( <S> )* )? <RPAREN> ) {
3361+
c.validate();
3362+
return c;
3363+
}
3364+
}
3365+
3366+
3367+
33243368
CssCheckableValue mathclamp() :
33253369
{
33263370
CssCheckableValue v1, v2, v3;
@@ -3454,7 +3498,7 @@ CssCheckableValue calcnumbervalue() :
34543498
}
34553499
{
34563500
(
3457-
( ( operator=unaryOperator() )? n=<NUMBER> { v = new CssNumber(); v.set(addOperator(operator, n.image), ac); } ) |
3501+
( ( operator=unaryOperator() )? ( n=<NUMBER> | n=<IDENT> ) { v = new CssNumber(); v.set(addOperator(operator, n.image), ac); } ) |
34583502
( <LPAREN> ( <S> )* v=calcnumbersum() ( <S> )* <RPAREN> { try { ((CssCalc) v).setParenthesis(); } catch (Exception ex) { ex.printStackTrace();}} )
34593503
) {
34603504
return v;
@@ -3523,7 +3567,7 @@ CssCheckableValue v = null;
35233567
char operator = ' ';
35243568
}
35253569
{
3526-
( ( operator=unaryOperator() )? ( n=<NUMBER> { v = new CssNumber(); v.set(addOperator(operator, n.image), ac); }
3570+
( ( operator=unaryOperator() )? ( ( n=<NUMBER> | n=<IDENT> ) { v = new CssNumber(); v.set(addOperator(operator, n.image), ac); }
35273571
| n=<PERCENTAGE> { v = new CssPercentage(); v.set(addOperator(operator, n.image), ac); }
35283572
| n=<RELFONTLENGTH> { v = new CssLength(); v.set(addOperator(operator, n.image), ac); }
35293573
| n=<RELVIEWLENGTH> { v = new CssLength(); v.set(addOperator(operator, n.image), ac); }
@@ -3845,6 +3889,7 @@ String skip_to_matching_paren() {
38453889
case FUNCTIONCALC:
38463890
case FUNCTIONMATHN:
38473891
case FUNCTIONMATH1:
3892+
case FUNCTIONMATH2:
38483893
case FUNCTIONCLAMP:
38493894
case FUNCTIONATTR:
38503895
case FUNCTIONVAR:

org/w3c/css/values/CssMathFunction.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,47 @@ private int _checkAcceptableType(int type)
123123

124124
private void _computeResultingType(boolean first)
125125
throws InvalidParamException {
126+
switch (prefix) {
127+
case "clamp(":
128+
case "min(":
129+
case "max(":
130+
case "hypot(":
131+
_computeResultingTypeList(first);
132+
break;
133+
case "sin(":
134+
case "cos(":
135+
case "tan(":
136+
case "asin(":
137+
case "acos(":
138+
case "atan(":
139+
_computeResultingTypeTrig(first);
140+
break;
141+
default:
142+
throw new InvalidParamException("unrecognize", ac);
143+
}
144+
145+
}
146+
147+
private void _computeResultingTypeTrig(boolean first)
148+
throws InvalidParamException {
149+
int valtype;
150+
if (values.size() > 1) {
151+
throw new InvalidParamException("unrecognize", ac);
152+
}
153+
valtype = values.get(0).getType();
154+
if ((valtype == CssTypes.CSS_NUMBER) || (valtype == CssTypes.CSS_ANGLE)) {
155+
if (prefix.startsWith("a")) {
156+
computed_type = CssTypes.CSS_ANGLE;
157+
} else {
158+
computed_type = CssTypes.CSS_NUMBER;
159+
}
160+
} else {
161+
throw new InvalidParamException("incompatibletypes", toString(), ac);
162+
}
163+
}
164+
165+
private void _computeResultingTypeList(boolean first)
166+
throws InvalidParamException {
126167
int valtype = CssTypes.CSS_MATH_FUNCTION;
127168
boolean firstVal = true;
128169
CssValue prevVal = null;

org/w3c/css/values/CssNumber.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final int getType() {
2828
ApplContext ac;
2929
BigDecimal value;
3030
boolean isInt = false;
31+
String _strval = null;
3132

3233
/**
3334
* Create a new CssNumber
@@ -55,21 +56,48 @@ public CssNumber(float value) {
5556
*/
5657
public void set(String s, ApplContext ac)
5758
throws InvalidParamException {
59+
boolean negate = false;
60+
String val;
5861
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
5962
// check for scientific notation in CSS < 3
6063
if (s.indexOf('e') >= 0 || s.indexOf('E') >= 0) {
6164
throw new InvalidParamException("value", "number", s, ac);
6265
}
6366
}
64-
value = new BigDecimal(s);
65-
isInt = (s.indexOf('.') < 0);
67+
if (s.startsWith("-")) {
68+
negate = true;
69+
val = s.substring(1);
70+
} else if (s.startsWith("+")) {
71+
val = s.substring(1);
72+
} else {
73+
val = s;
74+
}
75+
76+
if (val.equalsIgnoreCase("pi")) {
77+
value = BigDecimal.valueOf(Math.PI);
78+
isInt = false;
79+
_strval = "pi";
80+
} else if (val.equalsIgnoreCase("e")) {
81+
value = BigDecimal.valueOf(Math.E);
82+
isInt = false;
83+
_strval = "e";
84+
} else {
85+
value = new BigDecimal(val);
86+
isInt = (val.indexOf('.') < 0);
6687
/* CSS integers are not value-based integers.
6788
try {
6889
value.toBigIntegerExact();
6990
isInt = true;
7091
} catch (ArithmeticException e) {
7192
isInt = false;
7293
} */
94+
}
95+
if (negate) {
96+
value = value.negate();
97+
if (_strval != null) {
98+
_strval = '-' + _strval;
99+
}
100+
}
73101
this.ac = ac;
74102
}
75103

@@ -242,6 +270,9 @@ public CssNumber getNumber() throws InvalidParamException {
242270
* Returns a string representation of the object.
243271
*/
244272
public String toString() {
273+
if (_strval != null) {
274+
return _strval;
275+
}
245276
return value.toPlainString();
246277
}
247278

0 commit comments

Comments
 (0)