|
| 1 | +// |
| 2 | +// Author: Yves Lafon <ylafon@w3.org> |
| 3 | +// |
| 4 | +// (c) COPYRIGHT MIT, ERCIM, Keio University, Beihang, 2012. |
| 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/2024/WD-css-text-4-20240219/#propdef-text-spacing |
| 22 | + * @see CssTextAutospace |
| 23 | + * @see CssTextSpacingTrim |
| 24 | + */ |
| 25 | +public class CssTextSpacing extends org.w3c.css.properties.css.CssTextSpacing { |
| 26 | + |
| 27 | + private static CssIdent[] allowed_values; |
| 28 | + |
| 29 | + static { |
| 30 | + String[] id_values = {"none", "auto"}; |
| 31 | + allowed_values = new CssIdent[id_values.length]; |
| 32 | + int i = 0; |
| 33 | + for (String s : id_values) { |
| 34 | + allowed_values[i++] = CssIdent.getIdent(s); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static CssIdent getAllowedIdent(CssIdent ident) { |
| 39 | + for (CssIdent id : allowed_values) { |
| 40 | + if (id.equals(ident)) { |
| 41 | + return id; |
| 42 | + } |
| 43 | + } |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Create a new CssTextSpacing |
| 50 | + */ |
| 51 | + public CssTextSpacing() { |
| 52 | + value = initial; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new CssTextSpacing |
| 57 | + * |
| 58 | + * @param expression The expression for this property |
| 59 | + * @throws InvalidParamException Expressions are incorrect |
| 60 | + */ |
| 61 | + public CssTextSpacing(ApplContext ac, CssExpression expression, boolean check) |
| 62 | + throws InvalidParamException { |
| 63 | + ArrayList<CssValue> values = new ArrayList<CssValue>(); |
| 64 | + CssValue val; |
| 65 | + CssExpression spacexp = null; |
| 66 | + boolean has_trim = false; |
| 67 | + CssIdent id; |
| 68 | + char op; |
| 69 | + |
| 70 | + setByUser(); |
| 71 | + |
| 72 | + while (!expression.end()) { |
| 73 | + val = expression.getValue(); |
| 74 | + op = expression.getOperator(); |
| 75 | + |
| 76 | + if (val.getType() != CssTypes.CSS_IDENT) { |
| 77 | + throw new InvalidParamException("value", |
| 78 | + expression.getValue(), |
| 79 | + getPropertyName(), ac); |
| 80 | + } |
| 81 | + id = val.getIdent(); |
| 82 | + if (CssIdent.isCssWide(id)) { |
| 83 | + if (expression.getCount() > 1) { |
| 84 | + throw new InvalidParamException("value", |
| 85 | + expression.getValue(), |
| 86 | + getPropertyName(), ac); |
| 87 | + } |
| 88 | + values.add(val); |
| 89 | + } else if (getAllowedIdent(id) != null) { |
| 90 | + // the single values |
| 91 | + if (expression.getCount() > 1) { |
| 92 | + throw new InvalidParamException("value", |
| 93 | + expression.getValue(), |
| 94 | + getPropertyName(), ac); |
| 95 | + } |
| 96 | + values.add(val); |
| 97 | + } else if (CssTextSpacingTrim.getSpacingTrimIdent(id) != null) { |
| 98 | + if (has_trim) { |
| 99 | + throw new InvalidParamException("value", |
| 100 | + expression.getValue(), |
| 101 | + getPropertyName(), ac); |
| 102 | + } |
| 103 | + has_trim = true; |
| 104 | + values.add(val); |
| 105 | + } else if (CssTextAutospace.getAutospaceIdent(id) != null) { |
| 106 | + if (spacexp == null) { |
| 107 | + spacexp = new CssExpression(); |
| 108 | + } |
| 109 | + // FIXME one way would be to add _all_ the matching idents |
| 110 | + // FIXME then refuse new ones (as avoid values being mixed between |
| 111 | + // FIXME autospace and spacing-trim) |
| 112 | + spacexp.addValue(val); |
| 113 | + spacexp.setOperator(op); |
| 114 | + } else { |
| 115 | + throw new InvalidParamException("value", |
| 116 | + expression.getValue(), |
| 117 | + getPropertyName(), ac); |
| 118 | + } |
| 119 | + if (op != SPACE) { |
| 120 | + throw new InvalidParamException("operator", op, |
| 121 | + getPropertyName(), ac); |
| 122 | + } |
| 123 | + expression.next(); |
| 124 | + } |
| 125 | + if (spacexp != null) { |
| 126 | + values.add(CssTextAutospace.checkAutoSpace(ac, spacexp, this)); |
| 127 | + } |
| 128 | + value = (values.size() == 1) ? values.get(0) : new CssValueList(values); |
| 129 | + } |
| 130 | + |
| 131 | + public CssTextSpacing(ApplContext ac, CssExpression expression) |
| 132 | + throws InvalidParamException { |
| 133 | + this(ac, expression, false); |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | + |
0 commit comments