Skip to content

Commit

Permalink
simplify code (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 15, 2024
1 parent 6a6ca44 commit 9dce68c
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/main/java/org/htmlunit/html/AbstractDomNodeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.w3c.dom.Node;
Expand All @@ -35,7 +35,7 @@ public abstract class AbstractDomNodeList<E extends DomNode> extends AbstractLis
implements DomNodeList<E>, Serializable {

/** This node list's root node. */
private DomNode node_;
private final DomNode node_;

/** Element cache, used to avoid XPath expression evaluation as much as possible. */
private List<E> cachedElements_;
Expand All @@ -47,14 +47,18 @@ public abstract class AbstractDomNodeList<E extends DomNode> extends AbstractLis
*/
public AbstractDomNodeList(final DomNode node) {
super();
if (node != null) {
node_ = node;
final DomHtmlAttributeChangeListenerImpl listener = new DomHtmlAttributeChangeListenerImpl(this);
node_.addDomChangeListener(listener);
if (node_ instanceof HtmlElement) {
((HtmlElement) node_).addHtmlAttributeChangeListener(listener);
cachedElements_ = null;
}
if (node == null) {
node_ = null;
cachedElements_ = Collections.EMPTY_LIST;
return;
}

node_ = node;
final DomHtmlAttributeChangeListenerImpl listener = new DomHtmlAttributeChangeListenerImpl(this);
node_.addDomChangeListener(listener);
if (node_ instanceof HtmlElement) {
((HtmlElement) node_).addHtmlAttributeChangeListener(listener);
cachedElements_ = null;
}
}

Expand All @@ -78,12 +82,7 @@ protected DomNode getDomNode() {
*/
private List<E> getNodes() {
if (cachedElements_ == null) {
if (node_ == null) {
cachedElements_ = new ArrayList<>();
}
else {
cachedElements_ = provideElements();
}
cachedElements_ = provideElements();
}
return cachedElements_;
}
Expand Down Expand Up @@ -177,7 +176,7 @@ public void attributeReplaced(final HtmlAttributeChangeEvent event) {
private void clearCache() {
if (nodeList_ != null) {
final AbstractDomNodeList<?> nodes = nodeList_.get();
if (nodes != null) {
if (nodes != null && nodes.node_ != null) {
nodes.cachedElements_ = null;
}
}
Expand Down

0 comments on commit 9dce68c

Please sign in to comment.