Skip to content

Commit

Permalink
StyleElement implements no longer the Comparable interface. For our s…
Browse files Browse the repository at this point in the history
…pecial needs we now using the static method compareToByImportanceAndSpecificity(StyleElement, StyleElement).
  • Loading branch information
rbri committed Feb 10, 2025
1 parent 8af9f01 commit 7736d75
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

<body>
<release version="4.10.0" date="February xx, 2025" description="Chrome/Edge 132, Firefox 135, Javascript Errors, Bugfixes">
<action type="update" dev="RhinoTeam">
StyleElement implements no longer the Comparable interface. For our special needs we now using the
static method compareToByImportanceAndSpecificity(StyleElement, StyleElement).
</action>
<action type="update" dev="RhinoTeam">
core-js: load more classes lazily.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/org/htmlunit/css/StyleElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author Ronald Brill
* @author Frank Danek
*/
public class StyleElement implements Comparable<StyleElement>, Serializable {
public class StyleElement implements Serializable {
/** CSS important property constant. */
public static final String PRIORITY_IMPORTANT = "important";

Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/htmlunit/html/DomElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,6 +111,13 @@ public class DomElement extends DomNamespaceNode implements Element {
private String styleString_;
private LinkedHashMap<String, StyleElement> styleMap_;

private static final Comparator<StyleElement> STYLE_ELEMENT_COMPARATOR = new Comparator<StyleElement>() {
@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.
*/
Expand Down Expand Up @@ -604,7 +612,7 @@ public void writeStyleToElement(final Map<String, StyleElement> styleMap) {

final StringBuilder builder = new StringBuilder();
final List<StyleElement> 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(' ');
Expand Down Expand Up @@ -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<DomElement> iterator = getChildElements().iterator();
while (iterator.hasNext()) {
iterator.next();
counter++;
}
return counter;
Expand Down

0 comments on commit 7736d75

Please sign in to comment.