- From: Yves Lafon via cvs-syncmail <cvsmail@w3.org>
- Date: Fri, 07 Sep 2012 14:55:27 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2002/css-validator/org/w3c/css/values In directory hutz:/tmp/cvs-serv15037/values Modified Files: CssLength.java Added Files: CssUnitsCSS1.java CssUnitsCSS2.java CssUnitsCSS21.java CssUnitsCSS3.java Log Message: first pass at adding units depending on CSS level (including the absolute/relative check --- NEW FILE: CssUnitsCSS3.java --- // // $Id: CssUnitsCSS3.java,v 1.1 2012/09/07 14:55:25 ylafon Exp $ // From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr) // // (c) COPYRIGHT MIT and INRIA, 1997. // 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; /** * @spec http://www.w3.org/TR/2012/CR-css3-values-20120828/ */ public class CssUnitsCSS3 { private static final String[] relative_length_units = { "em", "ex", "ch", "rem", "vw", "vh", "vmin", "vmax" }; private static final String[] absolute_length_units = { "in", "cm", "mm", "pt", "pc", "px" }; public static final String[] angle_units = { "deg", "grad", "rad", "turn" }; public static final String[] time_units = { "ms", "s" }; public static final String[] frequency_units = { "kHz", "Hz" }; public static final String[] resolution_units = { "dpi", "dpcm", "ddpx" }; protected static String getRelativeLengthUnit(String unit) { for (String s : relative_length_units) { if (s.equals(unit)) { return s; } } return null; } protected static String getAbsoluteLengthUnit(String unit) { for (String s : absolute_length_units) { if (s.equals(unit)) { return s; } } return null; } protected static void parseLengthUnit(String unit, CssLength length, ApplContext ac) throws InvalidParamException { String matchedUnit = getRelativeLengthUnit(unit); if (matchedUnit != null) { length.absolute = false; } else { matchedUnit = getAbsoluteLengthUnit(unit); if (matchedUnit == null) { throw new InvalidParamException("unit", unit, ac); } length.absolute = true; } length.unit = matchedUnit; } } Index: CssLength.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/values/CssLength.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- CssLength.java 6 Sep 2012 12:37:58 -0000 1.10 +++ CssLength.java 7 Sep 2012 14:55:25 -0000 1.11 @@ -1,9 +1,9 @@ -// // $Id$ // From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr) // Updated September 25th 2000 Sijtsche de Jong (sy.de.jong@let.rug.nl) +// Updated 2012 by Yves Lafon <yves@w3.org> // -// (c) COPYRIGHT MIT and INRIA, 1997. +// (c) COPYRIGHT MIT, ERCIM and Keio University, 1997. // Please first read the full copyright statement in file COPYRIGHT.html package org.w3c.css.values; @@ -99,16 +99,8 @@ private BigDecimal value; - private int unit; - private static String[] units = {"mm", "cm", "pt", "pc", "em", - "ex", "px", "in", "gd"}; - private static int[] hash_units; - - static { - hash_units = new int[units.length]; - for (int i = 0; i < units.length; i++) - hash_units[i] = units[i].hashCode(); - } + protected String unit; + protected boolean absolute = false; /** * Create a new CssLength @@ -124,32 +116,46 @@ * @throws InvalidParamException The unit is incorrect */ public void set(String s, ApplContext ac) throws InvalidParamException { - s = s.toLowerCase(); - int length = s.length(); - String unit = s.substring(length - 2, length); - this.value = new BigDecimal(s.substring(0, length - 2)); - - this.unit = 2; // there is no unit by default - - if (unit.equals("gd") && (cssversion.equals("css2"))) { - throw new InvalidParamException("unit", unit, ac); + String low_s = s.toLowerCase(); + int length = low_s.length(); + int unitIdx = length - 1; + char c = low_s.charAt(unitIdx); + while (unitIdx > 0 && c <= 'z' && c >= 'a') { + c = low_s.charAt(--unitIdx); } - - if (value.floatValue() != 0) { - int hash = unit.hashCode(); - int i = 0; - while (i < units.length) { - if (hash == hash_units[i]) { - this.unit = i; - return; - } - i++; - } - } else { - return; + if (unitIdx == 0 || (unitIdx == length - 1)) { + throw new InvalidParamException("unit", s, ac); + } + // we go back to the beginning of the unit + unitIdx++; + String unit_str = low_s.substring(unitIdx, length); + // let's test the unit + boolean ok = true; + // TODO check the if (!BigDecimal.ZERO.equals(value))) test + // that was here earlier + // seems legit to always test the unit no matter the value + switch (ac.getCssVersion()) { + case CSS1: + CssUnitsCSS1.parseLengthUnit(unit_str, this, ac); + break; + case CSS2: + CssUnitsCSS2.parseLengthUnit(unit_str, this, ac); + break; + case CSS21: + CssUnitsCSS21.parseLengthUnit(unit_str, this, ac); + break; + case CSS3: + CssUnitsCSS3.parseLengthUnit(unit_str, this, ac); + break; + default: + throw new InvalidParamException("unit", s, ac); + } + try { + value = new BigDecimal(low_s.substring(0, unitIdx)); + } catch (NumberFormatException nex) { + throw new InvalidParamException("invalid-number", + low_s.substring(0, unitIdx), ac); } - - throw new InvalidParamException("unit", unit, ac); } // return self @@ -204,7 +210,21 @@ * Returns the current value */ public String getUnit() { - return units[unit]; + return unit; + } + + /** + * tells if it is relative or not + */ + public boolean isRelative() { + return !absolute; + } + + /** + * tells if it is absolute or not + */ + public boolean isAbsolute() { + return absolute; } /** @@ -214,7 +234,7 @@ if (BigDecimal.ZERO.equals(value)) { return value.toPlainString(); } - return value.toPlainString() + getUnit(); + return value.toPlainString() + unit; } /** @@ -225,7 +245,7 @@ public boolean equals(Object value) { return (value instanceof CssLength && this.value.equals(((CssLength) value).value) && - unit == ((CssLength) value).unit); + unit.equals(((CssLength) value).unit)); } } --- NEW FILE: CssUnitsCSS21.java --- // // $Id: CssUnitsCSS21.java,v 1.1 2012/09/07 14:55:25 ylafon Exp $ // From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr) // // (c) COPYRIGHT MIT and INRIA, 1997. // 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; /** * @spec http://www.w3.org/TR/2011/REC-CSS2-20110607/syndata.html#values */ public class CssUnitsCSS21 { private static final String[] relative_length_units = { "em", "ex" }; private static final String[] absolute_length_units = { "in", "cm", "mm", "pt", "pc", "px" }; // defined in Appendix A.2 // http://www.w3.org/TR/2011/REC-CSS2-20110607/aural.html#aural-intro public static final String[] angle_units = { "deg", "grad", "rad" }; public static final String[] time_units = { "ms", "s" }; public static final String[] frequency_units = { "kHz", "Hz" }; protected static String getRelativeLengthUnit(String unit) { for (String s : relative_length_units) { if (s.equals(unit)) { return s; } } return null; } protected static String getAbsoluteLengthUnit(String unit) { for (String s : absolute_length_units) { if (s.equals(unit)) { return s; } } return null; } protected static void parseLengthUnit(String unit, CssLength length, ApplContext ac) throws InvalidParamException { String matchedUnit = getRelativeLengthUnit(unit); if (matchedUnit != null) { length.absolute = false; } else { matchedUnit = getAbsoluteLengthUnit(unit); if (matchedUnit == null) { throw new InvalidParamException("unit", unit, ac); } length.absolute = true; } length.unit = matchedUnit; } } --- NEW FILE: CssUnitsCSS2.java --- // // $Id: CssUnitsCSS2.java,v 1.1 2012/09/07 14:55:25 ylafon Exp $ // From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr) // // (c) COPYRIGHT MIT and INRIA, 1997. // 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; /** * @spec http://www.w3.org/TR/2008/REC-CSS2-20080411/syndata.html#values */ public class CssUnitsCSS2 { private static final String[] relative_length_units = { "em", "ex", "px" }; private static final String[] absolute_length_units = { "in", "cm", "mm", "pt", "pc" }; private static final String[] angle_units = { "deg", "grad", "rad" }; private static final String[] time_units = { "ms", "s" }; private static final String[] frequency_units = { "kHz", "Hz" }; protected static String getRelativeLengthUnit(String unit) { for (String s : relative_length_units) { if (s.equals(unit)) { return s; } } return null; } protected static String getAbsoluteLengthUnit(String unit) { for (String s : absolute_length_units) { if (s.equals(unit)) { return s; } } return null; } protected static void parseLengthUnit(String unit, CssLength length, ApplContext ac) throws InvalidParamException { String matchedUnit = getRelativeLengthUnit(unit); if (matchedUnit != null) { length.absolute = false; } else { matchedUnit = getAbsoluteLengthUnit(unit); if (matchedUnit == null) { throw new InvalidParamException("unit", unit, ac); } length.absolute = true; } length.unit = matchedUnit; } } --- NEW FILE: CssUnitsCSS1.java --- // // $Id: CssUnitsCSS1.java,v 1.1 2012/09/07 14:55:25 ylafon Exp $ // From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr) // // (c) COPYRIGHT MIT and INRIA, 1997. // 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; /** * @spec http://www.w3.org/TR/2008/REC-CSS1-20080411/#units */ public class CssUnitsCSS1 { private static final String[] relative_length_units = { "em", "ex", "px" }; private static final String[] absolute_length_units = { "in", "cm", "mm", "pt", "pc" }; protected static String getRelativeLengthUnit(String unit) { for (String s : relative_length_units) { if (s.equals(unit)) { return s; } } return null; } protected static String getAbsoluteLengthUnit(String unit) { for (String s : absolute_length_units) { if (s.equals(unit)) { return s; } } return null; } protected static void parseLengthUnit(String unit, CssLength length, ApplContext ac) throws InvalidParamException { String matchedUnit = getRelativeLengthUnit(unit); if (matchedUnit != null) { length.absolute = false; } else { matchedUnit = getAbsoluteLengthUnit(unit); if (matchedUnit == null) { throw new InvalidParamException("unit", unit, ac); } length.absolute = true; } length.unit = matchedUnit; } }
Received on Friday, 7 September 2012 14:55:33 UTC