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;
16  
17  import static org.htmlunit.BrowserVersionFeatures.EVENT_SCROLL_UIEVENT;
18  import static org.htmlunit.BrowserVersionFeatures.JS_WINDOW_OPEN_LOCAL_FILE_THROWS;
19  import static org.htmlunit.BrowserVersionFeatures.JS_WINDOW_OPEN_SANDBOXED_THROWS;
20  import static org.htmlunit.BrowserVersionFeatures.JS_WINDOW_SELECTION_NULL_IF_INVISIBLE;
21  import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
22  import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
23  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
24  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
25  
26  import java.io.IOException;
27  import java.io.ObjectInputStream;
28  import java.io.Serializable;
29  import java.lang.reflect.Method;
30  import java.net.MalformedURLException;
31  import java.net.URL;
32  import java.util.ArrayList;
33  import java.util.EnumMap;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.function.Predicate;
38  import java.util.function.Supplier;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.htmlunit.AlertHandler;
43  import org.htmlunit.BrowserVersion;
44  import org.htmlunit.ConfirmHandler;
45  import org.htmlunit.ElementNotFoundException;
46  import org.htmlunit.Page;
47  import org.htmlunit.PrintHandler;
48  import org.htmlunit.PromptHandler;
49  import org.htmlunit.ScriptException;
50  import org.htmlunit.ScriptResult;
51  import org.htmlunit.SgmlPage;
52  import org.htmlunit.StatusHandler;
53  import org.htmlunit.StorageHolder.Type;
54  import org.htmlunit.TopLevelWindow;
55  import org.htmlunit.WebAssert;
56  import org.htmlunit.WebClient;
57  import org.htmlunit.WebConsole;
58  import org.htmlunit.WebWindow;
59  import org.htmlunit.WebWindowNotFoundException;
60  import org.htmlunit.corejs.javascript.AccessorSlot;
61  import org.htmlunit.corejs.javascript.Context;
62  import org.htmlunit.corejs.javascript.EcmaError;
63  import org.htmlunit.corejs.javascript.Function;
64  import org.htmlunit.corejs.javascript.JavaScriptException;
65  import org.htmlunit.corejs.javascript.NativeConsole.Level;
66  import org.htmlunit.corejs.javascript.NativeObject;
67  import org.htmlunit.corejs.javascript.NativePromise;
68  import org.htmlunit.corejs.javascript.Scriptable;
69  import org.htmlunit.corejs.javascript.ScriptableObject;
70  import org.htmlunit.corejs.javascript.Slot;
71  import org.htmlunit.corejs.javascript.TopLevel;
72  import org.htmlunit.corejs.javascript.VarScope;
73  import org.htmlunit.css.ComputedCssStyleDeclaration;
74  import org.htmlunit.html.BaseFrameElement;
75  import org.htmlunit.html.DomElement;
76  import org.htmlunit.html.DomNode;
77  import org.htmlunit.html.FrameWindow;
78  import org.htmlunit.html.HtmlAnchor;
79  import org.htmlunit.html.HtmlAttributeChangeEvent;
80  import org.htmlunit.html.HtmlButton;
81  import org.htmlunit.html.HtmlElement;
82  import org.htmlunit.html.HtmlEmbed;
83  import org.htmlunit.html.HtmlForm;
84  import org.htmlunit.html.HtmlImage;
85  import org.htmlunit.html.HtmlInput;
86  import org.htmlunit.html.HtmlMap;
87  import org.htmlunit.html.HtmlObject;
88  import org.htmlunit.html.HtmlPage;
89  import org.htmlunit.html.HtmlSelect;
90  import org.htmlunit.html.HtmlTextArea;
91  import org.htmlunit.javascript.AbstractJavaScriptEngine;
92  import org.htmlunit.javascript.HtmlUnitContextFactory;
93  import org.htmlunit.javascript.HtmlUnitScriptable;
94  import org.htmlunit.javascript.JavaScriptEngine;
95  import org.htmlunit.javascript.PostponedAction;
96  import org.htmlunit.javascript.configuration.JsxClass;
97  import org.htmlunit.javascript.configuration.JsxConstant;
98  import org.htmlunit.javascript.configuration.JsxConstructor;
99  import org.htmlunit.javascript.configuration.JsxFunction;
100 import org.htmlunit.javascript.configuration.JsxGetter;
101 import org.htmlunit.javascript.configuration.JsxSetter;
102 import org.htmlunit.javascript.host.crypto.Crypto;
103 import org.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration;
104 import org.htmlunit.javascript.host.css.MediaQueryList;
105 import org.htmlunit.javascript.host.css.StyleMedia;
106 import org.htmlunit.javascript.host.dom.AbstractList.EffectOnCache;
107 import org.htmlunit.javascript.host.dom.DOMException;
108 import org.htmlunit.javascript.host.dom.Document;
109 import org.htmlunit.javascript.host.dom.Node;
110 import org.htmlunit.javascript.host.dom.Selection;
111 import org.htmlunit.javascript.host.event.Event;
112 import org.htmlunit.javascript.host.event.EventTarget;
113 import org.htmlunit.javascript.host.event.MessageEvent;
114 import org.htmlunit.javascript.host.event.MouseEvent;
115 import org.htmlunit.javascript.host.event.UIEvent;
116 import org.htmlunit.javascript.host.fetch.Fetch;
117 import org.htmlunit.javascript.host.html.DocumentProxy;
118 import org.htmlunit.javascript.host.html.HTMLCollection;
119 import org.htmlunit.javascript.host.html.HTMLDocument;
120 import org.htmlunit.javascript.host.html.HTMLElement;
121 import org.htmlunit.javascript.host.performance.Performance;
122 import org.htmlunit.javascript.host.speech.SpeechSynthesis;
123 import org.htmlunit.javascript.host.xml.XMLDocument;
124 import org.htmlunit.util.StringUtils;
125 import org.htmlunit.util.UrlUtils;
126 import org.htmlunit.xml.XmlPage;
127 
128 /**
129  * JavaScript host object for {@code Window}.
130  *
131  * @author Mike Bowler
132  * @author Chen Jun
133  * @author David K. Taylor
134  * @author Christian Sell
135  * @author Darrell DeBoer
136  * @author Marc Guillemot
137  * @author Dierk Koenig
138  * @author Daniel Gredler
139  * @author David D. Kilzer
140  * @author Chris Erskine
141  * @author Ahmed Ashour
142  * @author Ronald Brill
143  * @author Frank Danek
144  * @author Carsten Steul
145  * @author Colin Alworth
146  * @author Atsushi Nakagawa
147  * @author Sven Strickroth
148  * @author Lai Quang Duong
149  *
150  * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window">MDN Documentation</a>
151  */
152 @JsxClass
153 @SuppressWarnings("PMD.TooManyFields")
154 public class Window extends EventTarget implements WindowOrWorkerGlobalScope, AutoCloseable {
155 
156     private static final Log LOG = LogFactory.getLog(Window.class);
157 
158     /** To be documented. */
159     @JsxConstant({CHROME, EDGE})
160     public static final int TEMPORARY = 0;
161 
162     /** To be documented. */
163     @JsxConstant({CHROME, EDGE})
164     public static final int PERSISTENT = 1;
165 
166     private static final Method GETTER_LENGTH;
167     private static final Method SETTER_LENGTH;
168     private static final Method GETTER_SELF;
169     private static final Method SETTER_SELF;
170     private static final Method GETTER_PARENT;
171     private static final Method SETTER_PARENT;
172     private static final Method GETTER_FRAMES;
173     private static final Method SETTER_FRAMES;
174 
175     static {
176         try {
177             GETTER_LENGTH = Window.class.getDeclaredMethod("jsGetLength");
178             SETTER_LENGTH = Window.class.getDeclaredMethod("jsSetLength", Scriptable.class);
179 
180             GETTER_SELF = Window.class.getDeclaredMethod("jsGetSelf");
181             SETTER_SELF = Window.class.getDeclaredMethod("jsSetSelf", Scriptable.class);
182 
183             GETTER_PARENT = Window.class.getDeclaredMethod("jsGetParent");
184             SETTER_PARENT = Window.class.getDeclaredMethod("jsSetParent", Scriptable.class);
185 
186             GETTER_FRAMES = Window.class.getDeclaredMethod("jsGetFrames");
187             SETTER_FRAMES = Window.class.getDeclaredMethod("jsSetFrames", Scriptable.class);
188         }
189         catch (NoSuchMethodException | SecurityException e) {
190             throw new RuntimeException(e);
191         }
192     }
193 
194     private Scriptable lengthShadow_;
195     private Scriptable selfShadow_;
196     private Scriptable parentShadow_;
197     private Scriptable framesShadow_;
198 
199     private Document document_;
200     private DocumentProxy documentProxy_;
201     private Navigator navigator_;
202     private Object clientInformation_;
203     // the value set by script (origin is replaceable)
204     private Object originShadow_;
205     private WebWindow webWindow_;
206     private WindowProxy windowProxy_;
207     private Screen screen_;
208     private History history_;
209     private Location location_;
210     private Selection selection_;
211     private Event currentEvent_;
212     private String status_ = "";
213     private Map<Class<? extends Scriptable>, Scriptable> prototypes_ = new HashMap<>();
214     private Object controllers_;
215     private Object opener_;
216     private Crypto crypto_;
217     private Scriptable performance_;
218 
219     private final EnumMap<Type, Storage> storages_ = new EnumMap<>(Type.class);
220 
221     private transient List<AnimationFrame> animationFrames_ = new ArrayList<>();
222 
223     private record AnimationFrame(long id_, Function callback_) {
224     }
225 
226     /**
227      * Creates an instance.
228      *
229      * @param cx the current context
230      * @param scope the scope
231      * @param args the arguments
232      * @param ctorObj the function object
233      * @param inNewExpr Is new or not
234      * @return the java object to allow JavaScript to access
235      */
236     @JsxConstructor
237     public static Scriptable jsConstructor(final Context cx, final VarScope scope,
238             final Object[] args, final Function ctorObj, final boolean inNewExpr) {
239         throw JavaScriptEngine.typeError("Illegal constructor");
240     }
241 
242     /**
243      * Restores the transient fields during deserialization.
244      * @param stream the stream to read the object from
245      * @throws IOException if an IO error occurs
246      * @throws ClassNotFoundException if a class is not found
247      */
248     private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
249         stream.defaultReadObject();
250         animationFrames_ = new ArrayList<>();
251     }
252 
253     /**
254      * Returns the prototype object corresponding to the specified HtmlUnit class inside the window scope.
255      * @param jsClass the class whose prototype is to be returned
256      * @return the prototype object corresponding to the specified class inside the specified scope
257      */
258     @Override
259     public Scriptable getPrototype(final Class<? extends HtmlUnitScriptable> jsClass) {
260         return prototypes_.get(jsClass);
261     }
262 
263     /**
264      * Sets the prototypes for HtmlUnit host classes.
265      * @param map a Map of ({@link Class}, {@link Scriptable})
266      */
267     public void setPrototypes(final Map<Class<? extends Scriptable>, Scriptable> map) {
268         prototypes_ = map;
269     }
270 
271     /**
272      * The JavaScript function {@code alert()}.
273      * @param message the message
274      */
275     @JsxFunction
276     public void alert(final Object message) {
277         // use Object as parameter and perform String conversion by ourself
278         // this allows to place breakpoint here and "see" the message object and its properties
279         final String stringMessage = JavaScriptEngine.toString(message);
280         final AlertHandler handler = getWebWindow().getWebClient().getAlertHandler();
281         if (handler == null) {
282             if (LOG.isWarnEnabled()) {
283                 LOG.warn("window.alert(\"" + stringMessage + "\") no alert handler installed");
284             }
285         }
286         else {
287             handler.handleAlert(document_.getPage(), stringMessage);
288         }
289     }
290 
291     /**
292      * Creates a base-64 encoded ASCII string from a string of binary data.
293      * @param stringToEncode string to encode
294      * @return the encoded string
295      */
296     @JsxFunction
297     @Override
298     public String btoa(final String stringToEncode) {
299         return WindowOrWorkerGlobalScopeMixin.btoa(stringToEncode, this);
300     }
301 
302     /**
303      * Starts the process of fetching a resource from the network.
304      *
305      * @param input the resource to fetch - a url or a {@code Request}
306      * @param init the options (optional)
307      * @return a promise for the {@code Response}
308      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch">MDN Documentation</a>
309      */
310     @JsxFunction
311     public NativePromise fetch(final Object input, final Object init) {
312         return Fetch.fetch(this, input, init);
313     }
314 
315     /**
316      * Decodes a string of data which has been encoded using base-64 encoding.
317      * @param encodedData the encoded string
318      * @return the decoded value
319      */
320     @JsxFunction
321     @Override
322     public String atob(final String encodedData) {
323         return WindowOrWorkerGlobalScopeMixin.atob(encodedData, this);
324     }
325 
326     /**
327      * The JavaScript function {@code confirm}.
328      * @param message the message
329      * @return true if ok was pressed, false if cancel was pressed
330      */
331     @JsxFunction
332     public boolean confirm(final String message) {
333         final ConfirmHandler handler = getWebWindow().getWebClient().getConfirmHandler();
334         if (handler == null) {
335             if (LOG.isWarnEnabled()) {
336                 LOG.warn("window.confirm(\""
337                         + message + "\") no confirm handler installed, simulating the OK button");
338             }
339             return true;
340         }
341         return handler.handleConfirm(document_.getPage(), message);
342     }
343 
344     /**
345      * The JavaScript function {@code prompt}.
346      * @param message the message
347      * @param defaultValue the default value displayed in the text input field
348      * @return the value typed in or {@code null} if the user pressed {@code cancel}
349      */
350     @JsxFunction
351     public String prompt(final String message, final Object defaultValue) {
352         final PromptHandler handler = getWebWindow().getWebClient().getPromptHandler();
353         if (handler == null) {
354             if (LOG.isWarnEnabled()) {
355                 LOG.warn("window.prompt(\"" + message + "\") no prompt handler installed");
356             }
357             return null;
358         }
359         final String defaultText = JavaScriptEngine.isUndefined(defaultValue)
360                                         ? null
361                                         : JavaScriptEngine.toString(defaultValue);
362         return handler.handlePrompt(document_.getPage(), message, defaultText);
363     }
364 
365     /**
366      * Returns the JavaScript property {@code document}.
367      * @return the document
368      */
369     @JsxGetter(propertyName = "document")
370     public DocumentProxy getDocument_js() {
371         return documentProxy_;
372     }
373 
374     /**
375      * Returns the window's current document.
376      * @return the window's current document
377      */
378     public Document getDocument() {
379         return document_;
380     }
381 
382     /**
383      * Returns the current event.
384      * @return the current event, or {@code null} if no event is currently available
385      */
386     @JsxGetter
387     public Object getEvent() {
388         if (currentEvent_ == null) {
389             return JavaScriptEngine.UNDEFINED;
390         }
391         return currentEvent_;
392     }
393 
394     /**
395      * Returns the current event (used internally regardless of the emulation mode).
396      * @return the current event, or {@code null} if no event is currently available
397      */
398     public Event getCurrentEvent() {
399         return currentEvent_;
400     }
401 
402     /**
403      * Sets the current event.
404      * @param event the current event
405      */
406     public void setCurrentEvent(final Event event) {
407         currentEvent_ = event;
408     }
409 
410     /**
411      * Opens a new window.
412      *
413      * @param url when a new document is opened, <i>url</i> is a String that specifies a MIME type for the document.
414      *        When a new window is opened, <i>url</i> is a String that specifies the URL to render in the new window
415      * @param name the name
416      * @param features the features
417      * @param replace whether to replace in the history list or no
418      * @return the newly opened window, or {@code null} if popup windows have been disabled
419      * @see org.htmlunit.WebClientOptions#isPopupBlockerEnabled()
420      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/open">MDN documentation</a>
421      */
422     @JsxFunction
423     public WindowProxy open(final Object url, final Object name, final Object features,
424             final Object replace) {
425         String urlString = null;
426         if (!JavaScriptEngine.isUndefined(url)) {
427             urlString = JavaScriptEngine.toString(url);
428         }
429         String windowName = "";
430         if (!JavaScriptEngine.isUndefined(name)) {
431             windowName = JavaScriptEngine.toString(name);
432         }
433         String featuresString = null;
434         if (!JavaScriptEngine.isUndefined(features)) {
435             featuresString = JavaScriptEngine.toString(features);
436         }
437         // a sandboxed document without 'allow-popups' can't open windows
438         if (getWebWindow().getEnclosedPage() instanceof HtmlPage openerPage
439                 && !openerPage.isSandboxAllowed("allow-popups")) {
440             if (getBrowserVersion().hasFeature(JS_WINDOW_OPEN_SANDBOXED_THROWS)) {
441                 throw JavaScriptEngine.asJavaScriptException(this,
442                         "The document is sandboxed and lacks the 'allow-popups' flag.",
443                         DOMException.INVALID_ACCESS_ERR);
444             }
445             return null;
446         }
447 
448         final WebClient webClient = getWebWindow().getWebClient();
449 
450         if (webClient.getOptions().isPopupBlockerEnabled()) {
451             LOG.debug("Ignoring window.open() invocation because popups are blocked.");
452             return null;
453         }
454 
455         boolean replaceCurrentEntryInBrowsingHistory = false;
456         if (!JavaScriptEngine.isUndefined(replace)) {
457             replaceCurrentEntryInBrowsingHistory = JavaScriptEngine.toBoolean(replace);
458         }
459         if ((featuresString != null || replaceCurrentEntryInBrowsingHistory) && LOG.isDebugEnabled()) {
460             LOG.debug(
461                    "window.open: features and replaceCurrentEntryInBrowsingHistory "
462                     + "not implemented: url=[" + urlString
463                     + "] windowName=[" + windowName
464                     + "] features=[" + featuresString
465                     + "] replaceCurrentEntry=[" + replaceCurrentEntryInBrowsingHistory
466                     + "]");
467         }
468 
469         // if specified name is the name of an existing window, then hold it
470         if (StringUtils.isEmptyOrNull(urlString) && !StringUtils.isEmptyString(windowName)) {
471             try {
472                 final WebWindow webWindow = webClient.getWebWindowByName(windowName);
473                 return getProxy(webWindow);
474             }
475             catch (final WebWindowNotFoundException ignored) {
476                 // nothing
477             }
478         }
479         final URL newUrl = makeUrlForOpenWindow(urlString);
480 
481         // accessing to local resource is forbidden for security reason;
482         // like in the browsers no window is opened and an existing window with this name is not navigated
483         // (but returned); Firefox throws
484         final Page openerPage = getWebWindow().getEnclosedPage();
485         if (newUrl != null && openerPage != null && !isLocalResourceAccessAllowed(openerPage, newUrl)) {
486             webClient.getIncorrectnessListener().notify("Not allowed to load local resource: " + newUrl, this);
487             if (webClient.getBrowserVersion().hasFeature(JS_WINDOW_OPEN_LOCAL_FILE_THROWS)) {
488                 throw JavaScriptEngine.constructError("Error", "Access to '" + newUrl + "' from script denied");
489             }
490             if (StringUtils.isEmptyOrNull(windowName)) {
491                 return null;
492             }
493             final WebWindow existingWindow = webClient.findTargetWindow(webWindow_, windowName);
494             if (existingWindow == null) {
495                 return null;
496             }
497             return getProxy(existingWindow);
498         }
499 
500         final WebWindow newWebWindow = webClient.openWindow(newUrl, windowName, webWindow_);
501         return getProxy(newWebWindow);
502     }
503 
504     private static boolean isLocalResourceAccessAllowed(final Page page, final URL url) {
505         if (page instanceof HtmlPage htmlPage) {
506             return htmlPage.isLocalResourceAccessAllowed(url);
507         }
508         return page.getEnclosingWindow().getWebClient().getOptions().isLocalFileAccessFromRemotePagesAllowed()
509                 || UrlUtils.isLocalResourceAccessAllowed(page.getUrl(), url);
510     }
511 
512     private URL makeUrlForOpenWindow(final String urlString) {
513         if (urlString.isEmpty()) {
514             return UrlUtils.URL_ABOUT_BLANK;
515         }
516 
517         try {
518             final Page page = getWebWindow().getEnclosedPage();
519             if (page != null && page.isHtmlPage()) {
520                 return ((HtmlPage) page).getFullyQualifiedUrl(urlString);
521             }
522             return new URL(urlString);
523         }
524         catch (final MalformedURLException e) {
525             if (LOG.isWarnEnabled()) {
526                 LOG.error("Unable to create URL for openWindow: relativeUrl=[" + urlString + "]", e);
527             }
528             return null;
529         }
530     }
531 
532     /**
533      * Sets a chunk of JavaScript to be invoked at some specified time later.
534      * The invocation occurs only if the window is opened after the delay
535      * and does not contain an other page than the one that originated the setTimeout.
536      *
537      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">
538      *     MDN web docs</a>
539      *
540      * @param context the JavaScript context
541      * @param scope the scope
542      * @param thisObj the scriptable
543      * @param args the arguments passed into the method
544      * @param function the function
545      * @return the id of the created timer
546      */
547     @JsxFunction
548     public static Object setTimeout(final Context context, final VarScope scope,
549             final Scriptable thisObj, final Object[] args, final Function function) {
550         return WindowOrWorkerGlobalScopeMixin.setTimeout(context, thisObj, args, function);
551     }
552 
553     /**
554      * Queues a microtask to be executed.
555      *
556      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask">
557      *     MDN web docs</a>
558      * @param context the JavaScript context
559      * @param scope the scope
560      * @param thisObj the scriptable
561      * @param args the arguments passed into the method
562      * @param function the function
563      * @return undefined
564      */
565     @JsxFunction
566     public static Object queueMicrotask(final Context context, final VarScope scope,
567             final Scriptable thisObj, final Object[] args, final Function function) {
568         return WindowOrWorkerGlobalScopeMixin.queueMicrotask(thisObj, args);
569     }
570 
571     /**
572      * Sets a chunk of JavaScript to be invoked each time a specified number of milliseconds has elapsed.
573      *
574      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval">
575      *     MDN web docs</a>
576      * @param context the JavaScript context
577      * @param scope the scope
578      * @param thisObj the scriptable
579      * @param args the arguments passed into the method
580      * @param function the function
581      * @return the id of the created interval
582      */
583     @JsxFunction
584     public static Object setInterval(final Context context, final VarScope scope,
585             final Scriptable thisObj, final Object[] args, final Function function) {
586         return WindowOrWorkerGlobalScopeMixin.setInterval(context, thisObj, args, function);
587     }
588 
589     /**
590      * Cancels a one-time timeout that was previously established by a call to
591      * {@link #setTimeout(Context, VarScope, Scriptable, Object[], Function)}.
592      *
593      * <p>If {@code timeoutId} is {@code 0} or negative, this method does nothing,
594      * since valid job IDs are always positive integers. This matches browser behavior
595      * where {@code clearTimeout(0)} is a safe no-op, commonly used when {@code 0}
596      * is employed as a sentinel value to indicate "no active timeout".</p>
597      *
598      * <p>If the given ID does not correspond to any active timeout (e.g. it has
599      * already fired or been cancelled), this method returns silently without
600      * throwing an error, consistent with the HTML Living Standard.</p>
601      *
602      * @param timeoutId the ID of the timeout to cancel, as returned by
603      *        {@link #setTimeout(Context, VarScope, Scriptable, Object[], Function)};
604      *        values of {@code 0} or less are ignored
605      * @see #setTimeout(Context, VarScope, Scriptable, Object[], Function)
606      * @see #clearInterval(int)
607      * @see <a href="https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-cleartimeout">
608      *      HTML Living Standard – clearTimeout</a>
609      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout">
610      *      MDN Web Docs – clearTimeout</a>
611      */
612     @JsxFunction
613     public void clearTimeout(final int timeoutId) {
614         if (timeoutId > 0) {
615             if (LOG.isDebugEnabled()) {
616                 LOG.debug("clearTimeout(" + timeoutId + ")");
617             }
618             getWebWindow().getJobManager().removeJob(timeoutId);
619         }
620     }
621 
622     /**
623      * Cancels a repeating interval that was previously established by a call to
624      * {@link #setInterval(Context, VarScope, Scriptable, Object[], Function)}.
625      *
626      * <p>If {@code intervalID} is {@code 0} or negative, this method does nothing,
627      * since valid job IDs are always positive integers. This matches browser behavior
628      * where {@code clearInterval(0)} is a safe no-op, commonly used when {@code 0}
629      * is employed as a sentinel value to indicate "no active interval".</p>
630      *
631      * <p>If the given ID does not correspond to any active interval (e.g. it has
632      * already fired or been cancelled), this method returns silently without
633      * throwing an error, consistent with the HTML Living Standard.</p>
634      *
635      * @param intervalID the ID of the interval to cancel, as returned by
636      *        {@link #setInterval(Context, VarScope, Scriptable, Object[], Function)};
637      *        values of {@code 0} or less are ignored
638      * @see #setInterval(Context, VarScope, Scriptable, Object[], Function)
639      * @see #clearTimeout(int)
640      * @see <a href="https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-clearinterval">
641      *      HTML Living Standard – clearInterval</a>
642      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/clearInterval">
643      *      MDN Web Docs – clearInterval</a>
644      */
645     @JsxFunction
646     public void clearInterval(final int intervalID) {
647         if (intervalID > 0) {
648             if (LOG.isDebugEnabled()) {
649                 LOG.debug("clearInterval(" + intervalID + ")");
650             }
651             getWebWindow().getJobManager().removeJob(intervalID);
652         }
653     }
654 
655     /**
656      * Returns the JavaScript property {@code navigator}.
657      * @return the navigator
658      */
659     @JsxGetter
660     public Navigator getNavigator() {
661         return navigator_;
662     }
663 
664     /**
665      * Returns the JavaScript property {@code clientInformation}.
666      * @return the client information
667      */
668     @JsxGetter
669     public Object getClientInformation() {
670         if (clientInformation_ != null) {
671             return clientInformation_;
672         }
673         return navigator_;
674     }
675 
676     /**
677      * Returns the {@code origin} property: the serialized origin of the document of this window.
678      * The origin of an about:blank or about:srcdoc document is the origin of its creator; the origin
679      * of a sandboxed document without 'allow-same-origin' (opaque origin) is 'null'.
680      *
681      * @return the origin
682      * @see <a href="https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin-dev">HTML Living Standard
683      *     - window.origin</a>
684      */
685     @JsxGetter
686     public Object getOrigin() {
687         if (originShadow_ != null) {
688             return originShadow_;
689         }
690 
691         final Page page = getWebWindow().getEnclosedPage();
692         if (page == null) {
693             return "null";
694         }
695         final URL url;
696         if (page instanceof HtmlPage htmlPage) {
697             if (htmlPage.isOpaqueOrigin()) {
698                 return "null";
699             }
700             url = htmlPage.getOriginUrl();
701         }
702         else {
703             url = page.getUrl();
704         }
705         return serializeOrigin(url);
706     }
707 
708     /**
709      * Sets the {@code origin} property; like the browsers, this replaces the value
710      * (see https://webidl.spec.whatwg.org/#Replaceable).
711      *
712      * @param origin the new value
713      */
714     @JsxSetter
715     public void setOrigin(final Object origin) {
716         originShadow_ = origin;
717     }
718 
719     /**
720      * Serializes the origin of the given url; only http(s) urls have a tuple origin,
721      * all other urls (about:, data:, file:...) have an opaque origin ('null').
722      *
723      * @param url the url
724      * @return the serialized origin
725      */
726     private static String serializeOrigin(final URL url) {
727         final String protocol = url.getProtocol();
728         if (!"http".equals(protocol) && !"https".equals(protocol)) {
729             return "null";
730         }
731 
732         final StringBuilder origin = new StringBuilder().append(protocol).append("://").append(url.getHost());
733         final int port = url.getPort();
734         if (port != -1 && port != url.getDefaultPort()) {
735             origin.append(':').append(port);
736         }
737         return origin.toString();
738     }
739 
740     /**
741      * Sets the {@code clientInformation} property.
742      *
743      * @param clientInformation the new value
744      */
745     @JsxSetter
746     public void setClientInformation(final Object clientInformation) {
747         clientInformation_ = clientInformation;
748     }
749 
750     /**
751      * Returns the window property. This is a synonym for {@code self}.
752      * @return the window property (a reference to <code>this</code>)
753      */
754     @JsxGetter(propertyName = "window")
755     public Window getWindow_js() {
756         return this;
757     }
758 
759     /**
760      * Returns the {@code localStorage} property.
761      * @return the {@code localStorage} property
762      */
763     @JsxGetter
764     public Storage getLocalStorage() {
765         checkStorageAccess();
766         return getStorage(Type.LOCAL_STORAGE);
767     }
768 
769     /**
770      * Returns the {@code sessionStorage} property.
771      * @return the {@code sessionStorage} property
772      */
773     @JsxGetter
774     public Storage getSessionStorage() {
775         checkStorageAccess();
776         return getStorage(Type.SESSION_STORAGE);
777     }
778 
779     /**
780      * Like the browsers, a document with an opaque origin (a sandboxed document without
781      * 'allow-same-origin') has no access to the storage.
782      */
783     private void checkStorageAccess() {
784         if (getWebWindow().getEnclosedPage() instanceof HtmlPage htmlPage && htmlPage.isOpaqueOrigin()) {
785             throw JavaScriptEngine.asJavaScriptException(this,
786                     "The document is sandboxed and lacks the 'allow-same-origin' flag.",
787                     DOMException.SECURITY_ERR);
788         }
789     }
790 
791     /**
792      * Gets the storage of the specified type.
793      * @param storageType the type
794      * @return the storage
795      */
796     public Storage getStorage(final Type storageType) {
797         return storages_.computeIfAbsent(storageType,
798             k -> {
799                 final WebWindow webWindow = getWebWindow();
800                 final Map<String, String> store = webWindow.getWebClient().getStorageHolder().
801                         getStore(storageType, webWindow.getEnclosedPage());
802                 return new Storage(this, store, storageType);
803             }
804         );
805     }
806 
807     /**
808      * Returns the {@code location} property.
809      * @return the {@code location} property
810      */
811     @JsxGetter
812     public Location getLocation() {
813         return location_;
814     }
815 
816     /**
817      * Sets the {@code location} property. This will cause a reload of the window.
818      * @param newLocation the URL of the new content
819      * @throws IOException when location loading fails
820      */
821     @JsxSetter
822     public void setLocation(final String newLocation) throws IOException {
823         location_.setHref(newLocation);
824     }
825 
826     /**
827      * Logs messages to the browser's standard output (stdout). If the browser was started
828      * from a terminal, output sent to dump() will appear in the terminal.
829      * Output from dump() is not sent to the browser's developer tools console.
830      * To log to the developer tools console, use console.log().
831      * <p>
832      * HtmlUnit always uses the WebConsole.
833      * </p>
834      * @param message the message to log
835      */
836     @JsxFunction({FF, FF_ESR})
837     public void dump(final String message) {
838         final WebConsole console = getWebWindow().getWebClient().getWebConsole();
839         console.print(Context.getCurrentContext(), getParentScope(), Level.INFO, new String[] {message}, null);
840     }
841 
842     /**
843      * Invokes all the animation callbacks registered for this window by
844      * calling {@link #requestAnimationFrame(Object)} once.
845      * @return the number of pending animation callbacks
846      */
847     public int animateAnimationsFrames() {
848         final List<AnimationFrame> animationFrames = new ArrayList<>(animationFrames_);
849         animationFrames_.clear();
850 
851         final double now = System.nanoTime() / 1_000_000d;
852         final Object[] args = {now};
853 
854         final WebWindow ww = getWindow().getWebWindow();
855         final JavaScriptEngine jsEngine = (JavaScriptEngine) ww.getWebClient().getJavaScriptEngine();
856 
857         for (final AnimationFrame animationFrame : animationFrames) {
858             jsEngine.callFunction((HtmlPage) ww.getEnclosedPage(),
859                         animationFrame.callback_, getParentScope(), this, args);
860         }
861         return animationFrames_.size();
862     }
863 
864     /**
865      * Add callback to the list of animationFrames.
866      * @param callback the function to call when it's time to update the animation
867      * @return an identification id
868      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame">MDN Doc</a>
869      */
870     @JsxFunction
871     public int requestAnimationFrame(final Object callback) {
872         if (callback instanceof Function function) {
873             final int id = animationFrames_.size();
874             final AnimationFrame animationFrame = new AnimationFrame(id, function);
875             animationFrames_.add(animationFrame);
876             return id;
877         }
878         return -1;
879     }
880 
881     /**
882      * Remove the callback from the list of animationFrames.
883      * @param requestId the ID value returned by the call to window.requestAnimationFrame()
884      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/cancelAnimationFrame">MDN Doc</a>
885      */
886     @JsxFunction
887     public void cancelAnimationFrame(final Object requestId) {
888         final int id = (int) JavaScriptEngine.toNumber(requestId);
889 
890         animationFrames_.removeIf(animationFrame -> animationFrame.id_ == id);
891     }
892 
893     /**
894      * Returns the {@code screen} property.
895      * @return the {@code screen} property
896      */
897     @JsxGetter
898     public Screen getScreen() {
899         return screen_;
900     }
901 
902     /**
903      * Returns the {@code history} property.
904      * @return the {@code history} property
905      */
906     @JsxGetter
907     public History getHistory() {
908         return history_;
909     }
910 
911     /**
912      * Returns the {@code external} property.
913      * @return the {@code external} property
914      */
915     @JsxGetter
916     public External getExternal() {
917         final External external = new External();
918         external.setParentScope(getParentScope());
919         external.setPrototype(getPrototype(external.getClass()));
920         return external;
921     }
922 
923     /**
924      * Initializes this window.
925      * @param scope the scope
926      * @param webWindow the web window corresponding to this window
927      * @param pageToEnclose the page that will become the enclosing page
928      */
929     public void initialize(final TopLevel scope, final WebWindow webWindow, final Page pageToEnclose) {
930         webWindow_ = webWindow;
931         webWindow_.setScriptableObject(this);
932 
933         defineProperty(scope, "length", null, GETTER_LENGTH, SETTER_LENGTH, ScriptableObject.READONLY);
934         defineProperty(scope, "self", null, GETTER_SELF, SETTER_SELF, ScriptableObject.READONLY);
935         defineProperty(scope, "parent", null, GETTER_PARENT, SETTER_PARENT, ScriptableObject.READONLY);
936         defineProperty(scope, "frames", null, GETTER_FRAMES, SETTER_FRAMES, ScriptableObject.READONLY);
937 
938         windowProxy_ = new WindowProxy(webWindow_);
939 
940         if (pageToEnclose instanceof XmlPage) {
941             document_ = new XMLDocument();
942         }
943         else {
944             document_ = new HTMLDocument();
945         }
946         document_.setParentScope(scope);
947         document_.setPrototype(getPrototype(document_.getClass()));
948         document_.setWindow(this);
949 
950         if (pageToEnclose instanceof SgmlPage page) {
951             document_.setDomNode(page);
952 
953             if (page.isHtmlPage()) {
954                 final HtmlPage htmlPage = (HtmlPage) page;
955 
956                 htmlPage.addAutoCloseable(this);
957             }
958         }
959 
960         documentProxy_ = new DocumentProxy(webWindow_);
961 
962         navigator_ = new Navigator();
963         navigator_.setParentScope(scope);
964         navigator_.setPrototype(getPrototype(navigator_.getClass()));
965 
966         screen_ = new Screen(getWebWindow().getScreen());
967         screen_.setParentScope(scope);
968         screen_.setPrototype(getPrototype(screen_.getClass()));
969 
970         history_ = new History();
971         history_.setParentScope(scope);
972         history_.setPrototype(getPrototype(history_.getClass()));
973 
974         location_ = new Location();
975         location_.setParentScope(scope);
976         location_.setPrototype(getPrototype(location_.getClass()));
977         location_.initialize(scope, this, pageToEnclose);
978 
979         // like a JS new Object()
980         controllers_ = JavaScriptEngine.newObject(scope);
981 
982         if (webWindow_ instanceof TopLevelWindow window) {
983             final WebWindow opener = window.getOpener();
984             if (opener != null) {
985                 opener_ = opener.getScriptableObject();
986             }
987         }
988     }
989 
990     /**
991      * Initialize the object.
992      * @param enclosedPage the page containing the JavaScript
993      */
994     public void initialize(final Page enclosedPage) {
995         if (enclosedPage != null && enclosedPage.isHtmlPage()) {
996             final HtmlPage htmlPage = (HtmlPage) enclosedPage;
997 
998             // Windows don't have corresponding DomNodes so set the domNode
999             // variable to be the page. If this isn't set then HtmlUnitScriptable.get()
1000             // won't work properly
1001             setDomNode(htmlPage);
1002             clearEventListenersContainer();
1003 
1004             WebAssert.notNull("document_", document_);
1005             document_.setDomNode(htmlPage);
1006         }
1007     }
1008 
1009     /**
1010      * Initializes the object. Only called for Windows with no contents.
1011      */
1012     public void initialize() {
1013         // Empty.
1014     }
1015 
1016     /**
1017      * Returns the value of the {@code top} property.
1018      * @return the value of {@code top}
1019      */
1020     @JsxGetter
1021     public Object getTop() {
1022         final WebWindow top = getWebWindow().getTopWindow();
1023         return top.getScriptableObject();
1024     }
1025 
1026     /**
1027      * Returns the value of the {@code opener} property.
1028      * @return the value of the {@code opener}, or {@code null} for a top level window
1029      */
1030     @JsxGetter
1031     public Object getOpener() {
1032         Object opener = opener_;
1033         if (opener instanceof Window window) {
1034             opener = window.windowProxy_;
1035         }
1036         return opener;
1037     }
1038 
1039     /**
1040      * Sets the {@code opener} property.
1041      * @param newValue the new value
1042      */
1043     @JsxSetter
1044     public void setOpener(final Object newValue) {
1045         opener_ = newValue;
1046     }
1047 
1048     /**
1049      * Returns the (i)frame in which the window is contained.
1050      * @return {@code null} for a top level window
1051      */
1052     @JsxGetter
1053     public HtmlUnitScriptable getFrameElement() {
1054         final WebWindow window = getWebWindow();
1055         if (window instanceof FrameWindow frameWindow) {
1056             return frameWindow.getFrameElement().getScriptableObject();
1057         }
1058         return null;
1059     }
1060 
1061     /**
1062      * Returns the number of frames contained by this window.
1063      * @return the number of frames contained by this window
1064      */
1065     @JsxGetter
1066     public Object getLength() {
1067         return JavaScriptEngine.UNDEFINED;
1068     }
1069 
1070     /**
1071      * Gets the {@code length} property. Setting this shadows the
1072      * defined value (see https://webidl.spec.whatwg.org/#Replaceable)
1073      * @return the shadow value if set otherwise the number of frames
1074      */
1075     public Object jsGetLength() {
1076         if (lengthShadow_ != null) {
1077             return lengthShadow_;
1078         }
1079 
1080         final HTMLCollection frames = getFrames();
1081         if (frames != null) {
1082             return frames.getLength();
1083         }
1084         return 0;
1085     }
1086 
1087     /**
1088      * Sets the {@code length} property. Setting this shadows the
1089      * defined value (see https://webidl.spec.whatwg.org/#Replaceable)
1090      * @param lengthShadow the value to overwrite the defined property value
1091      */
1092     public void jsSetLength(final Scriptable lengthShadow) {
1093         lengthShadow_ = lengthShadow;
1094     }
1095 
1096     /**
1097      * Returns the {@code self} property.
1098      * @return this
1099      */
1100     @JsxGetter
1101     public Object getSelf() {
1102         return JavaScriptEngine.UNDEFINED;
1103     }
1104 
1105     /**
1106      * Gets the {@code self} property. Setting this shadows the
1107      * defined value (see https://webidl.spec.whatwg.org/#Replaceable)
1108      * @return the shadow value if set otherwise the number of frames
1109      */
1110     public Object jsGetSelf() {
1111         if (selfShadow_ != null) {
1112             return selfShadow_;
1113         }
1114 
1115         return this;
1116     }
1117 
1118     /**
1119      * Sets the {@code self} property. Setting this shadows the
1120      * defined value (see https://webidl.spec.whatwg.org/#Replaceable)
1121      * @param selfShadow the value to overwrite the defined property value
1122      */
1123     public void jsSetSelf(final Scriptable selfShadow) {
1124         selfShadow_ = selfShadow;
1125     }
1126 
1127     /**
1128      * Returns the value of the {@code parent} property.
1129      * @return the value of the {@code parent} property
1130      */
1131     @JsxGetter
1132     public Object getParent() {
1133         return JavaScriptEngine.UNDEFINED;
1134     }
1135 
1136     /**
1137      * Gets the {@code parent} property. Setting this shadows the
1138      * defined value (see https://webidl.spec.whatwg.org/#Replaceable)
1139      * @return the shadow value if set otherwise the number of frames
1140      */
1141     public Object jsGetParent() {
1142         if (parentShadow_ != null) {
1143             return parentShadow_;
1144         }
1145 
1146         final WebWindow parent = getWebWindow().getParentWindow();
1147         return parent.getScriptableObject();
1148     }
1149 
1150     /**
1151      * Sets the {@code parent} property. Setting this shadows the
1152      * defined value (see https://webidl.spec.whatwg.org/#Replaceable)
1153      * @param parentShadow the value to overwrite the defined property value
1154      */
1155     public void jsSetParent(final Scriptable parentShadow) {
1156         parentShadow_ = parentShadow;
1157     }
1158 
1159     /**
1160      * Returns the value of the {@code frames} property.
1161      * @return the value of the {@code frames} property
1162      */
1163     @JsxGetter(propertyName = "frames")
1164     public Object getFrames_js() {
1165         return JavaScriptEngine.UNDEFINED;
1166     }
1167 
1168     /**
1169      * Gets the {@code frames} property. Setting this shadows the
1170      * defined value (see https://webidl.spec.whatwg.org/#Replaceable)
1171      * @return the shadow value if set otherwise the number of frames
1172      */
1173     public Object jsGetFrames() {
1174         if (framesShadow_ != null) {
1175             return framesShadow_;
1176         }
1177 
1178         return this;
1179     }
1180 
1181     /**
1182      * Sets the {@code frames} property. Setting this shadows the
1183      * defined value (see https://webidl.spec.whatwg.org/#Replaceable)
1184      * @param framesShadow the value to overwrite the defined property value
1185      */
1186     public void jsSetFrames(final Scriptable framesShadow) {
1187         framesShadow_ = framesShadow;
1188     }
1189 
1190     /**
1191      * Returns the live collection of frames contained by this window.
1192      * @return the live collection of frames contained by this window
1193      */
1194     private HTMLCollection getFrames() {
1195         final Page page = getWebWindow().getEnclosedPage();
1196         if (page instanceof HtmlPage htmlPage) {
1197             return new HTMLCollectionFrames(htmlPage);
1198         }
1199         return null;
1200     }
1201 
1202     /**
1203      * Returns the WebWindow associated with this Window.
1204      * @return the WebWindow
1205      */
1206     public WebWindow getWebWindow() {
1207         return webWindow_;
1208     }
1209 
1210     /**
1211      * Sets the focus to this element.
1212      */
1213     @JsxFunction
1214     public void focus() {
1215         final WebWindow window = getWebWindow();
1216         window.getWebClient().setCurrentWindow(window);
1217     }
1218 
1219     /**
1220      * Removes focus from this element.
1221      */
1222     @JsxFunction
1223     public void blur() {
1224         LOG.debug("window.blur() not implemented");
1225     }
1226 
1227     /**
1228      * Closes this window.
1229      */
1230     @JsxFunction(functionName = "close")
1231     public void close_js() {
1232         final WebWindow webWindow = getWebWindow();
1233         if (webWindow instanceof TopLevelWindow window) {
1234             window.close();
1235         }
1236         else {
1237             webWindow.getWebClient().deregisterWebWindow(webWindow);
1238         }
1239     }
1240 
1241     /**
1242      * Indicates if this window is closed.
1243      * @return {@code true} if this window is closed
1244      */
1245     @JsxGetter
1246     public boolean isClosed() {
1247         final WebWindow webWindow = getWebWindow();
1248         return !webWindow.getWebClient().containsWebWindow(webWindow);
1249     }
1250 
1251     /**
1252      * Does nothing.
1253      * @param x the horizontal position
1254      * @param y the vertical position
1255      */
1256     @JsxFunction
1257     public void moveTo(final int x, final int y) {
1258         LOG.debug("window.moveTo() not implemented");
1259     }
1260 
1261     /**
1262      * Does nothing.
1263      * @param x the horizontal position
1264      * @param y the vertical position
1265      */
1266     @JsxFunction
1267     public void moveBy(final int x, final int y) {
1268         LOG.debug("window.moveBy() not implemented");
1269     }
1270 
1271     /**
1272      * Does nothing.
1273      * @param width the width offset
1274      * @param height the height offset
1275      */
1276     @JsxFunction
1277     public void resizeBy(final int width, final int height) {
1278         LOG.debug("window.resizeBy() not implemented");
1279     }
1280 
1281     /**
1282      * Does nothing.
1283      * @param width the width of the Window in pixel after resize
1284      * @param height the height of the Window in pixel after resize
1285      */
1286     @JsxFunction
1287     public void resizeTo(final int width, final int height) {
1288         LOG.debug("window.resizeTo() not implemented");
1289     }
1290 
1291     /**
1292      * Scrolls to the specified location on the page.
1293      * @param x the horizontal position to scroll to
1294      * @param y the vertical position to scroll to
1295      */
1296     @JsxFunction
1297     public void scroll(final Scriptable x, final Scriptable y) {
1298         scrollTo(x, y);
1299     }
1300 
1301     /**
1302      * Scrolls the window content the specified distance.
1303      * @param x the horizontal distance to scroll by
1304      * @param y the vertical distance to scroll by
1305      */
1306     @JsxFunction
1307     public void scrollBy(final Scriptable x, final Scriptable y) {
1308         final HTMLElement body = document_.getBody();
1309         if (body != null) {
1310             int xOff = 0;
1311             int yOff = 0;
1312             if (y != null) {
1313                 xOff = JavaScriptEngine.toInt32(x);
1314                 yOff = JavaScriptEngine.toInt32(y);
1315             }
1316             else {
1317                 if (!(x instanceof NativeObject)) {
1318                     throw JavaScriptEngine.typeError("eee");
1319                 }
1320                 if (x.has("left", x)) {
1321                     xOff = JavaScriptEngine.toInt32(x.get("left", x));
1322                 }
1323                 if (x.has("top", x)) {
1324                     yOff = JavaScriptEngine.toInt32(x.get("top", x));
1325                 }
1326             }
1327 
1328             body.setScrollLeft(body.getScrollLeft() + xOff);
1329             body.setScrollTop(body.getScrollTop() + yOff);
1330 
1331             fireScrollEvent(body);
1332         }
1333 
1334         fireScrollEvent(document_);
1335     }
1336 
1337     private void fireScrollEvent(final Node node) {
1338         final Event event;
1339         if (getBrowserVersion().hasFeature(EVENT_SCROLL_UIEVENT)) {
1340             event = new UIEvent(node, Event.TYPE_SCROLL);
1341         }
1342         else {
1343             event = new Event(node, Event.TYPE_SCROLL);
1344             event.setCancelable(false);
1345         }
1346         node.fireEvent(event);
1347     }
1348 
1349     /**
1350      * Scrolls the window content down by the specified number of lines.
1351      * @param lines the number of lines to scroll down
1352      */
1353     @JsxFunction({FF, FF_ESR})
1354     public void scrollByLines(final int lines) {
1355         final HTMLElement body = document_.getBody();
1356         if (body != null) {
1357             body.setScrollTop(body.getScrollTop() + (19 * lines));
1358 
1359             fireScrollEvent(body);
1360         }
1361 
1362         fireScrollEvent(document_);
1363     }
1364 
1365     /**
1366      * Scrolls the window content down by the specified number of pages.
1367      * @param pages the number of pages to scroll down
1368      */
1369     @JsxFunction({FF, FF_ESR})
1370     public void scrollByPages(final int pages) {
1371         final HTMLElement body = document_.getBody();
1372         if (body != null) {
1373             body.setScrollTop(body.getScrollTop() + (getInnerHeight() * pages));
1374 
1375             fireScrollEvent(body);
1376         }
1377 
1378         fireScrollEvent(document_);
1379     }
1380 
1381     /**
1382      * Scrolls to the specified location on the page.
1383      * @param x the horizontal position to scroll to
1384      * @param y the vertical position to scroll to
1385      */
1386     @JsxFunction
1387     public void scrollTo(final Scriptable x, final Scriptable y) {
1388         final HTMLElement body = document_.getBody();
1389         if (body != null) {
1390             int xOff;
1391             int yOff;
1392             if (y != null) {
1393                 xOff = JavaScriptEngine.toInt32(x);
1394                 yOff = JavaScriptEngine.toInt32(y);
1395             }
1396             else {
1397                 if (!(x instanceof NativeObject)) {
1398                     throw JavaScriptEngine.typeError("eee");
1399                 }
1400 
1401                 xOff = body.getScrollLeft();
1402                 yOff = body.getScrollTop();
1403 
1404                 if (x.has("left", x)) {
1405                     xOff = JavaScriptEngine.toInt32(x.get("left", x));
1406                 }
1407                 if (x.has("top", x)) {
1408                     yOff = JavaScriptEngine.toInt32(x.get("top", x));
1409                 }
1410             }
1411             body.setScrollLeft(xOff);
1412             body.setScrollTop(yOff);
1413 
1414             fireScrollEvent(body);
1415         }
1416 
1417         fireScrollEvent(document_);
1418     }
1419 
1420     /**
1421      * Returns the {@code onload} property. Note that this is not necessarily a function if something else has been set.
1422      * @return the {@code onload} property
1423      */
1424     @JsxGetter
1425     public Function getOnload() {
1426         return getEventHandler(Event.TYPE_LOAD);
1427     }
1428 
1429     /**
1430      * Sets the value of the {@code onload} event handler.
1431      * @param onload the new handler
1432      */
1433     @JsxSetter
1434     public void setOnload(final Object onload) {
1435         setHandlerForJavaScript(Event.TYPE_LOAD, onload);
1436     }
1437 
1438     /**
1439      * Sets the value of the {@code onblur} event handler.
1440      * @param onblur the new handler
1441      */
1442     @JsxSetter
1443     public void setOnblur(final Object onblur) {
1444         setHandlerForJavaScript(Event.TYPE_BLUR, onblur);
1445     }
1446 
1447     /**
1448      * Returns the {@code onblur} property (not necessary a function if something else has been set).
1449      * @return the {@code onblur} property
1450      */
1451     @JsxGetter
1452     public Function getOnblur() {
1453         return getEventHandler(Event.TYPE_BLUR);
1454     }
1455 
1456     /**
1457      * Returns the {@code onclick} property (not necessary a function if something else has been set).
1458      * @return the {@code onclick} property
1459      */
1460     @JsxGetter
1461     public Function getOnclick() {
1462         return getEventHandler(MouseEvent.TYPE_CLICK);
1463     }
1464 
1465     /**
1466      * Sets the value of the {@code onclick} event handler.
1467      * @param onclick the new handler
1468      */
1469     @JsxSetter
1470     public void setOnclick(final Object onclick) {
1471         setHandlerForJavaScript(MouseEvent.TYPE_CLICK, onclick);
1472     }
1473 
1474     /**
1475      * Returns the {@code ondblclick} property (not necessary a function if something else has been set).
1476      * @return the {@code ondblclick} property
1477      */
1478     @JsxGetter
1479     public Function getOndblclick() {
1480         return getEventHandler(MouseEvent.TYPE_DBL_CLICK);
1481     }
1482 
1483     /**
1484      * Sets the value of the {@code ondblclick} event handler.
1485      * @param ondblclick the new handler
1486      */
1487     @JsxSetter
1488     public void setOndblclick(final Object ondblclick) {
1489         setHandlerForJavaScript(MouseEvent.TYPE_DBL_CLICK, ondblclick);
1490     }
1491 
1492     /**
1493      * Returns the {@code onhashchange} property (not necessary a function if something else has been set).
1494      * @return the {@code onhashchange} property
1495      */
1496     @JsxGetter
1497     public Function getOnhashchange() {
1498         return getEventHandler(Event.TYPE_HASH_CHANGE);
1499     }
1500 
1501     /**
1502      * Sets the value of the {@code onhashchange} event handler.
1503      * @param onhashchange the new handler
1504      */
1505     @JsxSetter
1506     public void setOnhashchange(final Object onhashchange) {
1507         setHandlerForJavaScript(Event.TYPE_HASH_CHANGE, onhashchange);
1508     }
1509 
1510     /**
1511      * Returns the value of the window's {@code name} property.
1512      * @return the value of the window's {@code name} property
1513      */
1514     @JsxGetter
1515     public String getName() {
1516         return getWebWindow().getName();
1517     }
1518 
1519      /**
1520       * Sets the value of the window's {@code name} property.
1521       * @param name the value of the window's {@code name} property
1522       */
1523     @JsxSetter
1524     public void setName(final String name) {
1525         getWebWindow().setName(name);
1526     }
1527 
1528     /**
1529      * Returns the value of the window's {@code onbeforeunload} property.
1530      * @return the value of the window's {@code onbeforeunload} property
1531      */
1532     @JsxGetter
1533     public Function getOnbeforeunload() {
1534         return getEventHandler(Event.TYPE_BEFORE_UNLOAD);
1535     }
1536 
1537     /**
1538      * Sets the value of the window's {@code onbeforeunload} property.
1539      * @param onbeforeunload the value of the window's {@code onbeforeunload} property
1540      */
1541     @JsxSetter
1542     public void setOnbeforeunload(final Object onbeforeunload) {
1543         setHandlerForJavaScript(Event.TYPE_BEFORE_UNLOAD, onbeforeunload);
1544     }
1545 
1546     /**
1547      * Returns the value of the window's {@code onerror} property.
1548      * @return the value of the window's {@code onerror} property
1549      */
1550     @JsxGetter
1551     public Function getOnerror() {
1552         return getEventHandler(Event.TYPE_ERROR);
1553     }
1554 
1555     /**
1556      * Sets the value of the window's {@code onerror} property.
1557      * @param onerror the value of the window's {@code onerror} property
1558      */
1559     @JsxSetter
1560     public void setOnerror(final Object onerror) {
1561         setHandlerForJavaScript(Event.TYPE_ERROR, onerror);
1562     }
1563 
1564     /**
1565      * Returns the value of the window's {@code onmessage} property.
1566      * @return the value of the window's {@code onmessage} property
1567      */
1568     @JsxGetter
1569     public Function getOnmessage() {
1570         return getEventHandler(Event.TYPE_MESSAGE);
1571     }
1572 
1573     /**
1574      * Sets the value of the window's {@code onmessage} property.
1575      * @param onmessage the value of the window's {@code onmessage} property
1576      */
1577     @JsxSetter
1578     public void setOnmessage(final Object onmessage) {
1579         setHandlerForJavaScript(Event.TYPE_MESSAGE, onmessage);
1580     }
1581 
1582     /**
1583      * Triggers the {@code onerror} handler, if one has been set.
1584      * @param e the error that needs to be reported
1585      */
1586     public void triggerOnError(final ScriptException e) {
1587         final Function f = getOnerror();
1588         if (f != null) {
1589             String msg = e.getMessage();
1590             final String url = e.getPage().getUrl().toExternalForm();
1591 
1592             final int line = e.getFailingLineNumber();
1593             final int column = e.getFailingColumnNumber();
1594 
1595             final TopLevel scope = getTopLevelScope(getParentScope());
1596 
1597             Object jsError = e.getMessage();
1598             if (e.getCause() instanceof JavaScriptException) {
1599                 msg = "uncaught exception: " + e.getCause().getMessage();
1600                 jsError = ((JavaScriptException) e.getCause()).getValue();
1601             }
1602             else if (e.getCause() instanceof EcmaError ecmaError) {
1603                 msg = "uncaught " + e.getCause().getMessage();
1604 
1605                 final Scriptable err = JavaScriptEngine.newObject(scope, "Error", JavaScriptEngine.EMPTY_ARGS);
1606                 ScriptableObject.putProperty(err, "message", ecmaError.getMessage());
1607                 ScriptableObject.putProperty(err, "fileName", ecmaError.sourceName());
1608                 ScriptableObject.putProperty(err, "lineNumber", Integer.valueOf(ecmaError.lineNumber()));
1609                 jsError = err;
1610             }
1611 
1612             final Object[] args = {msg, url, Integer.valueOf(line), Integer.valueOf(column), jsError};
1613             f.call(Context.getCurrentContext(), scope, this, args);
1614         }
1615     }
1616 
1617     private void setHandlerForJavaScript(final String eventName, final Object handler) {
1618         getEventListenersContainer().setEventHandler(eventName, handler);
1619     }
1620 
1621     /**
1622      * {@inheritDoc}
1623      */
1624     @Override
1625     protected Object getWithPreemption(final String name) {
1626         final DomNode domNode = getDomNodeOrNull();
1627         if (domNode == null) {
1628             return NOT_FOUND;
1629         }
1630 
1631         // May be attempting to retrieve a frame by name.
1632         final HtmlPage page = (HtmlPage) domNode.getPage();
1633         Object result = getFrameWindowByName(page, name);
1634 
1635         if (result == NOT_FOUND) {
1636             result = getElementsByName(page, name);
1637 
1638             if (result == NOT_FOUND) {
1639                 // May be attempting to retrieve element by ID (try map-backed operation again instead of XPath).
1640                 try {
1641                     final HtmlElement htmlElement = page.getHtmlElementById(name);
1642                     result = getScriptableFor(htmlElement);
1643                 }
1644                 catch (final ElementNotFoundException e) {
1645                     result = NOT_FOUND;
1646                 }
1647             }
1648         }
1649 
1650         if (result instanceof Window window) {
1651             final WebWindow webWindow = window.getWebWindow();
1652             result = getProxy(webWindow);
1653         }
1654 
1655         return result;
1656     }
1657 
1658     /**
1659      * {@inheritDoc}
1660      */
1661     @Override
1662     public Object get(final int index, final Scriptable start) {
1663         if (index < 0 || getWebWindow() == null) {
1664             return JavaScriptEngine.UNDEFINED;
1665         }
1666 
1667         final HTMLCollection frames = getFrames();
1668         if (frames == null || index >= frames.getLength()) {
1669             return JavaScriptEngine.UNDEFINED;
1670         }
1671         return frames.item(Integer.valueOf(index));
1672     }
1673 
1674     private static Object getFrameWindowByName(final HtmlPage page, final String name) {
1675         try {
1676             return page.getFrameByName(name).getScriptableObject();
1677         }
1678         catch (final ElementNotFoundException e) {
1679             return NOT_FOUND;
1680         }
1681     }
1682 
1683     private Object getElementsByName(final HtmlPage page, final String name) {
1684 
1685         // May be attempting to retrieve element(s) by name. IMPORTANT: We're using map-backed operations
1686         // like getHtmlElementsByName() and getHtmlElementById() as much as possible, so as to avoid XPath
1687         // overhead. We only use an XPath-based operation when we have to (where there is more than one
1688         // matching element). This optimization appears to improve performance in certain situations by ~15%
1689         // vs using XPath-based operations throughout.
1690         final List<DomElement> elements = page.getElementsByName(name);
1691 
1692         final Filter filter = new Filter(false);
1693 
1694         elements.removeIf(domElement -> !filter.matches(domElement));
1695 
1696         if (elements.isEmpty()) {
1697             return NOT_FOUND;
1698         }
1699 
1700         if (elements.size() == 1) {
1701             return getScriptableFor(elements.get(0));
1702         }
1703 
1704         // Null must be changed to '' for proper collection initialization.
1705         final String expElementName = "null".equals(name) ? "" : name;
1706 
1707         final HTMLCollection coll = new HTMLCollection(page, true);
1708         coll.setElementsSupplier(
1709                 (Supplier<List<DomNode>> & Serializable)
1710                 () -> {
1711                     final List<DomElement> expElements = page.getElementsByName(expElementName);
1712                     final List<DomNode> result = new ArrayList<>(expElements.size());
1713 
1714                     for (final DomElement domElement : expElements) {
1715                         if (filter.matches(domElement)) {
1716                             result.add(domElement);
1717                         }
1718                     }
1719                     return result;
1720                 });
1721 
1722         coll.setEffectOnCacheFunction(
1723                 (java.util.function.Function<HtmlAttributeChangeEvent, EffectOnCache> & Serializable)
1724                 event -> {
1725                     if ("name".equals(event.getName())) {
1726                         return EffectOnCache.RESET;
1727                     }
1728                     return EffectOnCache.NONE;
1729                 });
1730 
1731         return coll;
1732     }
1733 
1734     /**
1735      * Returns the proxy for the specified window.
1736      * @param w the window whose proxy is to be returned
1737      * @return the proxy for the specified window
1738      */
1739     public static WindowProxy getProxy(final WebWindow w) {
1740         return ((Window) w.getScriptableObject()).windowProxy_;
1741     }
1742 
1743     /**
1744      * Returns the text from the status line.
1745      * @return the status line text
1746      */
1747     @JsxGetter
1748     public String getStatus() {
1749         return status_;
1750     }
1751 
1752     /**
1753      * Sets the text from the status line.
1754      * @param message the status line text
1755      */
1756     @JsxSetter
1757     public void setStatus(final String message) {
1758         status_ = message;
1759 
1760         final StatusHandler statusHandler = webWindow_.getWebClient().getStatusHandler();
1761         if (statusHandler != null) {
1762             statusHandler.statusMessageChanged(webWindow_.getEnclosedPage(), message);
1763         }
1764     }
1765 
1766     /**
1767      * Returns the {@code innerWidth}.
1768      * @return the {@code innerWidth}
1769      * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_window_ref28.html">Mozilla doc</a>
1770      */
1771     @JsxGetter
1772     public int getInnerWidth() {
1773         return getWebWindow().getInnerWidth();
1774     }
1775 
1776     /**
1777      * Sets the {@code innerWidth}.
1778      * @param width the {@code innerWidth}
1779      */
1780     @JsxSetter
1781     public void setInnerWidth(final int width) {
1782         getWebWindow().setInnerWidth(width);
1783     }
1784 
1785     /**
1786      * Returns the {@code outerWidth}.
1787      * @return the {@code outerWidth}
1788      * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_window_ref79.html">Mozilla doc</a>
1789      */
1790     @JsxGetter
1791     public int getOuterWidth() {
1792         return getWebWindow().getOuterWidth();
1793     }
1794 
1795     /**
1796      * Sets the {@code outerWidth}.
1797      * @param width the {@code outerWidth}
1798      */
1799     @JsxSetter
1800     public void setOuterWidth(final int width) {
1801         getWebWindow().setOuterWidth(width);
1802     }
1803 
1804     /**
1805      * Returns the {@code innerHeight}.
1806      * @return the {@code innerHeight}
1807      * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_window_ref27.html">Mozilla doc</a>
1808      */
1809     @JsxGetter
1810     public int getInnerHeight() {
1811         return getWebWindow().getInnerHeight();
1812     }
1813 
1814     /**
1815      * Sets the {@code innerHeight}.
1816      * @param height the {@code innerHeight}
1817      */
1818     @JsxSetter
1819     public void setInnerHeight(final int height) {
1820         getWebWindow().setInnerHeight(height);
1821     }
1822 
1823     /**
1824      * Returns the {@code outerHeight}.
1825      * @return the {@code outerHeight}
1826      * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_window_ref78.html">Mozilla doc</a>
1827      */
1828     @JsxGetter
1829     public int getOuterHeight() {
1830         return getWebWindow().getOuterHeight();
1831     }
1832 
1833     /**
1834      * Sets the {@code outerHeight}.
1835      * @param height the {@code outerHeight}
1836      */
1837     @JsxSetter
1838     public void setOuterHeight(final int height) {
1839         getWebWindow().setOuterHeight(height);
1840     }
1841 
1842     /**
1843      * Prints the current page. The current implementation uses the {@link PrintHandler}
1844      * defined for the {@link WebClient} to process the window.
1845      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/print">MDN documentation</a>
1846      */
1847     @JsxFunction
1848     public void print() {
1849         final PrintHandler handler = getWebWindow().getWebClient().getPrintHandler();
1850         if (handler == null) {
1851             LOG.info("No PrintHandler installed - window.print() ignored");
1852             return;
1853         }
1854 
1855         final SgmlPage sgmlPage = getDocument().getPage();
1856         if (!(sgmlPage instanceof HtmlPage page)) {
1857             LOG.debug("Page is not an HtmlPage - window.print() ignored");
1858             return;
1859         }
1860 
1861         Event event = new Event(this, Event.TYPE_BEFOREPRINT);
1862         fireEvent(event);
1863 
1864         page.setPrinting(true);
1865         try {
1866             handler.handlePrint(page);
1867         }
1868         finally {
1869             page.setPrinting(false);
1870         }
1871         event = new Event(this, Event.TYPE_AFTERPRINT);
1872         fireEvent(event);
1873     }
1874 
1875     /**
1876      * Does nothing special anymore.
1877      * @param type the type of events to capture
1878      * @see HTMLDocument#captureEvents(String)
1879      */
1880     @JsxFunction
1881     public void captureEvents(final String type) {
1882         // Empty.
1883     }
1884 
1885     /**
1886      * Does nothing special anymore.
1887      * @param type the type of events to capture
1888      * @see HTMLDocument#releaseEvents(String)
1889      */
1890     @JsxFunction
1891     public void releaseEvents(final String type) {
1892         // Empty.
1893     }
1894 
1895     /**
1896      * Returns computed style of the element. Computed style represents the final computed values
1897      * of all CSS properties for the element. This method's return value is of the same type as
1898      * that of <code>element.style</code>, but the value returned by this method is read-only.
1899      *
1900      * @param element the element
1901      * @param pseudoElement a string specifying the pseudo-element to match (may be {@code null});
1902      *        e.g. ':before'
1903      * @return the computed style
1904      */
1905     @JsxFunction
1906     public ComputedCSSStyleDeclaration getComputedStyle(final Object element, final String pseudoElement) {
1907         if (!(element instanceof Element e)) {
1908             throw JavaScriptEngine.typeError("parameter 1 is not of type 'Element'");
1909         }
1910 
1911         final ComputedCssStyleDeclaration style = getWebWindow().getComputedStyle(e.getDomNodeOrDie(), pseudoElement);
1912         return new ComputedCSSStyleDeclaration(e, style);
1913     }
1914 
1915     /**
1916      * Returns the current selection.
1917      * @return the current selection
1918      */
1919     @JsxFunction
1920     public Selection getSelection() {
1921         final WebWindow webWindow = getWebWindow();
1922         // return null if the window is in a frame that is not displayed
1923         if (webWindow instanceof FrameWindow frameWindow) {
1924             if (getBrowserVersion().hasFeature(JS_WINDOW_SELECTION_NULL_IF_INVISIBLE)
1925                     && !frameWindow.getFrameElement().isDisplayed()) {
1926                 return null;
1927             }
1928         }
1929         return getSelectionImpl();
1930     }
1931 
1932     /**
1933      * Returns the current selection.
1934      * @return the current selection
1935      */
1936     public Selection getSelectionImpl() {
1937         if (selection_ == null) {
1938             selection_ = new Selection();
1939             selection_.setParentScope(getParentScope());
1940             selection_.setPrototype(getPrototype(selection_.getClass()));
1941         }
1942         return selection_;
1943     }
1944 
1945     /**
1946      * Gets the {@code controllers}. The result doesn't currently matter but it is important to return an
1947      * object as some JavaScript libraries check it.
1948      * @see <a href="https://developer.mozilla.org/En/DOM/Window.controllers">Mozilla documentation</a>
1949      * @return some object
1950      */
1951     @JsxGetter({FF, FF_ESR})
1952     public Object getControllers() {
1953         return controllers_;
1954     }
1955 
1956     /**
1957      * Sets the {@code controllers}.
1958      * @param value the new value
1959      */
1960     @JsxSetter({FF, FF_ESR})
1961     public void setControllers(final Object value) {
1962         controllers_ = value;
1963     }
1964 
1965     /**
1966      * Returns the value of {@code mozInnerScreenX} property.
1967      * @return the value of {@code mozInnerScreenX} property
1968      */
1969     @JsxGetter({FF, FF_ESR})
1970     public int getMozInnerScreenX() {
1971         return 12;
1972     }
1973 
1974     /**
1975      * Returns the value of {@code mozInnerScreenY} property.
1976      * @return the value of {@code mozInnerScreenY} property
1977      */
1978     @JsxGetter({FF, FF_ESR})
1979     public int getMozInnerScreenY() {
1980         return 90;
1981     }
1982 
1983     private static final class Filter {
1984         private final boolean includeFormFields_;
1985 
1986         Filter(final boolean includeFormFields) {
1987             includeFormFields_ = includeFormFields;
1988         }
1989 
1990         boolean matches(final Object object) {
1991             if (object instanceof HtmlEmbed
1992                 || object instanceof HtmlForm
1993                 || object instanceof HtmlImage
1994                 || object instanceof HtmlObject) {
1995                 return true;
1996             }
1997 
1998             return includeFormFields_
1999                     && (object instanceof HtmlAnchor
2000                         || object instanceof HtmlButton
2001                         || object instanceof HtmlInput
2002                         || object instanceof HtmlMap
2003                         || object instanceof HtmlSelect
2004                         || object instanceof HtmlTextArea);
2005         }
2006     }
2007 
2008     /**
2009      * Should implement the stop() function on the window object.
2010      * (currently empty implementation)
2011      * @see <a href="https://developer.mozilla.org/en/DOM/window.stop">window.stop</a>
2012      */
2013     @JsxFunction
2014     public void stop() {
2015         //empty
2016     }
2017 
2018     /**
2019      * Returns the value of {@code pageXOffset} property.
2020      * @return the value of {@code pageXOffset} property
2021      */
2022     @JsxGetter
2023     public int getPageXOffset() {
2024         return 0;
2025     }
2026 
2027     /**
2028      * Returns the value of {@code pageYOffset} property.
2029      * @return the value of {@code pageYOffset} property
2030      */
2031     @JsxGetter
2032     public int getPageYOffset() {
2033         return 0;
2034     }
2035 
2036     /**
2037      * Returns the value of {@code scrollX} property.
2038      * @return the value of {@code scrollX} property
2039      */
2040     @JsxGetter
2041     public int getScrollX() {
2042         return 0;
2043     }
2044 
2045     /**
2046      * Returns the value of {@code scrollY} property.
2047      * @return the value of {@code scrollY} property
2048      */
2049     @JsxGetter
2050     public int getScrollY() {
2051         return 0;
2052     }
2053 
2054     /**
2055      * Returns the {@code netscape} property.
2056      *
2057      * @return the value of {@code netscape} property
2058      */
2059     @JsxGetter({FF, FF_ESR})
2060     public Netscape getNetscape() {
2061         return new Netscape(getParentScope());
2062     }
2063 
2064     /**
2065      * {@inheritDoc}
2066      * Used to allow re-declaration of constants (eg: "var undefined;").
2067      */
2068     @Override
2069     public boolean isConst(final Context cx, final String name) {
2070         if ("undefined".equals(name) || "Infinity".equals(name) || "NaN".equals(name)) {
2071             return false;
2072         }
2073 
2074         return super.isConst(cx, name);
2075     }
2076 
2077     /**
2078      * {@inheritDoc}
2079      */
2080     @Override
2081     public boolean dispatchEvent(final Event event) {
2082         event.setTarget(this);
2083         final ScriptResult result = fireEvent(event);
2084         return !event.isAborted(result);
2085     }
2086 
2087     /**
2088      * Getter for the {@code onchange} event handler.
2089      * @return the handler
2090      */
2091     @JsxGetter
2092     public Function getOnchange() {
2093         return getEventHandler(Event.TYPE_CHANGE);
2094     }
2095 
2096     /**
2097      * Setter for the {@code onchange} event handler.
2098      * @param onchange the handler
2099      */
2100     @JsxSetter
2101     public void setOnchange(final Object onchange) {
2102         setHandlerForJavaScript(Event.TYPE_CHANGE, onchange);
2103     }
2104 
2105     /**
2106      * Getter for the {@code onsubmit} event handler.
2107      * @return the handler
2108      */
2109     @JsxGetter
2110     public Function getOnsubmit() {
2111         return getEventHandler(Event.TYPE_SUBMIT);
2112     }
2113 
2114     /**
2115      * Setter for the {@code onsubmit} event handler.
2116      * @param onsubmit the handler
2117      */
2118     @JsxSetter
2119     public void setOnsubmit(final Object onsubmit) {
2120         setHandlerForJavaScript(Event.TYPE_SUBMIT, onsubmit);
2121     }
2122 
2123     /**
2124      * Posts a message.
2125      * @param context the current context
2126      * @param scope the scope
2127      * @param thisObj this object
2128      * @param args the script(s) to import
2129      * @param funObj the JS function called
2130      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage">MDN documentation</a>
2131      */
2132     @JsxFunction
2133     public static void postMessage(final Context context, final VarScope scope,
2134             final Scriptable thisObj, final Object[] args, final Function funObj) {
2135 
2136         // support the structured clone algorithm
2137         if (args.length < 1) {
2138             throw JavaScriptEngine.typeError("message not provided");
2139         }
2140         final Object message = args[0];
2141 
2142         String targetOrigin = "*";
2143         Object transfer = JavaScriptEngine.UNDEFINED;
2144 
2145         if (args.length > 1) {
2146             if (JavaScriptEngine.isArray(args[1])) {
2147                 transfer = args[1];
2148             }
2149             else {
2150                 targetOrigin = JavaScriptEngine.toString(args[1]);
2151             }
2152         }
2153 
2154         if (args.length > 2) {
2155             if (JavaScriptEngine.isArray(args[2])) {
2156                 transfer = args[2];
2157             }
2158         }
2159 
2160         final TopLevel topLevel = ScriptableObject.getTopLevelScope(scope);
2161         final Window sender = (Window) topLevel.getGlobalThis();
2162         final Window receiver = (Window) thisObj;
2163         final URL receiverURL = receiver.getWebWindow().getEnclosedPage().getUrl();
2164 
2165         final WebWindow webWindow = sender.getWebWindow();
2166         final Page page = webWindow.getEnclosedPage();
2167         final URL senderURL = page.getUrl();
2168 
2169         if (!StringUtils.equalsChar('*', targetOrigin)) {
2170             final URL targetURL;
2171             if (StringUtils.equalsChar('/', targetOrigin)) {
2172                 targetURL = senderURL;
2173             }
2174             else {
2175                 try {
2176                     targetURL = new URL(targetOrigin);
2177                 }
2178                 catch (final Exception e) {
2179                     throw JavaScriptEngine.asJavaScriptException(
2180                             sender,
2181                             "Failed to execute 'postMessage' on 'Window': Invalid target origin '"
2182                                     + targetOrigin + "' was specified (reason: " + e.getMessage() + ".",
2183                             DOMException.SYNTAX_ERR);
2184                 }
2185             }
2186 
2187             if (getPort(targetURL) != getPort(receiverURL)) {
2188                 return;
2189             }
2190             if (!targetURL.getHost().equals(receiverURL.getHost())) {
2191                 return;
2192             }
2193             if (!targetURL.getProtocol().equals(receiverURL.getProtocol())) {
2194                 return;
2195             }
2196         }
2197 
2198         final String origin;
2199         if (page instanceof HtmlPage senderPage && senderPage.isOpaqueOrigin()) {
2200             // the origin of a sandboxed document without 'allow-same-origin'
2201             origin = "null";
2202         }
2203         else {
2204             try {
2205                 final URL originUrl = UrlUtils.getUrlWithoutPathRefQuery(senderURL);
2206                 origin = UrlUtils.removeRedundantPort(originUrl).toExternalForm();
2207             }
2208             catch (final MalformedURLException e) {
2209                 throw JavaScriptEngine.throwAsScriptRuntimeEx(e);
2210             }
2211         }
2212 
2213         final MessageEvent event = new MessageEvent();
2214         event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin, "", sender, transfer);
2215         event.setParentScope(scope);
2216         event.setPrototype(receiver.getPrototype(event.getClass()));
2217 
2218         final AbstractJavaScriptEngine<?> jsEngine = webWindow.getWebClient().getJavaScriptEngine();
2219         final PostponedAction action = new PostponedAction(page, "Window.postMessage") {
2220             @Override
2221             public void execute() {
2222                 final HtmlUnitContextFactory cf = jsEngine.getContextFactory();
2223                 cf.call(cx -> receiver.dispatchEvent(event));
2224             }
2225         };
2226         jsEngine.addPostponedAction(action);
2227     }
2228 
2229     /**
2230      * Returns the port of the specified URL.
2231      * @param url the URL
2232      * @return the port
2233      */
2234     public static int getPort(final URL url) {
2235         int port = url.getPort();
2236         if (port == -1) {
2237             if ("http".equals(url.getProtocol())) {
2238                 port = 80;
2239             }
2240             else {
2241                 port = 443;
2242             }
2243         }
2244         return port;
2245     }
2246 
2247     /**
2248      * The performance attribute is defined as replacable
2249      * (https://w3c.github.io/hr-time/#the-performance-attribute) but not implemented
2250      * as that.
2251      * @return the value of the {@code performance} property
2252      */
2253     @JsxGetter
2254     public Scriptable getPerformance() {
2255         if (performance_ == null) {
2256             final Performance performance = new Performance();
2257             performance.setParentScope(getParentScope());
2258             performance.setPrototype(getPrototype(performance.getClass()));
2259             performance_ = performance;
2260         }
2261         return performance_;
2262     }
2263 
2264     /**
2265      * The performance attribute is defined as replacable
2266      * (https://w3c.github.io/hr-time/#the-performance-attribute) but not implemented
2267      * as that.
2268      * <p>
2269      * Sets the {@code performance} property.
2270      * </p>
2271      * @param performance the value to overwrite the defined property value
2272      */
2273     @JsxSetter
2274     public void setPerformance(final Scriptable performance) {
2275         performance_ = performance;
2276     }
2277 
2278     /**
2279      * Returns the {@code devicePixelRatio} property.
2280      * @return the {@code devicePixelRatio} property
2281      */
2282     @JsxGetter
2283     public int getDevicePixelRatio() {
2284         return 1;
2285     }
2286 
2287     /**
2288      * Returns the {@code styleMedia} property.
2289      * @return the {@code styleMedia} property
2290      */
2291     @JsxGetter({CHROME, EDGE})
2292     public StyleMedia getStyleMedia() {
2293         final StyleMedia styleMedia = new StyleMedia();
2294         styleMedia.setParentScope(getParentScope());
2295         styleMedia.setPrototype(getPrototype(styleMedia.getClass()));
2296         return styleMedia;
2297     }
2298 
2299     /**
2300      * Returns a new MediaQueryList object representing the parsed results of the specified media query string.
2301      *
2302      * @param mediaQueryString the media query
2303      * @return a new MediaQueryList object
2304      */
2305     @JsxFunction
2306     public MediaQueryList matchMedia(final String mediaQueryString) {
2307         final MediaQueryList mediaQueryList = new MediaQueryList(mediaQueryString);
2308         mediaQueryList.setParentScope(getParentScope());
2309         mediaQueryList.setPrototype(getPrototype(mediaQueryList.getClass()));
2310         return mediaQueryList;
2311     }
2312 
2313     /**
2314      * Stub only at the moment.
2315      * @param search the text string for which to search
2316      * @param caseSensitive if true, specifies a case-sensitive search
2317      * @param backwards if true, specifies a backward search
2318      * @param wrapAround if true, specifies a wrap around search
2319      * @param wholeWord if true, specifies a whole word search
2320      * @param searchInFrames if true, specifies a search in frames
2321      * @param showDialog if true, specifies a show Dialog.
2322      * @return false
2323      */
2324     @JsxFunction
2325     public boolean find(final String search, final boolean caseSensitive,
2326             final boolean backwards, final boolean wrapAround,
2327             final boolean wholeWord, final boolean searchInFrames, final boolean showDialog) {
2328         return false;
2329     }
2330 
2331     /**
2332      * Returns the {@code speechSynthesis} property.
2333      * @return the {@code speechSynthesis} property
2334      */
2335     @JsxGetter({CHROME, EDGE})
2336     public SpeechSynthesis getSpeechSynthesis() {
2337         final SpeechSynthesis speechSynthesis = new SpeechSynthesis();
2338         speechSynthesis.setParentScope(getParentScope());
2339         speechSynthesis.setPrototype(getPrototype(speechSynthesis.getClass()));
2340         return speechSynthesis;
2341     }
2342 
2343     /**
2344      * Returns the {@code offscreenBuffering} property.
2345      * @return the {@code offscreenBuffering} property
2346      */
2347     @JsxGetter({CHROME, EDGE})
2348     public boolean isOffscreenBuffering() {
2349         return true;
2350     }
2351 
2352     /**
2353      * Returns the {@code crypto} property.
2354      * @return the {@code crypto} property
2355      */
2356     @JsxGetter
2357     public Crypto getCrypto() {
2358         if (crypto_ == null) {
2359             crypto_ = new Crypto(this);
2360         }
2361         return crypto_;
2362     }
2363 
2364     /**
2365      * {@inheritDoc}
2366      */
2367     @Override
2368     public void close() {
2369         // nothing to do
2370     }
2371 
2372     /**
2373      * Returns the {@code onfocus} event handler.
2374      * @return the {@code onfocus} event handler
2375      */
2376     @JsxGetter
2377     public Function getOnfocus() {
2378         return getEventHandler(Event.TYPE_FOCUS);
2379     }
2380 
2381     /**
2382      * Sets the {@code onfocus} event handler.
2383      * @param onfocus the {@code onfocus} event handler
2384      */
2385     @JsxSetter
2386     public void setOnfocus(final Object onfocus) {
2387         setHandlerForJavaScript(Event.TYPE_FOCUS, onfocus);
2388     }
2389 
2390     /**
2391      * Returns the {@code ondragend} event handler.
2392      * @return the {@code ondragend} event handler
2393      */
2394     @JsxGetter
2395     public Function getOndragend() {
2396         return getEventHandler(Event.TYPE_DRAGEND);
2397     }
2398 
2399     /**
2400      * Sets the {@code ondragend} event handler.
2401      * @param ondragend the {@code ondragend} event handler
2402      */
2403     @JsxSetter
2404     public void setOndragend(final Object ondragend) {
2405         setHandlerForJavaScript(Event.TYPE_DRAGEND, ondragend);
2406     }
2407 
2408     /**
2409      * Returns the {@code oninvalid} event handler.
2410      * @return the {@code oninvalid} event handler
2411      */
2412     @JsxGetter
2413     public Function getOninvalid() {
2414         return getEventHandler(Event.TYPE_INVALID);
2415     }
2416 
2417     /**
2418      * Sets the {@code oninvalid} event handler.
2419      * @param oninvalid the {@code oninvalid} event handler
2420      */
2421     @JsxSetter
2422     public void setOninvalid(final Object oninvalid) {
2423         setHandlerForJavaScript(Event.TYPE_INVALID, oninvalid);
2424     }
2425 
2426     /**
2427      * Returns the {@code onpointerout} event handler.
2428      * @return the {@code onpointerout} event handler
2429      */
2430     @JsxGetter({CHROME, EDGE})
2431     public Function getOnpointerout() {
2432         return getEventHandler(Event.TYPE_POINTEROUT);
2433     }
2434 
2435     /**
2436      * Sets the {@code onpointerout} event handler.
2437      * @param onpointerout the {@code onpointerout} event handler
2438      */
2439     @JsxSetter({CHROME, EDGE})
2440     public void setOnpointerout(final Object onpointerout) {
2441         setHandlerForJavaScript(Event.TYPE_POINTEROUT, onpointerout);
2442     }
2443 
2444     /**
2445      * Returns the {@code onratechange} event handler.
2446      * @return the {@code onratechange} event handler
2447      */
2448     @JsxGetter
2449     public Function getOnratechange() {
2450         return getEventHandler(Event.TYPE_RATECHANGE);
2451     }
2452 
2453     /**
2454      * Sets the {@code onratechange} event handler.
2455      * @param onratechange the {@code onratechange} event handler
2456      */
2457     @JsxSetter
2458     public void setOnratechange(final Object onratechange) {
2459         setHandlerForJavaScript(Event.TYPE_RATECHANGE, onratechange);
2460     }
2461 
2462     /**
2463      * Returns the {@code onanimationiteration} event handler.
2464      * @return the {@code onanimationiteration} event handler
2465      */
2466     @JsxGetter
2467     public Function getOnanimationiteration() {
2468         return getEventHandler(Event.TYPE_ANIMATIONITERATION);
2469     }
2470 
2471     /**
2472      * Sets the {@code onanimationiteration} event handler.
2473      * @param onanimationiteration the {@code onanimationiteration} event handler
2474      */
2475     @JsxSetter
2476     public void setOnanimationiteration(final Object onanimationiteration) {
2477         setHandlerForJavaScript(Event.TYPE_ANIMATIONITERATION, onanimationiteration);
2478     }
2479 
2480     /**
2481      * Returns the {@code oncanplaythrough} event handler.
2482      * @return the {@code oncanplaythrough} event handler
2483      */
2484     @JsxGetter
2485     public Function getOncanplaythrough() {
2486         return getEventHandler(Event.TYPE_CANPLAYTHROUGH);
2487     }
2488 
2489     /**
2490      * Sets the {@code oncanplaythrough} event handler.
2491      * @param oncanplaythrough the {@code oncanplaythrough} event handler
2492      */
2493     @JsxSetter
2494     public void setOncanplaythrough(final Object oncanplaythrough) {
2495         setHandlerForJavaScript(Event.TYPE_CANPLAYTHROUGH, oncanplaythrough);
2496     }
2497 
2498     /**
2499      * Returns the {@code oncancel} event handler.
2500      * @return the {@code oncancel} event handler
2501      */
2502     @JsxGetter({CHROME, EDGE})
2503     public Function getOncancel() {
2504         return getEventHandler(Event.TYPE_CANCEL);
2505     }
2506 
2507     /**
2508      * Sets the {@code oncancel} event handler.
2509      * @param oncancel the {@code oncancel} event handler
2510      */
2511     @JsxSetter({CHROME, EDGE})
2512     public void setOncancel(final Object oncancel) {
2513         setHandlerForJavaScript(Event.TYPE_CANCEL, oncancel);
2514     }
2515 
2516     /**
2517      * Returns the {@code onpointerenter} event handler.
2518      * @return the {@code onpointerenter} event handler
2519      */
2520     @JsxGetter({CHROME, EDGE})
2521     public Function getOnpointerenter() {
2522         return getEventHandler(Event.TYPE_POINTERENTER);
2523     }
2524 
2525     /**
2526      * Sets the {@code onpointerenter} event handler.
2527      * @param onpointerenter the {@code onpointerenter} event handler
2528      */
2529     @JsxSetter({CHROME, EDGE})
2530     public void setOnpointerenter(final Object onpointerenter) {
2531         setHandlerForJavaScript(Event.TYPE_POINTERENTER, onpointerenter);
2532     }
2533 
2534     /**
2535      * Returns the {@code onselect} event handler.
2536      * @return the {@code onselect} event handler
2537      */
2538     @JsxGetter
2539     public Function getOnselect() {
2540         return getEventHandler(Event.TYPE_SELECT);
2541     }
2542 
2543     /**
2544      * Sets the {@code onselect} event handler.
2545      * @param onselect the {@code onselect} event handler
2546      */
2547     @JsxSetter
2548     public void setOnselect(final Object onselect) {
2549         setHandlerForJavaScript(Event.TYPE_SELECT, onselect);
2550     }
2551 
2552     /**
2553      * Returns the {@code onauxclick} event handler.
2554      * @return the {@code onauxclick} event handler
2555      */
2556     @JsxGetter({CHROME, EDGE})
2557     public Function getOnauxclick() {
2558         return getEventHandler(Event.TYPE_AUXCLICK);
2559     }
2560 
2561     /**
2562      * Sets the {@code onauxclick} event handler.
2563      * @param onauxclick the {@code onauxclick} event handler
2564      */
2565     @JsxSetter({CHROME, EDGE})
2566     public void setOnauxclick(final Object onauxclick) {
2567         setHandlerForJavaScript(Event.TYPE_AUXCLICK, onauxclick);
2568     }
2569 
2570     /**
2571      * Returns the {@code onscroll} event handler.
2572      * @return the {@code onscroll} event handler
2573      */
2574     @JsxGetter
2575     public Function getOnscroll() {
2576         return getEventHandler(Event.TYPE_SCROLL);
2577     }
2578 
2579     /**
2580      * Sets the {@code onscroll} event handler.
2581      * @param onscroll the {@code onscroll} event handler
2582      */
2583     @JsxSetter
2584     public void setOnscroll(final Object onscroll) {
2585         setHandlerForJavaScript(Event.TYPE_SCROLL, onscroll);
2586     }
2587 
2588     /**
2589      * Returns the {@code onkeydown} event handler.
2590      * @return the {@code onkeydown} event handler
2591      */
2592     @JsxGetter
2593     public Function getOnkeydown() {
2594         return getEventHandler(Event.TYPE_KEY_DOWN);
2595     }
2596 
2597     /**
2598      * Sets the {@code onkeydown} event handler.
2599      * @param onkeydown the {@code onkeydown} event handler
2600      */
2601     @JsxSetter
2602     public void setOnkeydown(final Object onkeydown) {
2603         setHandlerForJavaScript(Event.TYPE_KEY_DOWN, onkeydown);
2604     }
2605 
2606     /**
2607      * Returns the {@code onwebkitanimationstart} event handler.
2608      * @return the {@code onwebkitanimationstart} event handler
2609      */
2610     @JsxGetter({CHROME, EDGE})
2611     public Function getOnwebkitanimationstart() {
2612         return getEventHandler(Event.TYPE_WEBANIMATIONSTART);
2613     }
2614 
2615     /**
2616      * Sets the {@code onwebkitanimationstart} event handler.
2617      * @param onwebkitanimationstart the {@code onwebkitanimationstart} event handler
2618      */
2619     @JsxSetter({CHROME, EDGE})
2620     public void setOnwebkitanimationstart(final Object onwebkitanimationstart) {
2621         setHandlerForJavaScript(Event.TYPE_WEBANIMATIONSTART, onwebkitanimationstart);
2622     }
2623 
2624     /**
2625      * Returns the {@code onkeyup} event handler.
2626      * @return the {@code onkeyup} event handler
2627      */
2628     @JsxGetter
2629     public Function getOnkeyup() {
2630         return getEventHandler(Event.TYPE_KEY_UP);
2631     }
2632 
2633     /**
2634      * Sets the {@code onkeyup} event handler.
2635      * @param onkeyup the {@code onkeyup} event handler
2636      */
2637     @JsxSetter
2638     public void setOnkeyup(final Object onkeyup) {
2639         setHandlerForJavaScript(Event.TYPE_KEY_UP, onkeyup);
2640     }
2641 
2642     /**
2643      * Returns the {@code onreset} event handler.
2644      * @return the {@code onreset} event handler
2645      */
2646     @JsxGetter
2647     public Function getOnreset() {
2648         return getEventHandler(Event.TYPE_RESET);
2649     }
2650 
2651     /**
2652      * Sets the {@code onreset} event handler.
2653      * @param onreset the {@code onreset} event handler
2654      */
2655     @JsxSetter
2656     public void setOnreset(final Object onreset) {
2657         setHandlerForJavaScript(Event.TYPE_RESET, onreset);
2658     }
2659 
2660     /**
2661      * Returns the {@code onkeypress} event handler.
2662      * @return the {@code onkeypress} event handler
2663      */
2664     @JsxGetter
2665     public Function getOnkeypress() {
2666         return getEventHandler(Event.TYPE_KEY_PRESS);
2667     }
2668 
2669     /**
2670      * Sets the {@code onkeypress} event handler.
2671      * @param onkeypress the {@code onkeypress} event handler
2672      */
2673     @JsxSetter
2674     public void setOnkeypress(final Object onkeypress) {
2675         setHandlerForJavaScript(Event.TYPE_KEY_PRESS, onkeypress);
2676     }
2677 
2678     /**
2679      * Returns the {@code ondrag} event handler.
2680      * @return the {@code ondrag} event handler
2681      */
2682     @JsxGetter
2683     public Function getOndrag() {
2684         return getEventHandler(Event.TYPE_DRAG);
2685     }
2686 
2687     /**
2688      * Sets the {@code ondrag} event handler.
2689      * @param ondrag the {@code ondrag} event handler
2690      */
2691     @JsxSetter
2692     public void setOndrag(final Object ondrag) {
2693         setHandlerForJavaScript(Event.TYPE_DRAG, ondrag);
2694     }
2695 
2696     /**
2697      * Returns the {@code onseeked} event handler.
2698      * @return the {@code onseeked} event handler
2699      */
2700     @JsxGetter
2701     public Function getOnseeked() {
2702         return getEventHandler(Event.TYPE_SEEKED);
2703     }
2704 
2705     /**
2706      * Sets the {@code onseeked} event handler.
2707      * @param onseeked the {@code onseeked} event handler
2708      */
2709     @JsxSetter
2710     public void setOnseeked(final Object onseeked) {
2711         setHandlerForJavaScript(Event.TYPE_SEEKED, onseeked);
2712     }
2713 
2714     /**
2715      * Returns the {@code onoffline} event handler.
2716      * @return the {@code onoffline} event handler
2717      */
2718     @JsxGetter
2719     public Function getOnoffline() {
2720         return getEventHandler(Event.TYPE_OFFLINE);
2721     }
2722 
2723     /**
2724      * Sets the {@code onoffline} event handler.
2725      * @param onoffline the {@code onoffline} event handler
2726      */
2727     @JsxSetter
2728     public void setOnoffline(final Object onoffline) {
2729         setHandlerForJavaScript(Event.TYPE_OFFLINE, onoffline);
2730     }
2731 
2732     /**
2733      * Returns the {@code ondeviceorientation} event handler.
2734      * @return the {@code ondeviceorientation} event handler
2735      */
2736     @JsxGetter
2737     public Function getOndeviceorientation() {
2738         return getEventHandler(Event.TYPE_DEVICEORIENTATION);
2739     }
2740 
2741     /**
2742      * Sets the {@code ondeviceorientation} event handler.
2743      * @param ondeviceorientation the {@code ondeviceorientation} event handler
2744      */
2745     @JsxSetter
2746     public void setOndeviceorientation(final Object ondeviceorientation) {
2747         setHandlerForJavaScript(Event.TYPE_DEVICEORIENTATION, ondeviceorientation);
2748     }
2749 
2750     /**
2751      * Returns the {@code ontoggle} event handler.
2752      * @return the {@code ontoggle} event handler
2753      */
2754     @JsxGetter({CHROME, EDGE})
2755     public Function getOntoggle() {
2756         return getEventHandler(Event.TYPE_TOGGLE);
2757     }
2758 
2759     /**
2760      * Sets the {@code ontoggle} event handler.
2761      * @param ontoggle the {@code ontoggle} event handler
2762      */
2763     @JsxSetter({CHROME, EDGE})
2764     public void setOntoggle(final Object ontoggle) {
2765         setHandlerForJavaScript(Event.TYPE_TOGGLE, ontoggle);
2766     }
2767 
2768     /**
2769      * Returns the {@code onplay} event handler.
2770      * @return the {@code onplay} event handler
2771      */
2772     @JsxGetter
2773     public Function getOnplay() {
2774         return getEventHandler(Event.TYPE_PLAY);
2775     }
2776 
2777     /**
2778      * Sets the {@code onplay} event handler.
2779      * @param onplay the {@code onplay} event handler
2780      */
2781     @JsxSetter
2782     public void setOnplay(final Object onplay) {
2783         setHandlerForJavaScript(Event.TYPE_PLAY, onplay);
2784     }
2785 
2786     /**
2787      * Returns the {@code oncontextmenu} event handler.
2788      * @return the {@code oncontextmenu} event handler
2789      */
2790     @JsxGetter
2791     public Function getOncontextmenu() {
2792         return getEventHandler(MouseEvent.TYPE_CONTEXT_MENU);
2793     }
2794 
2795     /**
2796      * Sets the {@code oncontextmenu} event handler.
2797      * @param oncontextmenu the {@code oncontextmenu} event handler
2798      */
2799     @JsxSetter
2800     public void setOncontextmenu(final Object oncontextmenu) {
2801         setHandlerForJavaScript(MouseEvent.TYPE_CONTEXT_MENU, oncontextmenu);
2802     }
2803 
2804     /**
2805      * Returns the {@code onmousemove} event handler.
2806      * @return the {@code onmousemove} event handler
2807      */
2808     @JsxGetter
2809     public Function getOnmousemove() {
2810         return getEventHandler(MouseEvent.TYPE_MOUSE_MOVE);
2811     }
2812 
2813     /**
2814      * Sets the {@code onmousemove} event handler.
2815      * @param onmousemove the {@code onmousemove} event handler
2816      */
2817     @JsxSetter
2818     public void setOnmousemove(final Object onmousemove) {
2819         setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_MOVE, onmousemove);
2820     }
2821 
2822     /**
2823      * Returns the {@code onpointermove} event handler.
2824      * @return the {@code onpointermove} event handler
2825      */
2826     @JsxGetter({CHROME, EDGE})
2827     public Function getOnpointermove() {
2828         return getEventHandler(Event.TYPE_POINTERMOVE);
2829     }
2830 
2831     /**
2832      * Sets the {@code onpointermove} event handler.
2833      * @param onpointermove the {@code onpointermove} event handler
2834      */
2835     @JsxSetter({CHROME, EDGE})
2836     public void setOnpointermove(final Object onpointermove) {
2837         setHandlerForJavaScript(Event.TYPE_POINTERMOVE, onpointermove);
2838     }
2839 
2840     /**
2841      * Returns the {@code onmouseover} event handler.
2842      * @return the {@code onmouseover} event handler
2843      */
2844     @JsxGetter
2845     public Function getOnmouseover() {
2846         return getEventHandler(MouseEvent.TYPE_MOUSE_OVER);
2847     }
2848 
2849     /**
2850      * Sets the {@code onmouseover} event handler.
2851      * @param onmouseover the {@code onmouseover} event handler
2852      */
2853     @JsxSetter
2854     public void setOnmouseover(final Object onmouseover) {
2855         setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_OVER, onmouseover);
2856     }
2857 
2858     /**
2859      * Returns the {@code onlostpointercapture} event handler.
2860      * @return the {@code onlostpointercapture} event handler
2861      */
2862     @JsxGetter({CHROME, EDGE})
2863     public Function getOnlostpointercapture() {
2864         return getEventHandler(Event.TYPE_LOSTPOINTERCAPTURE);
2865     }
2866 
2867     /**
2868      * Sets the {@code onlostpointercapture} event handler.
2869      * @param onlostpointercapture the {@code onlostpointercapture} event handler
2870      */
2871     @JsxSetter({CHROME, EDGE})
2872     public void setOnlostpointercapture(final Object onlostpointercapture) {
2873         setHandlerForJavaScript(Event.TYPE_LOSTPOINTERCAPTURE, onlostpointercapture);
2874     }
2875 
2876     /**
2877      * Returns the {@code onpointerover} event handler.
2878      * @return the {@code onpointerover} event handler
2879      */
2880     @JsxGetter({CHROME, EDGE})
2881     public Function getOnpointerover() {
2882         return getEventHandler(Event.TYPE_POINTEROVER);
2883     }
2884 
2885     /**
2886      * Sets the {@code onpointerover} event handler.
2887      * @param onpointerover the {@code onpointerover} event handler
2888      */
2889     @JsxSetter({CHROME, EDGE})
2890     public void setOnpointerover(final Object onpointerover) {
2891         setHandlerForJavaScript(Event.TYPE_POINTEROVER, onpointerover);
2892     }
2893 
2894     /**
2895      * Returns the {@code onclose} event handler.
2896      * @return the {@code onclose} event handler
2897      */
2898     @JsxGetter({CHROME, EDGE})
2899     public Function getOnclose() {
2900         return getEventHandler(Event.TYPE_CLOSE);
2901     }
2902 
2903     /**
2904      * Sets the {@code onclose} event handler.
2905      * @param onclose the {@code onclose} event handler
2906      */
2907     @JsxSetter({CHROME, EDGE})
2908     public void setOnclose(final Object onclose) {
2909         setHandlerForJavaScript(Event.TYPE_CLOSE, onclose);
2910     }
2911 
2912     /**
2913      * Returns the {@code onanimationend} event handler.
2914      * @return the {@code onanimationend} event handler
2915      */
2916     @JsxGetter
2917     public Function getOnanimationend() {
2918         return getEventHandler(Event.TYPE_ANIMATIONEND);
2919     }
2920 
2921     /**
2922      * Sets the {@code onanimationend} event handler.
2923      * @param onanimationend the {@code onanimationend} event handler
2924      */
2925     @JsxSetter
2926     public void setOnanimationend(final Object onanimationend) {
2927         setHandlerForJavaScript(Event.TYPE_ANIMATIONEND, onanimationend);
2928     }
2929 
2930     /**
2931      * Returns the {@code ondragenter} event handler.
2932      * @return the {@code ondragenter} event handler
2933      */
2934     @JsxGetter
2935     public Function getOndragenter() {
2936         return getEventHandler(Event.TYPE_DRAGENTER);
2937     }
2938 
2939     /**
2940      * Sets the {@code ondragenter} event handler.
2941      * @param ondragenter the {@code ondragenter} event handler
2942      */
2943     @JsxSetter
2944     public void setOndragenter(final Object ondragenter) {
2945         setHandlerForJavaScript(Event.TYPE_DRAGENTER, ondragenter);
2946     }
2947 
2948     /**
2949      * Returns the {@code onafterprint} event handler.
2950      * @return the {@code onafterprint} event handler
2951      */
2952     @JsxGetter({FF, FF_ESR})
2953     public Function getOnafterprint() {
2954         return getEventHandler(Event.TYPE_AFTERPRINT);
2955     }
2956 
2957     /**
2958      * Sets the {@code onafterprint} event handler.
2959      * @param onafterprint the {@code onafterprint} event handler
2960      */
2961     @JsxSetter({FF, FF_ESR})
2962     public void setOnafterprint(final Object onafterprint) {
2963         setHandlerForJavaScript(Event.TYPE_AFTERPRINT, onafterprint);
2964     }
2965 
2966     /**
2967      * Returns the {@code onmozfullscreenerror} event handler.
2968      * @return the {@code onmozfullscreenerror} event handler
2969      */
2970     @JsxGetter({FF, FF_ESR})
2971     public Function getOnmozfullscreenerror() {
2972         return getEventHandler(Event.TYPE_MOZFULLSCREENERROR);
2973     }
2974 
2975     /**
2976      * Sets the {@code onmozfullscreenerror} event handler.
2977      * @param onmozfullscreenerror the {@code onmozfullscreenerror} event handler
2978      */
2979     @JsxSetter({FF, FF_ESR})
2980     public void setOnmozfullscreenerror(final Object onmozfullscreenerror) {
2981         setHandlerForJavaScript(Event.TYPE_MOZFULLSCREENERROR, onmozfullscreenerror);
2982     }
2983 
2984     /**
2985      * Returns the {@code onmouseleave} event handler.
2986      * @return the {@code onmouseleave} event handler
2987      */
2988     @JsxGetter
2989     public Function getOnmouseleave() {
2990         return getEventHandler(Event.TYPE_MOUSELEAVE);
2991     }
2992 
2993     /**
2994      * Sets the {@code onmouseleave} event handler.
2995      * @param onmouseleave the {@code onmouseleave} event handler
2996      */
2997     @JsxSetter
2998     public void setOnmouseleave(final Object onmouseleave) {
2999         setHandlerForJavaScript(Event.TYPE_MOUSELEAVE, onmouseleave);
3000     }
3001 
3002     /**
3003      * Returns the {@code onmousewheel} event handler.
3004      * @return the {@code onmousewheel} event handler
3005      */
3006     @JsxGetter({CHROME, EDGE})
3007     public Function getOnmousewheel() {
3008         return getEventHandler(Event.TYPE_MOUSEWHEEL);
3009     }
3010 
3011     /**
3012      * Sets the {@code onmousewheel} event handler.
3013      * @param onmousewheel the {@code onmousewheel} event handler
3014      */
3015     @JsxSetter({CHROME, EDGE})
3016     public void setOnmousewheel(final Object onmousewheel) {
3017         setHandlerForJavaScript(Event.TYPE_MOUSEWHEEL, onmousewheel);
3018     }
3019 
3020     /**
3021      * Returns the {@code onseeking} event handler.
3022      * @return the {@code onseeking} event handler
3023      */
3024     @JsxGetter
3025     public Function getOnseeking() {
3026         return getEventHandler(Event.TYPE_SEEKING);
3027     }
3028 
3029     /**
3030      * Sets the {@code onseeking} event handler.
3031      * @param onseeking the {@code onseeking} event handler
3032      */
3033     @JsxSetter
3034     public void setOnseeking(final Object onseeking) {
3035         setHandlerForJavaScript(Event.TYPE_SEEKING, onseeking);
3036     }
3037 
3038     /**
3039      * Returns the {@code oncuechange} event handler.
3040      * @return the {@code oncuechange} event handler
3041      */
3042     @JsxGetter({CHROME, EDGE})
3043     public Function getOncuechange() {
3044         return getEventHandler(Event.TYPE_CUECHANGE);
3045     }
3046 
3047     /**
3048      * Sets the {@code oncuechange} event handler.
3049      * @param oncuechange the {@code oncuechange} event handler
3050      */
3051     @JsxSetter({CHROME, EDGE})
3052     public void setOncuechange(final Object oncuechange) {
3053         setHandlerForJavaScript(Event.TYPE_CUECHANGE, oncuechange);
3054     }
3055 
3056     /**
3057      * Returns the {@code onpageshow} event handler.
3058      * @return the {@code onpageshow} event handler
3059      */
3060     @JsxGetter
3061     public Function getOnpageshow() {
3062         return getEventHandler(Event.TYPE_PAGESHOW);
3063     }
3064 
3065     /**
3066      * Sets the {@code onpageshow} event handler.
3067      * @param onpageshow the {@code onpageshow} event handler
3068      */
3069     @JsxSetter
3070     public void setOnpageshow(final Object onpageshow) {
3071         setHandlerForJavaScript(Event.TYPE_PAGESHOW, onpageshow);
3072     }
3073 
3074     /**
3075      * Returns the {@code onmozfullscreenchange} event handler.
3076      * @return the {@code onmozfullscreenchange} event handler
3077      */
3078     @JsxGetter({FF, FF_ESR})
3079     public Function getOnmozfullscreenchange() {
3080         return getEventHandler(Event.TYPE_MOZFULLSCREENCHANGE);
3081     }
3082 
3083     /**
3084      * Sets the {@code onmozfullscreenchange} event handler.
3085      * @param onmozfullscreenchange the {@code onmozfullscreenchange} event handler
3086      */
3087     @JsxSetter({FF, FF_ESR})
3088     public void setOnmozfullscreenchange(final Object onmozfullscreenchange) {
3089         setHandlerForJavaScript(Event.TYPE_MOZFULLSCREENCHANGE, onmozfullscreenchange);
3090     }
3091 
3092     /**
3093      * Returns the {@code ondurationchange} event handler.
3094      * @return the {@code ondurationchange} event handler
3095      */
3096     @JsxGetter
3097     public Function getOndurationchange() {
3098         return getEventHandler(Event.TYPE_DURATIONCHANGE);
3099     }
3100 
3101     /**
3102      * Sets the {@code ondurationchange} event handler.
3103      * @param ondurationchange the {@code ondurationchange} event handler
3104      */
3105     @JsxSetter
3106     public void setOndurationchange(final Object ondurationchange) {
3107         setHandlerForJavaScript(Event.TYPE_DURATIONCHANGE, ondurationchange);
3108     }
3109 
3110     /**
3111      * Returns the {@code onplaying} event handler.
3112      * @return the {@code onplaying} event handler
3113      */
3114     @JsxGetter
3115     public Function getOnplaying() {
3116         return getEventHandler(Event.TYPE_PLAYING);
3117     }
3118 
3119     /**
3120      * Sets the {@code onplaying} event handler.
3121      * @param onplaying the {@code onplaying} event handler
3122      */
3123     @JsxSetter
3124     public void setOnplaying(final Object onplaying) {
3125         setHandlerForJavaScript(Event.TYPE_PLAYING, onplaying);
3126     }
3127 
3128     /**
3129      * Returns the {@code onended} event handler.
3130      * @return the {@code onended} event handler
3131      */
3132     @JsxGetter
3133     public Function getOnended() {
3134         return getEventHandler(Event.TYPE_ENDED);
3135     }
3136 
3137     /**
3138      * Sets the {@code onended} event handler.
3139      * @param onended the {@code onended} event handler
3140      */
3141     @JsxSetter
3142     public void setOnended(final Object onended) {
3143         setHandlerForJavaScript(Event.TYPE_ENDED, onended);
3144     }
3145 
3146     /**
3147      * Returns the {@code onloadeddata} event handler.
3148      * @return the {@code onloadeddata} event handler
3149      */
3150     @JsxGetter
3151     public Function getOnloadeddata() {
3152         return getEventHandler(Event.TYPE_LOADEDDATA);
3153     }
3154 
3155     /**
3156      * Sets the {@code onloadeddata} event handler.
3157      * @param onloadeddata the {@code onloadeddata} event handler
3158      */
3159     @JsxSetter
3160     public void setOnloadeddata(final Object onloadeddata) {
3161         setHandlerForJavaScript(Event.TYPE_LOADEDDATA, onloadeddata);
3162     }
3163 
3164     /**
3165      * Returns the {@code onunhandledrejection} event handler.
3166      * @return the {@code onunhandledrejection} event handler
3167      */
3168     @JsxGetter({CHROME, EDGE})
3169     public Function getOnunhandledrejection() {
3170         return getEventHandler(Event.TYPE_UNHANDLEDREJECTION);
3171     }
3172 
3173     /**
3174      * Sets the {@code onunhandledrejection} event handler.
3175      * @param onunhandledrejection the {@code onunhandledrejection} event handler
3176      */
3177     @JsxSetter({CHROME, EDGE})
3178     public void setOnunhandledrejection(final Object onunhandledrejection) {
3179         setHandlerForJavaScript(Event.TYPE_UNHANDLEDREJECTION, onunhandledrejection);
3180     }
3181 
3182     /**
3183      * Returns the {@code onmouseout} event handler.
3184      * @return the {@code onmouseout} event handler
3185      */
3186     @JsxGetter
3187     public Function getOnmouseout() {
3188         return getEventHandler(MouseEvent.TYPE_MOUSE_OUT);
3189     }
3190 
3191     /**
3192      * Sets the {@code onmouseout} event handler.
3193      * @param onmouseout the {@code onmouseout} event handler
3194      */
3195     @JsxSetter
3196     public void setOnmouseout(final Object onmouseout) {
3197         setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_OUT, onmouseout);
3198     }
3199 
3200     /**
3201      * Returns the {@code onsuspend} event handler.
3202      * @return the {@code onsuspend} event handler
3203      */
3204     @JsxGetter
3205     public Function getOnsuspend() {
3206         return getEventHandler(Event.TYPE_SUSPEND);
3207     }
3208 
3209     /**
3210      * Sets the {@code onsuspend} event handler.
3211      * @param onsuspend the {@code onsuspend} event handler
3212      */
3213     @JsxSetter
3214     public void setOnsuspend(final Object onsuspend) {
3215         setHandlerForJavaScript(Event.TYPE_SUSPEND, onsuspend);
3216     }
3217 
3218     /**
3219      * Returns the {@code onwaiting} event handler.
3220      * @return the {@code onwaiting} event handler
3221      */
3222     @JsxGetter
3223     public Function getOnwaiting() {
3224         return getEventHandler(Event.TYPE_WAITING);
3225     }
3226 
3227     /**
3228      * Sets the {@code onwaiting} event handler.
3229      * @param onwaiting the {@code onwaiting} event handler
3230      */
3231     @JsxSetter
3232     public void setOnwaiting(final Object onwaiting) {
3233         setHandlerForJavaScript(Event.TYPE_WAITING, onwaiting);
3234     }
3235 
3236     /**
3237      * Returns the {@code oncanplay} event handler.
3238      * @return the {@code oncanplay} event handler
3239      */
3240     @JsxGetter
3241     public Function getOncanplay() {
3242         return getEventHandler(Event.TYPE_CANPLAY);
3243     }
3244 
3245     /**
3246      * Sets the {@code oncanplay} event handler.
3247      * @param oncanplay the {@code oncanplay} event handler
3248      */
3249     @JsxSetter
3250     public void setOncanplay(final Object oncanplay) {
3251         setHandlerForJavaScript(Event.TYPE_CANPLAY, oncanplay);
3252     }
3253 
3254     /**
3255      * Returns the {@code onmousedown} event handler.
3256      * @return the {@code onmousedown} event handler
3257      */
3258     @JsxGetter
3259     public Function getOnmousedown() {
3260         return getEventHandler(MouseEvent.TYPE_MOUSE_DOWN);
3261     }
3262 
3263     /**
3264      * Sets the {@code onmousedown} event handler.
3265      * @param onmousedown the {@code onmousedown} event handler
3266      */
3267     @JsxSetter
3268     public void setOnmousedown(final Object onmousedown) {
3269         setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_DOWN, onmousedown);
3270     }
3271 
3272     /**
3273      * Returns the {@code onlanguagechange} event handler.
3274      * @return the {@code onlanguagechange} event handler
3275      */
3276     @JsxGetter
3277     public Function getOnlanguagechange() {
3278         return getEventHandler(Event.TYPE_LANGUAGECHANGE);
3279     }
3280 
3281     /**
3282      * Sets the {@code onlanguagechange} event handler.
3283      * @param onlanguagechange the {@code onlanguagechange} event handler
3284      */
3285     @JsxSetter
3286     public void setOnlanguagechange(final Object onlanguagechange) {
3287         setHandlerForJavaScript(Event.TYPE_LANGUAGECHANGE, onlanguagechange);
3288     }
3289 
3290     /**
3291      * Returns the {@code onemptied} event handler.
3292      * @return the {@code onemptied} event handler
3293      */
3294     @JsxGetter
3295     public Function getOnemptied() {
3296         return getEventHandler(Event.TYPE_EMPTIED);
3297     }
3298 
3299     /**
3300      * Sets the {@code onemptied} event handler.
3301      * @param onemptied the {@code onemptied} event handler
3302      */
3303     @JsxSetter
3304     public void setOnemptied(final Object onemptied) {
3305         setHandlerForJavaScript(Event.TYPE_EMPTIED, onemptied);
3306     }
3307 
3308     /**
3309      * Returns the {@code onrejectionhandled} event handler.
3310      * @return the {@code onrejectionhandled} event handler
3311      */
3312     @JsxGetter({CHROME, EDGE})
3313     public Function getOnrejectionhandled() {
3314         return getEventHandler(Event.TYPE_REJECTIONHANDLED);
3315     }
3316 
3317     /**
3318      * Sets the {@code onrejectionhandled} event handler.
3319      * @param onrejectionhandled the {@code onrejectionhandled} event handler
3320      */
3321     @JsxSetter({CHROME, EDGE})
3322     public void setOnrejectionhandled(final Object onrejectionhandled) {
3323         setHandlerForJavaScript(Event.TYPE_REJECTIONHANDLED, onrejectionhandled);
3324     }
3325 
3326     /**
3327      * Returns the {@code onpointercancel} event handler.
3328      * @return the {@code onpointercancel} event handler
3329      */
3330     @JsxGetter({CHROME, EDGE})
3331     public Function getOnpointercancel() {
3332         return getEventHandler(Event.TYPE_POINTERCANCEL);
3333     }
3334 
3335     /**
3336      * Sets the {@code onpointercancel} event handler.
3337      * @param onpointercancel the {@code onpointercancel} event handler
3338      */
3339     @JsxSetter({CHROME, EDGE})
3340     public void setOnpointercancel(final Object onpointercancel) {
3341         setHandlerForJavaScript(Event.TYPE_POINTERCANCEL, onpointercancel);
3342     }
3343 
3344     /**
3345      * Returns the {@code onresize} event handler.
3346      * @return the {@code onresize} event handler
3347      */
3348     @JsxGetter
3349     public Function getOnresize() {
3350         return getEventHandler(Event.TYPE_RESIZE);
3351     }
3352 
3353     /**
3354      * Sets the {@code onresize} event handler.
3355      * @param onresize the {@code onresize} event handler
3356      */
3357     @JsxSetter
3358     public void setOnresize(final Object onresize) {
3359         setHandlerForJavaScript(Event.TYPE_RESIZE, onresize);
3360     }
3361 
3362     /**
3363      * Returns the {@code onpause} event handler.
3364      * @return the {@code onpause} event handler
3365      */
3366     @JsxGetter
3367     public Function getOnpause() {
3368         return getEventHandler(Event.TYPE_PAUSE);
3369     }
3370 
3371     /**
3372      * Sets the {@code onpause} event handler.
3373      * @param onpause the {@code onpause} event handler
3374      */
3375     @JsxSetter
3376     public void setOnpause(final Object onpause) {
3377         setHandlerForJavaScript(Event.TYPE_PAUSE, onpause);
3378     }
3379 
3380     /**
3381      * Returns the {@code onloadstart} event handler.
3382      * @return the {@code onloadstart} event handler
3383      */
3384     @JsxGetter
3385     public Function getOnloadstart() {
3386         return getEventHandler(Event.TYPE_LOAD_START);
3387     }
3388 
3389     /**
3390      * Sets the {@code onloadstart} event handler.
3391      * @param onloadstart the {@code onloadstart} event handler
3392      */
3393     @JsxSetter
3394     public void setOnloadstart(final Object onloadstart) {
3395         setHandlerForJavaScript(Event.TYPE_LOAD_START, onloadstart);
3396     }
3397 
3398     /**
3399      * Returns the {@code onprogress} event handler.
3400      * @return the {@code onprogress} event handler
3401      */
3402     @JsxGetter
3403     public Function getOnprogress() {
3404         return getEventHandler(Event.TYPE_PROGRESS);
3405     }
3406 
3407     /**
3408      * Sets the {@code onprogress} event handler.
3409      * @param onprogress the {@code onprogress} event handler
3410      */
3411     @JsxSetter
3412     public void setOnprogress(final Object onprogress) {
3413         setHandlerForJavaScript(Event.TYPE_PROGRESS, onprogress);
3414     }
3415 
3416     /**
3417      * Returns the {@code onpointerup} event handler.
3418      * @return the {@code onpointerup} event handler
3419      */
3420     @JsxGetter({CHROME, EDGE})
3421     public Function getOnpointerup() {
3422         return getEventHandler(Event.TYPE_POINTERUP);
3423     }
3424 
3425     /**
3426      * Sets the {@code onpointerup} event handler.
3427      * @param onpointerup the {@code onpointerup} event handler
3428      */
3429     @JsxSetter({CHROME, EDGE})
3430     public void setOnpointerup(final Object onpointerup) {
3431         setHandlerForJavaScript(Event.TYPE_POINTERUP, onpointerup);
3432     }
3433 
3434     /**
3435      * Returns the {@code onwheel} event handler.
3436      * @return the {@code onwheel} event handler
3437      */
3438     @JsxGetter
3439     public Function getOnwheel() {
3440         return getEventHandler(Event.TYPE_WHEEL);
3441     }
3442 
3443     /**
3444      * Sets the {@code onwheel} event handler.
3445      * @param onwheel the {@code onwheel} event handler
3446      */
3447     @JsxSetter
3448     public void setOnwheel(final Object onwheel) {
3449         setHandlerForJavaScript(Event.TYPE_WHEEL, onwheel);
3450     }
3451 
3452     /**
3453      * Returns the {@code onpointerleave} event handler.
3454      * @return the {@code onpointerleave} event handler
3455      */
3456     @JsxGetter({CHROME, EDGE})
3457     public Function getOnpointerleave() {
3458         return getEventHandler(Event.TYPE_POINTERLEAVE);
3459     }
3460 
3461     /**
3462      * Sets the {@code onpointerleave} event handler.
3463      * @param onpointerleave the {@code onpointerleave} event handler
3464      */
3465     @JsxSetter({CHROME, EDGE})
3466     public void setOnpointerleave(final Object onpointerleave) {
3467         setHandlerForJavaScript(Event.TYPE_POINTERLEAVE, onpointerleave);
3468     }
3469 
3470     /**
3471      * Returns the {@code onbeforeprint} event handler.
3472      * @return the {@code onbeforeprint} event handler
3473      */
3474     @JsxGetter({FF, FF_ESR})
3475     public Function getOnbeforeprint() {
3476         return getEventHandler(Event.TYPE_BEFOREPRINT);
3477     }
3478 
3479     /**
3480      * Sets the {@code onbeforeprint} event handler.
3481      * @param onbeforeprint the {@code onbeforeprint} event handler
3482      */
3483     @JsxSetter({FF, FF_ESR})
3484     public void setOnbeforeprint(final Object onbeforeprint) {
3485         setHandlerForJavaScript(Event.TYPE_BEFOREPRINT, onbeforeprint);
3486     }
3487 
3488     /**
3489      * Returns the {@code onstorage} event handler.
3490      * @return the {@code onstorage} event handler
3491      */
3492     @JsxGetter
3493     public Function getOnstorage() {
3494         return getEventHandler(Event.TYPE_STORAGE);
3495     }
3496 
3497     /**
3498      * Sets the {@code onstorage} event handler.
3499      * @param onstorage the {@code onstorage} event handler
3500      */
3501     @JsxSetter
3502     public void setOnstorage(final Object onstorage) {
3503         setHandlerForJavaScript(Event.TYPE_STORAGE, onstorage);
3504     }
3505 
3506     /**
3507      * Returns the {@code onanimationstart} event handler.
3508      * @return the {@code onanimationstart} event handler
3509      */
3510     @JsxGetter
3511     public Function getOnanimationstart() {
3512         return getEventHandler(Event.TYPE_ANIMATIONSTART);
3513     }
3514 
3515     /**
3516      * Sets the {@code onanimationstart} event handler.
3517      * @param onanimationstart the {@code onanimationstart} event handler
3518      */
3519     @JsxSetter
3520     public void setOnanimationstart(final Object onanimationstart) {
3521         setHandlerForJavaScript(Event.TYPE_ANIMATIONSTART, onanimationstart);
3522     }
3523 
3524     /**
3525      * Returns the {@code ontimeupdate} event handler.
3526      * @return the {@code ontimeupdate} event handler
3527      */
3528     @JsxGetter
3529     public Function getOntimeupdate() {
3530         return getEventHandler(Event.TYPE_TIMEUPDATE);
3531     }
3532 
3533     /**
3534      * Sets the {@code ontimeupdate} event handler.
3535      * @param ontimeupdate the {@code ontimeupdate} event handler
3536      */
3537     @JsxSetter
3538     public void setOntimeupdate(final Object ontimeupdate) {
3539         setHandlerForJavaScript(Event.TYPE_TIMEUPDATE, ontimeupdate);
3540     }
3541 
3542     /**
3543      * Returns the {@code onpagehide} event handler.
3544      * @return the {@code onpagehide} event handler
3545      */
3546     @JsxGetter
3547     public Function getOnpagehide() {
3548         return getEventHandler(Event.TYPE_PAGEHIDE);
3549     }
3550 
3551     /**
3552      * Sets the {@code onpagehide} event handler.
3553      * @param onpagehide the {@code onpagehide} event handler
3554      */
3555     @JsxSetter
3556     public void setOnpagehide(final Object onpagehide) {
3557         setHandlerForJavaScript(Event.TYPE_PAGEHIDE, onpagehide);
3558     }
3559 
3560     /**
3561      * Returns the {@code onwebkitanimationiteration} event handler.
3562      * @return the {@code onwebkitanimationiteration} event handler
3563      */
3564     @JsxGetter({CHROME, EDGE})
3565     public Function getOnwebkitanimationiteration() {
3566         return getEventHandler(Event.TYPE_WEBKITANIMATIONITERATION);
3567     }
3568 
3569     /**
3570      * Sets the {@code onwebkitanimationiteration} event handler.
3571      * @param onwebkitanimationiteration the {@code onwebkitanimationiteration} event handler
3572      */
3573     @JsxSetter({CHROME, EDGE})
3574     public void setOnwebkitanimationiteration(final Object onwebkitanimationiteration) {
3575         setHandlerForJavaScript(Event.TYPE_WEBKITANIMATIONITERATION, onwebkitanimationiteration);
3576     }
3577 
3578     /**
3579      * Returns the {@code onabort} event handler.
3580      * @return the {@code onabort} event handler
3581      */
3582     @JsxGetter
3583     public Function getOnabort() {
3584         return getEventHandler(Event.TYPE_ABORT);
3585     }
3586 
3587     /**
3588      * Sets the {@code onabort} event handler.
3589      * @param onabort the {@code onabort} event handler
3590      */
3591     @JsxSetter
3592     public void setOnabort(final Object onabort) {
3593         setHandlerForJavaScript(Event.TYPE_ABORT, onabort);
3594     }
3595 
3596     /**
3597      * Returns the {@code onloadedmetadata} event handler.
3598      * @return the {@code onloadedmetadata} event handler
3599      */
3600     @JsxGetter
3601     public Function getOnloadedmetadata() {
3602         return getEventHandler(Event.TYPE_LOADEDMETADATA);
3603     }
3604 
3605     /**
3606      * Sets the {@code onloadedmetadata} event handler.
3607      * @param onloadedmetadata the {@code onloadedmetadata} event handler
3608      */
3609     @JsxSetter
3610     public void setOnloadedmetadata(final Object onloadedmetadata) {
3611         setHandlerForJavaScript(Event.TYPE_LOADEDMETADATA, onloadedmetadata);
3612     }
3613 
3614     /**
3615      * Returns the {@code onmouseup} event handler.
3616      * @return the {@code onmouseup} event handler
3617      */
3618     @JsxGetter
3619     public Function getOnmouseup() {
3620         return getEventHandler(MouseEvent.TYPE_MOUSE_UP);
3621     }
3622 
3623     /**
3624      * Sets the {@code onmouseup} event handler.
3625      * @param onmouseup the {@code onmouseup} event handler
3626      */
3627     @JsxSetter
3628     public void setOnmouseup(final Object onmouseup) {
3629         setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_UP, onmouseup);
3630     }
3631 
3632     /**
3633      * Returns the {@code ondragover} event handler.
3634      * @return the {@code ondragover} event handler
3635      */
3636     @JsxGetter
3637     public Function getOndragover() {
3638         return getEventHandler(Event.TYPE_DRAGOVER);
3639     }
3640 
3641     /**
3642      * Sets the {@code ondragover} event handler.
3643      * @param ondragover the {@code ondragover} event handler
3644      */
3645     @JsxSetter
3646     public void setOndragover(final Object ondragover) {
3647         setHandlerForJavaScript(Event.TYPE_DRAGOVER, ondragover);
3648     }
3649 
3650     /**
3651      * Returns the {@code ononline} event handler.
3652      * @return the {@code ononline} event handler
3653      */
3654     @JsxGetter
3655     public Function getOnonline() {
3656         return getEventHandler(Event.TYPE_ONLINE);
3657     }
3658 
3659     /**
3660      * Sets the {@code ononline} event handler.
3661      * @param ononline the {@code ononline} event handler
3662      */
3663     @JsxSetter
3664     public void setOnonline(final Object ononline) {
3665         setHandlerForJavaScript(Event.TYPE_ONLINE, ononline);
3666     }
3667 
3668     /**
3669      * Returns the {@code onsearch} event handler.
3670      * @return the {@code onsearch} event handler
3671      */
3672     @JsxGetter({CHROME, EDGE})
3673     public Function getOnsearch() {
3674         return getEventHandler(Event.TYPE_SEARCH);
3675     }
3676 
3677     /**
3678      * Sets the {@code onsearch} event handler.
3679      * @param onsearch the {@code onsearch} event handler
3680      */
3681     @JsxSetter({CHROME, EDGE})
3682     public void setOnsearch(final Object onsearch) {
3683         setHandlerForJavaScript(Event.TYPE_SEARCH, onsearch);
3684     }
3685 
3686     /**
3687      * Returns the {@code oninput} event handler.
3688      * @return the {@code oninput} event handler
3689      */
3690     @JsxGetter
3691     public Function getOninput() {
3692         return getEventHandler(Event.TYPE_INPUT);
3693     }
3694 
3695     /**
3696      * Sets the {@code oninput} event handler.
3697      * @param oninput the {@code oninput} event handler
3698      */
3699     @JsxSetter
3700     public void setOninput(final Object oninput) {
3701         setHandlerForJavaScript(Event.TYPE_INPUT, oninput);
3702     }
3703 
3704     /**
3705      * Returns the {@code onwebkittransitionend} event handler.
3706      * @return the {@code onwebkittransitionend} event handler
3707      */
3708     @JsxGetter({CHROME, EDGE})
3709     public Function getOnwebkittransitionend() {
3710         return getEventHandler(Event.TYPE_WEBKITTRANSITIONEND);
3711     }
3712 
3713     /**
3714      * Sets the {@code onwebkittransitionend} event handler.
3715      * @param onwebkittransitionend the {@code onwebkittransitionend} event handler
3716      */
3717     @JsxSetter({CHROME, EDGE})
3718     public void setOnwebkittransitionend(final Object onwebkittransitionend) {
3719         setHandlerForJavaScript(Event.TYPE_WEBKITTRANSITIONEND, onwebkittransitionend);
3720     }
3721 
3722     /**
3723      * Returns the {@code ondevicemotion} event handler.
3724      * @return the {@code ondevicemotion} event handler
3725      */
3726     @JsxGetter
3727     public Function getOndevicemotion() {
3728         return getEventHandler(Event.TYPE_DEVICEMOTION);
3729     }
3730 
3731     /**
3732      * Sets the {@code ondevicemotion} event handler.
3733      * @param ondevicemotion the {@code ondevicemotion} event handler
3734      */
3735     @JsxSetter
3736     public void setOndevicemotion(final Object ondevicemotion) {
3737         setHandlerForJavaScript(Event.TYPE_DEVICEMOTION, ondevicemotion);
3738     }
3739 
3740     /**
3741      * Returns the {@code onstalled} event handler.
3742      * @return the {@code onstalled} event handler
3743      */
3744     @JsxGetter
3745     public Function getOnstalled() {
3746         return getEventHandler(Event.TYPE_STALLED);
3747     }
3748 
3749     /**
3750      * Sets the {@code onstalled} event handler.
3751      * @param onstalled the {@code onstalled} event handler
3752      */
3753     @JsxSetter
3754     public void setOnstalled(final Object onstalled) {
3755         setHandlerForJavaScript(Event.TYPE_STALLED, onstalled);
3756     }
3757 
3758     /**
3759      * Returns the {@code onmouseenter} event handler.
3760      * @return the {@code onmouseenter} event handler
3761      */
3762     @JsxGetter
3763     public Function getOnmouseenter() {
3764         return getEventHandler(Event.TYPE_MOUDEENTER);
3765     }
3766 
3767     /**
3768      * Sets the {@code onmouseenter} event handler.
3769      * @param onmouseenter the {@code onmouseenter} event handler
3770      */
3771     @JsxSetter
3772     public void setOnmouseenter(final Object onmouseenter) {
3773         setHandlerForJavaScript(Event.TYPE_MOUDEENTER, onmouseenter);
3774     }
3775 
3776     /**
3777      * Returns the {@code ondragleave} event handler.
3778      * @return the {@code ondragleave} event handler
3779      */
3780     @JsxGetter
3781     public Function getOndragleave() {
3782         return getEventHandler(Event.TYPE_DRAGLEAVE);
3783     }
3784 
3785     /**
3786      * Sets the {@code ondragleave} event handler.
3787      * @param ondragleave the {@code ondragleave} event handler
3788      */
3789     @JsxSetter
3790     public void setOndragleave(final Object ondragleave) {
3791         setHandlerForJavaScript(Event.TYPE_DRAGLEAVE, ondragleave);
3792     }
3793 
3794     /**
3795      * Returns the {@code onpointerdown} event handler.
3796      * @return the {@code onpointerdown} event handler
3797      */
3798     @JsxGetter({CHROME, EDGE})
3799     public Function getOnpointerdown() {
3800         return getEventHandler(Event.TYPE_POINTERDOWN);
3801     }
3802 
3803     /**
3804      * Sets the {@code onpointerdown} event handler.
3805      * @param onpointerdown the {@code onpointerdown} event handler
3806      */
3807     @JsxSetter({CHROME, EDGE})
3808     public void setOnpointerdown(final Object onpointerdown) {
3809         setHandlerForJavaScript(Event.TYPE_POINTERDOWN, onpointerdown);
3810     }
3811 
3812     /**
3813      * Returns the {@code ondrop} event handler.
3814      * @return the {@code ondrop} event handler
3815      */
3816     @JsxGetter
3817     public Function getOndrop() {
3818         return getEventHandler(Event.TYPE_DROP);
3819     }
3820 
3821     /**
3822      * Sets the {@code ondrop} event handler.
3823      * @param ondrop the {@code ondrop} event handler
3824      */
3825     @JsxSetter
3826     public void setOndrop(final Object ondrop) {
3827         setHandlerForJavaScript(Event.TYPE_DROP, ondrop);
3828     }
3829 
3830     /**
3831      * Returns the {@code onunload} event handler.
3832      * @return the {@code onunload} event handler
3833      */
3834     @JsxGetter
3835     public Function getOnunload() {
3836         return getEventHandler(Event.TYPE_UNLOAD);
3837     }
3838 
3839     /**
3840      * Sets the {@code onunload} event handler.
3841      * @param onunload the {@code onunload} event handler
3842      */
3843     @JsxSetter
3844     public void setOnunload(final Object onunload) {
3845         setHandlerForJavaScript(Event.TYPE_UNLOAD, onunload);
3846     }
3847 
3848     /**
3849      * Returns the {@code onwebkitanimationend} event handler.
3850      * @return the {@code onwebkitanimationend} event handler
3851      */
3852     @JsxGetter({CHROME, EDGE})
3853     public Function getOnwebkitanimationend() {
3854         return getEventHandler(Event.TYPE_WEBKITANIMATIONEND);
3855     }
3856 
3857     /**
3858      * Sets the {@code onwebkitanimationend} event handler.
3859      * @param onwebkitanimationend the {@code onwebkitanimationend} event handler
3860      */
3861     @JsxSetter({CHROME, EDGE})
3862     public void setOnwebkitanimationend(final Object onwebkitanimationend) {
3863         setHandlerForJavaScript(Event.TYPE_WEBKITANIMATIONEND, onwebkitanimationend);
3864     }
3865 
3866     /**
3867      * Returns the {@code ondragstart} event handler.
3868      * @return the {@code ondragstart} event handler
3869      */
3870     @JsxGetter
3871     public Function getOndragstart() {
3872         return getEventHandler(Event.TYPE_DRAGSTART);
3873     }
3874 
3875     /**
3876      * Sets the {@code ondragstart} event handler.
3877      * @param ondragstart the {@code ondragstart} event handler
3878      */
3879     @JsxSetter
3880     public void setOndragstart(final Object ondragstart) {
3881         setHandlerForJavaScript(Event.TYPE_DRAGSTART, ondragstart);
3882     }
3883 
3884     /**
3885      * Returns the {@code ontransitionend} event handler.
3886      * @return the {@code ontransitionend} event handler
3887      */
3888     @JsxGetter({CHROME, EDGE})
3889     public Function getOntransitionend() {
3890         return getEventHandler(Event.TYPE_TRANSITIONEND);
3891     }
3892 
3893     /**
3894      * Sets the {@code ontransitionend} event handler.
3895      * @param ontransitionend the {@code ontransitionend} event handler
3896      */
3897     @JsxSetter({CHROME, EDGE})
3898     public void setOntransitionend(final Object ontransitionend) {
3899         setHandlerForJavaScript(Event.TYPE_TRANSITIONEND, ontransitionend);
3900     }
3901 
3902     /**
3903      * Returns the {@code ondeviceorientationabsolute} event handler.
3904      * @return the {@code ondeviceorientationabsolute} event handler
3905      */
3906     @JsxGetter({CHROME, EDGE})
3907     public Function getOndeviceorientationabsolute() {
3908         return getEventHandler(Event.TYPE_DEVICEORIENTATIONABSOLUTE);
3909     }
3910 
3911     /**
3912      * Sets the {@code ondeviceorientationabsolute} event handler.
3913      * @param ondeviceorientationabsolute the {@code ondeviceorientationabsolute} event handler
3914      */
3915     @JsxSetter({CHROME, EDGE})
3916     public void setOndeviceorientationabsolute(final Object ondeviceorientationabsolute) {
3917         setHandlerForJavaScript(Event.TYPE_DEVICEORIENTATIONABSOLUTE, ondeviceorientationabsolute);
3918     }
3919 
3920     /**
3921      * Returns the {@code onvolumechange} event handler.
3922      * @return the {@code onvolumechange} event handler
3923      */
3924     @JsxGetter
3925     public Function getOnvolumechange() {
3926         return getEventHandler(Event.TYPE_VOLUMECHANGE);
3927     }
3928 
3929     /**
3930      * Sets the {@code onvolumechange} event handler.
3931      * @param onvolumechange the {@code onvolumechange} event handler
3932      */
3933     @JsxSetter
3934     public void setOnvolumechange(final Object onvolumechange) {
3935         setHandlerForJavaScript(Event.TYPE_VOLUMECHANGE, onvolumechange);
3936     }
3937 
3938     /**
3939      * Returns the {@code ongotpointercapture} event handler.
3940      * @return the {@code ongotpointercapture} event handler
3941      */
3942     @JsxGetter({CHROME, EDGE})
3943     public Function getOngotpointercapture() {
3944         return getEventHandler(Event.TYPE_GOTPOINTERCAPTURE);
3945     }
3946 
3947     /**
3948      * Sets the {@code ongotpointercapture} event handler.
3949      * @param ongotpointercapture the {@code ongotpointercapture} event handler
3950      */
3951     @JsxSetter({CHROME, EDGE})
3952     public void setOngotpointercapture(final Object ongotpointercapture) {
3953         setHandlerForJavaScript(Event.TYPE_GOTPOINTERCAPTURE, ongotpointercapture);
3954     }
3955 
3956     /**
3957      * Returns the {@code onpopstate} event handler.
3958      * @return the {@code onpopstate} event handler
3959      */
3960     @JsxGetter
3961     public Function getOnpopstate() {
3962         return getEventHandler(Event.TYPE_POPSTATE);
3963     }
3964 
3965     /**
3966      * Sets the {@code onpopstate} event handler.
3967      * @param onpopstate the {@code onpopstate} event handler
3968      */
3969     @JsxSetter
3970     public void setOnpopstate(final Object onpopstate) {
3971         setHandlerForJavaScript(Event.TYPE_POPSTATE, onpopstate);
3972     }
3973 
3974     /**
3975      * {@inheritDoc}
3976      * optimized version
3977      */
3978     @Override
3979     public BrowserVersion getBrowserVersion() {
3980         return getWebWindow().getWebClient().getBrowserVersion();
3981     }
3982 
3983     @Override
3984     public void put(final String name, final Scriptable start, final Object value) {
3985         // see https://dom.spec.whatwg.org/#window-current-event
3986         // because event is replaceable we need this hack here
3987         if ("event".equals(name)) {
3988             final Slot slot = querySlot(Context.getCurrentContext(), "event");
3989             if (slot instanceof AccessorSlot) {
3990                 delete("event");
3991             }
3992         }
3993         super.put(name, start, value);
3994     }
3995 
3996     /**
3997      * Returns a boolean indicating whether the current context is secure (true) or not (false).
3998      * @return a boolean indicating whether the current context is secure (true) or not (false).
3999      */
4000     @JsxGetter
4001     public boolean isIsSecureContext() {
4002         final Page page = getWebWindow().getEnclosedPage();
4003         if (page instanceof HtmlPage htmlPage) {
4004             // about:blank and about:srcdoc documents are judged by the url of their creator
4005             return UrlUtils.isPotentiallyTrustworthy(htmlPage.getOriginUrl());
4006         }
4007         return page != null && UrlUtils.isPotentiallyTrustworthy(page.getUrl());
4008     }
4009 }
4010 
4011 class HTMLCollectionFrames extends HTMLCollection {
4012     private static final Log LOG = LogFactory.getLog(HTMLCollectionFrames.class);
4013 
4014     HTMLCollectionFrames(final HtmlPage page) {
4015         super(page, false);
4016         this.setIsMatchingPredicate((Predicate<DomNode> & Serializable) node -> node instanceof BaseFrameElement);
4017     }
4018 
4019     @Override
4020     protected Scriptable getScriptableForElement(final Object obj) {
4021         final WebWindow window;
4022         if (obj instanceof BaseFrameElement element) {
4023             window = element.getEnclosedWindow();
4024         }
4025         else {
4026             window = ((FrameWindow) obj).getFrameElement().getEnclosedWindow();
4027         }
4028 
4029         return window.getScriptableObject();
4030     }
4031 
4032     @Override
4033     protected Object getWithPreemption(final String name) {
4034         final List<DomNode> elements = getElements();
4035 
4036         for (final Object next : elements) {
4037             final BaseFrameElement frameElt = (BaseFrameElement) next;
4038             final WebWindow window = frameElt.getEnclosedWindow();
4039             if (name.equals(window.getName())) {
4040                 if (LOG.isDebugEnabled()) {
4041                     LOG.debug("Property \"" + name + "\" evaluated (by name) to " + window);
4042                 }
4043                 return getScriptableForElement(window);
4044             }
4045         }
4046 
4047         return NOT_FOUND;
4048     }
4049 }