Skip to content

Commit 6a59ab5

Browse files
committed
Make the map used as singleton read only to avoid side effects. Also the method implementations are much simpler for the empty map.
1 parent 8c364e0 commit 6a59ab5

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

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

-9
Original file line numberDiff line numberDiff line change
@@ -1663,19 +1663,10 @@ public void setInnerHtml(final String source) throws SAXException, IOException {
16631663
* The {@link NamedNodeMap} to store the node attributes.
16641664
*/
16651665
class NamedAttrNodeMapImpl implements Map<String, DomAttr>, NamedNodeMap, Serializable {
1666-
protected static final NamedAttrNodeMapImpl EMPTY_MAP = new NamedAttrNodeMapImpl();
1667-
16681666
private final OrderedFastHashMap<String, DomAttr> map_;
16691667
private final DomElement domNode_;
16701668
private final boolean caseSensitive_;
16711669

1672-
private NamedAttrNodeMapImpl() {
1673-
super();
1674-
domNode_ = null;
1675-
caseSensitive_ = true;
1676-
map_ = new OrderedFastHashMap<>(0);
1677-
}
1678-
16791670
NamedAttrNodeMapImpl(final DomElement domNode, final boolean caseSensitive) {
16801671
super();
16811672
if (domNode == null) {

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

+76-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public abstract class DomNode implements Cloneable, Serializable, Node {
9999
/** The name of the "element" property. Used when watching property change events. */
100100
public static final String PROPERTY_ELEMENT = "element";
101101

102+
private static final NamedNodeMap EMPTY_NAMED_NODE_MAP = new ReadOnlyEmptyNamedNodeMapImpl();
103+
102104
/** The owning page of this node. */
103105
private SgmlPage page_;
104106

@@ -662,7 +664,7 @@ public boolean hasAttributes() {
662664
*/
663665
@Override
664666
public NamedNodeMap getAttributes() {
665-
return NamedAttrNodeMapImpl.EMPTY_MAP;
667+
return EMPTY_NAMED_NODE_MAP;
666668
}
667669

668670
/**
@@ -1938,4 +1940,77 @@ public DomElement closest(final String selectorString) {
19381940
throw new CSSException("Error parsing CSS selectors from '" + selectorString + "': " + e.getMessage(), e);
19391941
}
19401942
}
1943+
1944+
/**
1945+
* An unmodifiable empty {@link NamedNodeMap} implementation.
1946+
*/
1947+
private static final class ReadOnlyEmptyNamedNodeMapImpl implements NamedNodeMap, Serializable {
1948+
private ReadOnlyEmptyNamedNodeMapImpl() {
1949+
super();
1950+
}
1951+
1952+
/**
1953+
* {@inheritDoc}
1954+
*/
1955+
@Override
1956+
public int getLength() {
1957+
return 0;
1958+
}
1959+
1960+
/**
1961+
* {@inheritDoc}
1962+
*/
1963+
@Override
1964+
public DomAttr getNamedItem(final String name) {
1965+
return null;
1966+
}
1967+
1968+
/**
1969+
* {@inheritDoc}
1970+
*/
1971+
@Override
1972+
public Node getNamedItemNS(final String namespaceURI, final String localName) {
1973+
return null;
1974+
}
1975+
1976+
/**
1977+
* {@inheritDoc}
1978+
*/
1979+
@Override
1980+
public Node item(final int index) {
1981+
return null;
1982+
}
1983+
1984+
/**
1985+
* {@inheritDoc}
1986+
*/
1987+
@Override
1988+
public Node removeNamedItem(final String name) throws DOMException {
1989+
return null;
1990+
}
1991+
1992+
/**
1993+
* {@inheritDoc}
1994+
*/
1995+
@Override
1996+
public Node removeNamedItemNS(final String namespaceURI, final String localName) {
1997+
return null;
1998+
}
1999+
2000+
/**
2001+
* {@inheritDoc}
2002+
*/
2003+
@Override
2004+
public DomAttr setNamedItem(final Node node) {
2005+
throw new UnsupportedOperationException("ReadOnlyEmptyNamedAttrNodeMapImpl.setNamedItem");
2006+
}
2007+
2008+
/**
2009+
* {@inheritDoc}
2010+
*/
2011+
@Override
2012+
public Node setNamedItemNS(final Node node) throws DOMException {
2013+
throw new UnsupportedOperationException("ReadOnlyEmptyNamedAttrNodeMapImpl.setNamedItemNS");
2014+
}
2015+
}
19412016
}

0 commit comments

Comments
 (0)