// // @author Yves Lafon // // (c) COPYRIGHT MIT, ERCIM, Keio, Beihang, 2016. // Please first read the full copyright statement in file COPYRIGHT.html package org.w3c.css.values; import org.w3c.css.util.ApplContext; import org.w3c.css.util.InvalidParamException; import java.math.BigDecimal; /** * A CSS calc(). * * @spec https://www.w3.org/TR/2015/CR-css-values-3-20150611/#calc-notation */ public class CssCalc extends CssCheckableValue { public static final int type = CssTypes.CSS_CALC; public final int getRawType() { return type; } public final int getType() { if (computed_type == CssTypes.CSS_CALC) { return type; } return computed_type; } ApplContext ac; int computed_type = CssTypes.CSS_UNKNOWN; CssValue val1 = null; CssValue val2 = null; char operator; boolean hasParen = false; String _toString = null; boolean implicit_function = true; /** * Create a new CssCalc */ public CssCalc() { } public CssCalc(ApplContext ac) { this(ac, null); } public CssCalc(CssValue value) { this(null, value); } public CssCalc(ApplContext ac, CssValue value) { if (ac != null) { this.ac = ac; } if (value != null) { if (value.getRawType() == CssTypes.CSS_CALC) { CssCalc c = (CssCalc) value; contains_variable = c.hasCssVariable(); } else if (value.getType() == CssTypes.CSS_VARIABLE) { contains_variable = true; } } computed_type = value.getType(); val1 = value; } public void setImplicitFunction(boolean v) { implicit_function = v; _toString = null; } public void set(String s, ApplContext ac) throws InvalidParamException { // we don't support this way of setting the value // as we rely on the parsing to create it incrementally throw new InvalidParamException("unrecognize", s, ac); } public void setValue(BigDecimal d) { // we don't support this way of setting the value // as we rely on the parsing to create it incrementally } /** * Add one operand, if we already got one we will... Add one operand. * * @param value * @return */ public CssCalc setLeftSide(CssValue value) throws InvalidParamException { if (val1 != null) { throw new InvalidParamException("unrecognized", val1, ac); } val1 = value; if (val1.getType() == CssTypes.CSS_VARIABLE) { contains_variable = true; } _toString = null; return this; } public CssCalc addRightSide(String oper, CssValue value) throws InvalidParamException { _toString = null; switch (oper) { case "+": operator = CssOperator.PLUS; break; case "-": operator = CssOperator.MINUS; break; case "*": operator = CssOperator.MUL; break; case "/": operator = CssOperator.DIV; break; default: throw new InvalidParamException("operator", oper, ac); } val2 = value; if (val2.getType() == CssTypes.CSS_VARIABLE) { contains_variable = true; } _computeResultingType(false); return this; } public CssCalc setParenthesis() { hasParen = true; return this; } public void validate() throws InvalidParamException { _computeResultingType(true); } private void _checkAcceptableType(int type) throws InvalidParamException { // , , ,