use of com.gargoylesoftware.htmlunit.javascript.host.event.EventHandler in project htmlunit by HtmlUnit.
the class HTMLElement method setAttribute.
/**
* Sets an attribute.
* See also <a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-F68F082">
* the DOM reference</a>
*
* @param name Name of the attribute to set
* @param value Value to set the attribute to
*/
@Override
public void setAttribute(String name, final String value) {
getDomNodeOrDie().setAttribute(name, value);
// call corresponding event handler setOnxxx if found
if (!name.isEmpty()) {
name = name.toLowerCase(Locale.ROOT);
if (name.startsWith("on")) {
try {
name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
final Method method = getClass().getMethod("set" + name, METHOD_PARAMS_OBJECT);
method.invoke(this, new EventHandler(getDomNodeOrDie(), name.substring(2), value));
} catch (final NoSuchMethodException | IllegalAccessException e) {
// silently ignore
} catch (final InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
}
}
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.EventHandler in project htmlunit by HtmlUnit.
the class Element method createEventHandler.
/**
* Create the event handler function from the attribute value.
* @param eventName the event name (ex: "onclick")
* @param attrValue the attribute value
*/
protected void createEventHandler(final String eventName, final String attrValue) {
final DomElement htmlElt = getDomNodeOrDie();
// TODO: check that it is an "allowed" event for the browser, and take care to the case
final BaseFunction eventHandler = new EventHandler(htmlElt, eventName, attrValue);
setEventHandler(eventName, eventHandler);
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.EventHandler in project htmlunit by HtmlUnit.
the class ScriptElementSupport method executeInlineScriptIfNeeded.
/**
* Executes this script node as inline script if necessary and/or possible.
*/
private static void executeInlineScriptIfNeeded(final DomElement element) {
if (!isExecutionNeeded(element, false, false)) {
return;
}
final ScriptElement scriptElement = (ScriptElement) element;
final String src = scriptElement.getSrcAttribute();
if (src != ATTRIBUTE_NOT_DEFINED) {
return;
}
final String forr = element.getAttributeDirect("for");
String event = element.getAttributeDirect("event");
// The event name can be like "onload" or "onload()".
if (event.endsWith("()")) {
event = event.substring(0, event.length() - 2);
}
final String scriptCode = getScriptCode(element);
if (event != ATTRIBUTE_NOT_DEFINED && forr != ATTRIBUTE_NOT_DEFINED && element.hasFeature(JS_SCRIPT_SUPPORTS_FOR_AND_EVENT_WINDOW) && "window".equals(forr)) {
final Window window = element.getPage().getEnclosingWindow().getScriptableObject();
final BaseFunction function = new EventHandler(element, event, scriptCode);
window.getEventListenersContainer().addEventListener(StringUtils.substring(event, 2), function, false);
return;
}
if (forr == ATTRIBUTE_NOT_DEFINED || "onload".equals(event)) {
final String url = element.getPage().getUrl().toExternalForm();
final int line1 = element.getStartLineNumber();
final int line2 = element.getEndLineNumber();
final int col1 = element.getStartColumnNumber();
final int col2 = element.getEndColumnNumber();
final String desc = "script in " + url + " from (" + line1 + ", " + col1 + ") to (" + line2 + ", " + col2 + ")";
scriptElement.setExecuted(true);
((HtmlPage) element.getPage()).executeJavaScript(scriptCode, desc, line1);
}
}
Aggregations