Skip to content

Commit

Permalink
Make the map used as singleton read only to avoid side effects. Also …
Browse files Browse the repository at this point in the history
…the method implementations are much simpler for the empty map.
  • Loading branch information
rbri committed Sep 30, 2024
1 parent 8c364e0 commit 6a59ab5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
9 changes: 0 additions & 9 deletions src/main/java/org/htmlunit/html/DomElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -1663,19 +1663,10 @@ public void setInnerHtml(final String source) throws SAXException, IOException {
* The {@link NamedNodeMap} to store the node attributes.
*/
class NamedAttrNodeMapImpl implements Map<String, DomAttr>, NamedNodeMap, Serializable {
protected static final NamedAttrNodeMapImpl EMPTY_MAP = new NamedAttrNodeMapImpl();

private final OrderedFastHashMap<String, DomAttr> map_;
private final DomElement domNode_;
private final boolean caseSensitive_;

private NamedAttrNodeMapImpl() {
super();
domNode_ = null;
caseSensitive_ = true;
map_ = new OrderedFastHashMap<>(0);
}

NamedAttrNodeMapImpl(final DomElement domNode, final boolean caseSensitive) {
super();
if (domNode == null) {
Expand Down
77 changes: 76 additions & 1 deletion src/main/java/org/htmlunit/html/DomNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public abstract class DomNode implements Cloneable, Serializable, Node {
/** The name of the "element" property. Used when watching property change events. */
public static final String PROPERTY_ELEMENT = "element";

private static final NamedNodeMap EMPTY_NAMED_NODE_MAP = new ReadOnlyEmptyNamedNodeMapImpl();

/** The owning page of this node. */
private SgmlPage page_;

Expand Down Expand Up @@ -662,7 +664,7 @@ public boolean hasAttributes() {
*/
@Override
public NamedNodeMap getAttributes() {
return NamedAttrNodeMapImpl.EMPTY_MAP;
return EMPTY_NAMED_NODE_MAP;
}

/**
Expand Down Expand Up @@ -1938,4 +1940,77 @@ public DomElement closest(final String selectorString) {
throw new CSSException("Error parsing CSS selectors from '" + selectorString + "': " + e.getMessage(), e);
}
}

/**
* An unmodifiable empty {@link NamedNodeMap} implementation.
*/
private static final class ReadOnlyEmptyNamedNodeMapImpl implements NamedNodeMap, Serializable {
private ReadOnlyEmptyNamedNodeMapImpl() {
super();
}

/**
* {@inheritDoc}
*/
@Override
public int getLength() {
return 0;
}

/**
* {@inheritDoc}
*/
@Override
public DomAttr getNamedItem(final String name) {
return null;
}

/**
* {@inheritDoc}
*/
@Override
public Node getNamedItemNS(final String namespaceURI, final String localName) {
return null;
}

/**
* {@inheritDoc}
*/
@Override
public Node item(final int index) {
return null;
}

/**
* {@inheritDoc}
*/
@Override
public Node removeNamedItem(final String name) throws DOMException {
return null;
}

/**
* {@inheritDoc}
*/
@Override
public Node removeNamedItemNS(final String namespaceURI, final String localName) {
return null;
}

/**
* {@inheritDoc}
*/
@Override
public DomAttr setNamedItem(final Node node) {
throw new UnsupportedOperationException("ReadOnlyEmptyNamedAttrNodeMapImpl.setNamedItem");
}

/**
* {@inheritDoc}
*/
@Override
public Node setNamedItemNS(final Node node) throws DOMException {
throw new UnsupportedOperationException("ReadOnlyEmptyNamedAttrNodeMapImpl.setNamedItemNS");
}
}
}

0 comments on commit 6a59ab5

Please sign in to comment.