Skip to content

Commit e0c9b0d

Browse files
authored
Merge pull request #323 from w3c/css-variable
handling definition of a variable.
2 parents 8332e44 + 21ecea6 commit e0c9b0d

19 files changed

+456
-189
lines changed

org/w3c/css/parser/CssFouffa.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.w3c.css.parser.analyzer.TokenMgrError;
2222
import org.w3c.css.properties.PropertiesLoader;
2323
import org.w3c.css.properties.css.CssProperty;
24+
import org.w3c.css.properties.css3.CssCustomProperty;
2425
import org.w3c.css.util.ApplContext;
2526
import org.w3c.css.util.CssVersion;
2627
import org.w3c.css.util.HTTPURL;
@@ -587,29 +588,32 @@ public CssProperty handleDeclaration(String property, CssExpression expression,
587588
if (Util.onDebug) {
588589
System.err.println("Creating " + property + ": " + expression);
589590
}
590-
if (property.startsWith("--")) {
591+
592+
if (property.startsWith("--") && (ac.getCssVersion().compareTo(CssVersion.CSS3) >= 0)) {
593+
prop = new CssCustomProperty(ac, property, expression);
591594
// css variable
592-
}
593-
final CssValue lastValue = expression.getLastValue();
595+
} else {
596+
final CssValue lastValue = expression.getLastValue();
594597

595-
if (allowBackslash9Hack() && lastValue != null && lastValue.hasBackslash9Hack()) {
596-
expression.markCssHack();
597-
}
598+
if (allowBackslash9Hack() && lastValue != null && lastValue.hasBackslash9Hack()) {
599+
expression.markCssHack();
600+
}
598601

599-
try {
600-
prop = properties.createProperty(ac, getAtRule(), property, expression);
601-
} catch (InvalidParamException e) {
602-
throw e;
603-
} catch (Exception e) {
604-
if (Util.onDebug) {
605-
e.printStackTrace();
602+
try {
603+
prop = properties.createProperty(ac, getAtRule(), property, expression);
604+
} catch (InvalidParamException e) {
605+
throw e;
606+
} catch (Exception e) {
607+
if (Util.onDebug) {
608+
e.printStackTrace();
609+
}
610+
throw new InvalidParamException(e.toString(), ac);
606611
}
607-
throw new InvalidParamException(e.toString(), ac);
608-
}
609612

610-
// set the importance
611-
if (important) {
612-
prop.setImportant();
613+
// set the importance
614+
if (important) {
615+
prop.setImportant();
616+
}
613617
}
614618
prop.setOrigin(origin);
615619
// set informations for errors and warnings
@@ -747,9 +751,9 @@ public void addCharSet(String charset) throws ParseException {
747751
if (charsetFromBOM && ac.getCssVersion().compareTo(CssVersion.CSS3) >= 0) {
748752
// TODO FIXME proper execption type.
749753
throw new ParseException(ac.getMsg().getString("parser.charset"));
750-
// CssError cerr = new CssError(getSourceFile(), getBeginLine(),
751-
// getBeginColumn(), getEndLine(), getEndColumn(), ex);
752-
// ac.getFrame().addError(cerr);
754+
// CssError cerr = new CssError(getSourceFile(), getBeginLine(),
755+
// getBeginColumn(), getEndLine(), getEndColumn(), ex);
756+
// ac.getFrame().addError(cerr);
753757
} else {
754758
Charset originalCharset = ac.getCharsetObjForURL(getURL());
755759
if (originalCharset == null) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// Author: Yves Lafon <ylafon@w3.org>
3+
//
4+
// (c) COPYRIGHT MIT, ERCIM and Keio University, 2021.
5+
// Please first read the full copyright statement in file COPYRIGHT.html
6+
package org.w3c.css.properties.css;
7+
8+
import org.w3c.css.util.ApplContext;
9+
import org.w3c.css.util.InvalidParamException;
10+
import org.w3c.css.values.CssExpression;
11+
12+
/**
13+
* @since CSS3
14+
*/
15+
public abstract class CssCustomProperty extends CssProperty {
16+
17+
public String variable_name = null;
18+
19+
/**
20+
* Create a new CssCustomProperty
21+
*/
22+
public CssCustomProperty() {
23+
}
24+
25+
public CssCustomProperty(ApplContext ac, String variablename, CssExpression expression)
26+
throws InvalidParamException {
27+
throw new InvalidParamException("value",
28+
expression.getValue().toString(),
29+
getPropertyName(), ac);
30+
}
31+
32+
public CssCustomProperty(ApplContext ac, String variablename, String unparsed_expression)
33+
throws InvalidParamException {
34+
throw new InvalidParamException("value",
35+
unparsed_expression,
36+
getPropertyName(), ac);
37+
}
38+
39+
/**
40+
* Returns the value of this property
41+
*/
42+
public Object get() {
43+
return value;
44+
}
45+
46+
/**
47+
* Returns the name of this property
48+
*/
49+
public final String getPropertyName() {
50+
return variable_name;
51+
}
52+
53+
/**
54+
* Returns true if this property is "softly" inherited
55+
* e.g. his value is equals to inherit
56+
*/
57+
public boolean isSoftlyInherited() {
58+
return false;
59+
}
60+
61+
/**
62+
* Returns a string representation of the object.
63+
*/
64+
public String toString() {
65+
return value.toString();
66+
}
67+
68+
/**
69+
* Compares two properties for equality.
70+
*
71+
* @param property The other property.
72+
*/
73+
public boolean equals(CssProperty property) {
74+
if (property instanceof CssCustomProperty) {
75+
return value.equals(property.value);
76+
}
77+
return false;
78+
}
79+
80+
}
81+

0 commit comments

Comments
 (0)