View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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 static org.htmlunit.BrowserVersionFeatures.JS_EVENT_INPUT_CTOR_INPUTTYPE;
18  
19  import org.htmlunit.corejs.javascript.ScriptableObject;
20  import org.htmlunit.javascript.JavaScriptEngine;
21  import org.htmlunit.javascript.configuration.JsxClass;
22  import org.htmlunit.javascript.configuration.JsxConstructor;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  
25  /**
26   * A JavaScript object for {@code InputEvent}.
27   *
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   */
31  @JsxClass
32  public class InputEvent extends UIEvent {
33  
34      private String data_;
35      private String inputType_;
36      private boolean isComposing_;
37  
38      /**
39       * Default constructor.
40       */
41      public InputEvent() {
42          super();
43          data_ = "";
44          inputType_ = "";
45      }
46  
47      /**
48       * JavaScript constructor.
49       *
50       * @param type the event type
51       * @param details the event details (optional)
52       */
53      @Override
54      @JsxConstructor
55      public void jsConstructor(final String type, final ScriptableObject details) {
56          super.jsConstructor(type, details);
57  
58          if (details != null && !JavaScriptEngine.isUndefined(details)) {
59              if (getBrowserVersion().hasFeature(JS_EVENT_INPUT_CTOR_INPUTTYPE)) {
60                  final Object inputType = details.get("inputType", details);
61                  if (!isMissingOrUndefined(inputType)) {
62                      inputType_ = JavaScriptEngine.toString(inputType);
63                  }
64              }
65  
66              final Object dataObj = details.get("data", details);
67              if (!isMissingOrUndefined(dataObj)) {
68                  data_ = JavaScriptEngine.toString(dataObj);
69              }
70  
71              final Object isComposing = details.get("isComposing", details);
72              if (!isMissingOrUndefined(isComposing)) {
73                  setIsComposing(JavaScriptEngine.toBoolean(isComposing));
74              }
75          }
76      }
77  
78      /**
79       * Returns whether or not the event is fired after the compositionstart and before the compositionend events.
80       * @return whether or not the event is fired while composing
81       */
82      @JsxGetter
83      public boolean getIsComposing() {
84          return isComposing_;
85      }
86  
87      /**
88       * Sets whether or not this event is fired after the compositionstart and before the compositionend events.
89       * @param isComposing whether or not this event is fired while composing
90       */
91      protected void setIsComposing(final boolean isComposing) {
92          isComposing_ = isComposing;
93      }
94  
95      /**
96       * Retrieves the data contained.
97       * @return the data contained
98       */
99      @JsxGetter
100     public Object getData() {
101         return data_;
102     }
103 
104     /**
105      * Retrieves the inputType.
106      * @return the inputType
107      */
108     @JsxGetter
109     public Object getInputType() {
110         return inputType_;
111     }
112 }