Skip to content
5 changes: 5 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
</properties>

<body>
<release version="2.40.1" date="xxxx, 2020" description="Bugfixes">
<action type="fix" dev="rbri" due-to="Ronny Shapiro">
Fix NPE getting event handlers without JS engine.
</action>
</release>
<release version="2.40.0" date="May 2, 2020" description="Bugfixes, Chrome 81, Firefox75">
<action type="add" dev="rbri" issue="161">
New WebClientConfiguration option ConnectionTimeToLive added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,9 +970,12 @@ public final void removeChild(final String tagName, final int i) {
* @return true if an event handler has been defined otherwise false
*/
public final boolean hasEventHandlers(final String eventName) {
final Object jsObj = getScriptableObject();
if (jsObj instanceof EventTarget) {
return ((EventTarget) jsObj).hasEventHandlers(eventName);

if (getPage().getWebClient().isJavaScriptEngineEnabled()) {
final Object jsObj = getScriptableObject();
if (jsObj instanceof EventTarget) {
return ((EventTarget) jsObj).hasEventHandlers(eventName);
}
}
return false;
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/gargoylesoftware/htmlunit/WebClient8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,22 @@ public void frameSetWithNoJS() throws Exception {
loadPage(webClient, html, null, URL_FIRST);
}
}

/**
* @throws Exception if something goes wrong
*/
@Test
public void imageEventHandlersWithNoJs() throws Exception {
final String html = "<html>\n"
+ "<head>\n"
+ "</head>\n"
+ "<body>\n"
+ "<img onerror='doSomething(this)' />\n"
+ "</body>\n"
+ "</html>";

try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
loadPage(webClient, html, null, URL_FIRST);
}
}
}