Skip to content

Commit b8fd157

Browse files
committed
simplified code
1 parent 65b91eb commit b8fd157

3 files changed

Lines changed: 54 additions & 47 deletions

File tree

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,11 @@ public StyleElement getStyleElement(final String name) {
274274
*/
275275
public StyleElement getStyleElementCaseInSensitive(final String name) {
276276
final Map<String, StyleElement> map = getStyleMap();
277-
for (final Map.Entry<String, StyleElement> entry : map.entrySet()) {
278-
if (entry.getKey().equalsIgnoreCase(name)) {
279-
return entry.getValue();
277+
if (map != null) {
278+
for (final Map.Entry<String, StyleElement> entry : map.entrySet()) {
279+
if (entry.getKey().equalsIgnoreCase(name)) {
280+
return entry.getValue();
281+
}
280282
}
281283
}
282284
return null;
@@ -620,16 +622,17 @@ public void writeStyleToElement(final Map<String, StyleElement> styleMap) {
620622
return;
621623
}
622624

623-
final StringBuilder builder = new StringBuilder();
624625
final List<StyleElement> styleElements = new ArrayList<>(styleMap.values());
625626
styleElements.sort(STYLE_ELEMENT_COMPARATOR);
627+
628+
final StringBuilder builder = new StringBuilder(styleElements.size() * 32);
626629
for (final StyleElement e : styleElements) {
627-
if (builder.length() != 0) {
630+
if (builder.length() > 0) {
628631
builder.append(' ');
629632
}
630633
builder.append(e.getName())
631-
.append(": ")
632-
.append(e.getValue());
634+
.append(": ")
635+
.append(e.getValue());
633636

634637
final String prio = e.getPriority();
635638
if (StringUtils.isNotBlank(prio)) {
@@ -798,11 +801,12 @@ public DomElement getFirstElementChild() {
798801
* @return the last child element node of this element. null if this element has no child elements
799802
*/
800803
public DomElement getLastElementChild() {
801-
DomElement lastChild = null;
802-
for (final DomElement domElement : getChildElements()) {
803-
lastChild = domElement;
804+
for (DomNode child = getLastChild(); child != null; child = child.getPreviousSibling()) {
805+
if (child instanceof DomElement elem) {
806+
return elem;
807+
}
804808
}
805-
return lastChild;
809+
return null;
806810
}
807811

808812
/**
@@ -811,9 +815,10 @@ public DomElement getLastElementChild() {
811815
*/
812816
public int getChildElementCount() {
813817
int counter = 0;
814-
815-
for (final DomElement domElement : getChildElements()) {
816-
counter++;
818+
for (DomNode child = getFirstChild(); child != null; child = child.getNextSibling()) {
819+
if (child instanceof DomElement) {
820+
counter++;
821+
}
817822
}
818823
return counter;
819824
}
@@ -1841,8 +1846,7 @@ public DomAttr put(final String key, final DomAttr value) {
18411846
@Override
18421847
public DomAttr remove(final Object key) {
18431848
if (key instanceof String string) {
1844-
final String name = fixName(string);
1845-
return map_.remove(name);
1849+
return map_.remove(fixName(string));
18461850
}
18471851
return null;
18481852
}
@@ -1884,8 +1888,7 @@ public boolean containsKey(final Object key) {
18841888
@Override
18851889
public DomAttr get(final Object key) {
18861890
if (key instanceof String string) {
1887-
final String name = fixName(string);
1888-
return map_.get(name);
1891+
return map_.get(fixName(string));
18891892
}
18901893
return null;
18911894
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ protected DomNamespaceNode(final String namespaceURI, final String qualifiedName
6767
*/
6868
@Override
6969
public String getNamespaceURI() {
70-
if (getPage().isHtmlPage()
71-
&& !(getPage() instanceof XHtmlPage)
70+
final SgmlPage page = getPage();
71+
if (page.isHtmlPage()
72+
&& !(page instanceof XHtmlPage)
7273
&& Html.XHTML_NAMESPACE.equals(namespaceURI_)
7374
&& XPathHelper.isProcessingXPath()) {
7475
// for xpath processing we have to strip the 'default' XHTML namespace for HTML pages to be able to find
@@ -113,8 +114,13 @@ public String getPrefix() {
113114
@Override
114115
public void setPrefix(final String prefix) {
115116
prefix_ = prefix;
116-
if (prefix_ != null && localName_ != null) {
117-
qualifiedName_ = prefix_ + ":" + localName_;
117+
if (localName_ != null) {
118+
if (prefix != null && !prefix.isEmpty()) {
119+
qualifiedName_ = prefix + ":" + localName_;
120+
}
121+
else {
122+
qualifiedName_ = localName_;
123+
}
118124
}
119125
}
120126

@@ -136,9 +142,10 @@ public void processImportNode(final Document doc) {
136142
// if we are importing from a namespace-aware source
137143
// we have to drop the XHtmlNamespace because we did this already
138144
// for the HTML document itself
139-
final SgmlPage page = (SgmlPage) doc.getDomNodeOrDie();
140-
if (page.isHtmlPage() && !(page instanceof XHtmlPage) && Html.XHTML_NAMESPACE.equals(namespaceURI_)) {
141-
namespaceURI_ = null;
145+
if (doc.getDomNodeOrDie() instanceof SgmlPage page) {
146+
if (page.isHtmlPage() && !(page instanceof XHtmlPage) && Html.XHTML_NAMESPACE.equals(namespaceURI_)) {
147+
namespaceURI_ = null;
148+
}
142149
}
143150
}
144151
}

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ protected final class DescendantDomElementsIterator extends AbstractDescendantIt
16781678
*/
16791679
@Override
16801680
protected boolean isAccepted(final DomNode node) {
1681-
return DomElement.class.isAssignableFrom(node.getClass());
1681+
return node instanceof DomElement;
16821682
}
16831683
}
16841684

@@ -1691,7 +1691,7 @@ protected final class DescendantHtmlElementsIterator extends AbstractDescendantI
16911691
*/
16921692
@Override
16931693
protected boolean isAccepted(final DomNode node) {
1694-
return HtmlElement.class.isAssignableFrom(node.getClass());
1694+
return node instanceof HtmlElement;
16951695
}
16961696
}
16971697

@@ -2125,10 +2125,12 @@ public boolean handles(final Event event) {
21252125
*/
21262126
public DomElement getPreviousElementSibling() {
21272127
DomNode node = getPreviousSibling();
2128-
while (node != null && !(node instanceof DomElement)) {
2129-
node = node.getPreviousSibling();
2128+
for ( ; node != null; node = node.getPreviousSibling()) {
2129+
if (node instanceof DomElement elem) {
2130+
return elem;
2131+
}
21302132
}
2131-
return (DomElement) node;
2133+
return null;
21322134
}
21332135

21342136
/**
@@ -2139,10 +2141,12 @@ public DomElement getPreviousElementSibling() {
21392141
*/
21402142
public DomElement getNextElementSibling() {
21412143
DomNode node = getNextSibling();
2142-
while (node != null && !(node instanceof DomElement)) {
2143-
node = node.getNextSibling();
2144+
for ( ; node != null; node = node.getNextSibling()) {
2145+
if (node instanceof DomElement elem) {
2146+
return elem;
2147+
}
21442148
}
2145-
return (DomElement) node;
2149+
return null;
21462150
}
21472151

21482152
/**
@@ -2162,22 +2166,15 @@ public DomElement closest(final String selectorString) {
21622166
// closest() only ever matches elements; if this node isn't one,
21632167
// start the search from the nearest ancestor element instead.
21642168
DomNode current = this;
2165-
while (current != null && !(current instanceof DomElement)) {
2166-
current = current.getParentNode();
2167-
}
2168-
2169-
while (current != null) {
2170-
final DomElement elem = (DomElement) current;
2171-
for (final Selector selector : selectorList) {
2172-
if (CssStyleSheet.selects(webClient.getBrowserVersion(), selector, elem, null, true, true)) {
2173-
return elem;
2169+
for ( ; current != null; current = current.getParentNode()) {
2170+
if (current instanceof DomElement elem) {
2171+
for (final Selector selector : selectorList) {
2172+
if (CssStyleSheet.selects(
2173+
webClient.getBrowserVersion(), selector, elem, null, true, true)) {
2174+
return elem;
2175+
}
21742176
}
21752177
}
2176-
2177-
do {
2178-
current = current.getParentNode();
2179-
}
2180-
while (current != null && !(current instanceof DomElement));
21812178
}
21822179
}
21832180
return null;

0 commit comments

Comments
 (0)