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 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 * JavaScript host object for {@code InputEvent}.
27 *
28 * @author Ahmed Ashour
29 * @author Ronald Brill
30 *
31 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/InputEvent">MDN Documentation</a>
32 */
33 @JsxClass
34 public class InputEvent extends UIEvent {
35
36 private String data_;
37 private String inputType_;
38 private boolean isComposing_;
39
40 /**
41 * Creates a new event instance.
42 */
43 public InputEvent() {
44 super();
45 data_ = "";
46 inputType_ = "";
47 }
48
49 /**
50 * Creates an instance of this event.
51 *
52 * @param type the event type
53 * @param details the event details (optional)
54 */
55 @Override
56 @JsxConstructor
57 public void jsConstructor(final String type, final ScriptableObject details) {
58 super.jsConstructor(type, details);
59
60 if (details != null && !JavaScriptEngine.isUndefined(details)) {
61 if (getBrowserVersion().hasFeature(JS_EVENT_INPUT_CTOR_INPUTTYPE)) {
62 final Object inputType = details.get("inputType", details);
63 if (!isMissingOrUndefined(inputType)) {
64 inputType_ = JavaScriptEngine.toString(inputType);
65 }
66 }
67
68 final Object dataObj = details.get("data", details);
69 if (!isMissingOrUndefined(dataObj)) {
70 data_ = JavaScriptEngine.toString(dataObj);
71 }
72
73 final Object isComposing = details.get("isComposing", details);
74 if (!isMissingOrUndefined(isComposing)) {
75 setIsComposing(JavaScriptEngine.toBoolean(isComposing));
76 }
77 }
78 }
79
80 /**
81 * Returns whether this event is fired after the {@code compositionstart} and before the {@code compositionend} events.
82 *
83 * @return {@code true} if the event is fired while composing
84 */
85 @JsxGetter
86 public boolean isIsComposing() {
87 return isComposing_;
88 }
89
90 /**
91 * Sets whether this event is fired after the {@code compositionstart} and before the {@code compositionend} events.
92 *
93 * @param isComposing {@code true} if the event is fired while composing
94 */
95 protected void setIsComposing(final boolean isComposing) {
96 isComposing_ = isComposing;
97 }
98
99 /**
100 * Returns the data contained in the event.
101 *
102 * @return the data
103 */
104 @JsxGetter
105 public Object getData() {
106 return data_;
107 }
108
109 /**
110 * Returns the input type of the event.
111 *
112 * @return the input type
113 */
114 @JsxGetter
115 public Object getInputType() {
116 return inputType_;
117 }
118 }