Skip to content

Commit 539e93a

Browse files
committed
prepare for partial analysis of css variable, when a single default is present, alter parsing for normal paramaters (need more work for nested declaration), currently only a failed attempt to just ignore)
1 parent 2fb749d commit 539e93a

File tree

11 files changed

+337
-14
lines changed

11 files changed

+337
-14
lines changed

org/w3c/css/parser/CssFouffa.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,9 @@ public CssProperty handleDeclaration(String property, CssExpression expression,
587587
if (Util.onDebug) {
588588
System.err.println("Creating " + property + ": " + expression);
589589
}
590-
590+
if (property.startsWith("--")) {
591+
// css variable
592+
}
591593
final CssValue lastValue = expression.getLastValue();
592594

593595
if (allowBackslash9Hack() && lastValue != null && lastValue.hasBackslash9Hack()) {

org/w3c/css/parser/CssPropertyFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ public synchronized CssProperty createProperty(ApplContext ac, AtRule atRule, St
253253
throw new WarningParamException("vendor-extension", expression.toStringFromStart());
254254
}
255255

256+
if (expression.hasCssVariable()) {
257+
throw new WarningParamException("css-variable", expression.toStringFromStart());
258+
}
259+
256260
if (ac.getTreatCssHacksAsWarnings() && expression.hasCssHack()) {
257261
throw new WarningParamException("css-hack", expression.toStringFromStart());
258262
}

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

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import org.w3c.css.values.CssRatio;
5959
import org.w3c.css.values.CssSemitone;
6060
import org.w3c.css.values.CssTypes;
6161
import org.w3c.css.values.CssVolume;
62+
import org.w3c.css.values.CssVariable;
6263
import org.w3c.css.properties.css.CssProperty;
6364
import org.w3c.css.parser.CssError;
6465
import org.w3c.css.parser.CssErrorToken;
@@ -405,11 +406,23 @@ public abstract class CssParser {
405406
if (f.getParameters().hasVendorExtensions()) {
406407
expr.markVendorExtension();
407408
}
409+
if (f.getParameters().hasCssVariable()) {
410+
expr.markCssVariable();
411+
}
408412
if (f.getParameters().hasCssHack()) {
409413
expr.markCssHack();
410414
}
415+
} else if ( v.getType() == CssTypes.CSS_CALC) {
416+
CssCalc c = (CssCalc) v;
417+
if (c.hasCssVariable()) {
418+
expr.markCssVariable();
419+
}
411420
}
412-
}
421+
} else if ( token == FUNCTIONVAR ) {
422+
if (v.getType() == CssTypes.CSS_VARIABLE) {
423+
expr.markCssVariable();
424+
}
425+
}
413426
if (n != null) {
414427
if (ac.getCssVersion() == CssVersion.CSS1 &&
415428
(n.image).equals("inherit")) {
@@ -2873,7 +2886,8 @@ void term(CssExpression exp) :
28732886
| func=mathmin() { setValue(func, exp, operator, null, FUNCTION); }
28742887
| func=mathclamp() { setValue(func, exp, operator, null, FUNCTION); }
28752888
| func=attr() { setValue(func, exp, operator, null, FUNCTION); }
2876-
| n=<FUNCTIONVAR> ( <S> )* <CUSTOM_PROPERTY_NAME> ( <COMMA> ( <S> )* expr() )* ( <S> )* <LPARAN>
2889+
| func=functionvar() { setValue(func, exp, operator, null, FUNCTIONVAR); }
2890+
/* | n=<FUNCTIONVAR> ( <S> )* <CUSTOM_PROPERTY_NAME> ( <COMMA> ( <S> )* expr() )* ( <S> )* <LPARAN> */
28772891
| func=function() { setValue(func, exp, operator, null, FUNCTION); }
28782892
| n=<STRING> { setValue(new CssString(), exp, operator, n, STRING); }
28792893
| n=<DIV> { setValue(new CssSwitch(), exp, operator, n, DIV); }
@@ -3114,6 +3128,9 @@ CssCheckableValue mathsum() :
31143128
nb_pre_sp = 0;
31153129
if (concat) {
31163130
nc = new CssCalc(ac, c);
3131+
if (c.hasCssVariable()) {
3132+
nc.markCssVariable();
3133+
}
31173134
nc.addRightSide(o.image.trim(), v2);
31183135
c = nc;
31193136
} else {
@@ -3130,6 +3147,7 @@ CssCheckableValue mathproduct() :
31303147
{ Token n,o;
31313148
CssCheckableValue v1 = null;
31323149
CssCheckableValue v2 = null;
3150+
CssValue f;
31333151
CssCalc c, nc;
31343152
char operator = ' ';
31353153
boolean concat = false;
@@ -3139,11 +3157,14 @@ CssCheckableValue mathproduct() :
31393157
( ( o="*" ( <S> )* v2=mathunit() )
31403158
|
31413159
( o=<DIV> ( <S> )* ( v2=calcnumbervalue()
3142-
| n=<FUNCTIONVAR> ( <S> )* <CUSTOM_PROPERTY_NAME> ( <COMMA> ( <S> )* expr() )* ( <S> )* <LPARAN> )
3160+
| n=<FUNCTIONVAR> ( <S> )* <CUSTOM_PROPERTY_NAME> ( <COMMA> ( <S> )* expr() )* ( <S> )* <LPARAN> { c.markCssVariable(); } )
31433161
)
31443162
) {
31453163
if (concat) {
31463164
nc = new CssCalc(ac, c);
3165+
if (c.hasCssVariable()) {
3166+
nc.markCssVariable();
3167+
}
31473168
nc.addRightSide(o.image, v2);
31483169
c = nc;
31493170
} else {
@@ -3247,7 +3268,7 @@ char operator = ' ';
32473268
| v=mathmin()
32483269
| v=mathclamp()
32493270
| v=attr()
3250-
| n=<FUNCTIONVAR> ( <S> )* <CUSTOM_PROPERTY_NAME> ( <COMMA> ( <S> )* expr() )* ( <S> )* <LPARAN>
3271+
| n=<FUNCTIONVAR> ( <S> )* <CUSTOM_PROPERTY_NAME> ( <COMMA> ( <S> )* expr() )* ( <S> )* <LPARAN> { CssCalc c = new CssCalc(); c.markCssVariable(); return c; }
32513272
) {
32523273
return v;
32533274
}
@@ -3273,6 +3294,35 @@ CssExpression exp = null;
32733294
}
32743295
}
32753296

3297+
CssValue functionvar() :
3298+
{ Token n;
3299+
CssExpression exp = null;
3300+
CssExpression e;
3301+
CssVariable var;
3302+
}
3303+
{
3304+
<FUNCTIONVAR> ( <S> )* n=<CUSTOM_PROPERTY_NAME> {
3305+
var = new CssVariable(ac, convertIdent(n.image));
3306+
}
3307+
( <COMMA> ( <S> )* e=expr() { if (exp == null) {
3308+
exp = e;
3309+
} else {
3310+
exp.setOperator(CssOperator.COMMA) ;
3311+
while (!e.end()) {
3312+
exp.addValue(e.getValue());
3313+
exp.setOperator(e.getOperator());
3314+
e.next();
3315+
}
3316+
}
3317+
} )* ( <S> )* <LPARAN>
3318+
{
3319+
if (exp != null) {
3320+
var.set(exp);
3321+
}
3322+
return var;
3323+
}
3324+
}
3325+
32763326
/**
32773327
* @exception ParseException exception during the parse
32783328
*/

org/w3c/css/properties/css3/CssAspectRatio.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.w3c.css.values.CssValue;
1515
import org.w3c.css.values.CssValueList;
1616

17-
import java.math.BigDecimal;
1817
import java.util.ArrayList;
1918

2019
import static org.w3c.css.values.CssOperator.SPACE;
@@ -49,8 +48,8 @@ public CssAspectRatio(ApplContext ac, CssExpression expression, boolean check)
4948
char op;
5049
int ratio_state = 0;
5150
setByUser();
52-
BigDecimal dividend = null;
53-
BigDecimal divisor = null;
51+
CssValue dividend = null;
52+
CssValue divisor = null;
5453

5554
while (!expression.end()) {
5655
val = expression.getValue();
@@ -60,11 +59,11 @@ public CssAspectRatio(ApplContext ac, CssExpression expression, boolean check)
6059
// so we are cheating and create a CssRatio when needed.
6160
case CssTypes.CSS_NUMBER:
6261
if (ratio_state == 0) {
63-
dividend = val.getNumber().getBigDecimalValue();
62+
dividend = val;
6463
ratio_state++;
6564
break;
6665
} else if (ratio_state == 2) {
67-
divisor = val.getNumber().getBigDecimalValue();
66+
divisor = val;
6867
ratio_state++;
6968
v.add(new CssRatio(dividend, divisor)) ;
7069
break;

org/w3c/css/util/Messages.properties.en

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ warning.noothermedium : Properties for other media might not work for usermedium
311311
warning.vendor-extension : \u201C%s\u201D is an unknown vendor extension
312312
warning.vendor-ext-pseudo-class : \u201C%s\u201D is an unknown vendor extended pseudo-class
313313
warning.vendor-ext-pseudo-element : \u201C%s\u201D is an unknown vendor extended pseudo-element
314+
warning.css-variable: Due to their dynamic nature, CSS variables are currently not statically checked
314315

315316
warning.css-hack : \u201C%s\u201D is a CSS hack
316317

org/w3c/css/util/Messages.properties.fr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ error.notforatsc: \u201C%s\u201D ne peut pas être utilisé pour le profil ATSC
420420
error.notfortv: \u201C%s\u201D ne peut pas être utilisé pour le profil TV
421421
error.notversion: \u201C%s\u201D ne peut pas être utilisé pour cette version CSS: \u201C%s\u201D
422422

423+
warning.css-variable: En raison de laur nature dynamique, les variables CSS ne sont pas vérifiées statiquement
424+
423425
warning.css-hack : \u201C%s\u201D est un hack CSS
424426

425427
warning.atsc : il se peut que \u201C%s\u201D ne soit pas supporté par atsc-tv

org/w3c/css/values/CssCalc.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public final int getType() {
3939
boolean hasParen = false;
4040
String _toString = null;
4141
boolean implicit_function = true;
42+
boolean contains_variable = false;
4243

4344

4445
/**
@@ -60,9 +61,21 @@ public CssCalc(ApplContext ac, CssValue value) {
6061
this.ac = ac;
6162
}
6263
if (value != null) {
63-
computed_type = value.getType();
64-
val1 = value;
64+
if (value.getRawType() == CssTypes.CSS_CALC) {
65+
CssCalc c = (CssCalc) value;
66+
contains_variable = c.hasCssVariable();
67+
}
6568
}
69+
computed_type = value.getType();
70+
val1 = value;
71+
}
72+
73+
public boolean hasCssVariable() {
74+
return contains_variable;
75+
}
76+
77+
public void markCssVariable() {
78+
contains_variable = true;
6679
}
6780

6881
public void setImplicitFunction(boolean v) {
@@ -146,6 +159,10 @@ private void _computeResultingType(boolean end)
146159
throws InvalidParamException {
147160
int valtype;
148161

162+
if (contains_variable) {
163+
// nothing to check as we may not have anything yet
164+
return;
165+
}
149166
if (val2 == null) {
150167
// we only have val1 to check.
151168
valtype = val1.getType();

org/w3c/css/values/CssExpression.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class CssExpression implements CssOperator {
2222

2323
private boolean vendor_extension = false;
2424
private boolean css_hack = false;
25+
private boolean css_variable = false;
2526

2627
public boolean hasVendorExtensions() {
2728
return vendor_extension;
@@ -39,6 +40,14 @@ public void markCssHack() {
3940
css_hack = true;
4041
}
4142

43+
public boolean hasCssVariable() {
44+
return css_variable;
45+
}
46+
47+
public void markCssVariable() {
48+
css_variable = true;
49+
}
50+
4251
/**
4352
* mark the current position, it can be set to this
4453
* position later by using reset

org/w3c/css/values/CssRatio.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public final int getType() {
2424
return type;
2525
}
2626

27-
BigDecimal w, h;
27+
BigDecimal w = null, h = null;
28+
CssValue gw = null, gh = null;
2829

2930

3031
/**
@@ -38,6 +39,37 @@ public CssRatio(BigDecimal w, BigDecimal h) {
3839
this.h = h;
3940
}
4041

42+
public CssRatio(BigDecimal w, CssValue gh) {
43+
this.w = w;
44+
try {
45+
this.h = gh.getNumber().getBigDecimalValue();
46+
} catch (Exception ex) {
47+
this.gh = gh;
48+
}
49+
}
50+
51+
public CssRatio(CssValue gw, BigDecimal h) {
52+
try {
53+
this.w = gw.getNumber().getBigDecimalValue();
54+
} catch (Exception ex) {
55+
this.gw = gw;
56+
}
57+
this.h = h;
58+
}
59+
60+
public CssRatio(CssValue gw, CssValue gh) {
61+
try {
62+
this.w = gw.getNumber().getBigDecimalValue();
63+
} catch (Exception ex) {
64+
this.gw = gw;
65+
}
66+
try {
67+
this.h = gh.getNumber().getBigDecimalValue();
68+
} catch (Exception ex) {
69+
this.gh = gh;
70+
}
71+
}
72+
4173
/**
4274
* Set the value of this ratio.
4375
*
@@ -91,7 +123,17 @@ public Object get() {
91123
*/
92124
public String toString() {
93125
StringBuilder sb = new StringBuilder();
94-
sb.append(w.toPlainString()).append('/').append(h.toPlainString());
126+
if (w != null) {
127+
sb.append(w.toPlainString());
128+
} else {
129+
sb.append(gw.toString()).append(' ');
130+
}
131+
sb.append('/');
132+
if (h != null) {
133+
sb.append(h.toPlainString());
134+
} else {
135+
sb.append(gh.toString());
136+
}
95137
return sb.toString();
96138
}
97139

@@ -110,6 +152,8 @@ public boolean equals(Object value) {
110152
return (ratio.compareTo(other_ratio) == 0);
111153
} catch (ClassCastException cce) {
112154
return false;
155+
} catch (Exception ex) {
156+
return false;
113157
}
114158
}
115159
}

org/w3c/css/values/CssTypes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class CssTypes {
3737
public static final int CSS_MIN = 24;
3838
public static final int CSS_MAX = 25;
3939
public static final int CSS_CLAMP = 26;
40+
public static final int CSS_VARIABLE = 27;
4041

4142
// not generated by the parser
4243
public static final int CSS_VALUE_LIST = 30;

0 commit comments

Comments
 (0)