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.html.DomNode;
21 import org.htmlunit.javascript.JavaScriptEngine;
22 import org.htmlunit.javascript.configuration.JsxClass;
23 import org.htmlunit.javascript.configuration.JsxConstructor;
24 import org.htmlunit.javascript.configuration.JsxGetter;
25
26 /**
27 * JavaScript host object for {@code InputEvent}.
28 *
29 * @author Ahmed Ashour
30 * @author Ronald Brill
31 *
32 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/InputEvent">MDN Documentation</a>
33 */
34 @JsxClass
35 public class InputEvent extends UIEvent {
36
37 private String data_;
38 private String inputType_;
39 private boolean isComposing_;
40
41 /**
42 * Creates a new event instance.
43 */
44 public InputEvent() {
45 super();
46 data_ = "";
47 inputType_ = "";
48 }
49
50 /**
51 * Creates a new input event for the given DOM node; used when typing changes the text
52 * of a text control. Like in the browsers, the event bubbles but can't be canceled.
53 *
54 * @param domNode the DOM node that triggered the event
55 * @param type the event type
56 * @param inputType the input type (e.g. {@code insertText} or {@code deleteContentBackward})
57 * @param data the inserted text or {@code null} if no text was inserted
58 */
59 public InputEvent(final DomNode domNode, final String type, final String inputType, final String data) {
60 super(domNode, type);
61 inputType_ = inputType;
62 data_ = data;
63 setCancelable(false);
64 }
65
66 /**
67 * Creates an instance of this event.
68 *
69 * @param type the event type
70 * @param details the event details (optional)
71 */
72 @Override
73 @JsxConstructor
74 public void jsConstructor(final String type, final ScriptableObject details) {
75 super.jsConstructor(type, details);
76
77 if (details != null && !JavaScriptEngine.isUndefined(details)) {
78 if (getBrowserVersion().hasFeature(JS_EVENT_INPUT_CTOR_INPUTTYPE)) {
79 final Object inputType = details.get("inputType", details);
80 if (!isMissingOrUndefined(inputType)) {
81 inputType_ = JavaScriptEngine.toString(inputType);
82 }
83 }
84
85 final Object dataObj = details.get("data", details);
86 if (!isMissingOrUndefined(dataObj)) {
87 data_ = JavaScriptEngine.toString(dataObj);
88 }
89
90 final Object isComposing = details.get("isComposing", details);
91 if (!isMissingOrUndefined(isComposing)) {
92 setIsComposing(JavaScriptEngine.toBoolean(isComposing));
93 }
94 }
95 }
96
97 /**
98 * Returns whether this event is fired after the {@code compositionstart} and before the {@code compositionend} events.
99 *
100 * @return {@code true} if the event is fired while composing
101 */
102 @JsxGetter
103 public boolean isIsComposing() {
104 return isComposing_;
105 }
106
107 /**
108 * Sets whether this event is fired after the {@code compositionstart} and before the {@code compositionend} events.
109 *
110 * @param isComposing {@code true} if the event is fired while composing
111 */
112 protected void setIsComposing(final boolean isComposing) {
113 isComposing_ = isComposing;
114 }
115
116 /**
117 * Returns the data contained in the event.
118 *
119 * @return the data
120 */
121 @JsxGetter
122 public Object getData() {
123 return data_;
124 }
125
126 /**
127 * Returns the input type of the event.
128 *
129 * @return the input type
130 */
131 @JsxGetter
132 public Object getInputType() {
133 return inputType_;
134 }
135 }