|
| 1 | +// |
| 2 | +// Author: Yves Lafon <ylafon@w3.org> |
| 3 | +// |
| 4 | +// (c) COPYRIGHT MIT, ERCIM, Keio, Beihang, 2021. |
| 5 | +// Please first read the full copyright statement in file COPYRIGHT.html |
| 6 | +package org.w3c.css.properties.css3; |
| 7 | + |
| 8 | +import org.w3c.css.util.ApplContext; |
| 9 | +import org.w3c.css.util.InvalidParamException; |
| 10 | +import org.w3c.css.values.CssExpression; |
| 11 | +import org.w3c.css.values.CssIdent; |
| 12 | +import org.w3c.css.values.CssTypes; |
| 13 | +import org.w3c.css.values.CssValue; |
| 14 | +import org.w3c.css.values.CssValueList; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | + |
| 18 | +import static org.w3c.css.values.CssOperator.SPACE; |
| 19 | + |
| 20 | +/** |
| 21 | + * @spec https://www.w3.org/TR/2021/WD-css-scrollbars-1-20210829/#propdef-scrollbar-color |
| 22 | + */ |
| 23 | +public class CssScrollbarColor extends org.w3c.css.properties.css.CssScrollbarColor { |
| 24 | + |
| 25 | + public static final CssIdent[] allowed_values; |
| 26 | + |
| 27 | + static { |
| 28 | + String[] _allowed_values = {"auto"}; |
| 29 | + allowed_values = new CssIdent[_allowed_values.length]; |
| 30 | + int i = 0; |
| 31 | + for (String s : _allowed_values) { |
| 32 | + allowed_values[i++] = CssIdent.getIdent(s); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static CssIdent getAllowedIdent(CssIdent ident) { |
| 37 | + for (CssIdent id : allowed_values) { |
| 38 | + if (id.equals(ident)) { |
| 39 | + return id; |
| 40 | + } |
| 41 | + } |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a new CssScrollbarColor |
| 47 | + */ |
| 48 | + public CssScrollbarColor() { |
| 49 | + value = initial; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a new CssScrollbarColor |
| 54 | + * |
| 55 | + * @param expression The expression for this property |
| 56 | + * @throws InvalidParamException Expressions are incorrect |
| 57 | + */ |
| 58 | + public CssScrollbarColor(ApplContext ac, CssExpression expression, boolean check) |
| 59 | + throws InvalidParamException { |
| 60 | + if (check && expression.getCount() > 2) { |
| 61 | + throw new InvalidParamException("unrecognize", ac); |
| 62 | + } |
| 63 | + |
| 64 | + setByUser(); |
| 65 | + |
| 66 | + CssValue val; |
| 67 | + org.w3c.css.values.CssColor c; |
| 68 | + CssColor tcolor; |
| 69 | + char op; |
| 70 | + |
| 71 | + val = expression.getValue(); |
| 72 | + op = expression.getOperator(); |
| 73 | + ArrayList<CssValue> values = new ArrayList<>(); |
| 74 | + |
| 75 | + switch (val.getType()) { |
| 76 | + case CssTypes.CSS_IDENT: |
| 77 | + if (inherit.equals(val)) { |
| 78 | + if (expression.getCount() != 1) { |
| 79 | + throw new InvalidParamException("value", |
| 80 | + val.toString(), |
| 81 | + getPropertyName(), ac); |
| 82 | + } |
| 83 | + value = val; |
| 84 | + break; |
| 85 | + } else { |
| 86 | + CssIdent ident = getAllowedIdent(val.getIdent()); |
| 87 | + // auto can be there once |
| 88 | + if (ident != null) { |
| 89 | + if (expression.getCount() != 1) { |
| 90 | + throw new InvalidParamException("value", |
| 91 | + val.toString(), |
| 92 | + getPropertyName(), ac); |
| 93 | + } |
| 94 | + value = val; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + // it must be a color instead |
| 99 | + try { |
| 100 | + c = new org.w3c.css.values.CssColor(ac, val.getIdent().toString()); |
| 101 | + } catch (InvalidParamException e) { |
| 102 | + // we recreate the exception, as it will have |
| 103 | + // the wrong property name otherwise |
| 104 | + throw new InvalidParamException("value", |
| 105 | + expression.getValue(), |
| 106 | + getPropertyName(), ac); |
| 107 | + } |
| 108 | + values.add(val); |
| 109 | + break; |
| 110 | + case CssTypes.CSS_FUNCTION: |
| 111 | + try { |
| 112 | + tcolor = new CssColor(ac, expression, check); |
| 113 | + value = val; |
| 114 | + break; |
| 115 | + } catch (InvalidParamException e) { |
| 116 | + // we recreate the exception, as it will have |
| 117 | + // the wrong property name otherwise |
| 118 | + throw new InvalidParamException("value", |
| 119 | + expression.getValue(), |
| 120 | + getPropertyName(), ac); |
| 121 | + } |
| 122 | + case CssTypes.CSS_HASH_IDENT: |
| 123 | + c = new org.w3c.css.values.CssColor(); |
| 124 | + c.setShortRGBColor(ac, val.getHashIdent().toString()); |
| 125 | + values.add(val); |
| 126 | + break; |
| 127 | + case CssTypes.CSS_COLOR: |
| 128 | + values.add(val); |
| 129 | + break; |
| 130 | + default: |
| 131 | + throw new InvalidParamException("value", |
| 132 | + val.toString(), |
| 133 | + getPropertyName(), ac); |
| 134 | + } |
| 135 | + expression.next(); |
| 136 | + |
| 137 | + if (!expression.end()) { |
| 138 | + // second value |
| 139 | + if (op != SPACE) { |
| 140 | + throw new InvalidParamException("operator", |
| 141 | + Character.toString(op), ac); |
| 142 | + } |
| 143 | + val = expression.getValue(); |
| 144 | + switch (val.getType()) { |
| 145 | + case CssTypes.CSS_IDENT: |
| 146 | + if (inherit.equals(val)) { |
| 147 | + throw new InvalidParamException("value", |
| 148 | + val.toString(), |
| 149 | + getPropertyName(), ac); |
| 150 | + } else { |
| 151 | + CssIdent ident = getAllowedIdent(val.getIdent()); |
| 152 | + // auto can be there first (and alone) |
| 153 | + if (ident != null) { |
| 154 | + throw new InvalidParamException("value", |
| 155 | + val.toString(), |
| 156 | + getPropertyName(), ac); |
| 157 | + } |
| 158 | + } |
| 159 | + // it must be a color instead |
| 160 | + try { |
| 161 | + c = new org.w3c.css.values.CssColor(ac, val.getIdent().toString()); |
| 162 | + } catch (InvalidParamException e) { |
| 163 | + // we recreate the exception, as it will have |
| 164 | + // the wrong property name otherwise |
| 165 | + throw new InvalidParamException("value", |
| 166 | + expression.getValue(), |
| 167 | + getPropertyName(), ac); |
| 168 | + } |
| 169 | + values.add(val); |
| 170 | + break; |
| 171 | + case CssTypes.CSS_FUNCTION: |
| 172 | + try { |
| 173 | + tcolor = new CssColor(ac, expression, check); |
| 174 | + value = val; |
| 175 | + break; |
| 176 | + } catch (InvalidParamException e) { |
| 177 | + // we recreate the exception, as it will have |
| 178 | + // the wrong property name otherwise |
| 179 | + throw new InvalidParamException("value", |
| 180 | + expression.getValue(), |
| 181 | + getPropertyName(), ac); |
| 182 | + } |
| 183 | + case CssTypes.CSS_HASH_IDENT: |
| 184 | + c = new org.w3c.css.values.CssColor(); |
| 185 | + c.setShortRGBColor(ac, val.getHashIdent().toString()); |
| 186 | + values.add(val); |
| 187 | + break; |
| 188 | + case CssTypes.CSS_COLOR: |
| 189 | + values.add(val); |
| 190 | + break; |
| 191 | + default: |
| 192 | + throw new InvalidParamException("value", |
| 193 | + val.toString(), |
| 194 | + getPropertyName(), ac); |
| 195 | + } |
| 196 | + expression.next(); |
| 197 | + } else { |
| 198 | + if (values.size() != 0) { |
| 199 | + // we got ony one color |
| 200 | + throw new InvalidParamException("unrecognize", ac); |
| 201 | + } |
| 202 | + } |
| 203 | + if (values.size() > 0) { |
| 204 | + value = new CssValueList(values); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public CssScrollbarColor(ApplContext ac, CssExpression expression) |
| 209 | + throws InvalidParamException { |
| 210 | + this(ac, expression, false); |
| 211 | + } |
| 212 | +} |
| 213 | + |
0 commit comments