Skip to content

Commit 7736d75

Browse files
committed
StyleElement implements no longer the Comparable interface. For our special needs we now using the static method compareToByImportanceAndSpecificity(StyleElement, StyleElement).
1 parent 8af9f01 commit 7736d75

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

src/changes/changes.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
<body>
1010
<release version="4.10.0" date="February xx, 2025" description="Chrome/Edge 132, Firefox 135, Javascript Errors, Bugfixes">
11+
<action type="update" dev="RhinoTeam">
12+
StyleElement implements no longer the Comparable interface. For our special needs we now using the
13+
static method compareToByImportanceAndSpecificity(StyleElement, StyleElement).
14+
</action>
1115
<action type="update" dev="RhinoTeam">
1216
core-js: load more classes lazily.
1317
</action>

src/main/java/org/htmlunit/css/AbstractCssStyleDeclaration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public String getStyleAttribute(final Definition definition1, final Definition d
137137
return element1.getValue();
138138
}
139139
if (element1 != null) {
140-
if (element1.compareTo(element2) > 0) {
140+
if (StyleElement.compareToByImportanceAndSpecificity(element1, element2) > 0) {
141141
return element1.getValue();
142142
}
143143
}

src/main/java/org/htmlunit/css/StyleElement.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* @author Ronald Brill
3636
* @author Frank Danek
3737
*/
38-
public class StyleElement implements Comparable<StyleElement>, Serializable {
38+
public class StyleElement implements Serializable {
3939
/** CSS important property constant. */
4040
public static final String PRIORITY_IMPORTANT = "important";
4141

@@ -133,28 +133,41 @@ public String toString() {
133133
}
134134

135135
/**
136-
* {@inheritDoc}
136+
* This takes only the importance, the specificity and the index into account.
137+
* The name and value properties are ignored.
138+
*
139+
* @param first the {@link StyleElement} to compare
140+
* @param second the {@link StyleElement} to compare with
141+
* @return a negative integer, zero, or a positive integer as this object
142+
* is less than, equal to, or greater than the specified object.
137143
*/
138-
@Override
139-
public int compareTo(final StyleElement e) {
140-
if (e == null) {
144+
public static int compareToByImportanceAndSpecificity(final StyleElement first, final StyleElement second) {
145+
if (first == null) {
146+
return second == null ? 0 : -1;
147+
}
148+
149+
if (second == null) {
141150
return 1;
142151
}
143152

144-
if (isImportant()) {
145-
if (!e.isImportant()) {
153+
if (first == second) {
154+
return 0;
155+
}
156+
157+
if (first.isImportant()) {
158+
if (!second.isImportant()) {
146159
return 1;
147160
}
148161
}
149162
else {
150-
if (e.isImportant()) {
163+
if (second.isImportant()) {
151164
return -1;
152165
}
153166
}
154167

155-
final int comp = getSpecificity().compareTo(e.getSpecificity());
168+
final int comp = first.getSpecificity().compareTo(second.getSpecificity());
156169
if (comp == 0) {
157-
return Long.compare(getIndex(), e.getIndex());
170+
return Long.compare(first.getIndex(), second.getIndex());
158171
}
159172
return comp;
160173
}

src/main/java/org/htmlunit/html/DomElement.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.ArrayList;
2626
import java.util.Collection;
2727
import java.util.Collections;
28+
import java.util.Comparator;
2829
import java.util.Iterator;
2930
import java.util.LinkedHashMap;
3031
import java.util.List;
@@ -110,6 +111,13 @@ public class DomElement extends DomNamespaceNode implements Element {
110111
private String styleString_;
111112
private LinkedHashMap<String, StyleElement> styleMap_;
112113

114+
private static final Comparator<StyleElement> STYLE_ELEMENT_COMPARATOR = new Comparator<StyleElement>() {
115+
@Override
116+
public int compare(final StyleElement first, final StyleElement second) {
117+
return StyleElement.compareToByImportanceAndSpecificity(first, second);
118+
}
119+
};
120+
113121
/**
114122
* Whether the Mouse is currently over this element or not.
115123
*/
@@ -604,7 +612,7 @@ public void writeStyleToElement(final Map<String, StyleElement> styleMap) {
604612

605613
final StringBuilder builder = new StringBuilder();
606614
final List<StyleElement> styleElements = new ArrayList<>(styleMap.values());
607-
Collections.sort(styleElements);
615+
Collections.sort(styleElements, STYLE_ELEMENT_COMPARATOR);
608616
for (final StyleElement e : styleElements) {
609617
if (builder.length() != 0) {
610618
builder.append(' ');
@@ -790,10 +798,12 @@ public DomElement getLastElementChild() {
790798
* Returns the current number of element nodes that are children of this element.
791799
* @return the current number of element nodes that are children of this element.
792800
*/
793-
@SuppressWarnings("PMD.UnusedLocalVariable")
794801
public int getChildElementCount() {
795802
int counter = 0;
796-
for (final DomElement elem : getChildElements()) {
803+
804+
final Iterator<DomElement> iterator = getChildElements().iterator();
805+
while (iterator.hasNext()) {
806+
iterator.next();
797807
counter++;
798808
}
799809
return counter;

0 commit comments

Comments
 (0)