- From: Yves Lafon via cvs-syncmail <cvsmail@w3.org>
- Date: Fri, 12 Aug 2011 21:19:03 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2002/css-validator/org/w3c/css/css In directory hutz:/tmp/cvs-serv29514/org/w3c/css/css Modified Files: CssCascadingOrder.java StyleSheet.java Log Message: cleanup Index: CssCascadingOrder.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/css/CssCascadingOrder.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- CssCascadingOrder.java 5 Jan 2010 13:49:33 -0000 1.8 +++ CssCascadingOrder.java 12 Aug 2011 21:19:01 -0000 1.9 @@ -7,88 +7,87 @@ package org.w3c.css.css; -import java.util.Enumeration; - import org.w3c.css.parser.CssSelectors; import org.w3c.css.parser.CssStyle; import org.w3c.css.properties.css.CssProperty; -import org.w3c.css.util.CompareFunction; -import org.w3c.css.util.QuickSortAlgorithm; -import org.w3c.css.util.SortAlgorithm; import org.w3c.css.util.Util; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Enumeration; + /** - * <H3> - * Cascading order - * </H3> - * + * <H3> + * Cascading order + * </H3> + * <p/> * <P> Conflicting rules are intrinsic to the CSS mechanism. To find the value * for an element/property combination, the following algorithm must be * followed: - * + * <p/> * <OL> * <LI> Find all declarations that apply to the element/property in question. * Declarations apply if the selector matches the element in question. If no * declarations apply, the inherited value is used. If there is no inherited * value (this is the case for the 'HTML' element and for properties that do * not inherit), the initial value is used. - * + * <p/> * <LI> Sort the declarations by explicit weight: declarations marked * '!important' carry more weight than unmarked (normal) declarations. - * + * <p/> * <LI> Sort by origin: the author's style sheets override the reader's style * sheet which override the UA's default values. An imported style sheet has * the same origin as the style sheet from which it is imported. - * + * <p/> * <LI> Sort by specificity of selector: more specific selectors will override * more general ones. To find the specificity, count the number of ID * attributes in the selector (a), the number of CLASS attributes in the * selector (b), and the number of tag names in the selector (c). Concatenating * the three numbers (in a number system with a large base) gives the * specificity. Some examples: - * + * <p/> * <PRE> - * LI {...} /* a=0 b=0 c=1 -> specificity = 1 * / - * UL LI {...} /* a=0 b=0 c=2 -> specificity = 2 * / - * UL OL LI {...} /* a=0 b=0 c=3 -> specificity = 3 * / - * LI.red {...} /* a=0 b=1 c=1 -> specificity = 11 * / - * UL OL LI.red {...} /* a=0 b=1 c=3 -> specificity = 13 * / - * #x34y {...} /* a=1 b=0 c=0 -> specificity = 100 * / + * LI {...} /* a=0 b=0 c=1 -> specificity = 1 * / + * UL LI {...} /* a=0 b=0 c=2 -> specificity = 2 * / + * UL OL LI {...} /* a=0 b=0 c=3 -> specificity = 3 * / + * LI.red {...} /* a=0 b=1 c=1 -> specificity = 11 * / + * UL OL LI.red {...} /* a=0 b=1 c=3 -> specificity = 13 * / + * #x34y {...} /* a=1 b=0 c=0 -> specificity = 100 * / * </PRE> - * + * <p/> * <P> Pseudo-elements and pseudo-classes are counted as normal elements and * classes, respectively. - * + * <p/> * <LI> Sort by order specified: if two rules have the same weight, the latter * specified wins. Rules in imported style sheets are considered to be before * any rules in the style sheet itself. * </OL> - * + * <p/> * <P> The search for the property value can be terminated whenever one rule * has a higher weight than the other rules that apply to the same * element/property combination. - * + * <p/> * <P> This strategy gives author's style sheets considerably higher weight * than those of the reader. It is therefore important that the reader has the * ability to turn off the influence of a certain style sheet, e.g. through a * pull-down menu. - * + * <p/> * <P> A declaration in the 'STYLE' attribute of an element has the same weight * as a declaration with an ID-based selector that is specified at the end of * the style sheet: - * + * <p/> * <PRE> * <STYLE TYPE="text/css"> - * #x97z { color: blue } + * #x97z { color: blue } * </STYLE> * <P ID=x97z STYLE="color: red"> * </PRE> - * + * <p/> * <P> In the above example, the color of the 'P' element would be * red. Although the specificity is the same for both declarations, the * declaration in the 'STYLE' attribute will override the one in the 'STYLE' * element because of cascading rule number 5. - * + * <p/> * <P> The UA may choose to honor other stylistic HTML attributes, for example * 'ALIGN'. If so, these attributes are translated to the corresponding CSS * rules with specificity equal to 1. The rules are assumed to be at the start @@ -108,127 +107,127 @@ * Order all properties and returns the winner. * * @param property The default value returned if there is no property. - * @param style The current style sheet where we can find all properties. + * @param style The current style sheet where we can find all properties. * @param selector The current context. * @return the property with the right value. */ public CssProperty order(CssProperty property, - StyleSheet style, CssSelectors selector) { - //int i = 0; - propertyData = new CssProperty[10]; - propertyCount = 0; - Util.verbose("CASCADING ORDER " + property.getPropertyName() - + " in " + selector); + StyleSheet style, CssSelectors selector) { + //int i = 0; + propertyData = new CssProperty[10]; + propertyCount = 0; + Util.verbose("CASCADING ORDER " + property.getPropertyName() + + " in " + selector); - // looking for all declarations that apply to the element/property in - // question. (step 1) - for (Enumeration e = style.getRules().elements(); e.hasMoreElements();) { - CssSelectors context = (CssSelectors) e.nextElement(); + // looking for all declarations that apply to the element/property in + // question. (step 1) + for (Enumeration e = style.getRules().elements(); e.hasMoreElements(); ) { + CssSelectors context = (CssSelectors) e.nextElement(); - Util.verbose("######## test with " + context - + " and " + selector); - // if (!selector.equals(context) && context.canApply(selector)) { - if (context.canApply(selector)) { - // here, don't try to resolve - CssProperty prop = - property.getPropertyInStyle(context.getStyle(), false); - Util.verbose("%%%%%%%%%%%%%%%%% Found " + context); - if (prop != null) { - addProperty(prop); - } - } - } + Util.verbose("######## test with " + context + + " and " + selector); + // if (!selector.equals(context) && context.canApply(selector)) { + if (context.canApply(selector)) { + // here, don't try to resolve + CssProperty prop = + property.getPropertyInStyle(context.getStyle(), false); + Util.verbose("%%%%%%%%%%%%%%%%% Found " + context); + if (prop != null) { + addProperty(prop); + } + } + } - if (propertyCount == 0) { - // if I found nothing - if (selector.getNext() != null && property.inherited()) { - // here, I can try to resolve - Util.verbose("Found nothing ... try the next " - + selector.getNext()); - CssStyle s = style.getStyle(selector.getNext()); - property = property.getPropertyInStyle(s, true); - } // else use the default value - } else { - Util.verbose("@@@@@@@@@@@@@@ FOUND " - + propertyCount + " properties"); - // runs the cascading order - property = getProperty(selector); + if (propertyCount == 0) { + // if I found nothing + if (selector.getNext() != null && property.inherited()) { + // here, I can try to resolve + Util.verbose("Found nothing ... try the next " + + selector.getNext()); + CssStyle s = style.getStyle(selector.getNext()); + property = property.getPropertyInStyle(s, true); + } // else use the default value + } else { + Util.verbose("@@@@@@@@@@@@@@ FOUND " + + propertyCount + " properties"); + // runs the cascading order + property = getProperty(selector); - if (property.isSoftlyInherited() - && selector.getNext() != null) { - // the value of the property is inherited, - // recompute again .... - CssStyle s = style.getStyle(selector.getNext()); - property = property.getPropertyInStyle(s, true); - } - } - // duplicate the property because I change the context ... - property = property.duplicate(); - property.setSelectors(selector); + if (property.isSoftlyInherited() + && selector.getNext() != null) { + // the value of the property is inherited, + // recompute again .... + CssStyle s = style.getStyle(selector.getNext()); + property = property.getPropertyInStyle(s, true); + } + } + // duplicate the property because I change the context ... + property = property.duplicate(); + property.setSelectors(selector); - return property; + return property; } // here you can find the algorithm for the cascading order private CssProperty getProperty(CssSelectors selector) { - SortAlgorithm sort = new QuickSortAlgorithm(); - - // sort by explicit weight and origin (step 2 and 3) - sort.sort(propertyData, 0, propertyCount - 1, - new CompareExplicitWeight()); - int old = propertyData[0].getExplicitWeight(); - int end = 0; - while (end < propertyCount && - propertyData[end].getExplicitWeight() == old) { - end++; - } - // sort by specificity (step 4) - sort.sort(propertyData, 0, end-1, new CompareSpecificity()); - old = propertyData[0].getSelectors().getSpecificity(); - end = 0; - while (end < propertyCount && - propertyData[end].getSelectors().getSpecificity() == old) { - end++; - } + // sort by explicit weight and origin (step 2 and 3) + Arrays.sort(propertyData, 0, propertyCount, new CompareExplicitWeight()); + int old = propertyData[0].getExplicitWeight(); + int end = 0; + while (end < propertyCount && + propertyData[end].getExplicitWeight() == old) { + end++; + } - // sort by order specified (step 5) - sort.sort(propertyData, 0, end - 1, new CompareOrderSpecified()); + // sort by specificity (step 4) + Arrays.sort(propertyData, 0, end, new CompareSpecificity()); + old = propertyData[0].getSelectors().getSpecificity(); + end = 0; + while (end < propertyCount && + propertyData[end].getSelectors().getSpecificity() == old) { + end++; + } - return propertyData[0]; + // sort by order specified (step 5) + Arrays.sort(propertyData, 0, end, new CompareOrderSpecified()); + return propertyData[0]; } private final void addProperty(CssProperty property) { - int oldCapacity = propertyData.length; - if (propertyCount + 1 > oldCapacity) { - CssProperty oldData[] = propertyData; - propertyData = new CssProperty[oldCapacity + capacityIncrement]; - System.arraycopy(oldData, 0, propertyData, 0, propertyCount); - } - propertyData[propertyCount++] = property; + int oldCapacity = propertyData.length; + if (propertyCount + 1 > oldCapacity) { + CssProperty oldData[] = propertyData; + propertyData = new CssProperty[oldCapacity + capacityIncrement]; + System.arraycopy(oldData, 0, propertyData, 0, propertyCount); + } + propertyData[propertyCount++] = property; } } // all compare functions used in the cascading order -class CompareExplicitWeight extends CompareFunction { - public boolean compare(Object obj1, Object obj2) { - return (((CssProperty) obj1).getExplicitWeight() > - ((CssProperty) obj2).getExplicitWeight()); +class CompareExplicitWeight implements Comparator<CssProperty> { + public int compare(CssProperty property1, CssProperty property2) { + int val1 = property1.getExplicitWeight(); + int val2 = property2.getExplicitWeight(); + return (val1 - val2); } } -class CompareSpecificity extends CompareFunction { - public boolean compare(Object obj1, Object obj2) { - return (((CssProperty) obj1).getSelectors().getSpecificity() > - ((CssProperty) obj2).getSelectors().getSpecificity()); +class CompareSpecificity implements Comparator<CssProperty> { + public int compare(CssProperty property1, CssProperty property2) { + int val1 = property1.getSelectors().getSpecificity(); + int val2 = property2.getSelectors().getSpecificity(); + return (val1 - val2); } } -class CompareOrderSpecified extends CompareFunction { - public boolean compare(Object obj1, Object obj2) { - return (((CssProperty) obj1).getOrderSpecified() > - ((CssProperty) obj2).getOrderSpecified()); +class CompareOrderSpecified implements Comparator<CssProperty> { + public int compare(CssProperty property1, CssProperty property2) { + long val1 = property1.getOrderSpecified(); + long val2 = property2.getOrderSpecified(); + return ((int) (val1 - val2)); } } Index: StyleSheet.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/css/StyleSheet.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -d -r1.16 -r1.17 --- StyleSheet.java 5 Jan 2010 13:49:33 -0000 1.16 +++ StyleSheet.java 12 Aug 2011 21:19:01 -0000 1.17 @@ -7,20 +7,20 @@ package org.w3c.css.css; -import java.io.PrintWriter; -import java.util.Hashtable; -import java.util.Vector; - import org.w3c.css.parser.AtRule; import org.w3c.css.parser.CssSelectors; import org.w3c.css.parser.CssStyle; import org.w3c.css.parser.Errors; import org.w3c.css.properties.css.CssProperty; import org.w3c.css.util.ApplContext; -import org.w3c.css.util.SortedHashtable; import org.w3c.css.util.Util; import org.w3c.css.util.Warnings; +import java.io.PrintWriter; +import java.util.Arrays; +import java.util.Hashtable; +import java.util.Vector; + /** * This class contains a style sheet with all rules, errors and warnings. * @@ -29,10 +29,10 @@ public class StyleSheet { private CssCascadingOrder cascading; - private SortedHashtable rules; - private Errors errors; + private Hashtable rules; + private Errors errors; private Warnings warnings; - private String type; + private String type; private Vector atRuleList; private boolean doNotAddRule; private boolean doNotAddAtRule; @@ -42,17 +42,17 @@ * Create a new StyleSheet. */ public StyleSheet() { - rules = new SortedHashtable(); - errors = new Errors(); - warnings = new Warnings(); - cascading = new CssCascadingOrder(); - atRuleList = new Vector(); + rules = new Hashtable(); + errors = new Errors(); + warnings = new Warnings(); + cascading = new CssCascadingOrder(); + atRuleList = new Vector(); } public void setWarningLevel(int warningLevel) { - warnings.setWarningLevel(warningLevel); + warnings.setWarningLevel(warningLevel); } - + /** * Get a style in a specific context. * No resolution are perfomed when this function is called @@ -61,21 +61,21 @@ * @return The style for the specific context. */ public CssStyle getStyle(CssSelectors context) { - if (debug) { - Util.verbose("StyleSheet.getStyle("+context+")"); - } - if (getContext(context) != null) { - CssSelectors realContext = (CssSelectors) getContext(context); - CssStyle style = realContext.getStyle(); - style.setStyleSheet(this); - style.setSelector(realContext); - return style; - } else { - getRules().put(context, context); - context.getStyle().setStyleSheet(this); - context.getStyle().setSelector(context); - return context.getStyle(); - } + if (debug) { + Util.verbose("StyleSheet.getStyle(" + context + ")"); + } + if (getContext(context) != null) { + CssSelectors realContext = (CssSelectors) getContext(context); + CssStyle style = realContext.getStyle(); + style.setStyleSheet(this); + style.setSelector(realContext); + return style; + } else { + getRules().put(context, context); + context.getStyle().setStyleSheet(this); + context.getStyle().setSelector(context); + return context.getStyle(); + } } @@ -86,28 +86,28 @@ * @param property The property to add */ public void addProperty(CssSelectors selector, CssProperty property) { - if (debug) { - Util.verbose("add property " - + getContext(selector) - + " " + property); - } - getContext(selector).addProperty(property, warnings); + if (debug) { + Util.verbose("add property " + + getContext(selector) + + " " + property); + } + getContext(selector).addProperty(property, warnings); } public void remove(CssSelectors selector) { - rules.remove(selector); + rules.remove(selector); } public void setType(String type) { - this.type = type; + this.type = type; } public String getType() { - if (type == null) { - return "text/css"; - } else { - return type; - } + if (type == null) { + return "text/css"; + } else { + return type; + } } /** @@ -116,9 +116,9 @@ * @param errors Some errors. */ public void addErrors(Errors errors) { - if (errors.getErrorCount() != 0) { - getErrors().addErrors(errors); - } + if (errors.getErrorCount() != 0) { + getErrors().addErrors(errors); + } } /** @@ -127,59 +127,61 @@ * @param warnings Some warnings. */ public void addWarnings(Warnings warnings) { - if (warnings.getWarningCount() != 0) - getWarnings().addWarnings(warnings); + if (warnings.getWarningCount() != 0) + getWarnings().addWarnings(warnings); } /** * Returns all errors. */ public final Errors getErrors() { - return errors; + return errors; } /** * Returns all warnings. */ public final Warnings getWarnings() { - return warnings; + return warnings; } /** * Returns all rules */ public final Hashtable getRules() { - return rules; + return rules; } /** * Returns the property for a context. * * @param property The default value returned if there is no property. - * @param style The current style sheet where we can find all properties + * @param style The current style sheet where we can find all properties * @param selector The current context * @return the property with the right value */ public final CssProperty CascadingOrder(CssProperty property, - StyleSheet style, - CssSelectors selector) { - return cascading.order(property, style, selector); + StyleSheet style, + CssSelectors selector) { + return cascading.order(property, style, selector); } /** * Find all conflicts for this style sheet. */ public void findConflicts(ApplContext ac) { - Hashtable rules = getRules(); - CssSelectors[] all = new CssSelectors[rules.size()]; - all = (CssSelectors[]) rules.values().toArray(all); - for (int i = 0; i< all.length; i++) { - all[i].markAsFinal(); - } - for (int i = 0; i< all.length; i++) { - CssSelectors sel = all[i]; - sel.findConflicts(ac, warnings, all); - } + Hashtable rules = getRules(); + CssSelectors[] all = new CssSelectors[rules.size()]; + all = (CssSelectors[]) rules.values().toArray(all); + Arrays.sort(all); + + for (int i = 0; i < all.length; i++) { + all[i].markAsFinal(); + } + for (int i = 0; i < all.length; i++) { + CssSelectors sel = all[i]; + sel.findConflicts(ac, warnings, all); + } } /** @@ -188,99 +190,99 @@ * @param selector the context to find. */ protected CssSelectors getContext(CssSelectors selector) { - if (getRules().containsKey(selector)) { - return (CssSelectors) getRules().get(selector); - } else { - if (selector.getNext() != null) { - CssSelectors next = getContext(selector.getNext()); - selector.setNext(next); - } - getRules().put(selector, selector); - return selector; - } + if (getRules().containsKey(selector)) { + return (CssSelectors) getRules().get(selector); + } else { + if (selector.getNext() != null) { + CssSelectors next = getContext(selector.getNext()); + selector.setNext(next); + } + getRules().put(selector, selector); + return selector; + } } /** * dump this style sheet. */ public void dump() { - StyleSheetGenerator style = - new StyleSheetGenerator("", this, "text", -1); - style.print(new PrintWriter(System.out)); + StyleSheetGenerator style = + new StyleSheetGenerator("", this, "text", -1); + style.print(new PrintWriter(System.out)); } //part added by Sijtsche de Jong public void addCharSet(String charset) { - this.charset = charset; + this.charset = charset; } public void newAtRule(AtRule atRule) { - CssRuleList rulelist = new CssRuleList(); - rulelist.addAtRule(atRule); - atRuleList.addElement(rulelist); - indent = " "; + CssRuleList rulelist = new CssRuleList(); + rulelist.addAtRule(atRule); + atRuleList.addElement(rulelist); + indent = " "; } public void endOfAtRule() { - if (!doNotAddAtRule) { - CssRuleList rulelist = new CssRuleList(); - atRuleList.addElement(rulelist); //for the new set of rules - } - important = false; - selectortext = ""; - indent = ""; - doNotAddAtRule = false; + if (!doNotAddAtRule) { + CssRuleList rulelist = new CssRuleList(); + atRuleList.addElement(rulelist); //for the new set of rules + } + important = false; + selectortext = ""; + indent = ""; + doNotAddAtRule = false; } public void setImportant(boolean important) { - this.important = important; + this.important = important; } public void setSelectorList(Vector selectors) { - String slave = selectors.toString(); - selectortext = slave.substring(slave.indexOf("[") + 1, - slave.lastIndexOf("]")); + String slave = selectors.toString(); + selectortext = slave.substring(slave.indexOf("[") + 1, + slave.lastIndexOf("]")); } public void setProperty(Vector properties) { - this.properties = properties; + this.properties = properties; } public void endOfRule() { - CssRuleList rulelist; - boolean useless; - if (!doNotAddRule) { - CssStyleRule stylerule = new CssStyleRule(indent, selectortext, - properties, important); - if (!atRuleList.isEmpty()) { - rulelist = (CssRuleList)atRuleList.lastElement(); - useless = atRuleList.removeElement(rulelist); - } else { - rulelist = new CssRuleList(); - } - rulelist.addStyleRule(stylerule); - atRuleList.addElement(rulelist); - } - selectortext = ""; - doNotAddRule = false; + CssRuleList rulelist; + boolean useless; + if (!doNotAddRule) { + CssStyleRule stylerule = new CssStyleRule(indent, selectortext, + properties, important); + if (!atRuleList.isEmpty()) { + rulelist = (CssRuleList) atRuleList.lastElement(); + useless = atRuleList.removeElement(rulelist); + } else { + rulelist = new CssRuleList(); + } + rulelist.addStyleRule(stylerule); + atRuleList.addElement(rulelist); + } + selectortext = ""; + doNotAddRule = false; } public void removeThisRule() { - doNotAddRule = true; + doNotAddRule = true; } public void removeThisAtRule() { - doNotAddAtRule = true; + doNotAddAtRule = true; } public Vector newGetRules() { - return atRuleList; + return atRuleList; } String selectortext; boolean important; Vector properties; - String indent = new String(); + String indent = new String(); public String charset; }
Received on Friday, 12 August 2011 21:19:05 UTC