Skip to content

Commit f4697a8

Browse files
committed
improved null handling
1 parent bd1af97 commit f4697a8

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,17 @@ protected void setAttributeNS(final String namespaceURI, final String qualifiedN
196196
final String attributeValue, final boolean notifyAttributeChangeListeners,
197197
final boolean notifyMutationObservers) {
198198

199+
final HtmlPage htmlPage = getHtmlPageOrNull();
200+
199201
// TODO: Clean up; this is a hack for HtmlElement living within an XmlPage.
200-
if (null == getHtmlPageOrNull()) {
202+
if (null == htmlPage) {
201203
super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
202204
notifyMutationObservers);
203205
return;
204206
}
205207

206208
final String oldAttributeValue = getAttribute(qualifiedName);
207-
final HtmlPage htmlPage = (HtmlPage) getPage();
208209
final boolean mappedElement = isAttachedToPage()
209-
&& htmlPage != null
210210
&& (DomElement.NAME_ATTRIBUTE.equals(qualifiedName) || DomElement.ID_ATTRIBUTE.equals(qualifiedName));
211211
if (mappedElement) {
212212
// cast is save here because isMappedElement checks for HtmlPage
@@ -292,12 +292,19 @@ private void fireAttributeChangeImpl(final HtmlAttributeChangeEvent event,
292292
*/
293293
@Override
294294
public Attr setAttributeNode(final Attr attribute) {
295+
final HtmlPage htmlPage = getHtmlPageOrNull();
296+
297+
// TODO: Clean up; this is a hack for HtmlElement living within an XmlPage.
298+
if (null == htmlPage) {
299+
return super.setAttributeNode(attribute);
300+
}
301+
295302
final String qualifiedName = attribute.getName();
296303
final String oldAttributeValue = getAttribute(qualifiedName);
297-
final HtmlPage htmlPage = (HtmlPage) getPage();
304+
298305
final boolean mappedElement = isAttachedToPage()
299-
&& htmlPage != null
300-
&& (DomElement.NAME_ATTRIBUTE.equals(qualifiedName) || DomElement.ID_ATTRIBUTE.equals(qualifiedName));
306+
&& (DomElement.NAME_ATTRIBUTE.equals(qualifiedName)
307+
|| DomElement.ID_ATTRIBUTE.equals(qualifiedName));
301308
if (mappedElement) {
302309
htmlPage.removeMappedElement(this, false, false);
303310
}
@@ -330,23 +337,27 @@ public void removeAttribute(final String attributeName) {
330337
}
331338

332339
final HtmlPage htmlPage = getHtmlPageOrNull();
333-
final boolean mapped = htmlPage != null
334-
&& (DomElement.NAME_ATTRIBUTE.equals(attributeName) || DomElement.ID_ATTRIBUTE.equals(attributeName));
340+
341+
// TODO: Clean up; this is a hack for HtmlElement living within an XmlPage.
342+
if (null == htmlPage) {
343+
super.removeAttribute(attributeName);
344+
return;
345+
}
346+
347+
final boolean mapped = DomElement.NAME_ATTRIBUTE.equals(attributeName) || DomElement.ID_ATTRIBUTE.equals(attributeName);
335348
if (mapped) {
336349
htmlPage.removeMappedElement(this, false, false);
337350
}
338351

339352
super.removeAttribute(attributeName);
340353

341-
if (htmlPage != null) {
342-
if (mapped) {
343-
htmlPage.addMappedElement(this, false);
344-
}
345-
346-
final HtmlAttributeChangeEvent event = new HtmlAttributeChangeEvent(this, attributeName, value);
347-
fireHtmlAttributeRemoved(event);
348-
htmlPage.fireHtmlAttributeRemoved(event);
354+
if (mapped) {
355+
htmlPage.addMappedElement(this, false);
349356
}
357+
358+
final HtmlAttributeChangeEvent event = new HtmlAttributeChangeEvent(this, attributeName, value);
359+
fireHtmlAttributeRemoved(event);
360+
htmlPage.fireHtmlAttributeRemoved(event);
350361
}
351362

352363
/**

0 commit comments

Comments
 (0)