|
| 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.properties.css.CssProperty; |
| 9 | +import org.w3c.css.util.ApplContext; |
| 10 | +import org.w3c.css.util.InvalidParamException; |
| 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/2024/WD-css-text-4-20240219/#text-autospace-property |
| 23 | + */ |
| 24 | +public class CssTextAutospace extends org.w3c.css.properties.css.CssTextAutospace { |
| 25 | + |
| 26 | + private static CssIdent[] allowed_values; |
| 27 | + private static CssIdent[] ideograph_axis, behaviour_axis; |
| 28 | + private static CssIdent single_val; |
| 29 | + |
| 30 | + static { |
| 31 | + String[] id_values = {"normal", "auto", "no-autospace", "ideograph-alpha", "ideograph-numeric", "punctuation", |
| 32 | + "insert", "replace"}; |
| 33 | + String[] id_axis = {"ideograph-alpha", "ideograph-numeric", "punctuation"}; |
| 34 | + String[] be_axis = {"insert", "replace"}; |
| 35 | + single_val = CssIdent.getIdent("no-autospace"); |
| 36 | + allowed_values = new CssIdent[id_values.length]; |
| 37 | + int i = 0; |
| 38 | + for (String s : id_values) { |
| 39 | + allowed_values[i++] = CssIdent.getIdent(s); |
| 40 | + } |
| 41 | + ideograph_axis = new CssIdent[id_axis.length]; |
| 42 | + i = 0; |
| 43 | + for (String s : id_axis) { |
| 44 | + ideograph_axis[i++] = CssIdent.getIdent(s); |
| 45 | + } |
| 46 | + behaviour_axis = new CssIdent[be_axis.length]; |
| 47 | + i = 0; |
| 48 | + for (String s : be_axis) { |
| 49 | + behaviour_axis[i++] = CssIdent.getIdent(s); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static CssIdent getAllowedIdent(CssIdent ident) { |
| 54 | + for (CssIdent id : allowed_values) { |
| 55 | + if (id.equals(ident)) { |
| 56 | + return id; |
| 57 | + } |
| 58 | + } |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + public static CssIdent getAutospaceIdent(CssIdent ident) { |
| 63 | + if (single_val.equals(ident)) { |
| 64 | + return single_val; |
| 65 | + } |
| 66 | + for (CssIdent id : ideograph_axis) { |
| 67 | + if (id.equals(ident)) { |
| 68 | + return id; |
| 69 | + } |
| 70 | + } |
| 71 | + for (CssIdent id : behaviour_axis) { |
| 72 | + if (id.equals(ident)) { |
| 73 | + return id; |
| 74 | + } |
| 75 | + } |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + public static boolean isIdeographAxis(CssIdent ident) { |
| 80 | + for (CssIdent id : ideograph_axis) { |
| 81 | + if (id.equals(ident)) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + public static boolean isBehaviourAxis(CssIdent ident) { |
| 89 | + for (CssIdent id : behaviour_axis) { |
| 90 | + if (id.equals(ident)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Create a new CssTextAutospace |
| 99 | + */ |
| 100 | + public CssTextAutospace() { |
| 101 | + value = initial; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Creates a new CssTextAutospace |
| 106 | + * |
| 107 | + * @param expression The expression for this property |
| 108 | + * @throws InvalidParamException Expressions are incorrect |
| 109 | + */ |
| 110 | + public CssTextAutospace(ApplContext ac, CssExpression expression, boolean check) |
| 111 | + throws InvalidParamException { |
| 112 | + CssIdent id; |
| 113 | + |
| 114 | + setByUser(); |
| 115 | + CssValue val = expression.getValue(); |
| 116 | + |
| 117 | + if (check && expression.getCount() > 4) { |
| 118 | + throw new InvalidParamException("unrecognize", ac); |
| 119 | + } |
| 120 | + |
| 121 | + if (val.getType() != CssTypes.CSS_IDENT) { |
| 122 | + throw new InvalidParamException("value", |
| 123 | + expression.getValue(), |
| 124 | + getPropertyName(), ac); |
| 125 | + } |
| 126 | + id = val.getIdent(); |
| 127 | + if (CssIdent.isCssWide(id)) { |
| 128 | + if (expression.getCount() > 1) { |
| 129 | + throw new InvalidParamException("value", |
| 130 | + expression.getValue(), |
| 131 | + getPropertyName(), ac); |
| 132 | + } |
| 133 | + value = val; |
| 134 | + expression.next(); |
| 135 | + } else if (getAllowedIdent(id) != null) { |
| 136 | + // ident or <autospace> |
| 137 | + if (getAutospaceIdent(id) != null) { |
| 138 | + // <autospace> |
| 139 | + value = checkAutoSpace(ac, expression, this); |
| 140 | + } else { |
| 141 | + if (expression.getCount() > 1) { |
| 142 | + throw new InvalidParamException("value", |
| 143 | + expression.getValue(), |
| 144 | + getPropertyName(), ac); |
| 145 | + } |
| 146 | + value = val; |
| 147 | + expression.next(); |
| 148 | + } |
| 149 | + } else { |
| 150 | + throw new InvalidParamException("value", |
| 151 | + expression.getValue(), |
| 152 | + getPropertyName(), ac); |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + public CssTextAutospace(ApplContext ac, CssExpression expression) |
| 159 | + throws InvalidParamException { |
| 160 | + this(ac, expression, false); |
| 161 | + } |
| 162 | + |
| 163 | + // this function only check if the expression is an <autospace> |
| 164 | + public static CssValue checkAutoSpace(ApplContext ac, CssExpression expression, CssProperty caller) |
| 165 | + throws InvalidParamException { |
| 166 | + ArrayList<CssValue> values = new ArrayList<CssValue>(); |
| 167 | + CssValue val; |
| 168 | + CssIdent id; |
| 169 | + char op; |
| 170 | + boolean got_behaviour = false; |
| 171 | + |
| 172 | + if (expression.getCount() > 4) { |
| 173 | + throw new InvalidParamException("unrecognize", ac); |
| 174 | + } |
| 175 | + |
| 176 | + while (!expression.end()) { |
| 177 | + val = expression.getValue(); |
| 178 | + op = expression.getOperator(); |
| 179 | + |
| 180 | + if (val.getType() != CssTypes.CSS_IDENT) { |
| 181 | + throw new InvalidParamException("value", |
| 182 | + expression.getValue(), |
| 183 | + caller.getPropertyName(), ac); |
| 184 | + } |
| 185 | + id = val.getIdent(); |
| 186 | + if (getAutospaceIdent(id) != null) { |
| 187 | + if (!isIdeographAxis(id) && !isBehaviourAxis(id)) { |
| 188 | + if (expression.getCount() > 1) { |
| 189 | + throw new InvalidParamException("value", |
| 190 | + expression.getValue(), |
| 191 | + caller.getPropertyName(), ac); |
| 192 | + } |
| 193 | + values.add(val); |
| 194 | + } else { |
| 195 | + // check we don't have two of those |
| 196 | + if (isBehaviourAxis(id)) { |
| 197 | + if (got_behaviour) { |
| 198 | + throw new InvalidParamException("value", |
| 199 | + expression.getValue(), |
| 200 | + caller.getPropertyName(), ac); |
| 201 | + } else { |
| 202 | + got_behaviour = true; |
| 203 | + } |
| 204 | + } |
| 205 | + // avoid duplicates. TODO is that a good enough check? See attr/var |
| 206 | + if (values.contains(id)) { |
| 207 | + throw new InvalidParamException("value", |
| 208 | + expression.getValue(), |
| 209 | + caller.getPropertyName(), ac); |
| 210 | + } |
| 211 | + values.add(val); |
| 212 | + } |
| 213 | + } else { |
| 214 | + throw new InvalidParamException("value", |
| 215 | + expression.getValue(), |
| 216 | + caller.getPropertyName(), ac); |
| 217 | + } |
| 218 | + if (op != SPACE) { |
| 219 | + throw new InvalidParamException("operator", op, |
| 220 | + caller.getPropertyName(), ac); |
| 221 | + } |
| 222 | + expression.next(); |
| 223 | + } |
| 224 | + return (values.size() == 1) ? values.get(0) : new CssValueList(values); |
| 225 | + |
| 226 | + } |
| 227 | + |
| 228 | +} |
| 229 | + |
0 commit comments