View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.event;
16  
17  import org.htmlunit.corejs.javascript.BaseFunction;
18  import org.htmlunit.corejs.javascript.Context;
19  import org.htmlunit.corejs.javascript.Function;
20  import org.htmlunit.corejs.javascript.JavaScriptException;
21  import org.htmlunit.corejs.javascript.Scriptable;
22  import org.htmlunit.corejs.javascript.VarScope;
23  import org.htmlunit.html.DomElement;
24  import org.htmlunit.html.DomNode;
25  import org.htmlunit.html.HtmlPage;
26  import org.htmlunit.html.csp.ContentSecurityPolicy;
27  import org.htmlunit.javascript.JavaScriptEngine;
28  
29  /**
30   * Wraps event handler code as a {@link Function} object.
31   *
32   * @author Marc Guillemot
33   * @author Ronald Brill
34   */
35  public class EventHandler extends BaseFunction {
36      private final DomNode node_;
37      private final String eventName_;
38      private final String jsSnippet_;
39      private Function realFunction_;
40      private boolean blockedByContentSecurityPolicy_;
41  
42      /**
43       * Builds a function that will execute the given JavaScript code.
44       *
45       * @param scope the scope
46       * @param node the element for which the event is built
47       * @param eventName the event name for which this handler is created
48       * @param jsSnippet the JavaScript code snippet to execute
49       */
50      public EventHandler(final VarScope scope, final DomNode node, final String eventName, final String jsSnippet) {
51          super(scope);
52          node_ = node;
53          eventName_ = eventName;
54          jsSnippet_ = jsSnippet;
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public Object call(final Context cx, final Object nt, final VarScope scope,
62          final Object thisObj, final Object[] args)
63          throws JavaScriptException {
64  
65          // the js object to which this event is attached has to be the scope
66          // final HtmlUnitScriptable jsObj = node_.getScriptableObject();
67          // have changed this - the scope is now thisObj to fix
68          // https://github.com/HtmlUnit/htmlunit/issues/347
69          // but still have not found any description about the right scope
70  
71          // compile "just in time"
72          if (realFunction_ == null) {
73              if (blockedByContentSecurityPolicy_) {
74                  return JavaScriptEngine.UNDEFINED;
75              }
76              if (node_ instanceof DomElement element
77                      && element.getPage() instanceof HtmlPage page
78                      && !ContentSecurityPolicy.allowsScriptAsAttribute(page, element, jsSnippet_)) {
79                  blockedByContentSecurityPolicy_ = true;
80                  return JavaScriptEngine.UNDEFINED;
81              }
82  
83              final String js = "function on" + eventName_ + "(event) { " + jsSnippet_ + " \n}";
84              realFunction_ = cx.compileFunction(scope, js, eventName_ + " event for " + node_
85                                      + " in " + node_.getPage().getUrl(), 0, null);
86              realFunction_.setParentScope(scope);
87          }
88  
89          return realFunction_.call(cx, scope, thisObj, args);
90      }
91  
92      /**
93       * Returns the JavaScript source representation of this function declaration.
94       *
95       * @param typeHint the type hint
96       * @return the JavaScript source of the function declaration
97       * @see org.htmlunit.corejs.javascript.ScriptableObject#getDefaultValue(java.lang.Class)
98       */
99      @Override
100     public Object getDefaultValue(final Class<?> typeHint) {
101         return "function on" + eventName_ + "(event) { " + jsSnippet_ + " }";
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public Object get(final String name, final Scriptable start) {
109         // quick and dirty
110         if ("toString".equals(name)) {
111             return new BaseFunction(getParentScope()) {
112                 @Override
113                 public Object call(final Context cx, final Object nt, final VarScope scope,
114                         final Object thisObj, final Object[] args) {
115                     return "function on" + eventName_ + "(event) { " + jsSnippet_ + " }";
116                 }
117             };
118         }
119 
120         return super.get(name, start);
121     }
122 }