use of com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget in project htmlunit by HtmlUnit.
the class NamedAttrNodeMapImpl method fireEvent.
/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
*
* Fires the event on the element. Nothing is done if JavaScript is disabled.
* @param event the event to fire
* @return the execution result, or {@code null} if nothing is executed
*/
public ScriptResult fireEvent(final Event event) {
final WebClient client = getPage().getWebClient();
if (!client.isJavaScriptEnabled()) {
return null;
}
if (!handles(event)) {
return null;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Firing " + event);
}
final EventTarget jsElt = getScriptableObject();
final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
final ScriptResult result = cf.callSecured(cx -> jsElt.fireEvent(event), getHtmlPageOrNull());
if (event.isAborted(result)) {
preventDefault();
}
return result;
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget in project htmlunit by HtmlUnit.
the class ScriptElementSupport method executeEvent.
private static void executeEvent(final DomElement element, final String type) {
final EventTarget eventTarget = element.getScriptableObject();
final Event event = new Event(element, type);
eventTarget.executeEventLocally(event);
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget in project htmlunit by HtmlUnit.
the class HtmlPage method executeEventHandlersIfNeeded.
/**
* Looks for and executes any appropriate event handlers. Looks for body and frame tags.
* @param eventType either {@link Event#TYPE_LOAD}, {@link Event#TYPE_UNLOAD}, or {@link Event#TYPE_BEFORE_UNLOAD}
* @return {@code true} if user accepted <tt>onbeforeunload</tt> (not relevant to other events)
*/
private boolean executeEventHandlersIfNeeded(final String eventType) {
// If JavaScript isn't enabled, there's nothing for us to do.
if (!getWebClient().isJavaScriptEnabled()) {
return true;
}
// Execute the specified event on the document element.
final WebWindow window = getEnclosingWindow();
if (window.getScriptableObject() instanceof Window) {
final Event event;
if (eventType.equals(Event.TYPE_BEFORE_UNLOAD)) {
event = new BeforeUnloadEvent(this, eventType);
} else {
event = new Event(this, eventType);
}
// here so it could be used with HtmlPage.
if (LOG.isDebugEnabled()) {
LOG.debug("Firing " + event);
}
final EventTarget jsNode;
if (Event.TYPE_DOM_DOCUMENT_LOADED.equals(eventType)) {
jsNode = this.getScriptableObject();
} else {
// The load/beforeunload/unload events target Document but paths Window only (tested in Chrome/FF)
jsNode = window.getScriptableObject();
}
final HtmlUnitContextFactory cf = ((JavaScriptEngine) getWebClient().getJavaScriptEngine()).getContextFactory();
cf.callSecured(cx -> jsNode.fireEvent(event), this);
if (!isOnbeforeunloadAccepted(this, event)) {
return false;
}
}
// If this page was loaded in a frame, execute the version of the event specified on the frame tag.
if (window instanceof FrameWindow) {
final FrameWindow fw = (FrameWindow) window;
final BaseFrameElement frame = fw.getFrameElement();
// if part of a document fragment, then the load event is not triggered
if (Event.TYPE_LOAD.equals(eventType) && frame.getParentNode() instanceof DomDocumentFragment) {
return true;
}
if (frame.hasEventHandlers("on" + eventType)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing on" + eventType + " handler for " + frame);
}
if (window.getScriptableObject() instanceof Window) {
final Event event;
if (Event.TYPE_BEFORE_UNLOAD.equals(eventType)) {
event = new BeforeUnloadEvent(frame, eventType);
} else {
// ff does not trigger the onload event in this case
if (PageDenied.BY_CONTENT_SECURIRY_POLICY == fw.getPageDenied() && hasFeature(JS_EVENT_LOAD_SUPPRESSED_BY_CONTENT_SECURIRY_POLICY)) {
return true;
}
event = new Event(frame, eventType);
}
// This fires the "load" event for the <frame> element which, like all non-window
// load events, propagates up to Document but not Window. The "load" event for
// <frameset> on the other hand, like that of <body>, is handled above where it is
// fired against Document and directed to Window.
frame.fireEvent(event);
if (!isOnbeforeunloadAccepted((HtmlPage) frame.getPage(), event)) {
return false;
}
}
}
}
return true;
}
Aggregations