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