|
| 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.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-transforms-2-20211109/#propdef-rotate |
| 22 | + */ |
| 23 | +public class CssRotate extends org.w3c.css.properties.css.CssRotate { |
| 24 | + |
| 25 | + public static final CssIdent[] allowedValues; |
| 26 | + |
| 27 | + static { |
| 28 | + String[] _allowedValues = {"x", "y", "z"}; |
| 29 | + allowedValues = new CssIdent[_allowedValues.length]; |
| 30 | + for (int i = 0; i < allowedValues.length; i++) { |
| 31 | + allowedValues[i] = CssIdent.getIdent(_allowedValues[i]); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static final CssIdent getAllowedValue(CssIdent ident) { |
| 36 | + for (CssIdent id : allowedValues) { |
| 37 | + if (id.equals(ident)) { |
| 38 | + return id; |
| 39 | + } |
| 40 | + } |
| 41 | + return null; |
| 42 | + } |
| 43 | + /** |
| 44 | + * Create a new CssRotate |
| 45 | + */ |
| 46 | + public CssRotate() { |
| 47 | + value = initial; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new CssRotate |
| 52 | + * |
| 53 | + * @param expression The expression for this property |
| 54 | + * @throws InvalidParamException Expressions are incorrect |
| 55 | + */ |
| 56 | + public CssRotate(ApplContext ac, CssExpression expression, boolean check) |
| 57 | + throws InvalidParamException { |
| 58 | + if (check && expression.getCount() > 4) { |
| 59 | + throw new InvalidParamException("unrecognize", ac); |
| 60 | + } |
| 61 | + CssValue val; |
| 62 | + ArrayList<CssValue> v = new ArrayList<>(); |
| 63 | + char op; |
| 64 | + int nbNumber = 0; |
| 65 | + boolean gotIdent = false; |
| 66 | + boolean gotAngle = false; |
| 67 | + setByUser(); |
| 68 | + |
| 69 | + |
| 70 | + while (!expression.end()) { |
| 71 | + val = expression.getValue(); |
| 72 | + op = expression.getOperator(); |
| 73 | + |
| 74 | + switch (val.getType()) { |
| 75 | + case CssTypes.CSS_NUMBER: |
| 76 | + // if we got an axis or more than 3 values, report error |
| 77 | + if (gotIdent || (nbNumber == 3)) { |
| 78 | + throw new InvalidParamException("value", val.toString(), |
| 79 | + getPropertyName(), ac); |
| 80 | + } |
| 81 | + nbNumber++; |
| 82 | + v.add(val); |
| 83 | + break; |
| 84 | + case CssTypes.CSS_ANGLE: |
| 85 | + // if we got numbers, we must get them all before the angle value |
| 86 | + if (gotAngle || nbNumber == 1 || nbNumber == 2) { |
| 87 | + throw new InvalidParamException("value", val.toString(), |
| 88 | + getPropertyName(), ac); |
| 89 | + } |
| 90 | + gotAngle = true; |
| 91 | + v.add(val); |
| 92 | + break; |
| 93 | + case CssTypes.CSS_IDENT: |
| 94 | + CssIdent id = val.getIdent(); |
| 95 | + if (none.equals(id) || CssIdent.isCssWide(id)) { |
| 96 | + if (expression.getCount() > 1) { |
| 97 | + throw new InvalidParamException("value", val.toString(), |
| 98 | + getPropertyName(), ac); |
| 99 | + } |
| 100 | + v.add(val); |
| 101 | + break; |
| 102 | + } |
| 103 | + // if wa got a number (ie: defining axis) or another ident axis, report error |
| 104 | + if (!gotIdent && (nbNumber == 0) && getAllowedValue(id) != null) { |
| 105 | + v.add(val); |
| 106 | + gotIdent = true; |
| 107 | + break; |
| 108 | + } |
| 109 | + // unrecognize ident, or unwanted one, let it fail |
| 110 | + default: |
| 111 | + throw new InvalidParamException("value", |
| 112 | + val.toString(), |
| 113 | + getPropertyName(), ac); |
| 114 | + } |
| 115 | + if (op != SPACE) { |
| 116 | + throw new InvalidParamException("operator", |
| 117 | + Character.toString(op), ac); |
| 118 | + } |
| 119 | + expression.next(); |
| 120 | + } |
| 121 | + // sanity check, if we got an axis, we should get an angle, if we got number we should get 3 of them |
| 122 | + if ((nbNumber > 0 && nbNumber != 3) || ((gotIdent || (nbNumber != 0)) && !gotAngle)) { |
| 123 | + throw new InvalidParamException("value", |
| 124 | + v.toString(), |
| 125 | + getPropertyName(), ac); |
| 126 | + } |
| 127 | + value = (v.size() == 1) ? v.get(0) : new CssValueList(v); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + public CssRotate(ApplContext ac, CssExpression expression) |
| 132 | + throws InvalidParamException { |
| 133 | + this(ac, expression, false); |
| 134 | + } |
| 135 | +} |
| 136 | + |
0 commit comments