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.dom;
16  
17  import static org.htmlunit.BrowserVersionFeatures.JS_DOCUMENT_EVALUATE_RECREATES_RESULT;
18  import static org.htmlunit.BrowserVersionFeatures.JS_DOCUMENT_SELECTION_RANGE_COUNT;
19  import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
20  import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
21  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
22  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
23  
24  import java.io.IOException;
25  import java.io.Serializable;
26  import java.lang.reflect.InvocationTargetException;
27  import java.net.URL;
28  import java.time.ZoneId;
29  import java.time.format.DateTimeFormatter;
30  import java.util.ArrayList;
31  import java.util.Date;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Locale;
35  import java.util.Map;
36  import java.util.Set;
37  import java.util.function.Predicate;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.htmlunit.HttpHeader;
42  import org.htmlunit.Page;
43  import org.htmlunit.SgmlPage;
44  import org.htmlunit.StringWebResponse;
45  import org.htmlunit.WebResponse;
46  import org.htmlunit.WebWindow;
47  import org.htmlunit.corejs.javascript.Callable;
48  import org.htmlunit.corejs.javascript.Context;
49  import org.htmlunit.corejs.javascript.Function;
50  import org.htmlunit.corejs.javascript.NativeFunction;
51  import org.htmlunit.corejs.javascript.Scriptable;
52  import org.htmlunit.corejs.javascript.ScriptableObject;
53  import org.htmlunit.corejs.javascript.VarScope;
54  import org.htmlunit.cssparser.parser.CSSException;
55  import org.htmlunit.html.DomComment;
56  import org.htmlunit.html.DomDocumentFragment;
57  import org.htmlunit.html.DomElement;
58  import org.htmlunit.html.DomNode;
59  import org.htmlunit.html.DomText;
60  import org.htmlunit.html.FrameWindow;
61  import org.htmlunit.html.Html;
62  import org.htmlunit.html.HtmlAnchor;
63  import org.htmlunit.html.HtmlArea;
64  import org.htmlunit.html.HtmlAttributeChangeEvent;
65  import org.htmlunit.html.HtmlElement;
66  import org.htmlunit.html.HtmlEmbed;
67  import org.htmlunit.html.HtmlForm;
68  import org.htmlunit.html.HtmlFrameSet;
69  import org.htmlunit.html.HtmlImage;
70  import org.htmlunit.html.HtmlPage;
71  import org.htmlunit.html.HtmlRb;
72  import org.htmlunit.html.HtmlRp;
73  import org.htmlunit.html.HtmlRt;
74  import org.htmlunit.html.HtmlRtc;
75  import org.htmlunit.html.HtmlScript;
76  import org.htmlunit.html.HtmlSvg;
77  import org.htmlunit.html.HtmlUnknownElement;
78  import org.htmlunit.html.UnknownElementFactory;
79  import org.htmlunit.html.XHtmlPage;
80  import org.htmlunit.html.impl.SimpleRange;
81  import org.htmlunit.http.Cookie;
82  import org.htmlunit.http.HttpUtils;
83  import org.htmlunit.httpclient.HtmlUnitBrowserCompatCookieSpec;
84  import org.htmlunit.javascript.HtmlUnitScriptable;
85  import org.htmlunit.javascript.JavaScriptEngine;
86  import org.htmlunit.javascript.configuration.JsxClass;
87  import org.htmlunit.javascript.configuration.JsxConstructor;
88  import org.htmlunit.javascript.configuration.JsxFunction;
89  import org.htmlunit.javascript.configuration.JsxGetter;
90  import org.htmlunit.javascript.configuration.JsxSetter;
91  import org.htmlunit.javascript.configuration.JsxStaticFunction;
92  import org.htmlunit.javascript.host.Element;
93  import org.htmlunit.javascript.host.FontFaceSet;
94  import org.htmlunit.javascript.host.Location;
95  import org.htmlunit.javascript.host.NativeFunctionPrefixResolver;
96  import org.htmlunit.javascript.host.Window;
97  import org.htmlunit.javascript.host.animations.AnimationEvent;
98  import org.htmlunit.javascript.host.css.StyleSheetList;
99  import org.htmlunit.javascript.host.dom.AbstractList.EffectOnCache;
100 import org.htmlunit.javascript.host.event.BeforeUnloadEvent;
101 import org.htmlunit.javascript.host.event.CompositionEvent;
102 import org.htmlunit.javascript.host.event.CustomEvent;
103 import org.htmlunit.javascript.host.event.DeviceMotionEvent;
104 import org.htmlunit.javascript.host.event.DeviceOrientationEvent;
105 import org.htmlunit.javascript.host.event.DragEvent;
106 import org.htmlunit.javascript.host.event.Event;
107 import org.htmlunit.javascript.host.event.FocusEvent;
108 import org.htmlunit.javascript.host.event.HashChangeEvent;
109 import org.htmlunit.javascript.host.event.KeyboardEvent;
110 import org.htmlunit.javascript.host.event.MessageEvent;
111 import org.htmlunit.javascript.host.event.MouseEvent;
112 import org.htmlunit.javascript.host.event.PointerEvent;
113 import org.htmlunit.javascript.host.event.PopStateEvent;
114 import org.htmlunit.javascript.host.event.ProgressEvent;
115 import org.htmlunit.javascript.host.event.TextEvent;
116 import org.htmlunit.javascript.host.event.UIEvent;
117 import org.htmlunit.javascript.host.event.WheelEvent;
118 import org.htmlunit.javascript.host.html.HTMLAllCollection;
119 import org.htmlunit.javascript.host.html.HTMLBodyElement;
120 import org.htmlunit.javascript.host.html.HTMLCollection;
121 import org.htmlunit.javascript.host.html.HTMLElement;
122 import org.htmlunit.javascript.host.html.HTMLFrameSetElement;
123 import org.htmlunit.util.StringUtils;
124 import org.htmlunit.util.UrlUtils;
125 import org.htmlunit.xpath.xml.utils.PrefixResolver;
126 import org.w3c.dom.CDATASection;
127 import org.w3c.dom.DOMException;
128 import org.w3c.dom.DocumentType;
129 import org.w3c.dom.ProcessingInstruction;
130 
131 /**
132  * A JavaScript object for {@code Document}.
133  *
134  * @author Mike Bowler
135  * @author David K. Taylor
136  * @author Chen Jun
137  * @author Christian Sell
138  * @author Chris Erskine
139  * @author Marc Guillemot
140  * @author Daniel Gredler
141  * @author Michael Ottati
142  * @author George Murnock
143  * @author Ahmed Ashour
144  * @author Rob Di Marco
145  * @author Ronald Brill
146  * @author Chuck Dumont
147  * @author Frank Danek
148  * @author Madis Pärn
149  * @author Lai Quang Duong
150  * @author Sven Strickroth
151  *
152  * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document">MDN Documentation</a>
153  * @see <a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-7068919">W3C Dom Level 1</a>
154  */
155 @JsxClass
156 public class Document extends Node {
157 
158     private static final Log LOG = LogFactory.getLog(Document.class);
159 
160     /**
161      * See <a href="https://developer.mozilla.org/en/Rich-Text_Editing_in_Mozilla#Executing_Commands">
162      *     Executing Commands</a>.
163      */
164     private static final Set<String> EXECUTE_CMDS_FF = new HashSet<>();
165     private static final Set<String> EXECUTE_CMDS_CHROME = new HashSet<>();
166     /** The formatter to use for the <code>lastModified</code> attribute. */
167     private static final DateTimeFormatter LAST_MODIFIED_DATE_FORMATTER
168                                             = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
169 
170     /** Contains all supported DOM level 2 events. */
171     private static final Map<String, Class<? extends Event>> SUPPORTED_DOM2_EVENT_TYPE_MAP;
172     /** Contains all supported DOM level 3 events. DOM level 2 events are not included. */
173     private static final Map<String, Class<? extends Event>> SUPPORTED_DOM3_EVENT_TYPE_MAP;
174     /** Contains all supported vendor specific events. */
175     private static final Map<String, Class<? extends Event>> SUPPORTED_VENDOR_EVENT_TYPE_MAP;
176 
177     /*
178       Initializes the supported event type map.
179       Map<String, Class> which maps strings a caller may use when calling into
180       {@link #createEvent(String)} to the associated event class. To support a new
181       event creation type, the event type and associated class need to be added into this map in
182       the static initializer. The map is unmodifiable. Any class that is a value in this map MUST
183       have a no-arg constructor.
184      */
185     static {
186         SUPPORTED_DOM2_EVENT_TYPE_MAP = Map.of(
187                 "HTMLEvents", Event.class,
188                 "MouseEvents", MouseEvent.class,
189                 "UIEvents", UIEvent.class);
190 
191         SUPPORTED_DOM3_EVENT_TYPE_MAP = Map.ofEntries(
192                 Map.entry("Event", Event.class),
193                 Map.entry("KeyboardEvent", KeyboardEvent.class),
194                 Map.entry("MouseEvent", MouseEvent.class),
195                 Map.entry("MessageEvent", MessageEvent.class),
196                 Map.entry("UIEvent", UIEvent.class),
197                 Map.entry("CustomEvent", CustomEvent.class),
198                 Map.entry("CompositionEvent", CompositionEvent.class),
199                 Map.entry("DragEvent", DragEvent.class),
200                 Map.entry("TextEvent", TextEvent.class),
201                 Map.entry("DeviceMotionEvent", DeviceMotionEvent.class),
202                 Map.entry("DeviceOrientationEvent", DeviceOrientationEvent.class));
203 
204         SUPPORTED_VENDOR_EVENT_TYPE_MAP = Map.ofEntries(
205                 Map.entry("BeforeUnloadEvent", BeforeUnloadEvent.class),
206                 Map.entry("Events", Event.class),
207                 Map.entry("HashChangeEvent", HashChangeEvent.class),
208                 Map.entry("KeyEvents", KeyboardEvent.class),
209                 Map.entry("PointerEvent", PointerEvent.class),
210                 Map.entry("PopStateEvent", PopStateEvent.class),
211                 Map.entry("ProgressEvent", ProgressEvent.class),
212                 Map.entry("FocusEvent", FocusEvent.class),
213                 Map.entry("WheelEvent", WheelEvent.class),
214                 Map.entry("AnimationEvent", AnimationEvent.class));
215     }
216 
217     private Window window_;
218     private DOMImplementation implementation_;
219     private String designMode_;
220     private String compatMode_;
221     private int documentMode_ = -1;
222     private String domain_;
223     private String lastModified_;
224     private ScriptableObject currentScript_;
225     private transient FontFaceSet fonts_;
226     private transient StyleSheetList styleSheetList_;
227 
228     static {
229         // commands
230         String[] cmds = {
231             "BackColor", "BackgroundImageCache" /* Undocumented */,
232             "Bold",
233             "CreateLink", "Delete",
234             "FontName", "FontSize", "ForeColor", "FormatBlock",
235             "Indent", "InsertHorizontalRule", "InsertImage",
236             "InsertOrderedList", "InsertParagraph", "InsertUnorderedList",
237             "Italic", "JustifyCenter", "JustifyFull", "JustifyLeft", "JustifyNone",
238             "JustifyRight",
239             "Outdent",
240             "Print",
241             "Redo", "RemoveFormat",
242             "SelectAll", "StrikeThrough", "Subscript", "Superscript",
243             "Underline", "Undo", "Unlink", "Unselect"
244         };
245         for (final String cmd : cmds) {
246             if (!"Bold".equals(cmd)) {
247                 EXECUTE_CMDS_CHROME.add(cmd.toLowerCase(Locale.ROOT));
248             }
249         }
250 
251         cmds = new String[] {
252             "backColor", "bold", "contentReadOnly", "copy", "createLink", "cut", "decreaseFontSize", "delete",
253             "fontName", "fontSize", "foreColor", "formatBlock", "heading", "hiliteColor", "increaseFontSize",
254             "indent", "insertHorizontalRule", "insertHTML", "insertImage", "insertOrderedList", "insertUnorderedList",
255             "insertParagraph", "italic",
256             "justifyCenter", "JustifyFull", "justifyLeft", "justifyRight", "outdent", "paste", "redo",
257             "removeFormat", "selectAll", "strikeThrough", "subscript", "superscript", "underline", "undo", "unlink",
258             "useCSS", "styleWithCSS"
259         };
260         for (final String cmd : cmds) {
261             EXECUTE_CMDS_FF.add(cmd.toLowerCase(Locale.ROOT));
262             if (!"bold".equals(cmd)) {
263                 EXECUTE_CMDS_CHROME.add(cmd.toLowerCase(Locale.ROOT));
264             }
265         }
266     }
267 
268     /**
269      * JavaScript constructor.
270      */
271     @Override
272     @JsxConstructor
273     public void jsConstructor() {
274         throw JavaScriptEngine.typeErrorIllegalConstructor();
275     }
276 
277     /**
278      * Parses the given string of HTML without sanitizing it and returns a new HTMLDocument.
279      *
280      * @param cx the current context
281      * @param scope the scope
282      * @param thisObj the scriptable this object
283      * @param args the arguments
284      * @param funObj the function object
285      * @return a newly created {@link org.htmlunit.javascript.host.html.HTMLDocument}
286      *
287      * @see <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsehtmlunsafe">
288      *     HTML spec - parseHTMLUnsafe</a>
289      */
290     @JsxStaticFunction
291     public static Document parseHTMLUnsafe(final Context cx, final VarScope scope,
292             final Scriptable thisObj, final Object[] args, final Function funObj) {
293         if (args.length < 1) {
294             throw JavaScriptEngine
295                     .typeError("Document.parseHTMLUnsafe: At least 1 argument required, but only 0 passed");
296         }
297 
298         final Window win = getWindow(thisObj);
299         if (JavaScriptEngine.isUndefined(args[0])) {
300             return win.getDocument();
301         }
302 
303         final String html = JavaScriptEngine.toString(args[0]);
304 
305         try {
306             final WebWindow webWindow = win.getWebWindow();
307             final WebResponse webResponse = new StringWebResponse(html, webWindow.getEnclosedPage().getUrl());
308             return DOMParser.parseHtmlDocument(win.getDocument(), webResponse, webWindow);
309         }
310         catch (final IOException e) {
311             throw JavaScriptEngine.syntaxError("Parsing failed" + e.getMessage());
312         }
313     }
314 
315     /**
316      * Sets the Window JavaScript object that encloses this document.
317      * @param window the Window JavaScript object that encloses this document
318      */
319     public void setWindow(final Window window) {
320         window_ = window;
321     }
322 
323     /**
324      * Returns the value of the {@code location} property.
325      * @return the value of the {@code location} property
326      */
327     @JsxGetter
328     public Location getLocation() {
329         if (window_ == null) {
330             return null;
331         }
332         return window_.getLocation();
333     }
334 
335     /**
336      * Sets the value of the {@code location} property. The location's default property is "href",
337      * so setting "document.location='http://www.sf.net'" is equivalent to setting
338      * "document.location.href='http://www.sf.net'".
339      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/location">MDN Documentation</a>
340      * @param location the location to navigate to
341      * @throws IOException when location loading fails
342      */
343     @JsxSetter
344     public void setLocation(final String location) throws IOException {
345         window_.setLocation(location);
346     }
347 
348     /**
349      * Returns the value of the {@code referrer} property.
350      * @return the value of the {@code referrer} property
351      */
352     @JsxGetter
353     public String getReferrer() {
354         String referrer = "";
355         final WebResponse webResponse = getPage().getWebResponse();
356         if (webResponse != null) {
357             referrer = webResponse.getWebRequest().getAdditionalHeaders().get(HttpHeader.REFERER);
358             if (referrer == null) {
359                 referrer = "";
360             }
361         }
362         return referrer;
363     }
364 
365     /**
366      * Gets the JavaScript property {@code documentElement} for the document.
367      * @return the root node for the document
368      */
369     @JsxGetter
370     public Element getDocumentElement() {
371         final Object documentElement = getPage().getDocumentElement();
372         if (documentElement == null) {
373             // for instance with an XML document with parsing error
374             return null;
375         }
376         return (Element) getScriptableFor(documentElement);
377     }
378 
379     /**
380      * Gets the JavaScript property {@code rootElement}.
381      * @return the root node for the document
382      */
383     @JsxGetter
384     public Element getRootElement() {
385         return null;
386     }
387 
388     /**
389      * Gets the JavaScript property {@code doctype} for the document.
390      * @return the DocumentType of the document
391      */
392     @JsxGetter
393     public HtmlUnitScriptable getDoctype() {
394         final Object documentType = getPage().getDoctype();
395         if (documentType == null) {
396             return null;
397         }
398         return getScriptableFor(documentType);
399     }
400 
401     /**
402      * Returns a value which indicates whether or not the document can be edited.
403      * @return a value which indicates whether or not the document can be edited
404      */
405     @JsxGetter
406     public String getDesignMode() {
407         if (designMode_ == null) {
408             designMode_ = "off";
409         }
410         return designMode_;
411     }
412 
413     /**
414      * Sets a value which indicates whether or not the document can be edited.
415      * @param mode a value which indicates whether or not the document can be edited
416      */
417     @JsxSetter
418     public void setDesignMode(final String mode) {
419         if ("on".equalsIgnoreCase(mode)) {
420             designMode_ = "on";
421             final SgmlPage page = getPage();
422             if (page != null && page.isHtmlPage()
423                     && getBrowserVersion().hasFeature(JS_DOCUMENT_SELECTION_RANGE_COUNT)) {
424                 final HtmlPage htmlPage = (HtmlPage) page;
425                 final DomNode child = htmlPage.getBody().getFirstChild();
426                 final DomNode rangeNode = child == null ? htmlPage.getBody() : child;
427                 htmlPage.setSelectionRange(new SimpleRange(rangeNode, 0));
428             }
429         }
430         else if ("off".equalsIgnoreCase(mode)) {
431             designMode_ = "off";
432         }
433     }
434 
435     /**
436      * Returns the page that this document is modeling.
437      * @return the page that this document is modeling
438      */
439     public SgmlPage getPage() {
440         return (SgmlPage) getDomNodeOrDie();
441     }
442 
443     /**
444      * Gets the window in which this document is contained.
445      * @return the window
446      */
447     @JsxGetter
448     public Object getDefaultView() {
449         return getWindow();
450     }
451 
452     /**
453      * Creates a new document fragment.
454      * @return a newly created document fragment
455      */
456     @JsxFunction
457     public HtmlUnitScriptable createDocumentFragment() {
458         final DomDocumentFragment fragment = getDomNodeOrDie().getPage().createDocumentFragment();
459         final DocumentFragment node = new DocumentFragment();
460         node.setParentScope(getParentScope());
461         node.setPrototype(getPrototype(node.getClass()));
462         node.setDomNode(fragment);
463         return getScriptableFor(fragment);
464     }
465 
466     /**
467      * Creates a new HTML attribute with the specified name.
468      *
469      * @param attributeName the name of the attribute to create
470      * @return an attribute with the specified name
471      */
472     @JsxFunction
473     public Attr createAttribute(final String attributeName) {
474         return getPage().createAttribute(attributeName).getScriptableObject();
475     }
476 
477     /**
478      * Imports a node from another document to this document.
479      * The source node is not altered or removed from the original document;
480      * this method creates a new copy of the source node.
481      *
482      * @param importedNode the node to import
483      * @param deep Whether to recursively import the subtree under the specified node; or not
484      * @return the imported node that belongs to this Document
485      */
486     @JsxFunction
487     public HtmlUnitScriptable importNode(final Node importedNode, final boolean deep) {
488         DomNode domNode = importedNode.getDomNodeOrDie();
489         domNode = domNode.cloneNode(deep);
490         domNode.processImportNode(this);
491         for (final DomNode childNode : domNode.getDescendants()) {
492             childNode.processImportNode(this);
493         }
494         return domNode.getScriptableObject();
495     }
496 
497     /**
498      * Adopts a node from an external document.
499      * The node and its subtree are removed from the document it's in (if any),
500      * and its ownerDocument is changed to the current document.
501      * The node can then be inserted into the current document.
502      *
503      * @param externalNode the node from another document to be adopted
504      * @return the adopted node that can be used in the current document
505      */
506     @JsxFunction
507     public HtmlUnitScriptable adoptNode(final Node externalNode) {
508         externalNode.remove();
509 
510         final DomNode domNode = externalNode.getDomNodeOrDie();
511         domNode.processImportNode(this);
512         for (final DomNode childNode : domNode.getDescendants()) {
513             childNode.processImportNode(this);
514         }
515         return domNode.getScriptableObject();
516     }
517 
518     /**
519      * Returns the implementation object of the current document.
520      * @return implementation-specific object
521      */
522     @JsxGetter
523     public DOMImplementation getImplementation() {
524         if (implementation_ == null) {
525             implementation_ = new DOMImplementation();
526             implementation_.setParentScope(getParentScope());
527             implementation_.setPrototype(getPrototype(implementation_.getClass()));
528         }
529         return implementation_;
530     }
531 
532     /**
533      * Adapts any DOM node to resolve namespaces so that an XPath expression can be easily
534      * evaluated relative to the context of the node where it appeared within the document.
535      * @param nodeResolver the node to be used as a context for namespace resolution
536      * @return an XPathNSResolver which resolves namespaces with respect to the definitions
537      *         in scope for a specified node
538      */
539     @JsxFunction
540     public NativeXPathNSResolver createNSResolver(final Node nodeResolver) {
541         final NativeXPathNSResolver resolver = new NativeXPathNSResolver();
542         resolver.setElement(nodeResolver);
543         resolver.setParentScope(getParentScope());
544         resolver.setPrototype(getPrototype(resolver.getClass()));
545         return resolver;
546     }
547 
548     /**
549      * Create a new DOM text node with the given data.
550      *
551      * @param newData the string value for the text node
552      * @return the new text node or NOT_FOUND if there is an error
553      */
554     @JsxFunction
555     public HtmlUnitScriptable createTextNode(final String newData) {
556         final DomNode domNode = new DomText(getDomNodeOrDie().getPage(), newData);
557         return makeScriptableFor(domNode);
558     }
559 
560     /**
561      * Creates a new Comment.
562      * @param comment the comment text
563      * @return the new Comment
564      */
565     @JsxFunction
566     public HtmlUnitScriptable createComment(final String comment) {
567         final DomNode domNode = new DomComment(getDomNodeOrDie().getPage(), comment);
568         return getScriptableFor(domNode);
569     }
570 
571     /**
572      * Evaluates an XPath expression string and returns a result of the specified type if possible.
573      * @param expression the XPath expression string to be parsed and evaluated
574      * @param contextNode the context node for the evaluation of this XPath expression
575      * @param resolver the resolver permits translation of all prefixes, including the XML namespace prefix,
576      *        within the XPath expression into appropriate namespace URIs.
577      * @param type If a specific type is specified, then the result will be returned as the corresponding type
578      * @param result the result object which may be reused and returned by this method
579      * @return the result of the evaluation of the XPath expression
580      */
581     @JsxFunction
582     public XPathResult evaluate(final String expression, final Node contextNode,
583             final Object resolver, final int type, final Object result) {
584         XPathResult xPathResult = null;
585         if (result instanceof XPathResult pathResult) {
586             xPathResult = pathResult;
587 
588             if (getBrowserVersion().hasFeature(JS_DOCUMENT_EVALUATE_RECREATES_RESULT)) {
589                 xPathResult = new XPathResult();
590                 xPathResult.setParentScope(getParentScope());
591                 xPathResult.setPrototype(getPrototype(xPathResult.getClass()));
592             }
593         }
594         else if (result == null
595                 || JavaScriptEngine.isUndefined(result)
596                 || result instanceof ScriptableObject) {
597             xPathResult = new XPathResult();
598             xPathResult.setParentScope(getParentScope());
599             xPathResult.setPrototype(getPrototype(xPathResult.getClass()));
600         }
601         else {
602             throw JavaScriptEngine.typeError("Argument 5 of Document.evaluate has to be an XPathResult or null.");
603         }
604 
605         try {
606             PrefixResolver prefixResolver = null;
607             if (resolver instanceof NativeFunction function) {
608                 prefixResolver = new NativeFunctionPrefixResolver(
609                                             function, contextNode.getParentScope());
610             }
611             else if (resolver instanceof PrefixResolver prefixResolver1) {
612                 prefixResolver = prefixResolver1;
613             }
614             xPathResult.init(contextNode.getDomNodeOrDie().getByXPath(expression, prefixResolver), type);
615             return xPathResult;
616         }
617         catch (final Exception e) {
618             throw JavaScriptEngine.asJavaScriptException(
619                     getWindow(),
620                     "Failed to execute 'evaluate': " + e.getMessage(),
621                     org.htmlunit.javascript.host.dom.DOMException.SYNTAX_ERR);
622         }
623     }
624 
625     /**
626      * Creates a new element with the given tag name.
627      *
628      * @param tagName the tag name
629      * @return the new HTML element, or NOT_FOUND if the tag is not supported
630      */
631     @JsxFunction
632     public HtmlUnitScriptable createElement(final Object tagName) {
633         if (tagName == null || JavaScriptEngine.isUndefined(tagName)) {
634             final org.w3c.dom.Node element = getPage().createElement("unknown");
635             ((HtmlUnknownElement) element).markAsCreatedByJavascript();
636             return getScriptableFor(element);
637         }
638 
639         // https://dom.spec.whatwg.org/#dom-document-createelement
640         // NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D]
641         //                       | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF]
642         //                       | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
643         // NameChar      ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
644         // Name          ::= NameStartChar (NameChar)*
645 
646         // but I have no idea what the browsers are doing
647         // the following code is a wild guess that might be good enough for the moment
648 
649         // April 2026
650         // looks like the browsers using a more simple approach now
651         // see
652         //   org.htmlunit.javascript.host.dom.DocumentTest.documentCreateElementValidTagNames()
653         //   org.htmlunit.javascript.host.dom.DocumentTest.documentCreateElementValidTagNames1000()
654         //   org.htmlunit.javascript.host.dom.DocumentTest.documentCreateElementValidTagNames2000()
655         //   org.htmlunit.javascript.host.dom.DocumentTest.documentCreateElementValidTagNames3000()
656         //   org.htmlunit.javascript.host.dom.DocumentTest.documentCreateElementValidTagNames4000()
657 
658         final String tagNameString = JavaScriptEngine.toString(tagName);
659         if (tagNameString.length() > 0) {
660             final int firstChar = tagNameString.charAt(0);
661             if (firstChar < 128
662                     && !Character.isLetter(firstChar)
663                     && firstChar != ':' && firstChar != '_') {
664                 if (LOG.isInfoEnabled()) {
665                     LOG.info("createElement: Provided string '" + tagNameString + "' contains an invalid character");
666                 }
667                 throw JavaScriptEngine.asJavaScriptException(
668                         getWindow(),
669                         "createElement: Provided string '" + tagNameString + "' contains an invalid character",
670                         org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
671             }
672 
673             final int length = tagNameString.length();
674             for (int i = 1; i < length; i++) {
675                 final int c = tagNameString.charAt(i);
676                 if (c < 128) {
677                     if (c == 0
678                             || c == 9
679                             || c == 10
680                             || c == 12
681                             || c == 13
682                             || c == ' '
683                             || c == '/'
684                             || c == '>') {
685                         if (LOG.isInfoEnabled()) {
686                             LOG.info("createElement: Provided string '"
687                                         + tagNameString + "' contains an invalid character");
688                         }
689                         throw JavaScriptEngine.asJavaScriptException(
690                                 getWindow(),
691                                 "createElement: Provided string '" + tagNameString
692                                     + "' contains an invalid character",
693                                 org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
694                     }
695                 }
696             }
697         }
698 
699         org.w3c.dom.Node element = getPage().createElement(tagNameString);
700 
701         if (element instanceof HtmlImage image) {
702             image.markAsCreatedByJavascript();
703         }
704         else if (element instanceof HtmlRb rb) {
705             rb.markAsCreatedByJavascript();
706         }
707         else if (element instanceof HtmlRp rp) {
708             rp.markAsCreatedByJavascript();
709         }
710         else if (element instanceof HtmlRt rt) {
711             rt.markAsCreatedByJavascript();
712         }
713         else if (element instanceof HtmlRtc rtc) {
714             rtc.markAsCreatedByJavascript();
715         }
716         else if (element instanceof HtmlUnknownElement unknownElement) {
717             unknownElement.markAsCreatedByJavascript();
718         }
719         else if (element instanceof HtmlSvg) {
720             element = UnknownElementFactory.INSTANCE.createElementNS(getPage(), "", "svg", null);
721             ((HtmlUnknownElement) element).markAsCreatedByJavascript();
722         }
723         final HtmlUnitScriptable jsElement = getScriptableFor(element);
724 
725         if (jsElement == NOT_FOUND) {
726             if (LOG.isDebugEnabled()) {
727                 LOG.debug("createElement(" + tagName
728                     + ") cannot return a result as there isn't a JavaScript object for the element "
729                     + element.getClass().getName());
730             }
731         }
732         return jsElement;
733     }
734 
735     /**
736      * Creates a new HTML element with the given tag name, and name.
737      *
738      * @param namespaceURI the URI that identifies an XML namespace
739      * @param qualifiedName the qualified name of the element type to instantiate
740      * @return the new HTML element, or NOT_FOUND if the tag is not supported
741      */
742     @JsxFunction
743     public HtmlUnitScriptable createElementNS(final String namespaceURI, final String qualifiedName) {
744         if ("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul".equals(namespaceURI)) {
745             throw JavaScriptEngine.typeError("XUL not available");
746         }
747 
748         final org.w3c.dom.Element element;
749         if (Html.XHTML_NAMESPACE.equals(namespaceURI)
750                 || Html.SVG_NAMESPACE.equals(namespaceURI)) {
751             element = getPage().createElementNS(namespaceURI, qualifiedName);
752         }
753         else {
754             element = new DomElement(namespaceURI, qualifiedName, getPage(), null);
755         }
756         return getScriptableFor(element);
757     }
758 
759     /**
760      * Returns all the descendant elements with the specified tag name.
761      * @param tagName the name to search for
762      * @return all the descendant elements with the specified tag name
763      */
764     @JsxFunction
765     public HTMLCollection getElementsByTagName(final String tagName) {
766         final HTMLCollection collection = new HTMLCollection(getDomNodeOrDie(), false);
767 
768         if (StringUtils.equalsChar('*', tagName)) {
769             collection.setIsMatchingPredicate((Predicate<DomNode> & Serializable) node -> true);
770         }
771         else {
772             collection.setIsMatchingPredicate(
773                     (Predicate<DomNode> & Serializable)
774                     node -> tagName.equalsIgnoreCase(node.getNodeName()));
775         }
776 
777         return collection;
778     }
779 
780     /**
781      * Returns a list of elements with the given tag name belonging to the given namespace.
782      * @param namespaceURI the namespace URI of elements to look for
783      * @param localName is either the local name of elements to look for or the special value "*",
784      *                  which matches all elements.
785      * @return a live NodeList of found elements in the order they appear in the tree
786      */
787     @JsxFunction
788     public HTMLCollection getElementsByTagNameNS(final Object namespaceURI, final String localName) {
789         final HTMLCollection elements = new HTMLCollection(getDomNodeOrDie(), false);
790         elements.setIsMatchingPredicate(
791                 (Predicate<DomNode> & Serializable)
792                 node -> localName.equals(node.getLocalName()));
793         return elements;
794     }
795 
796     /**
797      * Returns the value of the {@code activeElement} property.
798      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement">MDN Documentation</a>
799      * @return the value of the {@code activeElement} property
800      */
801     @JsxGetter
802     public Object getActiveElement() {
803         return null;
804     }
805 
806     /**
807      * Returns the character encoding of the current document.
808      * @return the character encoding of the current document
809      */
810     @JsxGetter
811     public String getCharacterSet() {
812         if (!(getPage() instanceof HtmlPage)) {
813             // TODO: implement XmlPage.getCharset
814             return "";
815         }
816         return getPage().getCharset().name();
817     }
818 
819     /**
820      * Retrieves the character set used to encode the document.
821      * @return the character set used to encode the document
822      */
823     @JsxGetter
824     public String getCharset() {
825         if (!(getPage() instanceof HtmlPage)) {
826             // TODO: implement XmlPage.getCharset
827             return "";
828         }
829         return getPage().getCharset().name();
830     }
831 
832     /**
833      * Returns the value of the JavaScript property {@code anchors}.
834      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/anchors">MDN Documentation</a>
835      * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_doc_ref4.html#1024543">
836      *     Gecko DOM reference</a>
837      * @return the value of this property
838      */
839     @JsxGetter
840     public HTMLCollection getAnchors() {
841         final HTMLCollection anchors = new HTMLCollection(getDomNodeOrDie(), true);
842 
843         anchors.setIsMatchingPredicate(
844                 (Predicate<DomNode> & Serializable)
845                 node -> {
846                     if (!(node instanceof HtmlAnchor anchor)) {
847                         return false;
848                     }
849                     return anchor.hasAttribute(DomElement.NAME_ATTRIBUTE);
850                 });
851 
852         anchors.setEffectOnCacheFunction(
853                 (java.util.function.Function<HtmlAttributeChangeEvent, EffectOnCache> & Serializable)
854                     event -> {
855                         if (DomElement.NAME_ATTRIBUTE.equals(event.getName())
856                                 || DomElement.ID_ATTRIBUTE.equals(event.getName())) {
857                             return EffectOnCache.RESET;
858                         }
859                         return EffectOnCache.NONE;
860                     });
861 
862         return anchors;
863     }
864 
865     /**
866      * Returns the value of the JavaScript property {@code applets}.
867      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/applets">
868      *     MDN Documentation</a>
869      * @return the value of this property
870      */
871     @JsxGetter
872     public HTMLCollection getApplets() {
873         return new HTMLCollection(getDomNodeOrDie(), false);
874     }
875 
876     /**
877      * Returns this document's {@code body} element.
878      * @return this document's {@code body} element
879      */
880     @JsxGetter
881     public HTMLElement getBody() {
882         final Page page = getPage();
883         if (page instanceof HtmlPage htmlPage) {
884             final HtmlElement body = htmlPage.getBody();
885             if (body != null) {
886                 return body.getScriptableObject();
887             }
888 
889             // strange but this returns the frameset element
890             final DomElement doc = htmlPage.getDocumentElement();
891             if (doc != null) {
892                 for (final DomNode node : doc.getChildren()) {
893                     if (node instanceof HtmlFrameSet) {
894                         return node.getScriptableObject();
895                     }
896                 }
897             }
898         }
899         return null;
900     }
901 
902     /**
903      * Sets the {@code body} element of the document.
904      * @param htmlElement the new html element
905      */
906     @JsxSetter
907     public void setBody(final HTMLElement htmlElement) {
908         if (htmlElement instanceof HTMLBodyElement || htmlElement instanceof HTMLFrameSetElement) {
909             final Page page = getPage();
910             if (page instanceof HtmlPage htmlPage) {
911                 final HtmlElement body = htmlPage.getBody();
912                 if (body != null) {
913                     body.replace(htmlElement.getDomNodeOrDie());
914                 }
915             }
916             return;
917         }
918         throw JavaScriptEngine.asJavaScriptException(
919                 getWindow(),
920                 "Failed to set the 'body' property on 'Document': "
921                         + "The new body element is of type '" +  htmlElement.getTagName() + "'. "
922                         + "It must be either a 'BODY' or 'FRAMESET' element.",
923                 org.htmlunit.javascript.host.dom.DOMException.HIERARCHY_REQUEST_ERR);
924     }
925 
926     /**
927      * JavaScript function {@code close}.
928      * <p>See <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/section-dynamic.html">
929      * http://www.whatwg.org/specs/web-apps/current-work/multipage/section-dynamic.html</a> for
930      * a good description of the semantics of open(), write(), writeln() and close().</p>
931      *
932      * @throws IOException if an IO problem occurs
933      */
934     @JsxFunction({CHROME, EDGE})
935     public void close() throws IOException {
936         // nothing to do
937     }
938 
939     /**
940      * Returns the {@code compatMode} property.
941      * @return the {@code compatMode} property
942      */
943     @JsxGetter
944     public String getCompatMode() {
945         // initialize the modes
946         getDocumentMode();
947         return compatMode_;
948     }
949 
950     /**
951      * Returns the {@code documentMode} property.
952      * @return the {@code documentMode} property
953      */
954     public int getDocumentMode() {
955         if (documentMode_ != -1) {
956             return documentMode_;
957         }
958 
959         compatMode_ = "CSS1Compat";
960 
961         if (isQuirksDocType()) {
962             compatMode_ = "BackCompat";
963         }
964 
965         final float version = getBrowserVersion().getBrowserVersionNumeric();
966         documentMode_ = (int) Math.floor(version);
967         return documentMode_;
968     }
969 
970     private boolean isQuirksDocType() {
971         final DocumentType docType = getPage().getDoctype();
972         if (docType != null) {
973             final String systemId = docType.getSystemId();
974             if (systemId != null) {
975                 if ("http://www.w3.org/TR/html4/strict.dtd".equals(systemId)) {
976                     return false;
977                 }
978 
979                 if ("http://www.w3.org/TR/html4/loose.dtd".equals(systemId)) {
980                     final String publicId = docType.getPublicId();
981                     if ("-//W3C//DTD HTML 4.01 Transitional//EN".equals(publicId)) {
982                         return false;
983                     }
984                 }
985 
986                 if ("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd".equals(systemId)
987                     || "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd".equals(systemId)) {
988                     return false;
989                 }
990             }
991             else if (docType.getPublicId() == null) {
992                 return docType.getName() == null;
993             }
994         }
995         return true;
996     }
997 
998     /**
999      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1000      *
1001      * Called from the HTMLParser if an 'X-UA-Compatible' meta tag found.
1002      * @param documentMode the mode forced by the meta tag
1003      */
1004     public void forceDocumentMode(final int documentMode) {
1005         documentMode_ = documentMode;
1006         compatMode_ = documentMode == 5 ? "BackCompat" : "CSS1Compat";
1007     }
1008 
1009     /**
1010      * Returns the first element within the document that matches the specified group of selectors.
1011      * @param selectors the selectors
1012      * @return null if no matches are found; otherwise, it returns the first matching element
1013      */
1014     @JsxFunction
1015     public Node querySelector(final String selectors) {
1016         try {
1017             final Element doc = getDocumentElement();
1018             final DomNode scope = doc == null ? null : doc.getDomNodeOrNull();
1019             final DomNode node = getDomNodeOrDie().querySelector(selectors, scope);
1020             if (node != null) {
1021                 return node.getScriptableObject();
1022             }
1023             return null;
1024         }
1025         catch (final CSSException e) {
1026             throw JavaScriptEngine.asJavaScriptException(
1027                     getWindow(),
1028                     "An invalid or illegal selector was specified (selector: '"
1029                             + selectors + "' error: " + e.getMessage() + ").",
1030                     org.htmlunit.javascript.host.dom.DOMException.SYNTAX_ERR);
1031         }
1032     }
1033 
1034     /**
1035      * Retrieves all element nodes from descendants of the starting element node that match any selector
1036      * within the supplied selector strings.
1037      * The NodeList object returned by the querySelectorAll() method must be static, not live.
1038      * @param selectors the selectors
1039      * @return the static node list
1040      */
1041     @JsxFunction
1042     public NodeList querySelectorAll(final String selectors) {
1043         try {
1044             final DomNode domNode = getDomNodeOrDie();
1045             final Element doc = getDocumentElement();
1046             final DomNode scope = doc == null ? null : doc.getDomNodeOrNull();
1047             return NodeList.staticNodeList(getParentScope(), domNode.querySelectorAll(selectors, scope));
1048         }
1049         catch (final CSSException e) {
1050             throw JavaScriptEngine.asJavaScriptException(
1051                     getWindow(),
1052                     "An invalid or illegal selector was specified (selector: '"
1053                             + selectors + "' error: " + e.getMessage() + ").",
1054                     org.htmlunit.javascript.host.dom.DOMException.SYNTAX_ERR);
1055         }
1056     }
1057 
1058     /**
1059      * Indicates if the command is supported.
1060      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/queryCommandSupported">MDN Documentation</a>
1061      * @param cmd the command identifier
1062      * @return {@code true} if the command is supported
1063      */
1064     @JsxFunction
1065     public boolean queryCommandSupported(final String cmd) {
1066         return hasCommand(cmd, true);
1067     }
1068 
1069     private boolean hasCommand(final String cmd, final boolean includeBold) {
1070         if (cmd == null) {
1071             return false;
1072         }
1073 
1074         final String cmdLC = cmd.toLowerCase(Locale.ROOT);
1075         if (getBrowserVersion().isChrome() || getBrowserVersion().isEdge()) {
1076             return EXECUTE_CMDS_CHROME.contains(cmdLC) || (includeBold && "bold".equalsIgnoreCase(cmd));
1077         }
1078         return EXECUTE_CMDS_FF.contains(cmdLC);
1079     }
1080 
1081     /**
1082      * Indicates if the command can be successfully executed using <code>execCommand</code>, given
1083      * the current state of the document.
1084      * @param cmd the command identifier
1085      * @return {@code true} if the command can be successfully executed
1086      */
1087     @JsxFunction
1088     public boolean queryCommandEnabled(final String cmd) {
1089         return hasCommand(cmd, true);
1090     }
1091 
1092     /**
1093      * Executes a command.
1094      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand">MDN Documentation</a>
1095      * @param cmd the command identifier
1096      * @param userInterface display a user interface if the command supports one
1097      * @param value the string, number, or other value to assign (possible values depend on the command)
1098      * @return {@code true} if the command was successful, {@code false} otherwise
1099      */
1100     @JsxFunction
1101     public boolean execCommand(final String cmd, final boolean userInterface, final Object value) {
1102         if (!hasCommand(cmd, false)) {
1103             return false;
1104         }
1105         if (LOG.isWarnEnabled()) {
1106             LOG.warn("Nothing done for execCommand(" + cmd + ", ...) (feature not implemented)");
1107         }
1108         return true;
1109     }
1110 
1111     /**
1112      * Returns the value of the {@code URL} property.
1113      * @return the value of the {@code URL} property
1114      */
1115     @JsxGetter(propertyName = "URL")
1116     public String getURL_js() {
1117         return getPage().getUrl().toExternalForm();
1118     }
1119 
1120     /**
1121      * Returns the value of the {@code documentURI} property.
1122      * @return the value of the {@code documentURI} property
1123      */
1124     @JsxGetter
1125     public String getDocumentURI() {
1126         return getURL_js();
1127     }
1128 
1129     /**
1130      * Returns the {@code cookie} property.
1131      * @return the {@code cookie} property
1132      */
1133     @JsxGetter
1134     public String getCookie() {
1135         final SgmlPage sgmlPage = getPage();
1136 
1137         final StringBuilder builder = new StringBuilder();
1138         final Set<Cookie> cookies = sgmlPage.getWebClient().getCookies(sgmlPage.getUrl());
1139         final List<Cookie> visibleCookies = new ArrayList<>();
1140         for (final Cookie cookie : cookies) {
1141             if (cookie.isHttpOnly()) {
1142                 continue;
1143             }
1144             visibleCookies.add(cookie);
1145         }
1146 
1147         // RFC 6265 §5.4: longer path first; equal-length paths sorted by creation time ascending
1148         // TODO time ascending
1149         visibleCookies.sort((a, b) -> {
1150             final String pathA = a.getPath();
1151             final String pathB = b.getPath();
1152             final int lenA = pathA == null ? 0 : pathA.length();
1153             final int lenB = pathB == null ? 0 : pathB.length();
1154             return Integer.compare(lenB, lenA);
1155         });
1156 
1157         for (final Cookie cookie : visibleCookies) {
1158             if (builder.length() != 0) {
1159                 builder.append("; ");
1160             }
1161             if (!HtmlUnitBrowserCompatCookieSpec.EMPTY_COOKIE_NAME.equals(cookie.getName())) {
1162                 builder.append(cookie.getName()).append('=');
1163             }
1164             builder.append(cookie.getValue());
1165         }
1166 
1167         return builder.toString();
1168     }
1169 
1170     /**
1171      * Adds a cookie, as long as cookies are enabled.
1172      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie">MDN Documentation</a>
1173      * @param newCookie in the format "name=value[;expires=date][;domain=domainname][;path=path][;secure]"
1174      */
1175     @JsxSetter
1176     public void setCookie(final String newCookie) {
1177         if (StringUtils.isBlank(newCookie)) {
1178             return;
1179         }
1180 
1181         final SgmlPage sgmlPage = getPage();
1182         sgmlPage.getWebClient().addCookie(newCookie, sgmlPage.getUrl(), this);
1183     }
1184 
1185     /**
1186      * Implementation of the {@link org.w3c.dom.events.DocumentEvent} interface's
1187      * {@link org.w3c.dom.events.DocumentEvent#createEvent(String)} method. The method creates an
1188      * uninitialized event of the specified type.
1189      *
1190      * @see <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-DocumentEvent">DocumentEvent</a>
1191      * @param eventType the event type to create
1192      * @return an event object for the specified type
1193      * @throws DOMException if the event type is not supported (will have a type of
1194      *         DOMException.NOT_SUPPORTED_ERR)
1195      */
1196     @JsxFunction
1197     public Event createEvent(final String eventType) throws DOMException {
1198         Class<? extends Event> clazz = SUPPORTED_DOM2_EVENT_TYPE_MAP.get(eventType);
1199         if (clazz == null) {
1200             clazz = SUPPORTED_DOM3_EVENT_TYPE_MAP.get(eventType);
1201         }
1202 
1203         if (clazz == null
1204                 && ("Events".equals(eventType)
1205                     || "HashChangeEvent".equals(eventType)
1206                     || "BeforeUnloadEvent".equals(eventType)
1207                     || "FocusEvent".equals(eventType))) {
1208             clazz = SUPPORTED_VENDOR_EVENT_TYPE_MAP.get(eventType);
1209         }
1210 
1211         if (clazz == null) {
1212             throw JavaScriptEngine.asJavaScriptException(
1213                     this,
1214                     "Event Type '" + eventType + "' is not supported.",
1215                     org.htmlunit.javascript.host.dom.DOMException.NOT_SUPPORTED_ERR);
1216         }
1217 
1218         try {
1219             final Event event = clazz.getDeclaredConstructor().newInstance();
1220             event.setParentScope(getParentScope());
1221             event.setPrototype(getPrototype(clazz));
1222             event.eventCreated();
1223             return event;
1224         }
1225         catch (final InstantiationException | IllegalAccessException
1226                 | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
1227             throw JavaScriptEngine.reportRuntimeError("Failed to instantiate event: class ='" + clazz.getName()
1228                             + "' for event type of '" + eventType + "': " + e.getMessage());
1229         }
1230     }
1231 
1232     /**
1233      * Returns a new NodeIterator object.
1234      *
1235      * @param root The root node at which to begin the NodeIterator's traversal.
1236      * @param whatToShow an optional unsigned long representing a bitmask created by combining
1237      *        the constant properties of {@link NodeFilter}; defaults to {@link NodeFilter#SHOW_ALL}
1238      * @param filter an object implementing the {@link NodeFilter} interface
1239      * @return a new NodeIterator object
1240      */
1241     @JsxFunction
1242     public NodeIterator createNodeIterator(final Node root, final Object whatToShow, final Scriptable filter) {
1243         // the dom iterator uses the int based constants of org.w3c.dom.traversal.NodeFilter (SHOW_ALL is -1)
1244         final int whatToShowI = JavaScriptEngine.isUndefined(whatToShow)
1245                 ? org.w3c.dom.traversal.NodeFilter.SHOW_ALL
1246                 : JavaScriptEngine.toInt32(whatToShow);
1247         final org.w3c.dom.traversal.NodeFilter filterWrapper = createFilterWrapper(filter, false);
1248         final NodeIterator iterator = new NodeIterator(root, whatToShowI, filterWrapper);
1249         iterator.setParentScope(getParentScope());
1250         iterator.setPrototype(getPrototype(iterator.getClass()));
1251         return iterator;
1252     }
1253 
1254     private static org.w3c.dom.traversal.NodeFilter createFilterWrapper(final Scriptable filter,
1255             final boolean filterFunctionOnly) {
1256         org.w3c.dom.traversal.NodeFilter filterWrapper = null;
1257         if (filter != null) {
1258             filterWrapper = n -> {
1259                 final Object[] args = {((DomNode) n).getScriptableObject()};
1260                 final Object response;
1261                 if (filter instanceof Callable callable) {
1262                     response = callable.call(Context.getCurrentContext(), filter.getParentScope(), filter, args);
1263                 }
1264                 else {
1265                     if (filterFunctionOnly) {
1266                         throw JavaScriptEngine.reportRuntimeError("only a function is allowed as filter");
1267                     }
1268                     response = ScriptableObject.callMethod(filter, "acceptNode", args);
1269                 }
1270                 return (short) JavaScriptEngine.toNumber(response);
1271             };
1272         }
1273         return filterWrapper;
1274     }
1275 
1276     /**
1277      * Creates and returns a new TreeWalker. The following JavaScript parameters are passed into this method:
1278      * <ul>
1279      *   <li>JavaScript param 1: The root node of the TreeWalker. Must not be {@code null}.</li>
1280      *   <li>JavaScript param 2: Flag specifying which types of nodes appear in the logical view of the TreeWalker.
1281      *       See {@link NodeFilter} for the set of possible Show_ values.</li>
1282      *   <li>JavaScript param 3: The {@link NodeFilter} to be used with this TreeWalker, or {@code null}
1283      *       to indicate no filter.</li>
1284      *   <li>JavaScript param 4: If {@code false}, the contents of EntityReference nodes are not present
1285      *       in the logical view.</li>
1286      * </ul>
1287      *
1288      * @see <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal.html">DOM-Level-2-Traversal-Range</a>
1289      * @param root the node which will serve as the root for the TreeWalker
1290      * @param whatToShow specifies which node types may appear in the logical view of the tree presented
1291      * @param filter the NodeFilter to be used with this TreeWalker, or null to indicate no filter
1292      * @param expandEntityReferences If false,
1293      *        the contents of EntityReference nodes are not presented in the logical view
1294      * @throws DOMException on attempt to create a TreeWalker with a root that is {@code null}
1295      * @return a new TreeWalker
1296      */
1297     @JsxFunction
1298     public TreeWalker createTreeWalker(final Node root, final double whatToShow, final Scriptable filter,
1299             final boolean expandEntityReferences) throws DOMException {
1300 
1301         // seems that Rhino doesn't like long as parameter type
1302         // this strange conversation preserves NodeFilter.SHOW_ALL
1303         final int whatToShowI = (int) Double.valueOf(whatToShow).longValue();
1304 
1305         final org.w3c.dom.traversal.NodeFilter filterWrapper = createFilterWrapper(filter, false);
1306         final TreeWalker t = new TreeWalker(root, whatToShowI, filterWrapper, false);
1307         t.setParentScope(getParentScope());
1308         t.setPrototype(staticGetPrototype(getWindow(this), TreeWalker.class));
1309         return t;
1310     }
1311 
1312     @SuppressWarnings("unchecked")
1313     private static Scriptable staticGetPrototype(final Window window,
1314             final Class<? extends HtmlUnitScriptable> javaScriptClass) {
1315         final Scriptable prototype = window.getPrototype(javaScriptClass);
1316         if (prototype == null && javaScriptClass != HtmlUnitScriptable.class) {
1317             return staticGetPrototype(window, (Class<? extends HtmlUnitScriptable>) javaScriptClass.getSuperclass());
1318         }
1319         return prototype;
1320     }
1321 
1322     /**
1323      * Creates and returns a new range.
1324      * @return a new range
1325      * @see <a href="http://www.xulplanet.com/references/objref/HTMLDocument.html#method_createRange">XUL Planet</a>
1326      */
1327     @JsxFunction
1328     public Range createRange() {
1329         final Range range = new Range(this);
1330         range.setParentScope(getParentScope());
1331         range.setPrototype(getPrototype(Range.class));
1332         return range;
1333     }
1334 
1335     /**
1336      * Returns the domain name of the server that served the document, or {@code null} if the server
1337      * cannot be identified by a domain name.
1338      * @return the domain name of the server that served the document
1339      * @see <a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-2250147">
1340      *     W3C documentation</a>
1341      */
1342     @JsxGetter
1343     public String getDomain() {
1344         if (domain_ == null && getPage().getWebResponse() != null) {
1345             URL url = getPage().getUrl();
1346             if (url == UrlUtils.URL_ABOUT_BLANK) {
1347                 final WebWindow w = getWindow().getWebWindow();
1348                 if (w instanceof FrameWindow window) {
1349                     url = window.getEnclosingPage().getUrl();
1350                 }
1351                 else {
1352                     return null;
1353                 }
1354             }
1355             domain_ = url.getHost().toLowerCase(Locale.ROOT);
1356         }
1357 
1358         return domain_;
1359     }
1360 
1361     /**
1362      * Sets the domain of this document.
1363      *
1364      * <p>Domains can only be set to suffixes of the existing domain
1365      * with the except of setting the domain to itself.</p>
1366      * <p>
1367      * The domain will be set according to the following rules:
1368      * </p>
1369      * <ol>
1370      * <li>If the newDomain.equalsIgnoreCase(currentDomain) the method returns with no error.</li>
1371      * <li>If the browser version is netscape, the newDomain is downshifted.</li>
1372      * <li>The change will take place if and only if the suffixes of the
1373      *       current domain and the new domain match AND there are at least
1374      *       two domain qualifiers e.g. the following transformations are legal
1375      *       d1.d2.d3.gargoylesoftware.com may be transformed to itself or:
1376      *          d2.d3.gargoylesoftware.com
1377      *             d3.gargoylesoftware.com
1378      *                gargoylesoftware.com
1379      * <br>
1380      *        transformation to:        com
1381      *        will fail
1382      * </li>
1383      * </ol>
1384      * <p>
1385      * TODO This code could be modified to understand country domain suffixes.
1386      * The domain www.bbc.co.uk should be trimmable only down to bbc.co.uk
1387      * trimming to co.uk should not be possible.
1388      * </p>
1389      * @param newDomain the new domain to set
1390      */
1391     @JsxSetter
1392     public void setDomain(final String newDomain) {
1393         final String domain = newDomain.toLowerCase(Locale.ROOT);
1394 
1395         final String currentDomain = getDomain();
1396         if (currentDomain.equalsIgnoreCase(domain)) {
1397             return;
1398         }
1399 
1400         if (domain.indexOf('.') == -1) {
1401             throw JavaScriptEngine.reportRuntimeError("Illegal domain value, cannot set domain from: \""
1402                     + currentDomain + "\" to: \"" + domain + "\" (new domain has to contain a dot).");
1403         }
1404 
1405         if (currentDomain.indexOf('.') > -1
1406                 && !currentDomain.toLowerCase(Locale.ROOT).endsWith("." + domain)) {
1407             throw JavaScriptEngine.reportRuntimeError("Illegal domain value, cannot set domain from: \""
1408                     + currentDomain + "\" to: \"" + domain + "\"");
1409         }
1410 
1411         domain_ = domain;
1412     }
1413 
1414     /**
1415      * Sets the {@code onclick} event handler for this element.
1416      * @param handler the {@code onclick} event handler for this element
1417      */
1418     @JsxSetter
1419     public void setOnclick(final Object handler) {
1420         setEventHandler(MouseEvent.TYPE_CLICK, handler);
1421     }
1422 
1423     /**
1424      * Returns the {@code onclick} event handler for this element.
1425      * @return the {@code onclick} event handler for this element
1426      */
1427     @JsxGetter
1428     public Function getOnclick() {
1429         return getEventHandler(MouseEvent.TYPE_CLICK);
1430     }
1431 
1432     /**
1433      * Sets the {@code ondblclick} event handler for this element.
1434      * @param handler the {@code ondblclick} event handler for this element
1435      */
1436     @JsxSetter
1437     public void setOndblclick(final Object handler) {
1438         setEventHandler(MouseEvent.TYPE_DBL_CLICK, handler);
1439     }
1440 
1441     /**
1442      * Returns the {@code ondblclick} event handler for this element.
1443      * @return the {@code ondblclick} event handler for this element
1444      */
1445     @JsxGetter
1446     public Function getOndblclick() {
1447         return getEventHandler(MouseEvent.TYPE_DBL_CLICK);
1448     }
1449 
1450     /**
1451      * Sets the {@code onblur} event handler for this element.
1452      * @param handler the {@code onblur} event handler for this element
1453      */
1454     @JsxSetter
1455     public void setOnblur(final Object handler) {
1456         setEventHandler(Event.TYPE_BLUR, handler);
1457     }
1458 
1459     /**
1460      * Returns the {@code onblur} event handler for this element.
1461      * @return the {@code onblur} event handler for this element
1462      */
1463     @JsxGetter
1464     public Function getOnblur() {
1465         return getEventHandler(Event.TYPE_BLUR);
1466     }
1467 
1468     /**
1469      * Sets the {@code onfocus} event handler for this element.
1470      * @param handler the {@code onfocus} event handler for this element
1471      */
1472     @JsxSetter
1473     public void setOnfocus(final Object handler) {
1474         setEventHandler(Event.TYPE_FOCUS, handler);
1475     }
1476 
1477     /**
1478      * Returns the {@code onfocus} event handler for this element.
1479      * @return the {@code onfocus} event handler for this element
1480      */
1481     @JsxGetter
1482     public Function getOnfocus() {
1483         return getEventHandler(Event.TYPE_FOCUS);
1484     }
1485 
1486     /**
1487      * Sets the {@code onkeydown} event handler for this element.
1488      * @param handler the {@code onkeydown} event handler for this element
1489      */
1490     @JsxSetter
1491     public void setOnkeydown(final Object handler) {
1492         setEventHandler(Event.TYPE_KEY_DOWN, handler);
1493     }
1494 
1495     /**
1496      * Returns the {@code onkeydown} event handler for this element.
1497      * @return the {@code onkeydown} event handler for this element
1498      */
1499     @JsxGetter
1500     public Function getOnkeydown() {
1501         return getEventHandler(Event.TYPE_KEY_DOWN);
1502     }
1503 
1504     /**
1505      * Sets the {@code onkeypress} event handler for this element.
1506      * @param handler the {@code onkeypress} event handler for this element
1507      */
1508     @JsxSetter
1509     public void setOnkeypress(final Object handler) {
1510         setEventHandler(Event.TYPE_KEY_PRESS, handler);
1511     }
1512 
1513     /**
1514      * Returns the {@code onkeypress} event handler for this element.
1515      * @return the {@code onkeypress} event handler for this element
1516      */
1517     @JsxGetter
1518     public Function getOnkeypress() {
1519         return getEventHandler(Event.TYPE_KEY_PRESS);
1520     }
1521 
1522     /**
1523      * Sets the {@code onkeyup} event handler for this element.
1524      * @param handler the {@code onkeyup} event handler for this element
1525      */
1526     @JsxSetter
1527     public void setOnkeyup(final Object handler) {
1528         setEventHandler(Event.TYPE_KEY_UP, handler);
1529     }
1530 
1531     /**
1532      * Returns the {@code onkeyup} event handler for this element.
1533      * @return the {@code onkeyup} event handler for this element
1534      */
1535     @JsxGetter
1536     public Function getOnkeyup() {
1537         return getEventHandler(Event.TYPE_KEY_UP);
1538     }
1539 
1540     /**
1541      * Sets the {@code onmousedown} event handler for this element.
1542      * @param handler the {@code onmousedown} event handler for this element
1543      */
1544     @JsxSetter
1545     public void setOnmousedown(final Object handler) {
1546         setEventHandler(MouseEvent.TYPE_MOUSE_DOWN, handler);
1547     }
1548 
1549     /**
1550      * Returns the {@code onmousedown} event handler for this element.
1551      * @return the {@code onmousedown} event handler for this element
1552      */
1553     @JsxGetter
1554     public Function getOnmousedown() {
1555         return getEventHandler(MouseEvent.TYPE_MOUSE_DOWN);
1556     }
1557 
1558     /**
1559      * Sets the {@code onmousemove} event handler for this element.
1560      * @param handler the {@code onmousemove} event handler for this element
1561      */
1562     @JsxSetter
1563     public void setOnmousemove(final Object handler) {
1564         setEventHandler(MouseEvent.TYPE_MOUSE_MOVE, handler);
1565     }
1566 
1567     /**
1568      * Returns the {@code onmousemove} event handler for this element.
1569      * @return the {@code onmousemove} event handler for this element
1570      */
1571     @JsxGetter
1572     public Function getOnmousemove() {
1573         return getEventHandler(MouseEvent.TYPE_MOUSE_MOVE);
1574     }
1575 
1576     /**
1577      * Sets the {@code onmouseout} event handler for this element.
1578      * @param handler the {@code onmouseout} event handler for this element
1579      */
1580     @JsxSetter
1581     public void setOnmouseout(final Object handler) {
1582         setEventHandler(MouseEvent.TYPE_MOUSE_OUT, handler);
1583     }
1584 
1585     /**
1586      * Returns the {@code onmouseout} event handler for this element.
1587      * @return the {@code onmouseout} event handler for this element
1588      */
1589     @JsxGetter
1590     public Function getOnmouseout() {
1591         return getEventHandler(MouseEvent.TYPE_MOUSE_OUT);
1592     }
1593 
1594     /**
1595      * Sets the {@code onmouseover} event handler for this element.
1596      * @param handler the {@code onmouseover} event handler for this element
1597      */
1598     @JsxSetter
1599     public void setOnmouseover(final Object handler) {
1600         setEventHandler(MouseEvent.TYPE_MOUSE_OVER, handler);
1601     }
1602 
1603     /**
1604      * Returns the {@code onmouseover} event handler for this element.
1605      * @return the {@code onmouseover} event handler for this element
1606      */
1607     @JsxGetter
1608     public Function getOnmouseover() {
1609         return getEventHandler(MouseEvent.TYPE_MOUSE_OVER);
1610     }
1611 
1612     /**
1613      * Sets the {@code onmouseup} event handler for this element.
1614      * @param handler the {@code onmouseup} event handler for this element
1615      */
1616     @JsxSetter
1617     public void setOnmouseup(final Object handler) {
1618         setEventHandler(MouseEvent.TYPE_MOUSE_UP, handler);
1619     }
1620 
1621     /**
1622      * Returns the {@code onmouseup} event handler for this element.
1623      * @return the {@code onmouseup} event handler for this element
1624      */
1625     @JsxGetter
1626     public Function getOnmouseup() {
1627         return getEventHandler(MouseEvent.TYPE_MOUSE_UP);
1628     }
1629 
1630     /**
1631      * Sets the {@code oncontextmenu} event handler for this element.
1632      * @param handler the {@code oncontextmenu} event handler for this element
1633      */
1634     @JsxSetter
1635     public void setOncontextmenu(final Object handler) {
1636         setEventHandler(MouseEvent.TYPE_CONTEXT_MENU, handler);
1637     }
1638 
1639     /**
1640      * Returns the {@code oncontextmenu} event handler for this element.
1641      * @return the {@code oncontextmenu} event handler for this element
1642      */
1643     @JsxGetter
1644     public Function getOncontextmenu() {
1645         return getEventHandler(MouseEvent.TYPE_CONTEXT_MENU);
1646     }
1647 
1648     /**
1649      * Sets the {@code onresize} event handler for this element.
1650      * @param handler the {@code onresize} event handler for this element
1651      */
1652     @JsxSetter
1653     public void setOnresize(final Object handler) {
1654         setEventHandler(Event.TYPE_RESIZE, handler);
1655     }
1656 
1657     /**
1658      * Returns the {@code onresize} event handler for this element.
1659      * @return the {@code onresize} event handler for this element
1660      */
1661     @JsxGetter
1662     public Function getOnresize() {
1663         return getEventHandler(Event.TYPE_RESIZE);
1664     }
1665 
1666     /**
1667      * Sets the {@code onerror} event handler for this element.
1668      * @param handler the {@code onerror} event handler for this element
1669      */
1670     @JsxSetter
1671     public void setOnerror(final Object handler) {
1672         setEventHandler(Event.TYPE_ERROR, handler);
1673     }
1674 
1675     /**
1676      * Returns the {@code onerror} event handler for this element.
1677      * @return the {@code onerror} event handler for this element
1678      */
1679     @JsxGetter
1680     public Function getOnerror() {
1681         return getEventHandler(Event.TYPE_ERROR);
1682     }
1683 
1684     /**
1685      * Returns the {@code oninput} event handler for this element.
1686      * @return the {@code oninput} event handler for this element
1687      */
1688     @JsxGetter
1689     public Function getOninput() {
1690         return getEventHandler(Event.TYPE_INPUT);
1691     }
1692 
1693     /**
1694      * Sets the {@code oninput} event handler for this element.
1695      * @param oninput the {@code oninput} event handler for this element
1696      */
1697     @JsxSetter
1698     public void setOninput(final Object oninput) {
1699         setEventHandler(Event.TYPE_INPUT, oninput);
1700     }
1701 
1702     /**
1703      * Returns the {@code hidden} property.
1704      * @return the {@code hidden} property
1705      */
1706     @JsxGetter
1707     public boolean isHidden() {
1708         return false;
1709     }
1710 
1711     /**
1712      * {@inheritDoc}
1713      */
1714     @Override
1715     @JsxGetter
1716     public int getChildElementCount() {
1717         int counter = 0;
1718         if (getPage().getDocumentElement() != null) {
1719             counter++;
1720         }
1721         return counter;
1722     }
1723 
1724     /**
1725      * Returns the element for the specified x coordinate and the specified y coordinate.
1726      * The current implementation always returns null element.
1727      *
1728      * @param x the x offset, in pixels
1729      * @param y the y offset, in pixels
1730      * @return the element for the specified x coordinate and the specified y coordinate
1731      */
1732     @JsxFunction
1733     public HtmlUnitScriptable elementFromPoint(final int x, final int y) {
1734         return null;
1735     }
1736 
1737     /**
1738      * Returns the value of the {@code forms} property.
1739      * @return the value of the {@code forms} property
1740      */
1741     @JsxGetter
1742     public HTMLCollection getForms() {
1743         final HTMLCollection forms = new HTMLCollection(getDomNodeOrDie(), false);
1744 
1745         forms.setIsMatchingPredicate(
1746                 (Predicate<DomNode> & Serializable)
1747                 node -> node instanceof HtmlForm && node.getPrefix() == null);
1748         return forms;
1749     }
1750 
1751     /**
1752      * Returns the value of the {@code embeds} property.
1753      * @return the value of the {@code embeds} property
1754      */
1755     @JsxGetter
1756     public HTMLCollection getEmbeds() {
1757         final HTMLCollection embeds = new HTMLCollection(getDomNodeOrDie(), false);
1758 
1759         embeds.setIsMatchingPredicate((Predicate<DomNode> & Serializable) node -> node instanceof HtmlEmbed);
1760         return embeds;
1761     }
1762 
1763     /**
1764      * Returns the value of the {@code embeds} property.
1765      * @return the value of the {@code embeds} property
1766      */
1767     @JsxGetter
1768     public HTMLCollection getImages() {
1769         final HTMLCollection images = new HTMLCollection(getDomNodeOrDie(), false);
1770 
1771         images.setIsMatchingPredicate((Predicate<DomNode> & Serializable) node -> node instanceof HtmlImage);
1772         return images;
1773     }
1774 
1775     /**
1776      * Returns the value of the {@code scripts} property.
1777      * @return the value of the {@code scripts} property
1778      */
1779     @JsxGetter
1780     public HTMLCollection getScripts() {
1781         final HTMLCollection scripts = new HTMLCollection(getDomNodeOrDie(), false);
1782 
1783         scripts.setIsMatchingPredicate((Predicate<DomNode> & Serializable) node -> node instanceof HtmlScript);
1784         return scripts;
1785     }
1786 
1787     /**
1788      * Retrieves a collection of stylesheet objects representing the style sheets that correspond
1789      * to each instance of a Link or
1790      * {@link org.htmlunit.javascript.host.css.CSSStyleDeclaration} object in the document.
1791      *
1792      * @return styleSheet collection
1793      */
1794     @JsxGetter
1795     public StyleSheetList getStyleSheets() {
1796         if (styleSheetList_ == null) {
1797             styleSheetList_ = new StyleSheetList(this);
1798         }
1799         return styleSheetList_;
1800     }
1801 
1802     /**
1803      * Returns the value of the {@code plugins} property.
1804      * @return the value of the {@code plugins} property
1805      */
1806     @JsxGetter
1807     public HTMLCollection getPlugins() {
1808         return getEmbeds();
1809     }
1810 
1811     /**
1812      * Returns the value of the JavaScript property {@code links}. Refer also to the
1813      * <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/links">MDN Documentation</a>.
1814      * @return the value of this property
1815      */
1816     @JsxGetter
1817     public HTMLCollection getLinks() {
1818         final HTMLCollection links = new HTMLCollection(getDomNodeOrDie(), true);
1819 
1820         links.setEffectOnCacheFunction(
1821                 (java.util.function.Function<HtmlAttributeChangeEvent, EffectOnCache> & Serializable)
1822                 event -> {
1823                     final HtmlElement node = event.getHtmlElement();
1824                     if ((node instanceof HtmlAnchor || node instanceof HtmlArea) && "href".equals(event.getName())) {
1825                         return EffectOnCache.RESET;
1826                     }
1827                     return EffectOnCache.NONE;
1828                 });
1829 
1830         links.setIsMatchingPredicate(
1831                 (Predicate<DomNode> & Serializable)
1832                 node ->
1833                     (node instanceof HtmlAnchor || node instanceof HtmlArea)
1834                     && ((HtmlElement) node).hasAttribute("href"));
1835 
1836         return links;
1837     }
1838 
1839     /**
1840      * Returns all the descendant elements with the specified class name.
1841      * @param className the name to search for
1842      * @return all the descendant elements with the specified class name
1843      * @see <a href="https://developer.mozilla.org/en/DOM/document.getElementsByClassName">Mozilla doc</a>
1844      */
1845     @JsxFunction
1846     public HTMLCollection getElementsByClassName(final String className) {
1847         return null;
1848     }
1849 
1850     /**
1851      * Returns all HTML elements that have a {@code name} attribute with the specified value.
1852      * <p>
1853      * Refer to <a href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259">
1854      * The DOM spec</a> for details.
1855      * </p>
1856      *
1857      * @param elementName - value of the {@code name} attribute to look for
1858      * @return all HTML elements that have a {@code name} attribute with the specified value
1859      */
1860     @JsxFunction
1861     public NodeList getElementsByName(final String elementName) {
1862         return null;
1863     }
1864 
1865     /**
1866      * Returns {@code false} if the active element in the document has no focus;
1867      * {@code true} if the active element in the document has focus.
1868      * @return whether the active element in the document has focus or not
1869      */
1870     @JsxFunction
1871     public boolean hasFocus() {
1872         return false;
1873     }
1874 
1875     /**
1876      * Returns this document's title.
1877      * @return this document's title
1878      */
1879     @JsxGetter
1880     public String getTitle() {
1881         return "";
1882     }
1883 
1884     /**
1885      * Sets this document's title.
1886      * @param title the new title
1887      */
1888     @JsxSetter
1889     public void setTitle(final String title) {
1890         // nothing to do
1891     }
1892 
1893     /**
1894      * {@inheritDoc}
1895      */
1896     @Override
1897     @JsxGetter
1898     public HTMLCollection getChildren() {
1899         return super.getChildren();
1900     }
1901 
1902     /**
1903      * Returns the {@code contentType} property.
1904      * @return the {@code contentType} property
1905      */
1906     @JsxGetter
1907     public String getContentType() {
1908         return getPage().getContentType();
1909     }
1910 
1911     /**
1912      * Returns the current selection.
1913      * @return the current selection
1914      */
1915     @JsxFunction
1916     public Selection getSelection() {
1917         return null;
1918     }
1919 
1920     /**
1921      * Returns this document's {@code head} element.
1922      * @return this document's {@code head} element
1923      */
1924     @JsxGetter
1925     public Object getHead() {
1926         return null;
1927     }
1928 
1929     /**
1930      * Returns a string representing the encoding under which the document was parsed.
1931      * @return a string representing the encoding under which the document was parsed
1932      */
1933     @JsxGetter
1934     public String getInputEncoding() {
1935         return getPage().getCharset().name();
1936     }
1937 
1938     /**
1939      * Returns the last modification date of the document.
1940      * @see <a href="https://developer.mozilla.org/en/DOM/document.lastModified">Mozilla documentation</a>
1941      * @return the date as string
1942      */
1943     @JsxGetter
1944     public String getLastModified() {
1945         if (lastModified_ == null) {
1946             final WebResponse webResponse = getPage().getWebResponse();
1947             final Date lastModified;
1948             if (webResponse != null) {
1949                 String stringDate = webResponse.getResponseHeaderValue("Last-Modified");
1950                 if (stringDate == null) {
1951                     stringDate = webResponse.getResponseHeaderValue("Date");
1952                 }
1953                 lastModified = parseDateOrNow(stringDate);
1954             }
1955             else {
1956                 lastModified = new Date();
1957             }
1958 
1959             final ZoneId zoneid = Context.getCurrentContext().getTimeZone().toZoneId();
1960             lastModified_ = LAST_MODIFIED_DATE_FORMATTER.format(lastModified.toInstant().atZone(zoneid));
1961         }
1962         return lastModified_;
1963     }
1964 
1965     private static Date parseDateOrNow(final String stringDate) {
1966         final Date date = HttpUtils.parseDate(stringDate);
1967         if (date == null) {
1968             return new Date();
1969         }
1970         return date;
1971     }
1972 
1973     /**
1974      * Mock for the moment.
1975      */
1976     @JsxFunction({FF, FF_ESR})
1977     public void releaseCapture() {
1978         // nothing to do
1979     }
1980 
1981     /**
1982      * Returns the ready state of the document.
1983      * @return the ready state of the document
1984      *
1985      * @see DomNode#READY_STATE_UNINITIALIZED
1986      * @see DomNode#READY_STATE_LOADING
1987      * @see DomNode#READY_STATE_LOADED
1988      * @see DomNode#READY_STATE_INTERACTIVE
1989      * @see DomNode#READY_STATE_COMPLETE
1990      */
1991     @JsxGetter
1992     public String getReadyState() {
1993         return getDomNodeOrDie().getReadyState();
1994     }
1995 
1996     /**
1997      * Does nothing special anymore.
1998      *
1999      * @param type the type of events to capture
2000      * @see Window#captureEvents(String)
2001      */
2002     @JsxFunction
2003     public void captureEvents(final String type) {
2004         // Empty.
2005     }
2006 
2007     /**
2008      * Does nothing special anymore.
2009      *
2010      * @param type the type of events to capture
2011      * @see Window#releaseEvents(String)
2012      */
2013     @JsxFunction
2014     public void releaseEvents(final String type) {
2015         // Empty.
2016     }
2017 
2018     /**
2019      * Returns the value of the {@code alinkColor} property.
2020      * @return the value of the {@code alinkColor} property
2021      */
2022     @JsxGetter
2023     public String getAlinkColor() {
2024         final HTMLElement body = getBody();
2025         if (body instanceof HTMLBodyElement element) {
2026             return element.getALink();
2027         }
2028         return null;
2029     }
2030 
2031     /**
2032      * Sets the value of the {@code alinkColor} property.
2033      * @param color the value of the {@code alinkColor} property
2034      */
2035     @JsxSetter
2036     public void setAlinkColor(final String color) {
2037         final HTMLElement body = getBody();
2038         if (body instanceof HTMLBodyElement element) {
2039             element.setALink(color);
2040         }
2041     }
2042 
2043     /**
2044      * Returns the value of the {@code bgColor} property.
2045      * @return the value of the {@code bgColor} property
2046      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/bgColor">MDN Documentation</a>
2047      */
2048     @JsxGetter
2049     public String getBgColor() {
2050         final HTMLElement body = getBody();
2051         if (body instanceof HTMLBodyElement element) {
2052             return element.getBgColor();
2053         }
2054         return null;
2055     }
2056 
2057     /**
2058      * Sets the value of the {@code bgColor} property.
2059      * @param color the value of the {@code bgColor} property
2060      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/bgColor">MDN Documentation</a>
2061      */
2062     @JsxSetter
2063     public void setBgColor(final String color) {
2064         final HTMLElement body = getBody();
2065         if (body instanceof HTMLBodyElement element) {
2066             element.setBgColor(color);
2067         }
2068     }
2069 
2070     /**
2071      * Returns the value of the {@code fgColor} property.
2072      * @return the value of the {@code fgColor} property
2073      */
2074     @JsxGetter
2075     public String getFgColor() {
2076         final HTMLElement body = getBody();
2077         if (body instanceof HTMLBodyElement element) {
2078             return element.getText();
2079         }
2080         return null;
2081     }
2082 
2083     /**
2084      * Sets the value of the {@code fgColor} property.
2085      * @param color the value of the {@code fgColor} property
2086      */
2087     @JsxSetter
2088     public void setFgColor(final String color) {
2089         final HTMLElement body = getBody();
2090         if (body instanceof HTMLBodyElement element) {
2091             element.setText(color);
2092         }
2093     }
2094 
2095     /**
2096      * Returns the value of the {@code linkColor} property.
2097      * @return the value of the {@code linkColor} property
2098      */
2099     @JsxGetter
2100     public String getLinkColor() {
2101         final HTMLElement body = getBody();
2102         if (body instanceof HTMLBodyElement element) {
2103             return element.getLink();
2104         }
2105         return null;
2106     }
2107 
2108     /**
2109      * Sets the value of the {@code linkColor} property.
2110      * @param color the value of the {@code linkColor} property
2111      */
2112     @JsxSetter
2113     public void setLinkColor(final String color) {
2114         final HTMLElement body = getBody();
2115         if (body instanceof HTMLBodyElement element) {
2116             element.setLink(color);
2117         }
2118     }
2119 
2120     /**
2121      * Returns the value of the {@code vlinkColor} property.
2122      * @return the value of the {@code vlinkColor} property
2123      */
2124     @JsxGetter
2125     public String getVlinkColor() {
2126         final HTMLElement body = getBody();
2127         if (body instanceof HTMLBodyElement element) {
2128             return element.getVLink();
2129         }
2130         return null;
2131     }
2132 
2133     /**
2134      * Sets the value of the {@code vlinkColor} property.
2135      * @param color the value of the {@code vlinkColor} property
2136      */
2137     @JsxSetter
2138     public void setVlinkColor(final String color) {
2139         final HTMLElement body = getBody();
2140         if (body instanceof HTMLBodyElement element) {
2141             element.setVLink(color);
2142         }
2143     }
2144 
2145     /**
2146      * {@inheritDoc}
2147      */
2148     @Override
2149     @JsxGetter
2150     public Element getLastElementChild() {
2151         return super.getLastElementChild();
2152     }
2153 
2154     /**
2155      * {@inheritDoc}
2156      */
2157     @Override
2158     @JsxGetter
2159     public Element getFirstElementChild() {
2160         return super.getFirstElementChild();
2161     }
2162 
2163     /**
2164      * Returns the {@code xmlEncoding} property.
2165      * @return the {@code xmlEncoding} property
2166      */
2167     @JsxGetter({CHROME, EDGE})
2168     public String getXmlEncoding() {
2169         return getPage().getXmlEncoding();
2170     }
2171 
2172     /**
2173      * Returns the {@code xmlStandalone} property.
2174      * @return the {@code xmlStandalone} property
2175      */
2176     @JsxGetter({CHROME, EDGE})
2177     public boolean isXmlStandalone() {
2178         return getPage().getXmlStandalone();
2179     }
2180 
2181     /**
2182      * Returns the {@code xmlVersion} property.
2183      * @return the {@code xmlVersion} property
2184      */
2185     @JsxGetter({CHROME, EDGE})
2186     public String getXmlVersion() {
2187         return getPage().getXmlVersion();
2188     }
2189 
2190     /**
2191      * Returns the {@code onabort} event handler for this element.
2192      * @return the {@code onabort} event handler for this element
2193      */
2194     @JsxGetter
2195     public Function getOnabort() {
2196         return getEventHandler(Event.TYPE_ABORT);
2197     }
2198 
2199     /**
2200      * Sets the {@code onabort} event handler for this element.
2201      * @param onabort the {@code onabort} event handler for this element
2202      */
2203     @JsxSetter
2204     public void setOnabort(final Object onabort) {
2205         setEventHandler(Event.TYPE_ABORT, onabort);
2206     }
2207 
2208     /**
2209      * Returns the {@code onauxclick} event handler for this element.
2210      * @return the {@code onauxclick} event handler for this element
2211      */
2212     @JsxGetter({CHROME, EDGE})
2213     public Function getOnauxclick() {
2214         return getEventHandler(Event.TYPE_AUXCLICK);
2215     }
2216 
2217     /**
2218      * Sets the {@code onauxclick} event handler for this element.
2219      * @param onauxclick the {@code onauxclick} event handler for this element
2220      */
2221     @JsxSetter({CHROME, EDGE})
2222     public void setOnauxclick(final Object onauxclick) {
2223         setEventHandler(Event.TYPE_AUXCLICK, onauxclick);
2224     }
2225 
2226     /**
2227      * Returns the {@code onbeforecopy} event handler for this element.
2228      * @return the {@code onbeforecopy} event handler for this element
2229      */
2230     @JsxGetter({CHROME, EDGE})
2231     public Function getOnbeforecopy() {
2232         return getEventHandler(Event.TYPE_BEFORECOPY);
2233     }
2234 
2235     /**
2236      * Sets the {@code onbeforecopy} event handler for this element.
2237      * @param onbeforecopy the {@code onbeforecopy} event handler for this element
2238      */
2239     @JsxSetter({CHROME, EDGE})
2240     public void setOnbeforecopy(final Object onbeforecopy) {
2241         setEventHandler(Event.TYPE_BEFORECOPY, onbeforecopy);
2242     }
2243 
2244     /**
2245      * Returns the {@code onbeforecut} event handler for this element.
2246      * @return the {@code onbeforecut} event handler for this element
2247      */
2248     @JsxGetter({CHROME, EDGE})
2249     public Function getOnbeforecut() {
2250         return getEventHandler(Event.TYPE_BEFORECUT);
2251     }
2252 
2253     /**
2254      * Sets the {@code onbeforecut} event handler for this element.
2255      * @param onbeforecut the {@code onbeforecut} event handler for this element
2256      */
2257     @JsxSetter({CHROME, EDGE})
2258     public void setOnbeforecut(final Object onbeforecut) {
2259         setEventHandler(Event.TYPE_BEFORECUT, onbeforecut);
2260     }
2261 
2262     /**
2263      * Returns the {@code onbeforepaste} event handler for this element.
2264      * @return the {@code onbeforepaste} event handler for this element
2265      */
2266     @JsxGetter({CHROME, EDGE})
2267     public Function getOnbeforepaste() {
2268         return getEventHandler(Event.TYPE_BEFOREPASTE);
2269     }
2270 
2271     /**
2272      * Sets the {@code onbeforepaste} event handler for this element.
2273      * @param onbeforepaste the {@code onbeforepaste} event handler for this element
2274      */
2275     @JsxSetter({CHROME, EDGE})
2276     public void setOnbeforepaste(final Object onbeforepaste) {
2277         setEventHandler(Event.TYPE_BEFOREPASTE, onbeforepaste);
2278     }
2279 
2280     /**
2281      * Returns the {@code oncancel} event handler for this element.
2282      * @return the {@code oncancel} event handler for this element
2283      */
2284     @JsxGetter({CHROME, EDGE})
2285     public Function getOncancel() {
2286         return getEventHandler(Event.TYPE_CANCEL);
2287     }
2288 
2289     /**
2290      * Sets the {@code oncancel} event handler for this element.
2291      * @param oncancel the {@code oncancel} event handler for this element
2292      */
2293     @JsxSetter({CHROME, EDGE})
2294     public void setOncancel(final Object oncancel) {
2295         setEventHandler(Event.TYPE_CANCEL, oncancel);
2296     }
2297 
2298     /**
2299      * Returns the {@code oncanplay} event handler for this element.
2300      * @return the {@code oncanplay} event handler for this element
2301      */
2302     @JsxGetter
2303     public Function getOncanplay() {
2304         return getEventHandler(Event.TYPE_CANPLAY);
2305     }
2306 
2307     /**
2308      * Sets the {@code oncanplay} event handler for this element.
2309      * @param oncanplay the {@code oncanplay} event handler for this element
2310      */
2311     @JsxSetter
2312     public void setOncanplay(final Object oncanplay) {
2313         setEventHandler(Event.TYPE_CANPLAY, oncanplay);
2314     }
2315 
2316     /**
2317      * Returns the {@code oncanplaythrough} event handler for this element.
2318      * @return the {@code oncanplaythrough} event handler for this element
2319      */
2320     @JsxGetter
2321     public Function getOncanplaythrough() {
2322         return getEventHandler(Event.TYPE_CANPLAYTHROUGH);
2323     }
2324 
2325     /**
2326      * Sets the {@code oncanplaythrough} event handler for this element.
2327      * @param oncanplaythrough the {@code oncanplaythrough} event handler for this element
2328      */
2329     @JsxSetter
2330     public void setOncanplaythrough(final Object oncanplaythrough) {
2331         setEventHandler(Event.TYPE_CANPLAYTHROUGH, oncanplaythrough);
2332     }
2333 
2334     /**
2335      * Returns the {@code onchange} event handler for this element.
2336      * @return the {@code onchange} event handler for this element
2337      */
2338     @JsxGetter
2339     public Function getOnchange() {
2340         return getEventHandler(Event.TYPE_CHANGE);
2341     }
2342 
2343     /**
2344      * Sets the {@code onchange} event handler for this element.
2345      * @param onchange the {@code onchange} event handler for this element
2346      */
2347     @JsxSetter
2348     public void setOnchange(final Object onchange) {
2349         setEventHandler(Event.TYPE_CHANGE, onchange);
2350     }
2351 
2352     /**
2353      * Returns the {@code onclose} event handler for this element.
2354      * @return the {@code onclose} event handler for this element
2355      */
2356     @JsxGetter({CHROME, EDGE})
2357     public Function getOnclose() {
2358         return getEventHandler(Event.TYPE_CLOSE);
2359     }
2360 
2361     /**
2362      * Sets the {@code onclose} event handler for this element.
2363      * @param onclose the {@code onclose} event handler for this element
2364      */
2365     @JsxSetter({CHROME, EDGE})
2366     public void setOnclose(final Object onclose) {
2367         setEventHandler(Event.TYPE_CLOSE, onclose);
2368     }
2369 
2370     /**
2371      * Returns the {@code oncopy} event handler for this element.
2372      * @return the {@code oncopy} event handler for this element
2373      */
2374     @JsxGetter
2375     public Function getOncopy() {
2376         return getEventHandler(Event.TYPE_COPY);
2377     }
2378 
2379     /**
2380      * Sets the {@code oncopy} event handler for this element.
2381      * @param oncopy the {@code oncopy} event handler for this element
2382      */
2383     @JsxSetter
2384     public void setOncopy(final Object oncopy) {
2385         setEventHandler(Event.TYPE_COPY, oncopy);
2386     }
2387 
2388     /**
2389      * Returns the {@code oncuechange} event handler for this element.
2390      * @return the {@code oncuechange} event handler for this element
2391      */
2392     @JsxGetter({CHROME, EDGE})
2393     public Function getOncuechange() {
2394         return getEventHandler(Event.TYPE_CUECHANGE);
2395     }
2396 
2397     /**
2398      * Sets the {@code oncuechange} event handler for this element.
2399      * @param oncuechange the {@code oncuechange} event handler for this element
2400      */
2401     @JsxSetter({CHROME, EDGE})
2402     public void setOncuechange(final Object oncuechange) {
2403         setEventHandler(Event.TYPE_CUECHANGE, oncuechange);
2404     }
2405 
2406     /**
2407      * Returns the {@code oncut} event handler for this element.
2408      * @return the {@code oncut} event handler for this element
2409      */
2410     @JsxGetter
2411     public Function getOncut() {
2412         return getEventHandler(Event.TYPE_CUT);
2413     }
2414 
2415     /**
2416      * Sets the {@code oncut} event handler for this element.
2417      * @param oncut the {@code oncut} event handler for this element
2418      */
2419     @JsxSetter
2420     public void setOncut(final Object oncut) {
2421         setEventHandler(Event.TYPE_CUT, oncut);
2422     }
2423 
2424     /**
2425      * Returns the {@code ondrag} event handler for this element.
2426      * @return the {@code ondrag} event handler for this element
2427      */
2428     @JsxGetter
2429     public Function getOndrag() {
2430         return getEventHandler(Event.TYPE_DRAG);
2431     }
2432 
2433     /**
2434      * Sets the {@code ondrag} event handler for this element.
2435      * @param ondrag the {@code ondrag} event handler for this element
2436      */
2437     @JsxSetter
2438     public void setOndrag(final Object ondrag) {
2439         setEventHandler(Event.TYPE_DRAG, ondrag);
2440     }
2441 
2442     /**
2443      * Returns the {@code ondragend} event handler for this element.
2444      * @return the {@code ondragend} event handler for this element
2445      */
2446     @JsxGetter
2447     public Function getOndragend() {
2448         return getEventHandler(Event.TYPE_DRAGEND);
2449     }
2450 
2451     /**
2452      * Sets the {@code ondragend} event handler for this element.
2453      * @param ondragend the {@code ondragend} event handler for this element
2454      */
2455     @JsxSetter
2456     public void setOndragend(final Object ondragend) {
2457         setEventHandler(Event.TYPE_DRAGEND, ondragend);
2458     }
2459 
2460     /**
2461      * Returns the {@code ondragenter} event handler for this element.
2462      * @return the {@code ondragenter} event handler for this element
2463      */
2464     @JsxGetter
2465     public Function getOndragenter() {
2466         return getEventHandler(Event.TYPE_DRAGENTER);
2467     }
2468 
2469     /**
2470      * Sets the {@code ondragenter} event handler for this element.
2471      * @param ondragenter the {@code ondragenter} event handler for this element
2472      */
2473     @JsxSetter
2474     public void setOndragenter(final Object ondragenter) {
2475         setEventHandler(Event.TYPE_DRAGENTER, ondragenter);
2476     }
2477 
2478     /**
2479      * Returns the {@code ondragleave} event handler for this element.
2480      * @return the {@code ondragleave} event handler for this element
2481      */
2482     @JsxGetter
2483     public Function getOndragleave() {
2484         return getEventHandler(Event.TYPE_DRAGLEAVE);
2485     }
2486 
2487     /**
2488      * Sets the {@code ondragleave} event handler for this element.
2489      * @param ondragleave the {@code ondragleave} event handler for this element
2490      */
2491     @JsxSetter
2492     public void setOndragleave(final Object ondragleave) {
2493         setEventHandler(Event.TYPE_DRAGLEAVE, ondragleave);
2494     }
2495 
2496     /**
2497      * Returns the {@code ondragover} event handler for this element.
2498      * @return the {@code ondragover} event handler for this element
2499      */
2500     @JsxGetter
2501     public Function getOndragover() {
2502         return getEventHandler(Event.TYPE_DRAGOVER);
2503     }
2504 
2505     /**
2506      * Sets the {@code ondragover} event handler for this element.
2507      * @param ondragover the {@code ondragover} event handler for this element
2508      */
2509     @JsxSetter
2510     public void setOndragover(final Object ondragover) {
2511         setEventHandler(Event.TYPE_DRAGOVER, ondragover);
2512     }
2513 
2514     /**
2515      * Returns the {@code ondragstart} event handler for this element.
2516      * @return the {@code ondragstart} event handler for this element
2517      */
2518     @JsxGetter
2519     public Function getOndragstart() {
2520         return getEventHandler(Event.TYPE_DRAGSTART);
2521     }
2522 
2523     /**
2524      * Sets the {@code ondragstart} event handler for this element.
2525      * @param ondragstart the {@code ondragstart} event handler for this element
2526      */
2527     @JsxSetter
2528     public void setOndragstart(final Object ondragstart) {
2529         setEventHandler(Event.TYPE_DRAGSTART, ondragstart);
2530     }
2531 
2532     /**
2533      * Returns the {@code ondrop} event handler for this element.
2534      * @return the {@code ondrop} event handler for this element
2535      */
2536     @JsxGetter
2537     public Function getOndrop() {
2538         return getEventHandler(Event.TYPE_DROP);
2539     }
2540 
2541     /**
2542      * Sets the {@code ondrop} event handler for this element.
2543      * @param ondrop the {@code ondrop} event handler for this element
2544      */
2545     @JsxSetter
2546     public void setOndrop(final Object ondrop) {
2547         setEventHandler(Event.TYPE_DROP, ondrop);
2548     }
2549 
2550     /**
2551      * Returns the {@code ondurationchange} event handler for this element.
2552      * @return the {@code ondurationchange} event handler for this element
2553      */
2554     @JsxGetter
2555     public Function getOndurationchange() {
2556         return getEventHandler(Event.TYPE_DURATIONCHANGE);
2557     }
2558 
2559     /**
2560      * Sets the {@code ondurationchange} event handler for this element.
2561      * @param ondurationchange the {@code ondurationchange} event handler for this element
2562      */
2563     @JsxSetter
2564     public void setOndurationchange(final Object ondurationchange) {
2565         setEventHandler(Event.TYPE_DURATIONCHANGE, ondurationchange);
2566     }
2567 
2568     /**
2569      * Returns the {@code onemptied} event handler for this element.
2570      * @return the {@code onemptied} event handler for this element
2571      */
2572     @JsxGetter
2573     public Function getOnemptied() {
2574         return getEventHandler(Event.TYPE_EMPTIED);
2575     }
2576 
2577     /**
2578      * Sets the {@code onemptied} event handler for this element.
2579      * @param onemptied the {@code onemptied} event handler for this element
2580      */
2581     @JsxSetter
2582     public void setOnemptied(final Object onemptied) {
2583         setEventHandler(Event.TYPE_EMPTIED, onemptied);
2584     }
2585 
2586     /**
2587      * Returns the {@code onended} event handler for this element.
2588      * @return the {@code onended} event handler for this element
2589      */
2590     @JsxGetter
2591     public Function getOnended() {
2592         return getEventHandler(Event.TYPE_ENDED);
2593     }
2594 
2595     /**
2596      * Sets the {@code onended} event handler for this element.
2597      * @param onended the {@code onended} event handler for this element
2598      */
2599     @JsxSetter
2600     public void setOnended(final Object onended) {
2601         setEventHandler(Event.TYPE_ENDED, onended);
2602     }
2603 
2604     /**
2605      * Returns the {@code ongotpointercapture} event handler for this element.
2606      * @return the {@code ongotpointercapture} event handler for this element
2607      */
2608     @JsxGetter({CHROME, EDGE})
2609     public Function getOngotpointercapture() {
2610         return getEventHandler(Event.TYPE_GOTPOINTERCAPTURE);
2611     }
2612 
2613     /**
2614      * Sets the {@code ongotpointercapture} event handler for this element.
2615      * @param ongotpointercapture the {@code ongotpointercapture} event handler for this element
2616      */
2617     @JsxSetter({CHROME, EDGE})
2618     public void setOngotpointercapture(final Object ongotpointercapture) {
2619         setEventHandler(Event.TYPE_GOTPOINTERCAPTURE, ongotpointercapture);
2620     }
2621 
2622     /**
2623      * Returns the {@code oninvalid} event handler for this element.
2624      * @return the {@code oninvalid} event handler for this element
2625      */
2626     @JsxGetter
2627     public Function getOninvalid() {
2628         return getEventHandler(Event.TYPE_INVALID);
2629     }
2630 
2631     /**
2632      * Sets the {@code oninvalid} event handler for this element.
2633      * @param oninvalid the {@code oninvalid} event handler for this element
2634      */
2635     @JsxSetter
2636     public void setOninvalid(final Object oninvalid) {
2637         setEventHandler(Event.TYPE_INVALID, oninvalid);
2638     }
2639 
2640     /**
2641      * Returns the {@code onload} event handler for this element.
2642      * @return the {@code onload} event handler for this element
2643      */
2644     @JsxGetter
2645     public Function getOnload() {
2646         return getEventHandler(Event.TYPE_LOAD);
2647     }
2648 
2649     /**
2650      * Sets the {@code onload} event handler for this element.
2651      * @param onload the {@code onload} event handler for this element
2652      */
2653     @JsxSetter
2654     public void setOnload(final Object onload) {
2655         setEventHandler(Event.TYPE_LOAD, onload);
2656     }
2657 
2658     /**
2659      * Returns the {@code onloadeddata} event handler for this element.
2660      * @return the {@code onloadeddata} event handler for this element
2661      */
2662     @JsxGetter
2663     public Function getOnloadeddata() {
2664         return getEventHandler(Event.TYPE_LOADEDDATA);
2665     }
2666 
2667     /**
2668      * Sets the {@code onloadeddata} event handler for this element.
2669      * @param onloadeddata the {@code onloadeddata} event handler for this element
2670      */
2671     @JsxSetter
2672     public void setOnloadeddata(final Object onloadeddata) {
2673         setEventHandler(Event.TYPE_LOADEDDATA, onloadeddata);
2674     }
2675 
2676     /**
2677      * Returns the {@code onloadedmetadata} event handler for this element.
2678      * @return the {@code onloadedmetadata} event handler for this element
2679      */
2680     @JsxGetter
2681     public Function getOnloadedmetadata() {
2682         return getEventHandler(Event.TYPE_LOADEDMETADATA);
2683     }
2684 
2685     /**
2686      * Sets the {@code onloadedmetadata} event handler for this element.
2687      * @param onloadedmetadata the {@code onloadedmetadata} event handler for this element
2688      */
2689     @JsxSetter
2690     public void setOnloadedmetadata(final Object onloadedmetadata) {
2691         setEventHandler(Event.TYPE_LOADEDMETADATA, onloadedmetadata);
2692     }
2693 
2694     /**
2695      * Returns the {@code onloadstart} event handler for this element.
2696      * @return the {@code onloadstart} event handler for this element
2697      */
2698     @JsxGetter
2699     public Function getOnloadstart() {
2700         return getEventHandler(Event.TYPE_LOAD_START);
2701     }
2702 
2703     /**
2704      * Sets the {@code onloadstart} event handler for this element.
2705      * @param onloadstart the {@code onloadstart} event handler for this element
2706      */
2707     @JsxSetter
2708     public void setOnloadstart(final Object onloadstart) {
2709         setEventHandler(Event.TYPE_LOAD_START, onloadstart);
2710     }
2711 
2712     /**
2713      * Returns the {@code onlostpointercapture} event handler for this element.
2714      * @return the {@code onlostpointercapture} event handler for this element
2715      */
2716     @JsxGetter({CHROME, EDGE})
2717     public Function getOnlostpointercapture() {
2718         return getEventHandler(Event.TYPE_LOSTPOINTERCAPTURE);
2719     }
2720 
2721     /**
2722      * Sets the {@code onlostpointercapture} event handler for this element.
2723      * @param onlostpointercapture the {@code onlostpointercapture} event handler for this element
2724      */
2725     @JsxSetter({CHROME, EDGE})
2726     public void setOnlostpointercapture(final Object onlostpointercapture) {
2727         setEventHandler(Event.TYPE_LOSTPOINTERCAPTURE, onlostpointercapture);
2728     }
2729 
2730     /**
2731      * Returns the {@code onmouseenter} event handler for this element.
2732      * @return the {@code onmouseenter} event handler for this element
2733      */
2734     @JsxGetter
2735     public Function getOnmouseenter() {
2736         return getEventHandler(Event.TYPE_MOUDEENTER);
2737     }
2738 
2739     /**
2740      * Sets the {@code onmouseenter} event handler for this element.
2741      * @param onmouseenter the {@code onmouseenter} event handler for this element
2742      */
2743     @JsxSetter
2744     public void setOnmouseenter(final Object onmouseenter) {
2745         setEventHandler(Event.TYPE_MOUDEENTER, onmouseenter);
2746     }
2747 
2748     /**
2749      * Returns the {@code onmouseleave} event handler for this element.
2750      * @return the {@code onmouseleave} event handler for this element
2751      */
2752     @JsxGetter
2753     public Function getOnmouseleave() {
2754         return getEventHandler(Event.TYPE_MOUSELEAVE);
2755     }
2756 
2757     /**
2758      * Sets the {@code onmouseleave} event handler for this element.
2759      * @param onmouseleave the {@code onmouseleave} event handler for this element
2760      */
2761     @JsxSetter
2762     public void setOnmouseleave(final Object onmouseleave) {
2763         setEventHandler(Event.TYPE_MOUSELEAVE, onmouseleave);
2764     }
2765 
2766     /**
2767      * Returns the {@code onmousewheel} event handler for this element.
2768      * @return the {@code onmousewheel} event handler for this element
2769      */
2770     @JsxGetter({CHROME, EDGE})
2771     public Function getOnmousewheel() {
2772         return getEventHandler(Event.TYPE_MOUSEWHEEL);
2773     }
2774 
2775     /**
2776      * Sets the {@code onmousewheel} event handler for this element.
2777      * @param onmousewheel the {@code onmousewheel} event handler for this element
2778      */
2779     @JsxSetter({CHROME, EDGE})
2780     public void setOnmousewheel(final Object onmousewheel) {
2781         setEventHandler(Event.TYPE_MOUSEWHEEL, onmousewheel);
2782     }
2783 
2784     /**
2785      * Returns the {@code onpaste} event handler for this element.
2786      * @return the {@code onpaste} event handler for this element
2787      */
2788     @JsxGetter
2789     public Function getOnpaste() {
2790         return getEventHandler(Event.TYPE_PASTE);
2791     }
2792 
2793     /**
2794      * Sets the {@code onpaste} event handler for this element.
2795      * @param onpaste the {@code onpaste} event handler for this element
2796      */
2797     @JsxSetter
2798     public void setOnpaste(final Object onpaste) {
2799         setEventHandler(Event.TYPE_PASTE, onpaste);
2800     }
2801 
2802     /**
2803      * Returns the {@code onpause} event handler for this element.
2804      * @return the {@code onpause} event handler for this element
2805      */
2806     @JsxGetter
2807     public Function getOnpause() {
2808         return getEventHandler(Event.TYPE_PAUSE);
2809     }
2810 
2811     /**
2812      * Sets the {@code onpause} event handler for this element.
2813      * @param onpause the {@code onpause} event handler for this element
2814      */
2815     @JsxSetter
2816     public void setOnpause(final Object onpause) {
2817         setEventHandler(Event.TYPE_PAUSE, onpause);
2818     }
2819 
2820     /**
2821      * Returns the {@code onplay} event handler for this element.
2822      * @return the {@code onplay} event handler for this element
2823      */
2824     @JsxGetter
2825     public Function getOnplay() {
2826         return getEventHandler(Event.TYPE_PLAY);
2827     }
2828 
2829     /**
2830      * Sets the {@code onplay} event handler for this element.
2831      * @param onplay the {@code onplay} event handler for this element
2832      */
2833     @JsxSetter
2834     public void setOnplay(final Object onplay) {
2835         setEventHandler(Event.TYPE_PLAY, onplay);
2836     }
2837 
2838     /**
2839      * Returns the {@code onplaying} event handler for this element.
2840      * @return the {@code onplaying} event handler for this element
2841      */
2842     @JsxGetter
2843     public Function getOnplaying() {
2844         return getEventHandler(Event.TYPE_PLAYING);
2845     }
2846 
2847     /**
2848      * Sets the {@code onplaying} event handler for this element.
2849      * @param onplaying the {@code onplaying} event handler for this element
2850      */
2851     @JsxSetter
2852     public void setOnplaying(final Object onplaying) {
2853         setEventHandler(Event.TYPE_PLAYING, onplaying);
2854     }
2855 
2856     /**
2857      * Returns the {@code onpointercancel} event handler for this element.
2858      * @return the {@code onpointercancel} event handler for this element
2859      */
2860     @JsxGetter({CHROME, EDGE})
2861     public Function getOnpointercancel() {
2862         return getEventHandler(Event.TYPE_POINTERCANCEL);
2863     }
2864 
2865     /**
2866      * Sets the {@code onpointercancel} event handler for this element.
2867      * @param onpointercancel the {@code onpointercancel} event handler for this element
2868      */
2869     @JsxSetter({CHROME, EDGE})
2870     public void setOnpointercancel(final Object onpointercancel) {
2871         setEventHandler(Event.TYPE_POINTERCANCEL, onpointercancel);
2872     }
2873 
2874     /**
2875      * Returns the {@code onpointerdown} event handler for this element.
2876      * @return the {@code onpointerdown} event handler for this element
2877      */
2878     @JsxGetter({CHROME, EDGE})
2879     public Function getOnpointerdown() {
2880         return getEventHandler(Event.TYPE_POINTERDOWN);
2881     }
2882 
2883     /**
2884      * Sets the {@code onpointerdown} event handler for this element.
2885      * @param onpointerdown the {@code onpointerdown} event handler for this element
2886      */
2887     @JsxSetter({CHROME, EDGE})
2888     public void setOnpointerdown(final Object onpointerdown) {
2889         setEventHandler(Event.TYPE_POINTERDOWN, onpointerdown);
2890     }
2891 
2892     /**
2893      * Returns the {@code onpointerenter} event handler for this element.
2894      * @return the {@code onpointerenter} event handler for this element
2895      */
2896     @JsxGetter({CHROME, EDGE})
2897     public Function getOnpointerenter() {
2898         return getEventHandler(Event.TYPE_POINTERENTER);
2899     }
2900 
2901     /**
2902      * Sets the {@code onpointerenter} event handler for this element.
2903      * @param onpointerenter the {@code onpointerenter} event handler for this element
2904      */
2905     @JsxSetter({CHROME, EDGE})
2906     public void setOnpointerenter(final Object onpointerenter) {
2907         setEventHandler(Event.TYPE_POINTERENTER, onpointerenter);
2908     }
2909 
2910     /**
2911      * Returns the {@code onpointerleave} event handler for this element.
2912      * @return the {@code onpointerleave} event handler for this element
2913      */
2914     @JsxGetter({CHROME, EDGE})
2915     public Function getOnpointerleave() {
2916         return getEventHandler(Event.TYPE_POINTERLEAVE);
2917     }
2918 
2919     /**
2920      * Sets the {@code onpointerleave} event handler for this element.
2921      * @param onpointerleave the {@code onpointerleave} event handler for this element
2922      */
2923     @JsxSetter({CHROME, EDGE})
2924     public void setOnpointerleave(final Object onpointerleave) {
2925         setEventHandler(Event.TYPE_POINTERLEAVE, onpointerleave);
2926     }
2927 
2928     /**
2929      * Returns the {@code onpointerlockchange} event handler for this element.
2930      * @return the {@code onpointerlockchange} event handler for this element
2931      */
2932     @JsxGetter({CHROME, EDGE})
2933     public Function getOnpointerlockchange() {
2934         return getEventHandler(Event.TYPE_POINTERLOCKCHANGE);
2935     }
2936 
2937     /**
2938      * Sets the {@code onpointerlockchange} event handler for this element.
2939      * @param onpointerlockchange the {@code onpointerlockchange} event handler for this element
2940      */
2941     @JsxSetter({CHROME, EDGE})
2942     public void setOnpointerlockchange(final Object onpointerlockchange) {
2943         setEventHandler(Event.TYPE_POINTERLOCKCHANGE, onpointerlockchange);
2944     }
2945 
2946     /**
2947      * Returns the {@code onpointerlockerror} event handler for this element.
2948      * @return the {@code onpointerlockerror} event handler for this element
2949      */
2950     @JsxGetter({CHROME, EDGE})
2951     public Function getOnpointerlockerror() {
2952         return getEventHandler(Event.TYPE_POINTERLOCKERROR);
2953     }
2954 
2955     /**
2956      * Sets the {@code onpointerlockerror} event handler for this element.
2957      * @param onpointerlockerror the {@code onpointerlockerror} event handler for this element
2958      */
2959     @JsxSetter({CHROME, EDGE})
2960     public void setOnpointerlockerror(final Object onpointerlockerror) {
2961         setEventHandler(Event.TYPE_POINTERLOCKERROR, onpointerlockerror);
2962     }
2963 
2964     /**
2965      * Returns the {@code onpointermove} event handler for this element.
2966      * @return the {@code onpointermove} event handler for this element
2967      */
2968     @JsxGetter({CHROME, EDGE})
2969     public Function getOnpointermove() {
2970         return getEventHandler(Event.TYPE_POINTERMOVE);
2971     }
2972 
2973     /**
2974      * Sets the {@code onpointermove} event handler for this element.
2975      * @param onpointermove the {@code onpointermove} event handler for this element
2976      */
2977     @JsxSetter({CHROME, EDGE})
2978     public void setOnpointermove(final Object onpointermove) {
2979         setEventHandler(Event.TYPE_POINTERMOVE, onpointermove);
2980     }
2981 
2982     /**
2983      * Returns the {@code onpointerout} event handler for this element.
2984      * @return the {@code onpointerout} event handler for this element
2985      */
2986     @JsxGetter({CHROME, EDGE})
2987     public Function getOnpointerout() {
2988         return getEventHandler(Event.TYPE_POINTEROUT);
2989     }
2990 
2991     /**
2992      * Sets the {@code onpointerout} event handler for this element.
2993      * @param onpointerout the {@code onpointerout} event handler for this element
2994      */
2995     @JsxSetter({CHROME, EDGE})
2996     public void setOnpointerout(final Object onpointerout) {
2997         setEventHandler(Event.TYPE_POINTEROUT, onpointerout);
2998     }
2999 
3000     /**
3001      * Returns the {@code onpointerover} event handler for this element.
3002      * @return the {@code onpointerover} event handler for this element
3003      */
3004     @JsxGetter({CHROME, EDGE})
3005     public Function getOnpointerover() {
3006         return getEventHandler(Event.TYPE_POINTEROVER);
3007     }
3008 
3009     /**
3010      * Sets the {@code onpointerover} event handler for this element.
3011      * @param onpointerover the {@code onpointerover} event handler for this element
3012      */
3013     @JsxSetter({CHROME, EDGE})
3014     public void setOnpointerover(final Object onpointerover) {
3015         setEventHandler(Event.TYPE_POINTEROVER, onpointerover);
3016     }
3017 
3018     /**
3019      * Returns the {@code onpointerup} event handler for this element.
3020      * @return the {@code onpointerup} event handler for this element
3021      */
3022     @JsxGetter({CHROME, EDGE})
3023     public Function getOnpointerup() {
3024         return getEventHandler(Event.TYPE_POINTERUP);
3025     }
3026 
3027     /**
3028      * Sets the {@code onpointerup} event handler for this element.
3029      * @param onpointerup the {@code onpointerup} event handler for this element
3030      */
3031     @JsxSetter({CHROME, EDGE})
3032     public void setOnpointerup(final Object onpointerup) {
3033         setEventHandler(Event.TYPE_POINTERUP, onpointerup);
3034     }
3035 
3036     /**
3037      * Returns the {@code onprogress} event handler for this element.
3038      * @return the {@code onprogress} event handler for this element
3039      */
3040     @JsxGetter
3041     public Function getOnprogress() {
3042         return getEventHandler(Event.TYPE_PROGRESS);
3043     }
3044 
3045     /**
3046      * Sets the {@code onprogress} event handler for this element.
3047      * @param onprogress the {@code onprogress} event handler for this element
3048      */
3049     @JsxSetter
3050     public void setOnprogress(final Object onprogress) {
3051         setEventHandler(Event.TYPE_PROGRESS, onprogress);
3052     }
3053 
3054     /**
3055      * Returns the {@code onratechange} event handler for this element.
3056      * @return the {@code onratechange} event handler for this element
3057      */
3058     @JsxGetter
3059     public Function getOnratechange() {
3060         return getEventHandler(Event.TYPE_RATECHANGE);
3061     }
3062 
3063     /**
3064      * Sets the {@code onratechange} event handler for this element.
3065      * @param onratechange the {@code onratechange} event handler for this element
3066      */
3067     @JsxSetter
3068     public void setOnratechange(final Object onratechange) {
3069         setEventHandler(Event.TYPE_RATECHANGE, onratechange);
3070     }
3071 
3072     /**
3073      * Returns the {@code onreadystatechange} event handler for this element.
3074      * @return the {@code onreadystatechange} event handler for this element
3075      */
3076     @JsxGetter
3077     public Function getOnreadystatechange() {
3078         return getEventHandler(Event.TYPE_READY_STATE_CHANGE);
3079     }
3080 
3081     /**
3082      * Sets the {@code onreadystatechange} event handler for this element.
3083      * @param onreadystatechange the {@code onreadystatechange} event handler for this element
3084      */
3085     @JsxSetter
3086     public void setOnreadystatechange(final Object onreadystatechange) {
3087         setEventHandler(Event.TYPE_READY_STATE_CHANGE, onreadystatechange);
3088     }
3089 
3090     /**
3091      * Returns the {@code onreset} event handler for this element.
3092      * @return the {@code onreset} event handler for this element
3093      */
3094     @JsxGetter
3095     public Function getOnreset() {
3096         return getEventHandler(Event.TYPE_RESET);
3097     }
3098 
3099     /**
3100      * Sets the {@code onreset} event handler for this element.
3101      * @param onreset the {@code onreset} event handler for this element
3102      */
3103     @JsxSetter
3104     public void setOnreset(final Object onreset) {
3105         setEventHandler(Event.TYPE_RESET, onreset);
3106     }
3107 
3108     /**
3109      * Returns the {@code onscroll} event handler for this element.
3110      * @return the {@code onscroll} event handler for this element
3111      */
3112     @JsxGetter
3113     public Function getOnscroll() {
3114         return getEventHandler(Event.TYPE_SCROLL);
3115     }
3116 
3117     /**
3118      * Sets the {@code onscroll} event handler for this element.
3119      * @param onscroll the {@code onscroll} event handler for this element
3120      */
3121     @JsxSetter
3122     public void setOnscroll(final Object onscroll) {
3123         setEventHandler(Event.TYPE_SCROLL, onscroll);
3124     }
3125 
3126     /**
3127      * Returns the {@code onsearch} event handler for this element.
3128      * @return the {@code onsearch} event handler for this element
3129      */
3130     @JsxGetter({CHROME, EDGE})
3131     public Function getOnsearch() {
3132         return getEventHandler(Event.TYPE_SEARCH);
3133     }
3134 
3135     /**
3136      * Sets the {@code onsearch} event handler for this element.
3137      * @param onsearch the {@code onsearch} event handler for this element
3138      */
3139     @JsxSetter({CHROME, EDGE})
3140     public void setOnsearch(final Object onsearch) {
3141         setEventHandler(Event.TYPE_SEARCH, onsearch);
3142     }
3143 
3144     /**
3145      * Returns the {@code onseeked} event handler for this element.
3146      * @return the {@code onseeked} event handler for this element
3147      */
3148     @JsxGetter
3149     public Function getOnseeked() {
3150         return getEventHandler(Event.TYPE_SEEKED);
3151     }
3152 
3153     /**
3154      * Sets the {@code onseeked} event handler for this element.
3155      * @param onseeked the {@code onseeked} event handler for this element
3156      */
3157     @JsxSetter
3158     public void setOnseeked(final Object onseeked) {
3159         setEventHandler(Event.TYPE_SEEKED, onseeked);
3160     }
3161 
3162     /**
3163      * Returns the {@code onseeking} event handler for this element.
3164      * @return the {@code onseeking} event handler for this element
3165      */
3166     @JsxGetter
3167     public Function getOnseeking() {
3168         return getEventHandler(Event.TYPE_SEEKING);
3169     }
3170 
3171     /**
3172      * Sets the {@code onseeking} event handler for this element.
3173      * @param onseeking the {@code onseeking} event handler for this element
3174      */
3175     @JsxSetter
3176     public void setOnseeking(final Object onseeking) {
3177         setEventHandler(Event.TYPE_SEEKING, onseeking);
3178     }
3179 
3180     /**
3181      * Returns the {@code onselect} event handler for this element.
3182      * @return the {@code onselect} event handler for this element
3183      */
3184     @JsxGetter
3185     public Function getOnselect() {
3186         return getEventHandler(Event.TYPE_SELECT);
3187     }
3188 
3189     /**
3190      * Sets the {@code onselect} event handler for this element.
3191      * @param onselect the {@code onselect} event handler for this element
3192      */
3193     @JsxSetter
3194     public void setOnselect(final Object onselect) {
3195         setEventHandler(Event.TYPE_SELECT, onselect);
3196     }
3197 
3198     /**
3199      * Returns the {@code onselectionchange} event handler for this element.
3200      * @return the {@code onselectionchange} event handler for this element
3201      */
3202     @JsxGetter({CHROME, EDGE})
3203     public Function getOnselectionchange() {
3204         return getEventHandler(Event.TYPE_SELECTIONCHANGE);
3205     }
3206 
3207     /**
3208      * Sets the {@code onselectionchange} event handler for this element.
3209      * @param onselectionchange the {@code onselectionchange} event handler for this element
3210      */
3211     @JsxSetter({CHROME, EDGE})
3212     public void setOnselectionchange(final Object onselectionchange) {
3213         setEventHandler(Event.TYPE_SELECTIONCHANGE, onselectionchange);
3214     }
3215 
3216     /**
3217      * Returns the {@code onselectstart} event handler for this element.
3218      * @return the {@code onselectstart} event handler for this element
3219      */
3220     @JsxGetter
3221     public Function getOnselectstart() {
3222         return getEventHandler(Event.TYPE_SELECTSTART);
3223     }
3224 
3225     /**
3226      * Sets the {@code onselectstart} event handler for this element.
3227      * @param onselectstart the {@code onselectstart} event handler for this element
3228      */
3229     @JsxSetter
3230     public void setOnselectstart(final Object onselectstart) {
3231         setEventHandler(Event.TYPE_SELECTSTART, onselectstart);
3232     }
3233 
3234     /**
3235      * Returns the {@code onstalled} event handler for this element.
3236      * @return the {@code onstalled} event handler for this element
3237      */
3238     @JsxGetter
3239     public Function getOnstalled() {
3240         return getEventHandler(Event.TYPE_STALLED);
3241     }
3242 
3243     /**
3244      * Sets the {@code onstalled} event handler for this element.
3245      * @param onstalled the {@code onstalled} event handler for this element
3246      */
3247     @JsxSetter
3248     public void setOnstalled(final Object onstalled) {
3249         setEventHandler(Event.TYPE_STALLED, onstalled);
3250     }
3251 
3252     /**
3253      * Returns the {@code onsubmit} event handler for this element.
3254      * @return the {@code onsubmit} event handler for this element
3255      */
3256     @JsxGetter
3257     public Function getOnsubmit() {
3258         return getEventHandler(Event.TYPE_SUBMIT);
3259     }
3260 
3261     /**
3262      * Sets the {@code onsubmit} event handler for this element.
3263      * @param onsubmit the {@code onsubmit} event handler for this element
3264      */
3265     @JsxSetter
3266     public void setOnsubmit(final Object onsubmit) {
3267         setEventHandler(Event.TYPE_SUBMIT, onsubmit);
3268     }
3269 
3270     /**
3271      * Returns the {@code onsuspend} event handler for this element.
3272      * @return the {@code onsuspend} event handler for this element
3273      */
3274     @JsxGetter
3275     public Function getOnsuspend() {
3276         return getEventHandler(Event.TYPE_SUSPEND);
3277     }
3278 
3279     /**
3280      * Sets the {@code onsuspend} event handler for this element.
3281      * @param onsuspend the {@code onsuspend} event handler for this element
3282      */
3283     @JsxSetter
3284     public void setOnsuspend(final Object onsuspend) {
3285         setEventHandler(Event.TYPE_SUSPEND, onsuspend);
3286     }
3287 
3288     /**
3289      * Returns the {@code ontimeupdate} event handler for this element.
3290      * @return the {@code ontimeupdate} event handler for this element
3291      */
3292     @JsxGetter
3293     public Function getOntimeupdate() {
3294         return getEventHandler(Event.TYPE_TIMEUPDATE);
3295     }
3296 
3297     /**
3298      * Sets the {@code ontimeupdate} event handler for this element.
3299      * @param ontimeupdate the {@code ontimeupdate} event handler for this element
3300      */
3301     @JsxSetter
3302     public void setOntimeupdate(final Object ontimeupdate) {
3303         setEventHandler(Event.TYPE_TIMEUPDATE, ontimeupdate);
3304     }
3305 
3306     /**
3307      * Returns the {@code ontoggle} event handler for this element.
3308      * @return the {@code ontoggle} event handler for this element
3309      */
3310     @JsxGetter({CHROME, EDGE})
3311     public Function getOntoggle() {
3312         return getEventHandler(Event.TYPE_TOGGLE);
3313     }
3314 
3315     /**
3316      * Sets the {@code ontoggle} event handler for this element.
3317      * @param ontoggle the {@code ontoggle} event handler for this element
3318      */
3319     @JsxSetter({CHROME, EDGE})
3320     public void setOntoggle(final Object ontoggle) {
3321         setEventHandler(Event.TYPE_TOGGLE, ontoggle);
3322     }
3323 
3324     /**
3325      * Returns the {@code onvolumechange} event handler for this element.
3326      * @return the {@code onvolumechange} event handler for this element
3327      */
3328     @JsxGetter
3329     public Function getOnvolumechange() {
3330         return getEventHandler(Event.TYPE_VOLUMECHANGE);
3331     }
3332 
3333     /**
3334      * Sets the {@code onvolumechange} event handler for this element.
3335      * @param onvolumechange the {@code onvolumechange} event handler for this element
3336      */
3337     @JsxSetter
3338     public void setOnvolumechange(final Object onvolumechange) {
3339         setEventHandler(Event.TYPE_VOLUMECHANGE, onvolumechange);
3340     }
3341 
3342     /**
3343      * Returns the {@code onwaiting} event handler for this element.
3344      * @return the {@code onwaiting} event handler for this element
3345      */
3346     @JsxGetter
3347     public Function getOnwaiting() {
3348         return getEventHandler(Event.TYPE_WAITING);
3349     }
3350 
3351     /**
3352      * Sets the {@code onwaiting} event handler for this element.
3353      * @param onwaiting the {@code onwaiting} event handler for this element
3354      */
3355     @JsxSetter
3356     public void setOnwaiting(final Object onwaiting) {
3357         setEventHandler(Event.TYPE_WAITING, onwaiting);
3358     }
3359 
3360     /**
3361      * Returns the {@code onwebkitfullscreenchange} event handler for this element.
3362      * @return the {@code onwebkitfullscreenchange} event handler for this element
3363      */
3364     @JsxGetter({CHROME, EDGE})
3365     public Function getOnwebkitfullscreenchange() {
3366         return getEventHandler(Event.TYPE_WEBKITFULLSCREENCHANGE);
3367     }
3368 
3369     /**
3370      * Sets the {@code onwebkitfullscreenchange} event handler for this element.
3371      * @param onwebkitfullscreenchange the {@code onwebkitfullscreenchange} event handler for this element
3372      */
3373     @JsxSetter({CHROME, EDGE})
3374     public void setOnwebkitfullscreenchange(final Object onwebkitfullscreenchange) {
3375         setEventHandler(Event.TYPE_WEBKITFULLSCREENCHANGE, onwebkitfullscreenchange);
3376     }
3377 
3378     /**
3379      * Returns the {@code onwebkitfullscreenerror} event handler for this element.
3380      * @return the {@code onwebkitfullscreenerror} event handler for this element
3381      */
3382     @JsxGetter({CHROME, EDGE})
3383     public Function getOnwebkitfullscreenerror() {
3384         return getEventHandler(Event.TYPE_WEBKITFULLSCREENERROR);
3385     }
3386 
3387     /**
3388      * Sets the {@code onwebkitfullscreenerror} event handler for this element.
3389      * @param onwebkitfullscreenerror the {@code onwebkitfullscreenerror} event handler for this element
3390      */
3391     @JsxSetter
3392     public void setOnwebkitfullscreenerror(final Object onwebkitfullscreenerror) {
3393         setEventHandler(Event.TYPE_WEBKITFULLSCREENERROR, onwebkitfullscreenerror);
3394     }
3395 
3396     /**
3397      * Returns the {@code onwheel} event handler for this element.
3398      * @return the {@code onwheel} event handler for this element
3399      */
3400     @JsxGetter
3401     public Function getOnwheel() {
3402         return getEventHandler(Event.TYPE_WHEEL);
3403     }
3404 
3405     /**
3406      * Sets the {@code onwheel} event handler for this element.
3407      * @param onwheel the {@code onwheel} event handler for this element
3408      */
3409     @JsxSetter
3410     public void setOnwheel(final Object onwheel) {
3411         setEventHandler(Event.TYPE_WHEEL, onwheel);
3412     }
3413 
3414     /**
3415      * Returns the {@code onafterscriptexecute} event handler for this element.
3416      * @return the {@code onafterscriptexecute} event handler for this element
3417      */
3418     @JsxGetter({FF, FF_ESR})
3419     public Function getOnafterscriptexecute() {
3420         return getEventHandler(Event.TYPE_AFTERSCRIPTEXECUTE);
3421     }
3422 
3423     /**
3424      * Sets the {@code onafterscriptexecute} event handler for this element.
3425      * @param onafterscriptexecute the {@code onafterscriptexecute} event handler for this element
3426      */
3427     @JsxSetter({FF, FF_ESR})
3428     public void setOnafterscriptexecute(final Object onafterscriptexecute) {
3429         setEventHandler(Event.TYPE_AFTERSCRIPTEXECUTE, onafterscriptexecute);
3430     }
3431 
3432     /**
3433      * Returns the {@code onbeforescriptexecute} event handler for this element.
3434      * @return the {@code onbeforescriptexecute} event handler for this element
3435      */
3436     @JsxGetter({FF, FF_ESR})
3437     public Function getOnbeforescriptexecute() {
3438         return getEventHandler(Event.TYPE_BEFORESCRIPTEXECUTE);
3439     }
3440 
3441     /**
3442      * Sets the {@code onbeforescriptexecute} event handler for this element.
3443      * @param onbeforescriptexecute the {@code onbeforescriptexecute} event handler for this element
3444      */
3445     @JsxSetter({FF, FF_ESR})
3446     public void setOnbeforescriptexecute(final Object onbeforescriptexecute) {
3447         setEventHandler(Event.TYPE_BEFORESCRIPTEXECUTE, onbeforescriptexecute);
3448     }
3449 
3450     /**
3451      * Returns the {@code onmozfullscreenchange} event handler for this element.
3452      * @return the {@code onmozfullscreenchange} event handler for this element
3453      */
3454     @JsxGetter({FF, FF_ESR})
3455     public Function getOnmozfullscreenchange() {
3456         return getEventHandler(Event.TYPE_MOZFULLSCREENCHANGE);
3457     }
3458 
3459     /**
3460      * Sets the {@code onmozfullscreenchange} event handler for this element.
3461      * @param onmozfullscreenchange the {@code onmozfullscreenchange} event handler for this element
3462      */
3463     @JsxSetter({FF, FF_ESR})
3464     public void setOnmozfullscreenchange(final Object onmozfullscreenchange) {
3465         setEventHandler(Event.TYPE_MOZFULLSCREENCHANGE, onmozfullscreenchange);
3466     }
3467 
3468     /**
3469      * Returns the {@code onmozfullscreenerror} event handler for this element.
3470      * @return the {@code onmozfullscreenerror} event handler for this element
3471      */
3472     @JsxGetter({FF, FF_ESR})
3473     public Function getOnmozfullscreenerror() {
3474         return getEventHandler(Event.TYPE_MOZFULLSCREENERROR);
3475     }
3476 
3477     /**
3478      * Sets the {@code onmozfullscreenerror} event handler for this element.
3479      * @param onmozfullscreenerror the {@code onmozfullscreenerror} event handler for this element
3480      */
3481     @JsxSetter({FF, FF_ESR})
3482     public void setOnmozfullscreenerror(final Object onmozfullscreenerror) {
3483         setEventHandler(Event.TYPE_MOZFULLSCREENERROR, onmozfullscreenerror);
3484     }
3485 
3486     /**
3487      * Returns the {@code currentScript} property.
3488      * @return the {@code currentScript}
3489      */
3490     @JsxGetter
3491     public ScriptableObject getCurrentScript() {
3492         return currentScript_;
3493     }
3494 
3495     /**
3496      * Sets the {@code currentScript} property.
3497      * @param script the {@code currentScript}
3498      */
3499     public void setCurrentScript(final ScriptableObject script) {
3500         currentScript_ = script;
3501     }
3502 
3503     /**
3504      * Returns the {@code FontFaceSet} property.
3505      * @return the {@code FontFaceSet}
3506      */
3507     @JsxGetter
3508     public ScriptableObject getFonts() {
3509         if (fonts_ == null) {
3510             final FontFaceSet fonts = new FontFaceSet();
3511             fonts.setParentScope(getParentScope());
3512             fonts.setPrototype(getPrototype(fonts.getClass()));
3513             fonts_ = fonts;
3514         }
3515         return fonts_;
3516     }
3517 
3518     /**
3519      * Returns the value of the {@code all} property.
3520      * @return the value of the {@code all} property
3521      */
3522     @JsxGetter
3523     public HTMLAllCollection getAll() {
3524         final HTMLAllCollection all = new HTMLAllCollection(getDomNodeOrDie());
3525         all.setAvoidObjectDetection(true);
3526         all.setIsMatchingPredicate((Predicate<DomNode> & Serializable) node -> true);
3527         return all;
3528     }
3529 
3530     /**
3531      * Returns the element with the specified ID, as long as it is an HTML element; {@code null} otherwise.
3532      * @param id the ID to search for
3533      * @return the element with the specified ID, as long as it is an HTML element; {@code null} otherwise
3534      */
3535     @JsxFunction
3536     public HtmlUnitScriptable getElementById(final String id) {
3537         final DomNode domNode = getDomNodeOrDie();
3538         for (final DomElement descendant : domNode.getDomElementDescendants()) {
3539             if (id.equals(descendant.getId())) {
3540                 return descendant.getScriptableObject();
3541             }
3542         }
3543         return null;
3544     }
3545 
3546     /**
3547      * Creates a new ProcessingInstruction.
3548      * @param context the JavaScript context
3549      * @param scope the scope
3550      * @param thisObj the scriptable
3551      * @param args the arguments passed into the method: target and data
3552      * @param function the function
3553      * @return the new ProcessingInstruction
3554      */
3555     @JsxFunction
3556     public static HtmlUnitScriptable createProcessingInstruction(final Context context, final VarScope scope,
3557             final Scriptable thisObj, final Object[] args, final Function function) {
3558         if (args.length < 2) {
3559             throw JavaScriptEngine.typeError("Failed to execute 'createProcessingInstruction' on 'Document': "
3560                     + "2 arguments required, but only " + args.length + " present.");
3561         }
3562 
3563         final Document document = (Document) thisObj;
3564         final String target = JavaScriptEngine.toString(args[0]);
3565         final String data = JavaScriptEngine.toString(args[1]);
3566 
3567         if (data.contains("?>") || !isValidProcessingInstructionTarget(target)) {
3568             throw JavaScriptEngine.asJavaScriptException(
3569                     document.getWindow(),
3570                     "createProcessingInstruction: Provided string contains an invalid character",
3571                     org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
3572         }
3573 
3574         final ProcessingInstruction node = document.getPage().createProcessingInstruction(target, data);
3575         return document.getScriptableFor(node);
3576     }
3577 
3578     // the XML Name production, simplified in the same way as it is done for createElement:
3579     // only the ASCII range is checked
3580     private static boolean isValidProcessingInstructionTarget(final String target) {
3581         if (target.isEmpty()) {
3582             return false;
3583         }
3584 
3585         final int first = target.charAt(0);
3586         if (first < 128 && !Character.isLetter(first) && first != ':' && first != '_') {
3587             return false;
3588         }
3589 
3590         final int length = target.length();
3591         for (int i = 1; i < length; i++) {
3592             final int c = target.charAt(i);
3593             if (c < 128 && !Character.isLetterOrDigit(c) && c != ':' && c != '_' && c != '-' && c != '.') {
3594                 return false;
3595             }
3596         }
3597         return true;
3598     }
3599 
3600     /**
3601      * Creates a new createCDATASection.
3602      * @param data the data
3603      * @return the new CDATASection
3604      */
3605     @JsxFunction
3606     public HtmlUnitScriptable createCDATASection(final String data) {
3607         final SgmlPage page = getPage();
3608         // https://dom.spec.whatwg.org/#dom-document-createcdatasection
3609         if (page instanceof HtmlPage && !(page instanceof XHtmlPage)) {
3610             throw JavaScriptEngine.asJavaScriptException(
3611                     getWindow(),
3612                     "createCDATASection: not supported for HTML documents",
3613                     org.htmlunit.javascript.host.dom.DOMException.NOT_SUPPORTED_ERR);
3614         }
3615         final CDATASection node = page.createCDATASection(data);
3616         return getScriptableFor(node);
3617     }
3618 
3619     /**
3620      * Does... nothing.
3621      * @see <a href="https://developer.mozilla.org/en/DOM/document.clear">Mozilla doc</a>
3622      */
3623     @JsxFunction
3624     public void clear() {
3625         // nothing
3626     }
3627 
3628     /**
3629      * {@inheritDoc}
3630      */
3631     @JsxFunction
3632     @Override
3633     public boolean contains(final Object element) {
3634         return getDocumentElement().contains(element);
3635     }
3636 }