diff --git a/src/changes/changes.xml b/src/changes/changes.xml index f16d0d6270..b09675cb1d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -8,6 +8,10 @@ + + StyleElement implements no longer the Comparable interface. For our special needs we now using the + static method compareToByImportanceAndSpecificity(StyleElement, StyleElement). + core-js: load more classes lazily. diff --git a/src/main/java/org/htmlunit/css/AbstractCssStyleDeclaration.java b/src/main/java/org/htmlunit/css/AbstractCssStyleDeclaration.java index c6a824bfb4..4e650546d7 100644 --- a/src/main/java/org/htmlunit/css/AbstractCssStyleDeclaration.java +++ b/src/main/java/org/htmlunit/css/AbstractCssStyleDeclaration.java @@ -137,7 +137,7 @@ public String getStyleAttribute(final Definition definition1, final Definition d return element1.getValue(); } if (element1 != null) { - if (element1.compareTo(element2) > 0) { + if (StyleElement.compareToByImportanceAndSpecificity(element1, element2) > 0) { return element1.getValue(); } } diff --git a/src/main/java/org/htmlunit/css/StyleElement.java b/src/main/java/org/htmlunit/css/StyleElement.java index 2383e3da2f..2e4f2a2714 100644 --- a/src/main/java/org/htmlunit/css/StyleElement.java +++ b/src/main/java/org/htmlunit/css/StyleElement.java @@ -35,7 +35,7 @@ * @author Ronald Brill * @author Frank Danek */ -public class StyleElement implements Comparable, Serializable { +public class StyleElement implements Serializable { /** CSS important property constant. */ public static final String PRIORITY_IMPORTANT = "important"; @@ -133,28 +133,41 @@ public String toString() { } /** - * {@inheritDoc} + * This takes only the importance, the specificity and the index into account. + * The name and value properties are ignored. + * + * @param first the {@link StyleElement} to compare + * @param second the {@link StyleElement} to compare with + * @return a negative integer, zero, or a positive integer as this object + * is less than, equal to, or greater than the specified object. */ - @Override - public int compareTo(final StyleElement e) { - if (e == null) { + public static int compareToByImportanceAndSpecificity(final StyleElement first, final StyleElement second) { + if (first == null) { + return second == null ? 0 : -1; + } + + if (second == null) { return 1; } - if (isImportant()) { - if (!e.isImportant()) { + if (first == second) { + return 0; + } + + if (first.isImportant()) { + if (!second.isImportant()) { return 1; } } else { - if (e.isImportant()) { + if (second.isImportant()) { return -1; } } - final int comp = getSpecificity().compareTo(e.getSpecificity()); + final int comp = first.getSpecificity().compareTo(second.getSpecificity()); if (comp == 0) { - return Long.compare(getIndex(), e.getIndex()); + return Long.compare(first.getIndex(), second.getIndex()); } return comp; } diff --git a/src/main/java/org/htmlunit/html/DomElement.java b/src/main/java/org/htmlunit/html/DomElement.java index 4ff748c962..ef5776e3f1 100644 --- a/src/main/java/org/htmlunit/html/DomElement.java +++ b/src/main/java/org/htmlunit/html/DomElement.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; @@ -110,6 +111,13 @@ public class DomElement extends DomNamespaceNode implements Element { private String styleString_; private LinkedHashMap styleMap_; + private static final Comparator STYLE_ELEMENT_COMPARATOR = new Comparator() { + @Override + public int compare(final StyleElement first, final StyleElement second) { + return StyleElement.compareToByImportanceAndSpecificity(first, second); + } + }; + /** * Whether the Mouse is currently over this element or not. */ @@ -604,7 +612,7 @@ public void writeStyleToElement(final Map styleMap) { final StringBuilder builder = new StringBuilder(); final List styleElements = new ArrayList<>(styleMap.values()); - Collections.sort(styleElements); + Collections.sort(styleElements, STYLE_ELEMENT_COMPARATOR); for (final StyleElement e : styleElements) { if (builder.length() != 0) { builder.append(' '); @@ -790,10 +798,12 @@ public DomElement getLastElementChild() { * Returns the current number of element nodes that are children of this element. * @return the current number of element nodes that are children of this element. */ - @SuppressWarnings("PMD.UnusedLocalVariable") public int getChildElementCount() { int counter = 0; - for (final DomElement elem : getChildElements()) { + + final Iterator iterator = getChildElements().iterator(); + while (iterator.hasNext()) { + iterator.next(); counter++; } return counter;