1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 import org.mozilla.javascript.BaseFunction; 43 import org.mozilla.javascript.Context; 44 import org.mozilla.javascript.Function; 45 import org.mozilla.javascript.JavaScriptException; 46 import org.mozilla.javascript.Scriptable; 47 48 import com.gargoylesoftware.htmlunit.html.HtmlElement; 49 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; 50 51 56 public class EventHandler extends BaseFunction { 57 private static final long serialVersionUID = 3257850965406068787L; 58 59 private final HtmlElement htmlElement_; 60 private final String jsSnippet_; 61 private Function realFunction_; 62 63 68 public EventHandler(final HtmlElement htmlElement, final String jsSnippet) { 69 htmlElement_ = htmlElement; 70 jsSnippet_ = "function(event) {" + jsSnippet + "}"; 71 } 72 73 77 public Object call(final Context cx, final Scriptable scope, 78 final Scriptable thisObj, final Object [] args) 79 throws JavaScriptException { 80 81 final SimpleScriptable jsObj = (SimpleScriptable) htmlElement_.getScriptObject(); 83 if (realFunction_ == null) { 85 realFunction_ = cx.compileFunction(jsObj, jsSnippet_, "event for " + htmlElement_, 1, null); 86 } 87 88 final Object result = realFunction_.call(cx, scope, thisObj, args); 89 90 return result; 91 } 92 93 98 public Object getDefaultValue(final Class typeHint) { 99 return jsSnippet_; 100 } 101 102 106 public Object get(final String name, final Scriptable start) { 107 if ("toString".equals(name)) { 109 return new BaseFunction() { 110 private static final long serialVersionUID = 3761409724511435061L; 111 112 public Object call(final Context cx, final Scriptable scope, 113 final Scriptable thisObj, final Object [] args) { 114 return jsSnippet_; 115 } 116 }; 117 } 118 119 return super.get(name, start); 120 } 121 122 126 protected final Log getLog() { 127 return LogFactory.getLog(getClass()); 128 } 129 } 130 | Popular Tags |