Skip to content

Commit

Permalink
avoid some not required object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Nov 22, 2024
1 parent 616522b commit 508bde2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/htmlunit/html/DefaultElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,16 @@ public HtmlElement createElementNS(final SgmlPage page, final String namespaceUR
* @return the map of attribute values for {@link HtmlElement}s
*/
static Map<String, DomAttr> toMap(final SgmlPage page, final Attributes attributes) {
// it is ok to return null here, the element ctor's are able to deal with that
if (attributes == null) {
return new OrderedFastHashMapWithLowercaseKeys<>(0);
return null;
}

final int length = attributes.getLength();
if (length == 0) {
return null;
}

final Map<String, DomAttr> attributeMap = new OrderedFastHashMapWithLowercaseKeys<>(length);

// small performance optimization if we know the attributes we can avoid some index lookups
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/htmlunit/html/DomElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ public DomElement(final String namespaceURI, final String qualifiedName, final S
final Map<String, DomAttr> attributes) {
super(namespaceURI, qualifiedName, page);

if (attributes != null) {
if (attributes == null) {
attributes_ = new NamedAttrNodeMapImpl(this, isAttributeCaseSensitive());
}
else {
attributes_ = new NamedAttrNodeMapImpl(this, isAttributeCaseSensitive(), attributes);

for (final DomAttr entry : attributes.values()) {
Expand All @@ -144,9 +147,6 @@ public DomElement(final String namespaceURI, final String qualifiedName, final S
}
}
}
else {
attributes_ = new NamedAttrNodeMapImpl(this, isAttributeCaseSensitive());
}
}

/**
Expand Down

0 comments on commit 508bde2

Please sign in to comment.