|
| 1 | +// |
| 2 | +// Author: Yves Lafon <ylafon@w3.org> |
| 3 | +// |
| 4 | +// (c) COPYRIGHT World Wide Web Consortium, 2025. |
| 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.CssCheckableValue; |
| 11 | +import org.w3c.css.values.CssExpression; |
| 12 | +import org.w3c.css.values.CssIdent; |
| 13 | +import org.w3c.css.values.CssTypes; |
| 14 | +import org.w3c.css.values.CssValue; |
| 15 | +import org.w3c.css.values.CssValueList; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | + |
| 19 | +import static org.w3c.css.values.CssOperator.SPACE; |
| 20 | + |
| 21 | +/** |
| 22 | + * @spec https://www.w3.org/TR/2021/WD-css-transforms-2-20211109/#propdef-translate |
| 23 | + */ |
| 24 | +public class CssTranslate extends org.w3c.css.properties.css.CssTranslate { |
| 25 | + |
| 26 | + /** |
| 27 | + * Create a new CssTranslate |
| 28 | + */ |
| 29 | + public CssTranslate() { |
| 30 | + value = initial; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a new CssTranslate |
| 35 | + * |
| 36 | + * @param expression The expression for this property |
| 37 | + * @throws InvalidParamException Expressions are incorrect |
| 38 | + */ |
| 39 | + public CssTranslate(ApplContext ac, CssExpression expression, boolean check) |
| 40 | + throws InvalidParamException { |
| 41 | + if (check && expression.getCount() > 3) { |
| 42 | + throw new InvalidParamException("unrecognize", ac); |
| 43 | + } |
| 44 | + CssValue val; |
| 45 | + ArrayList<CssValue> v = new ArrayList<>(); |
| 46 | + char op; |
| 47 | + setByUser(); |
| 48 | + |
| 49 | + |
| 50 | + while (!expression.end()) { |
| 51 | + val = expression.getValue(); |
| 52 | + op = expression.getOperator(); |
| 53 | + |
| 54 | + switch (val.getType()) { |
| 55 | + case CssTypes.CSS_NUMBER: |
| 56 | + // only 0 can be a length... |
| 57 | + CssCheckableValue p = val.getCheckableValue(); |
| 58 | + p.checkEqualsZero(ac, this); |
| 59 | + v.add(val); |
| 60 | + break; |
| 61 | + case CssTypes.CSS_PERCENTAGE: |
| 62 | + if (v.size() == 2) { |
| 63 | + // percentage can appear only in the first two values |
| 64 | + throw new InvalidParamException("value", val.toString(), |
| 65 | + getPropertyName(), ac); |
| 66 | + } |
| 67 | + case CssTypes.CSS_LENGTH: |
| 68 | + v.add(val); |
| 69 | + break; |
| 70 | + case CssTypes.CSS_IDENT: |
| 71 | + CssIdent id = val.getIdent(); |
| 72 | + if (none.equals(id) || CssIdent.isCssWide(id)) { |
| 73 | + if (expression.getCount() > 1) { |
| 74 | + throw new InvalidParamException("value", val.toString(), |
| 75 | + getPropertyName(), ac); |
| 76 | + } |
| 77 | + v.add(val); |
| 78 | + break; |
| 79 | + } |
| 80 | + // unrecognize ident, let it fail |
| 81 | + default: |
| 82 | + throw new InvalidParamException("value", |
| 83 | + val.toString(), |
| 84 | + getPropertyName(), ac); |
| 85 | + } |
| 86 | + if (op != SPACE) { |
| 87 | + throw new InvalidParamException("operator", |
| 88 | + Character.toString(op), ac); |
| 89 | + } |
| 90 | + expression.next(); |
| 91 | + } |
| 92 | + value = (v.size() == 1) ? v.get(0) : new CssValueList(v); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public CssTranslate(ApplContext ac, CssExpression expression) |
| 97 | + throws InvalidParamException { |
| 98 | + this(ac, expression, false); |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments