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.html;
16  
17  import static org.htmlunit.BrowserVersionFeatures.HTMLELEMENT_REMOVE_ACTIVE_TRIGGERS_BLUR_EVENT;
18  import static org.htmlunit.BrowserVersionFeatures.KEYBOARD_EVENT_SPECIAL_KEYPRESS;
19  import static org.htmlunit.css.CssStyleSheet.ABSOLUTE;
20  import static org.htmlunit.css.CssStyleSheet.FIXED;
21  import static org.htmlunit.css.CssStyleSheet.STATIC;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  import org.htmlunit.BrowserVersion;
30  import org.htmlunit.ElementNotFoundException;
31  import org.htmlunit.Page;
32  import org.htmlunit.ScriptResult;
33  import org.htmlunit.SgmlPage;
34  import org.htmlunit.WebAssert;
35  import org.htmlunit.WebClient;
36  import org.htmlunit.WebWindow;
37  import org.htmlunit.css.ComputedCssStyleDeclaration;
38  import org.htmlunit.html.impl.SelectableTextInput;
39  import org.htmlunit.javascript.HtmlUnitScriptable;
40  import org.htmlunit.javascript.host.dom.Document;
41  import org.htmlunit.javascript.host.dom.MutationObserver;
42  import org.htmlunit.javascript.host.event.Event;
43  import org.htmlunit.javascript.host.event.EventTarget;
44  import org.htmlunit.javascript.host.event.KeyboardEvent;
45  import org.htmlunit.javascript.host.html.HTMLDocument;
46  import org.htmlunit.javascript.host.html.HTMLElement;
47  import org.htmlunit.util.StringUtils;
48  import org.w3c.dom.Attr;
49  import org.w3c.dom.CDATASection;
50  import org.w3c.dom.Comment;
51  import org.w3c.dom.DOMException;
52  import org.w3c.dom.Element;
53  import org.w3c.dom.EntityReference;
54  import org.w3c.dom.Node;
55  import org.w3c.dom.ProcessingInstruction;
56  import org.w3c.dom.Text;
57  
58  /**
59   * An abstract wrapper for HTML elements.
60   *
61   * @author Mike Bowler
62   * @author Mike J. Bresnahan
63   * @author David K. Taylor
64   * @author Christian Sell
65   * @author David D. Kilzer
66   * @author Mike Gallaher
67   * @author Denis N. Antonioli
68   * @author Marc Guillemot
69   * @author Ahmed Ashour
70   * @author Daniel Gredler
71   * @author Dmitri Zoubkov
72   * @author Sudhan Moghe
73   * @author Ronald Brill
74   * @author Frank Danek
75   * @author Ronny Shapiro
76   * @author Lai Quang Duong
77   */
78  public abstract class HtmlElement extends DomElement {
79  
80      /**
81       * Enum for the different display styles.
82       */
83      public enum DisplayStyle {
84          /** Empty string. */
85          EMPTY(""),
86          /** none. */
87          NONE("none"),
88          /** block. */
89          BLOCK("block"),
90          /** contents. */
91          CONTENTS("contents"),
92          /** inline. */
93          INLINE("inline"),
94          /** inline-block. */
95          INLINE_BLOCK("inline-block"),
96          /** list-item. */
97          LIST_ITEM("list-item"),
98          /** table. */
99          TABLE("table"),
100         /** table-cell. */
101         TABLE_CELL("table-cell"),
102         /** table-column. */
103         TABLE_COLUMN("table-column"),
104         /** table-column-group. */
105         TABLE_COLUMN_GROUP("table-column-group"),
106         /** table-row. */
107         TABLE_ROW("table-row"),
108         /** table-row-group. */
109         TABLE_ROW_GROUP("table-row-group"),
110         /** table-header-group. */
111         TABLE_HEADER_GROUP("table-header-group"),
112         /** table-footer-group. */
113         TABLE_FOOTER_GROUP("table-footer-group"),
114         /** table-caption. */
115         TABLE_CAPTION("table-caption"),
116         /** ruby. */
117         RUBY("ruby"),
118         /** ruby-base. */
119         RUBY_BASE("ruby-base"),
120         /** ruby-text-container. */
121         RUBY_TEXT("ruby-text"),
122         /** ruby-text-container. */
123         RUBY_TEXT_CONTAINER("ruby-text-container");
124 
125         private final String value_;
126         DisplayStyle(final String value) {
127             value_ = value;
128         }
129 
130         /**
131          * The string used from js.
132          * @return the value as string
133          */
134         public String value() {
135             return value_;
136         }
137     }
138 
139     /**
140      * Constant indicating that a tab index value is out of bounds (less than <code>0</code> or greater
141      * than <code>32767</code>).
142      *
143      * @see #getTabIndex()
144      */
145     public static final Short TAB_INDEX_OUT_OF_BOUNDS = Short.valueOf(Short.MIN_VALUE);
146 
147     /** Constant 'required'. */
148     protected static final String ATTRIBUTE_REQUIRED = "required";
149     /** Constant 'checked'. */
150     protected static final String ATTRIBUTE_CHECKED = "checked";
151     /** Constant 'hidden'. */
152     protected static final String ATTRIBUTE_HIDDEN = "hidden";
153     /** Constant 'readonly'. */
154     protected static final String ATTRIBUTE_READONLY = "readonly";
155 
156     /** The listeners which are to be notified of attribute changes. */
157     private final List<HtmlAttributeChangeListener> attributeListeners_ = new ArrayList<>();
158 
159     /** The owning form for lost form children. */
160     private HtmlForm owningForm_;
161 
162     private boolean shiftPressed_;
163     private boolean ctrlPressed_;
164     private boolean altPressed_;
165 
166     /**
167      * Creates an instance.
168      *
169      * @param qualifiedName the qualified name of the element type to instantiate
170      * @param page the page that contains this element
171      * @param attributes a map ready initialized with the attributes for this element, or
172      *        {@code null}. The map will be stored as is, not copied.
173      */
174     protected HtmlElement(final String qualifiedName, final SgmlPage page,
175             final Map<String, DomAttr> attributes) {
176         this(Html.XHTML_NAMESPACE, qualifiedName, page, attributes);
177     }
178 
179     /**
180      * Creates an instance of a DOM element that can have a namespace.
181      *
182      * @param namespaceURI the URI that identifies an XML namespace
183      * @param qualifiedName the qualified name of the element type to instantiate
184      * @param page the page that contains this element
185      * @param attributes a map ready initialized with the attributes for this element, or
186      *        {@code null}. The map will be stored as is, not copied.
187      */
188     protected HtmlElement(final String namespaceURI, final String qualifiedName, final SgmlPage page,
189             final Map<String, DomAttr> attributes) {
190         super(namespaceURI, qualifiedName, page, attributes);
191     }
192 
193     /**
194      * {@inheritDoc}
195      */
196     @Override
197     protected void setAttributeNS(final String namespaceURI, final String qualifiedName,
198             final String attributeValue, final boolean notifyAttributeChangeListeners,
199             final boolean notifyMutationObservers) {
200 
201         final HtmlPage htmlPage = getHtmlPageOrNull();
202 
203         // TODO: Clean up; this is a hack for HtmlElement living within an XmlPage.
204         if (htmlPage == null) {
205             super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
206                     notifyMutationObservers);
207             return;
208         }
209 
210         final String oldAttributeValue = getAttribute(qualifiedName);
211         final boolean mappedElement = isAttachedToPage()
212                 && (DomElement.NAME_ATTRIBUTE.equals(qualifiedName) || DomElement.ID_ATTRIBUTE.equals(qualifiedName));
213         if (mappedElement) {
214             // cast is safe here because isMappedElement checks for HtmlPage
215             htmlPage.removeMappedElement(this, false, false);
216         }
217 
218         final HtmlAttributeChangeEvent event;
219         if (ATTRIBUTE_NOT_DEFINED == oldAttributeValue) {
220             event = new HtmlAttributeChangeEvent(this, qualifiedName, attributeValue);
221         }
222         else {
223             event = new HtmlAttributeChangeEvent(this, qualifiedName, oldAttributeValue);
224         }
225 
226         super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
227                 notifyMutationObservers);
228 
229         if (notifyAttributeChangeListeners) {
230             notifyAttributeChangeListeners(event, this, oldAttributeValue, notifyMutationObservers);
231         }
232 
233         fireAttributeChangeImpl(event, htmlPage, mappedElement, oldAttributeValue);
234     }
235 
236     /**
237      * Recursively notifies all {@link HtmlAttributeChangeListener}s.
238      * @param event the event
239      * @param element the element
240      * @param oldAttributeValue the old attribute value
241      * @param notifyMutationObservers whether to notify {@link MutationObserver}s or not
242      */
243     protected static void notifyAttributeChangeListeners(final HtmlAttributeChangeEvent event,
244             final HtmlElement element, final String oldAttributeValue, final boolean notifyMutationObservers) {
245         final List<HtmlAttributeChangeListener> listeners = new ArrayList<>(element.attributeListeners_);
246         if (ATTRIBUTE_NOT_DEFINED == oldAttributeValue) {
247             synchronized (listeners) {
248                 for (final HtmlAttributeChangeListener listener : listeners) {
249                     if (notifyMutationObservers || !(listener instanceof MutationObserver)) {
250                         listener.attributeAdded(event);
251                     }
252                 }
253             }
254         }
255         else {
256             synchronized (listeners) {
257                 for (final HtmlAttributeChangeListener listener : listeners) {
258                     if (notifyMutationObservers || !(listener instanceof MutationObserver)) {
259                         listener.attributeReplaced(event);
260                     }
261                 }
262             }
263         }
264 
265         final DomNode parentNode = element.getParentNode();
266         if (parentNode instanceof HtmlElement htmlElement) {
267             notifyAttributeChangeListeners(event, htmlElement, oldAttributeValue, notifyMutationObservers);
268         }
269     }
270 
271     private void fireAttributeChangeImpl(final HtmlAttributeChangeEvent event,
272             final HtmlPage htmlPage, final boolean mappedElement, final String oldAttributeValue) {
273         if (mappedElement) {
274             htmlPage.addMappedElement(this, false);
275         }
276 
277         if (ATTRIBUTE_NOT_DEFINED == oldAttributeValue) {
278             fireHtmlAttributeAdded(event);
279             htmlPage.fireHtmlAttributeAdded(event);
280         }
281         else {
282             fireHtmlAttributeReplaced(event);
283             htmlPage.fireHtmlAttributeReplaced(event);
284         }
285     }
286 
287     /**
288      * Sets the specified attribute. This method may be overridden by subclasses
289      * which are interested in specific attribute value changes, but such methods <b>must</b>
290      * invoke <code>super.setAttributeNode()</code>, and <b>should</b> consider the value of the
291      * <code>cloning</code> parameter when deciding whether or not to execute custom logic.
292      *
293      * @param attribute the attribute to set
294      * @return {@inheritDoc}
295      */
296     @Override
297     public Attr setAttributeNode(final Attr attribute) {
298         final HtmlPage htmlPage = getHtmlPageOrNull();
299 
300         // TODO: Clean up; this is a hack for HtmlElement living within an XmlPage.
301         if (htmlPage == null) {
302             return super.setAttributeNode(attribute);
303         }
304 
305         final String qualifiedName = attribute.getName();
306         final String oldAttributeValue = getAttribute(qualifiedName);
307 
308         final boolean mappedElement = isAttachedToPage()
309                 && (DomElement.NAME_ATTRIBUTE.equals(qualifiedName)
310                         || DomElement.ID_ATTRIBUTE.equals(qualifiedName));
311         if (mappedElement) {
312             htmlPage.removeMappedElement(this, false, false);
313         }
314 
315         final HtmlAttributeChangeEvent event;
316         if (ATTRIBUTE_NOT_DEFINED == oldAttributeValue) {
317             event = new HtmlAttributeChangeEvent(this, qualifiedName, attribute.getValue());
318         }
319         else {
320             event = new HtmlAttributeChangeEvent(this, qualifiedName, oldAttributeValue);
321         }
322         notifyAttributeChangeListeners(event, this, oldAttributeValue, true);
323 
324         final Attr result = super.setAttributeNode(attribute);
325 
326         fireAttributeChangeImpl(event, htmlPage, mappedElement, oldAttributeValue);
327 
328         return result;
329     }
330 
331     /**
332      * Removes an attribute specified by name from this element.
333      * @param attributeName the attribute attributeName
334      */
335     @Override
336     public void removeAttribute(final String attributeName) {
337         final String value = getAttribute(attributeName);
338         if (ATTRIBUTE_NOT_DEFINED == value) {
339             return;
340         }
341 
342         final HtmlPage htmlPage = getHtmlPageOrNull();
343 
344         // TODO: Clean up; this is a hack for HtmlElement living within an XmlPage.
345         if (htmlPage == null) {
346             super.removeAttribute(attributeName);
347             return;
348         }
349 
350         final boolean mapped = DomElement.NAME_ATTRIBUTE.equals(attributeName)
351                                 || DomElement.ID_ATTRIBUTE.equals(attributeName);
352         if (mapped) {
353             htmlPage.removeMappedElement(this, false, false);
354         }
355 
356         super.removeAttribute(attributeName);
357 
358         if (mapped) {
359             htmlPage.addMappedElement(this, false);
360         }
361 
362         final HtmlAttributeChangeEvent event = new HtmlAttributeChangeEvent(this, attributeName, value);
363         fireHtmlAttributeRemoved(event);
364         htmlPage.fireHtmlAttributeRemoved(event);
365     }
366 
367     /**
368      * Support for reporting HTML attribute changes. This method can be called when an attribute
369      * has been added, and it will send the appropriate {@link HtmlAttributeChangeEvent} to any
370      * registered {@link HtmlAttributeChangeListener}s.
371      * <p>
372      * Note that this method recursively calls this element's parent's
373      * {@link #fireHtmlAttributeAdded(HtmlAttributeChangeEvent)} method.
374      * </p>
375      *
376      * @param event the event
377      * @see #addHtmlAttributeChangeListener(HtmlAttributeChangeListener)
378      */
379     protected void fireHtmlAttributeAdded(final HtmlAttributeChangeEvent event) {
380         final DomNode parentNode = getParentNode();
381         if (parentNode instanceof HtmlElement element) {
382             element.fireHtmlAttributeAdded(event);
383         }
384     }
385 
386     /**
387      * Support for reporting HTML attribute changes. This method can be called when an attribute
388      * has been replaced, and it will send the appropriate {@link HtmlAttributeChangeEvent} to any
389      * registered {@link HtmlAttributeChangeListener}s.
390      * <p>
391      * Note that this method recursively calls this element's parent's
392      * {@link #fireHtmlAttributeReplaced(HtmlAttributeChangeEvent)} method.
393      * </p>
394      *
395      * @param event the event
396      * @see #addHtmlAttributeChangeListener(HtmlAttributeChangeListener)
397      */
398     protected void fireHtmlAttributeReplaced(final HtmlAttributeChangeEvent event) {
399         final DomNode parentNode = getParentNode();
400         if (parentNode instanceof HtmlElement element) {
401             element.fireHtmlAttributeReplaced(event);
402         }
403     }
404 
405     /**
406      * Support for reporting HTML attribute changes. This method can be called when an attribute
407      * has been removed, and it will send the appropriate {@link HtmlAttributeChangeEvent} to any
408      * registered {@link HtmlAttributeChangeListener}s.
409      * <p>
410      * Note that this method recursively calls this element's parent's
411      * {@link #fireHtmlAttributeRemoved(HtmlAttributeChangeEvent)} method.
412      * </p>
413      *
414      * @param event the event
415      * @see #addHtmlAttributeChangeListener(HtmlAttributeChangeListener)
416      */
417     protected void fireHtmlAttributeRemoved(final HtmlAttributeChangeEvent event) {
418         synchronized (attributeListeners_) {
419             for (final HtmlAttributeChangeListener listener : attributeListeners_) {
420                 listener.attributeRemoved(event);
421             }
422         }
423         final DomNode parentNode = getParentNode();
424         if (parentNode instanceof HtmlElement element) {
425             element.fireHtmlAttributeRemoved(event);
426         }
427     }
428 
429     /**
430      * Returns the same value as returned by {@link #getTagName()}.
431      *
432      * @return the same value as returned by {@link #getTagName()}
433      */
434     @Override
435     public String getNodeName() {
436         final String prefix = getPrefix();
437         if (prefix != null) {
438             // create string builder only if needed (performance)
439             final StringBuilder name = new StringBuilder(prefix.toLowerCase(Locale.ROOT))
440                 .append(':')
441                 .append(getLocalName().toLowerCase(Locale.ROOT));
442             return name.toString();
443         }
444         return getLocalName().toLowerCase(Locale.ROOT);
445     }
446 
447     /**
448      * Returns this element's tab index, if it has one. If the tab index is outside the
449      * valid range (less than <code>0</code> or greater than <code>32767</code>), this method
450      * returns {@link #TAB_INDEX_OUT_OF_BOUNDS}. If this element does not have
451      * a tab index, or its tab index is otherwise invalid, this method returns {@code null}.
452      *
453      * @return this element's tab index
454      */
455     public Short getTabIndex() {
456         final String index = getAttributeDirect("tabindex");
457         if (index == null || index.isEmpty()) {
458             return null;
459         }
460         try {
461             final long l = Long.parseLong(index);
462             if (l >= 0 && l <= Short.MAX_VALUE) {
463                 return Short.valueOf((short) l);
464             }
465             return TAB_INDEX_OUT_OF_BOUNDS;
466         }
467         catch (final NumberFormatException e) {
468             return null;
469         }
470     }
471 
472     /**
473      * Returns the first element with the specified tag name that is an ancestor to this element, or
474      * {@code null} if no such element is found.
475      * @param tagName the name of the tag searched (case insensitive)
476      * @return the first element with the specified tag name that is an ancestor to this element
477      */
478     public HtmlElement getEnclosingElement(final String tagName) {
479         final String tagNameLC = tagName.toLowerCase(Locale.ROOT);
480 
481         for (DomNode currentNode = getParentNode(); currentNode != null; currentNode = currentNode.getParentNode()) {
482             if (currentNode instanceof HtmlElement element && currentNode.getNodeName().equals(tagNameLC)) {
483                 return element;
484             }
485         }
486         return null;
487     }
488 
489     /**
490      * Returns the form which contains this element, or {@code null} if this element is not inside
491      * a form.
492      * @return the form which contains this element
493      */
494     public HtmlForm getEnclosingForm() {
495         final String formId = getAttribute("form");
496         if (ATTRIBUTE_NOT_DEFINED != formId) {
497             final Element formById = getPage().getElementById(formId);
498             if (formById instanceof HtmlForm form) {
499                 return form;
500             }
501             return null;
502         }
503 
504         if (owningForm_ != null) {
505             return owningForm_;
506         }
507         return (HtmlForm) getEnclosingElement("form");
508     }
509 
510     /**
511      * Returns the form which contains this element. If this element is not inside a form, this method
512      * throws an {@link IllegalStateException}.
513      * @return the form which contains this element
514      */
515     public HtmlForm getEnclosingFormOrDie() {
516         final HtmlForm form = getEnclosingForm();
517         if (form == null) {
518             throw new IllegalStateException("Element is not contained within a form: " + this);
519         }
520         return form;
521     }
522 
523     /**
524      * Simulates typing the specified text while this element has focus.
525      * Note that for some elements, typing '\n' submits the enclosed form.
526      * @param text the text you with to simulate typing
527      * @throws IOException If an IO error occurs
528      */
529     public void type(final String text) throws IOException {
530         for (final char ch : text.toCharArray()) {
531             type(ch);
532         }
533     }
534 
535     /**
536      * Simulates typing the specified character while this element has focus, returning the page contained
537      * by this element's window after typing. Note that it may or may not be the same as the original page,
538      * depending on the JavaScript event handlers, etc. Note also that for some elements, typing <code>'\n'</code>
539      * submits the enclosed form.
540      *
541      * @param c the character you wish to simulate typing
542      * @return the page that occupies this window after typing
543      * @throws IOException if an IO error occurs
544      */
545     public Page type(final char c) throws IOException {
546         return type(c, true);
547     }
548 
549     /**
550      * Simulates typing the specified character while this element has focus, returning the page contained
551      * by this element's window after typing. Note that it may or may not be the same as the original page,
552      * depending on the JavaScript event handlers, etc. Note also that for some elements, typing <code>'\n'</code>
553      * submits the enclosed form.
554      *
555      * @param c the character you wish to simulate typing
556      * @param lastType is this the last character to type
557      * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
558      * @throws IOException if an IO error occurs
559      */
560     private Page type(final char c, final boolean lastType)
561         throws IOException {
562         if (isDisabledElementAndDisabled()) {
563             return getPage();
564         }
565 
566         // make enclosing window the current one
567         getPage().getWebClient().setCurrentWindow(getPage().getEnclosingWindow());
568 
569         final HtmlPage page = (HtmlPage) getPage();
570         if (page.getFocusedElement() != this) {
571             focus();
572         }
573         final boolean isShiftNeeded = KeyboardEvent.isShiftNeeded(c, shiftPressed_);
574 
575         final Event shiftDown;
576         final ScriptResult shiftDownResult;
577         if (isShiftNeeded) {
578             shiftDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, KeyboardEvent.DOM_VK_SHIFT,
579                     true, ctrlPressed_, altPressed_);
580             shiftDownResult = fireEvent(shiftDown);
581         }
582         else {
583             shiftDown = null;
584             shiftDownResult = null;
585         }
586 
587         final Event keyDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, c,
588                                                 shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
589         final ScriptResult keyDownResult = fireEvent(keyDown);
590 
591         if (!keyDown.isAborted(keyDownResult)) {
592             final Event keyPress = new KeyboardEvent(this, Event.TYPE_KEY_PRESS, c,
593                     shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
594             final ScriptResult keyPressResult = fireEvent(keyPress);
595 
596             if ((shiftDown == null || !shiftDown.isAborted(shiftDownResult))
597                     && !keyPress.isAborted(keyPressResult)) {
598                 doType(c, lastType);
599             }
600         }
601 
602         final WebClient webClient = page.getWebClient();
603         if (this instanceof HtmlSelectableTextInput
604                 || this instanceof HtmlTextArea) {
605             fireEvent(new KeyboardEvent(this, Event.TYPE_INPUT, c,
606                                         shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_));
607         }
608 
609         HtmlElement eventSource = this;
610         if (!isAttachedToPage()) {
611             eventSource = page.getBody();
612         }
613 
614         if (eventSource != null) {
615             final Event keyUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, c,
616                                                     shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
617             eventSource.fireEvent(keyUp);
618 
619             if (isShiftNeeded) {
620                 final Event shiftUp = new KeyboardEvent(this, Event.TYPE_KEY_UP,
621                                         KeyboardEvent.DOM_VK_SHIFT,
622                                         false, ctrlPressed_, altPressed_);
623                 eventSource.fireEvent(shiftUp);
624             }
625         }
626 
627         final HtmlForm form = getEnclosingForm();
628         if (form != null && c == '\n' && isSubmittableByEnter()) {
629             for (final DomElement descendant : form.getDomElementDescendants()) {
630                 if (descendant instanceof HtmlSubmitInput) {
631                     return descendant.click();
632                 }
633             }
634 
635             form.submit((SubmittableElement) this);
636 
637             if (webClient.isJavaScriptEnabled()) {
638                 webClient.getJavaScriptEngine().processPostponedActions();
639             }
640         }
641 
642         return webClient.getCurrentWindow().getEnclosedPage();
643     }
644 
645     /**
646      * Simulates typing the specified key code while this element has focus, returning the page contained
647      * by this element's window after typing. Note that it may or may not be the same as the original page,
648      * depending on the JavaScript event handlers, etc.
649      * Note also that for some elements, typing <code>XXXXXXXXXXX</code>
650      * submits the enclosed form.
651      * <p>
652      * An example of predefined values is {@link KeyboardEvent#DOM_VK_PAGE_DOWN}.
653      * </p>
654      *
655      * @param keyCode the key code to simulate typing
656      * @return the page that occupies this window after typing
657      */
658     public Page type(final int keyCode) {
659         return type(keyCode, true, true, true, true);
660     }
661 
662     /**
663      * Simulates typing the specified {@link Keyboard} while this element has focus, returning the page contained
664      * by this element's window after typing. Note that it may or may not be the same as the original page,
665      * depending on the JavaScript event handlers, etc.
666      * Note also that for some elements, typing <code>XXXXXXXXXXX</code>
667      * submits the enclosed form.
668      *
669      * @param keyboard the keyboard
670      * @return the page that occupies this window after typing
671      * @throws IOException if an IO error occurs
672      */
673     public Page type(final Keyboard keyboard) throws IOException {
674         Page page = null;
675 
676         final List<Object[]> keys = keyboard.getKeys();
677 
678         if (keyboard.isStartAtEnd()) {
679             if (this instanceof SelectableTextInput textInput) {
680                 textInput.setSelectionStart(textInput.getText().length());
681             }
682             else {
683                 final DomText domText = getDoTypeNode();
684                 if (domText != null) {
685                     domText.moveSelectionToEnd();
686                 }
687             }
688         }
689 
690         final int size = keys.size();
691         for (int i = 0; i < size; i++) {
692             final Object[] entry = keys.get(i);
693             if (entry.length == 1) {
694                 type((char) entry[0], i == keys.size() - 1);
695             }
696             else {
697                 final int key = (int) entry[0];
698                 final boolean pressed = (boolean) entry[1];
699                 switch (key) {
700                     case KeyboardEvent.DOM_VK_SHIFT -> shiftPressed_ = pressed;
701                     case KeyboardEvent.DOM_VK_CONTROL -> ctrlPressed_ = pressed;
702                     case KeyboardEvent.DOM_VK_ALT -> altPressed_ = pressed;
703                     default -> { }
704                 }
705 
706                 if (pressed) {
707                     if (key == KeyboardEvent.DOM_VK_SHIFT
708                             || key == KeyboardEvent.DOM_VK_CONTROL
709                             || key == KeyboardEvent.DOM_VK_ALT) {
710                         page = type(key, true, false, false, i == keys.size() - 1);
711                     }
712                     else {
713                         page = type(key, true, true, true, i == keys.size() - 1);
714                     }
715                 }
716                 else {
717                     page = type(key, false, false, true, i == keys.size() - 1);
718                 }
719             }
720         }
721 
722         return page;
723     }
724 
725     private Page type(final int keyCode,
726                     final boolean fireKeyDown, final boolean fireKeyPress, final boolean fireKeyUp,
727                     final boolean lastType) {
728         if (isDisabledElementAndDisabled()) {
729             return getPage();
730         }
731 
732         final HtmlPage page = (HtmlPage) getPage();
733         if (page.getFocusedElement() != this) {
734             focus();
735         }
736 
737         final Event keyDown;
738         final ScriptResult keyDownResult;
739         if (fireKeyDown) {
740             keyDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
741             keyDownResult = fireEvent(keyDown);
742         }
743         else {
744             keyDown = null;
745             keyDownResult = null;
746         }
747 
748         final BrowserVersion browserVersion = page.getWebClient().getBrowserVersion();
749 
750         final Event keyPress;
751         final ScriptResult keyPressResult;
752         if (fireKeyPress && browserVersion.hasFeature(KEYBOARD_EVENT_SPECIAL_KEYPRESS)) {
753             keyPress = new KeyboardEvent(this, Event.TYPE_KEY_PRESS, keyCode,
754                     shiftPressed_, ctrlPressed_, altPressed_);
755 
756             keyPressResult = fireEvent(keyPress);
757         }
758         else {
759             keyPress = null;
760             keyPressResult = null;
761         }
762 
763         if (keyDown != null && !keyDown.isAborted(keyDownResult)
764                 && (keyPress == null || !keyPress.isAborted(keyPressResult))) {
765             doType(keyCode, lastType);
766         }
767 
768         if (this instanceof HtmlTextInput
769             || this instanceof HtmlTextArea
770             || this instanceof HtmlTelInput
771             || this instanceof HtmlNumberInput
772             || this instanceof HtmlSearchInput
773             || this instanceof HtmlPasswordInput) {
774             final Event input = new KeyboardEvent(this, Event.TYPE_INPUT, keyCode,
775                     shiftPressed_, ctrlPressed_, altPressed_);
776             fireEvent(input);
777         }
778 
779         if (fireKeyUp) {
780             final Event keyUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, keyCode,
781                     shiftPressed_, ctrlPressed_, altPressed_);
782             fireEvent(keyUp);
783         }
784 
785         return page.getWebClient().getCurrentWindow().getEnclosedPage();
786     }
787 
788     /**
789      * Performs the effective type action, called after the keyPress event and before the keyUp event.
790      * @param c the character you with to simulate typing
791      * @param lastType is this the last character to type
792      */
793     protected void doType(final char c, final boolean lastType) {
794         final DomText domText = getDoTypeNode();
795         if (domText != null) {
796             domText.doType(c, this, lastType);
797         }
798     }
799 
800     /**
801      * Performs the effective type action, called after the keyPress event and before the keyUp event.
802      * <p>
803      * An example of predefined values is {@link KeyboardEvent#DOM_VK_PAGE_DOWN}.
804      * </p>
805      *
806      * @param keyCode the key code wish to simulate typing
807      * @param lastType is this the last to type
808      */
809     protected void doType(final int keyCode, final boolean lastType) {
810         final DomText domText = getDoTypeNode();
811         if (domText != null) {
812             domText.doType(keyCode, this, lastType);
813         }
814     }
815 
816     /**
817      * Returns the node to type into.
818      * @return the node
819      */
820     private DomText getDoTypeNode() {
821         final HTMLElement scriptElement = getScriptableObject();
822         if (scriptElement.isIsContentEditable()
823                 || "on".equals(((Document) scriptElement.getOwnerDocument()).getDesignMode())) {
824 
825             DomNode node = this;
826             while (node.getLastChild() != null) {
827                 node = node.getLastChild();
828             }
829 
830             if (node instanceof DomText text) {
831                 return text;
832             }
833 
834             final DomText domText = new DomText(getPage(), "");
835             appendChild(domText);
836             return domText;
837         }
838         return null;
839     }
840 
841     /**
842      * Called from {@link DoTypeProcessor}.
843      * @param newValue the new value
844      * @param notifyAttributeChangeListeners to notify the associated {@link HtmlAttributeChangeListener}s
845      */
846     protected void typeDone(final String newValue, final boolean notifyAttributeChangeListeners) {
847         // nothing
848     }
849 
850     /**
851      * Indicates if the provided character can be "typed" in the element.
852      * @param c the character
853      * @return {@code true} if it is accepted
854      */
855     protected boolean acceptChar(final char c) {
856         // This range is this is private use area
857         // see http://www.unicode.org/charts/PDF/UE000.pdf
858         return (c < '\uE000' || c > '\uF8FF')
859                 && (c == ' ' || c == '\t' || c == '\u3000' || c == '\u2006' || !Character.isWhitespace(c));
860     }
861 
862     /**
863      * Returns {@code true} if clicking Enter (ASCII 10, or '\n') should submit the enclosed form (if any).
864      * The default implementation returns {@code false}.
865      * @return {@code true} if clicking Enter should submit the enclosed form (if any)
866      */
867     protected boolean isSubmittableByEnter() {
868         return false;
869     }
870 
871     /**
872      * Searches for an element based on the specified criteria, returning the first element which matches
873      * said criteria. Only elements which are descendants of this element are included in the search.
874      *
875      * @param elementName the name of the element to search for
876      * @param attributeName the name of the attribute to search for
877      * @param attributeValue the value of the attribute to search for
878      * @param <E> the sub-element type
879      * @return the first element which matches the specified search criteria
880      * @throws ElementNotFoundException if no element matches the specified search criteria
881      */
882     public final <E extends HtmlElement> E getOneHtmlElementByAttribute(final String elementName,
883             final String attributeName,
884         final String attributeValue) throws ElementNotFoundException {
885 
886         WebAssert.notNull("elementName", elementName);
887         WebAssert.notNull("attributeName", attributeName);
888         WebAssert.notNull("attributeValue", attributeValue);
889 
890         final List<E> list = getElementsByAttribute(elementName, attributeName, attributeValue);
891 
892         if (list.isEmpty()) {
893             throw new ElementNotFoundException(elementName, attributeName, attributeValue);
894         }
895 
896         return list.get(0);
897     }
898 
899     /**
900      * Returns all elements which are descendants of this element and match the specified search criteria.
901      *
902      * @param elementName the name of the element to search for
903      * @param attributeName the name of the attribute to search for
904      * @param attributeValue the value of the attribute to search for
905      * @param <E> the sub-element type
906      * @return all elements which are descendants of this element and match the specified search criteria
907      */
908     @SuppressWarnings("unchecked")
909     public final <E extends HtmlElement> List<E> getElementsByAttribute(
910             final String elementName,
911             final String attributeName,
912             final String attributeValue) {
913 
914         final List<E> list = new ArrayList<>();
915         final String lowerCaseTagName = elementName.toLowerCase(Locale.ROOT);
916 
917         for (final HtmlElement next : getHtmlElementDescendants()) {
918             if (next.getTagName().equals(lowerCaseTagName)) {
919                 final String attValue = next.getAttribute(attributeName);
920                 if (attValue.equals(attributeValue)) {
921                     list.add((E) next);
922                 }
923             }
924         }
925         return list;
926     }
927 
928     /**
929      * Appends a child element to this HTML element with the specified tag name
930      * if this HTML element does not already have a child with that tag name.
931      * Returns the appended child element, or the first existent child element
932      * with the specified tag name if none was appended.
933      * @param tagName the tag name of the child to append
934      * @return the added child, or the first existing child if none was added
935      */
936     public final HtmlElement appendChildIfNoneExists(final String tagName) {
937         final HtmlElement child;
938         final List<HtmlElement> children = getStaticElementsByTagName(tagName);
939         if (children.isEmpty()) {
940             // Add a new child and return it.
941             child = (HtmlElement) ((HtmlPage) getPage()).createElement(tagName);
942             appendChild(child);
943         }
944         else {
945             // Return the first existing child.
946             child = children.get(0);
947         }
948         return child;
949     }
950 
951     /**
952      * Removes the <code>i</code>th child element with the specified tag name
953      * from all relationships, if possible.
954      * @param tagName the tag name of the child to remove
955      * @param i the index of the child to remove
956      */
957     public final void removeChild(final String tagName, final int i) {
958         final List<HtmlElement> children = getStaticElementsByTagName(tagName);
959         if (i >= 0 && i < children.size()) {
960             children.get(i).remove();
961         }
962     }
963 
964     /**
965      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
966      * Returns {@code true} if this element has any JavaScript functions that need to be executed when the
967      * specified event occurs.
968      * @param eventName the name of the event, such as "onclick" or "onblur", etc
969      * @return true if an event handler has been defined otherwise false
970      */
971     public final boolean hasEventHandlers(final String eventName) {
972         if (getPage().getWebClient().isJavaScriptEngineEnabled()) {
973             final HtmlUnitScriptable jsObj = getScriptableObject();
974             if (jsObj instanceof EventTarget target) {
975                 return target.hasEventHandlers(eventName);
976             }
977         }
978         return false;
979     }
980 
981     /**
982      * Adds an HtmlAttributeChangeListener to the listener list.
983      * The listener is registered for all attributes of this HtmlElement,
984      * as well as descendant elements.
985      *
986      * @param listener the attribute change listener to be added
987      * @see #removeHtmlAttributeChangeListener(HtmlAttributeChangeListener)
988      */
989     public void addHtmlAttributeChangeListener(final HtmlAttributeChangeListener listener) {
990         WebAssert.notNull("listener", listener);
991         synchronized (attributeListeners_) {
992             attributeListeners_.add(listener);
993         }
994     }
995 
996     /**
997      * Removes an HtmlAttributeChangeListener from the listener list.
998      * This method should be used to remove HtmlAttributeChangeListener that were registered
999      * for all attributes of this HtmlElement, as well as descendant elements.
1000      *
1001      * @param listener the attribute change listener to be removed
1002      * @see #addHtmlAttributeChangeListener(HtmlAttributeChangeListener)
1003      */
1004     public void removeHtmlAttributeChangeListener(final HtmlAttributeChangeListener listener) {
1005         WebAssert.notNull("listener", listener);
1006         synchronized (attributeListeners_) {
1007             attributeListeners_.remove(listener);
1008         }
1009     }
1010 
1011     /**
1012      * {@inheritDoc}
1013      */
1014     @Override
1015     protected void checkChildHierarchy(final Node childNode) throws DOMException {
1016         if (!((childNode instanceof Element) || (childNode instanceof Text)
1017             || (childNode instanceof Comment) || (childNode instanceof ProcessingInstruction)
1018             || (childNode instanceof CDATASection) || (childNode instanceof EntityReference))) {
1019             throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
1020                 "The Element may not have a child of this type: " + childNode.getNodeType());
1021         }
1022         super.checkChildHierarchy(childNode);
1023     }
1024 
1025     /**
1026      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1027      *
1028      * Allows the parser to connect to a form that is not a parent of this due to malformed HTML code
1029      * @param form the owning form
1030      */
1031     public void setOwningForm(final HtmlForm form) {
1032         owningForm_ = form;
1033     }
1034 
1035     /**
1036      * Indicates if the attribute names are case sensitive.
1037      * @return {@code false}
1038      */
1039     @Override
1040     protected boolean isAttributeCaseSensitive() {
1041         return false;
1042     }
1043 
1044     /**
1045      * Returns the value of the attribute {@code lang}. Refer to the
1046      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1047      * documentation for details on the use of this attribute.
1048      *
1049      * @return the value of the attribute {@code lang} or an empty string if that attribute isn't defined
1050      */
1051     public final String getLangAttribute() {
1052         return getAttributeDirect("lang");
1053     }
1054 
1055     /**
1056      * Returns the value of the attribute {@code xml:lang}. Refer to the
1057      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1058      * documentation for details on the use of this attribute.
1059      *
1060      * @return the value of the attribute {@code xml:lang} or an empty string if that attribute isn't defined
1061      */
1062     public final String getXmlLangAttribute() {
1063         return getAttribute("xml:lang");
1064     }
1065 
1066     /**
1067      * Returns the value of the attribute {@code dir}. Refer to the
1068      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1069      * documentation for details on the use of this attribute.
1070      *
1071      * @return the value of the attribute {@code dir} or an empty string if that attribute isn't defined
1072      */
1073     public final String getTextDirectionAttribute() {
1074         return getAttributeDirect("dir");
1075     }
1076 
1077     /**
1078      * Returns the value of the attribute {@code onclick}. Refer to the
1079      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1080      * documentation for details on the use of this attribute.
1081      *
1082      * @return the value of the attribute {@code onclick} or an empty string if that attribute isn't defined
1083      */
1084     public final String getOnClickAttribute() {
1085         return getAttributeDirect("onclick");
1086     }
1087 
1088     /**
1089      * Returns the value of the attribute {@code ondblclick}. Refer to the
1090      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1091      * documentation for details on the use of this attribute.
1092      *
1093      * @return the value of the attribute {@code ondblclick} or an empty string if that attribute isn't defined
1094      */
1095     public final String getOnDblClickAttribute() {
1096         return getAttributeDirect("ondblclick");
1097     }
1098 
1099     /**
1100      * Returns the value of the attribute {@code onmousedown}. Refer to the
1101      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1102      * documentation for details on the use of this attribute.
1103      *
1104      * @return the value of the attribute {@code onmousedown} or an empty string if that attribute isn't defined
1105      */
1106     public final String getOnMouseDownAttribute() {
1107         return getAttributeDirect("onmousedown");
1108     }
1109 
1110     /**
1111      * Returns the value of the attribute {@code onmouseup}. Refer to the
1112      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1113      * documentation for details on the use of this attribute.
1114      *
1115      * @return the value of the attribute {@code onmouseup} or an empty string if that attribute isn't defined
1116      */
1117     public final String getOnMouseUpAttribute() {
1118         return getAttributeDirect("onmouseup");
1119     }
1120 
1121     /**
1122      * Returns the value of the attribute {@code onmouseover}. Refer to the
1123      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1124      * documentation for details on the use of this attribute.
1125      *
1126      * @return the value of the attribute {@code onmouseover} or an empty string if that attribute isn't defined
1127      */
1128     public final String getOnMouseOverAttribute() {
1129         return getAttributeDirect("onmouseover");
1130     }
1131 
1132     /**
1133      * Returns the value of the attribute {@code onmousemove}. Refer to the
1134      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1135      * documentation for details on the use of this attribute.
1136      *
1137      * @return the value of the attribute {@code onmousemove} or an empty string if that attribute isn't defined
1138      */
1139     public final String getOnMouseMoveAttribute() {
1140         return getAttributeDirect("onmousemove");
1141     }
1142 
1143     /**
1144      * Returns the value of the attribute {@code onmouseout}. Refer to the
1145      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1146      * documentation for details on the use of this attribute.
1147      *
1148      * @return the value of the attribute {@code onmouseout} or an empty string if that attribute isn't defined
1149      */
1150     public final String getOnMouseOutAttribute() {
1151         return getAttributeDirect("onmouseout");
1152     }
1153 
1154     /**
1155      * Returns the value of the attribute {@code onkeypress}. Refer to the
1156      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1157      * documentation for details on the use of this attribute.
1158      *
1159      * @return the value of the attribute {@code onkeypress} or an empty string if that attribute isn't defined
1160      */
1161     public final String getOnKeyPressAttribute() {
1162         return getAttributeDirect("onkeypress");
1163     }
1164 
1165     /**
1166      * Returns the value of the attribute {@code onkeydown}. Refer to the
1167      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1168      * documentation for details on the use of this attribute.
1169      *
1170      * @return the value of the attribute {@code onkeydown} or an empty string if that attribute isn't defined
1171      */
1172     public final String getOnKeyDownAttribute() {
1173         return getAttributeDirect("onkeydown");
1174     }
1175 
1176     /**
1177      * Returns the value of the attribute {@code onkeyup}. Refer to the
1178      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
1179      * documentation for details on the use of this attribute.
1180      *
1181      * @return the value of the attribute {@code onkeyup} or an empty string if that attribute isn't defined
1182      */
1183     public final String getOnKeyUpAttribute() {
1184         return getAttributeDirect("onkeyup");
1185     }
1186 
1187     /**
1188      * {@inheritDoc}
1189      */
1190     @Override
1191     public String getCanonicalXPath() {
1192         final DomNode parent = getParentNode();
1193         if (parent.getNodeType() == DOCUMENT_NODE) {
1194             return "/" + getNodeName();
1195         }
1196         return parent.getCanonicalXPath() + '/' + getXPathToken();
1197     }
1198 
1199     /**
1200      * Returns the XPath token for this node only.
1201      */
1202     private String getXPathToken() {
1203         final DomNode parent = getParentNode();
1204         int total = 0;
1205         int nodeIndex = 0;
1206         for (final DomNode child : parent.getChildren()) {
1207             if (child.getNodeType() == ELEMENT_NODE && child.getNodeName().equals(getNodeName())) {
1208                 total++;
1209             }
1210             if (child == this) {
1211                 nodeIndex = total;
1212             }
1213         }
1214 
1215         if (nodeIndex == 1 && total == 1) {
1216             return getNodeName();
1217         }
1218         return getNodeName() + '[' + nodeIndex + ']';
1219     }
1220 
1221     /**
1222      * Returns the value of the 'hidden' attribute or an empty string if not set.
1223      *
1224      * @return the value of the 'hidden' attribute or an empty string if not set.
1225      */
1226     public String getHidden() {
1227         return getAttributeDirect(ATTRIBUTE_HIDDEN);
1228     }
1229 
1230     /**
1231      * Returns true if the hidden attribute is set.
1232      *
1233      * @return true if the hidden attribute is set.
1234      */
1235     public boolean isHidden() {
1236         return ATTRIBUTE_NOT_DEFINED != getAttributeDirect(ATTRIBUTE_HIDDEN);
1237     }
1238 
1239     /**
1240      * Sets the {@code hidden} property.
1241      * If the provided string is empty, the 'hidden' attribute will be removed.
1242      * If the provided string is 'until-found' then the attribute value will be 'until-found'.
1243      * For all other provided strings the attribute will be set to ''.
1244      * @see #setHidden(boolean)
1245      * @param hidden the {@code hidden} property
1246      */
1247     public void setHidden(final String hidden) {
1248         if ("until-found".equalsIgnoreCase(hidden)) {
1249             setAttribute(ATTRIBUTE_HIDDEN, "until-found");
1250             return;
1251         }
1252 
1253         if (StringUtils.isEmptyString(hidden)) {
1254             removeAttribute(ATTRIBUTE_HIDDEN);
1255             return;
1256         }
1257 
1258         setAttribute(ATTRIBUTE_HIDDEN, "");
1259     }
1260 
1261     /**
1262      * Sets the {@code hidden} property.
1263      * @param hidden the {@code hidden} property
1264      */
1265     public void setHidden(final boolean hidden) {
1266         if (hidden) {
1267             setAttribute(ATTRIBUTE_HIDDEN, "");
1268             return;
1269         }
1270 
1271         removeAttribute(ATTRIBUTE_HIDDEN);
1272     }
1273 
1274     /**
1275      * {@inheritDoc}
1276      * Overwritten to support the hidden attribute (html5).
1277      */
1278     @Override
1279     public boolean isDisplayed() {
1280         if (isHidden()) {
1281             return false;
1282         }
1283         return super.isDisplayed();
1284     }
1285 
1286     /**
1287      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1288      *
1289      * Returns the default display style.
1290      *
1291      * @return the default display style
1292      */
1293     public DisplayStyle getDefaultStyleDisplay() {
1294         return DisplayStyle.BLOCK;
1295     }
1296 
1297     /**
1298      * Helper for src retrieval and normalization.
1299      *
1300      * @return the value of the attribute {@code src} with all line breaks removed
1301      *         or an empty string if that attribute isn't defined.
1302      */
1303     protected final String getSrcAttributeNormalized() {
1304         final String attrib = getAttributeDirect(SRC_ATTRIBUTE);
1305         if (ATTRIBUTE_NOT_DEFINED == attrib) {
1306             return attrib;
1307         }
1308 
1309         return StringUtils.replaceChars(attrib, "\r\n", "");
1310     }
1311 
1312     /**
1313      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1314      *
1315      * Detach this node from all relationships with other nodes.
1316      * This is the first step of a move.
1317      */
1318     @Override
1319     protected void detach() {
1320         final SgmlPage page = getPage();
1321         if (!page.getWebClient().isJavaScriptEngineEnabled()) {
1322             super.detach();
1323             return;
1324         }
1325 
1326         final HtmlUnitScriptable document = page.getScriptableObject();
1327 
1328         if (document instanceof HTMLDocument doc) {
1329             final Object activeElement = doc.getActiveElement();
1330 
1331             if (activeElement == getScriptableObject()) {
1332                 if (hasFeature(HTMLELEMENT_REMOVE_ACTIVE_TRIGGERS_BLUR_EVENT)) {
1333                     ((HtmlPage) page).setFocusedElement(null);
1334                 }
1335                 else {
1336                     ((HtmlPage) page).setElementWithFocus(null);
1337                 }
1338             }
1339             else {
1340                 for (final DomNode child : getChildNodes()) {
1341                     if (activeElement == child.getScriptableObject()) {
1342                         if (hasFeature(HTMLELEMENT_REMOVE_ACTIVE_TRIGGERS_BLUR_EVENT)) {
1343                             ((HtmlPage) page).setFocusedElement(null);
1344                         }
1345                         else {
1346                             ((HtmlPage) page).setElementWithFocus(null);
1347                         }
1348 
1349                         break;
1350                     }
1351                 }
1352             }
1353         }
1354         super.detach();
1355     }
1356 
1357     /**
1358      * {@inheritDoc}
1359      */
1360     @Override
1361     public boolean handles(final Event event) {
1362         if (Event.TYPE_BLUR.equals(event.getType()) || Event.TYPE_FOCUS.equals(event.getType())) {
1363             return this instanceof SubmittableElement || getTabIndex() != null;
1364         }
1365 
1366         if (isDisabledElementAndDisabled()) {
1367             return false;
1368         }
1369         return super.handles(event);
1370     }
1371 
1372     /**
1373      * Returns whether the {@code SHIFT} is currently pressed.
1374      * @return whether the {@code SHIFT} is currently pressed
1375      */
1376     protected boolean isShiftPressed() {
1377         return shiftPressed_;
1378     }
1379 
1380     /**
1381      * Returns whether the {@code CTRL} is currently pressed.
1382      * @return whether the {@code CTRL} is currently pressed
1383      */
1384     public boolean isCtrlPressed() {
1385         return ctrlPressed_;
1386     }
1387 
1388     /**
1389      * Returns whether the {@code ALT} is currently pressed.
1390      * @return whether the {@code ALT} is currently pressed
1391      */
1392     public boolean isAltPressed() {
1393         return altPressed_;
1394     }
1395 
1396     /**
1397      * Returns whether this element satisfies all form validation constraints set.
1398      * @return whether this element satisfies all form validation constraints set
1399      */
1400     public boolean isValid() {
1401         return !isRequiredSupported()
1402                 || ATTRIBUTE_NOT_DEFINED == getAttributeDirect(ATTRIBUTE_REQUIRED)
1403                 || !getAttributeDirect(VALUE_ATTRIBUTE).isEmpty();
1404     }
1405 
1406     /**
1407      * Returns whether this element supports the {@code required} constraint.
1408      * @return whether this element supports the {@code required} constraint
1409      */
1410     protected boolean isRequiredSupported() {
1411         return false;
1412     }
1413 
1414     /**
1415      * Returns the true if the required attribute is set.
1416      *
1417      * @return the true if the required attribute is set
1418      */
1419     public boolean isRequired() {
1420         return isRequiredSupported() && hasAttribute(ATTRIBUTE_REQUIRED);
1421     }
1422 
1423     /**
1424      * Returns the true if the required attribute is supported and set.
1425      *
1426      * @return the true if the required attribute is supported and set
1427      */
1428     public boolean isOptional() {
1429         return isRequiredSupported() && !hasAttribute(ATTRIBUTE_REQUIRED);
1430     }
1431 
1432     /**
1433      * Sets the {@code required} attribute.
1434      * @param required the new attribute value
1435      */
1436     public void setRequired(final boolean required) {
1437         if (isRequiredSupported()) {
1438             if (required) {
1439                 setAttribute(ATTRIBUTE_REQUIRED, ATTRIBUTE_REQUIRED);
1440             }
1441             else {
1442                 removeAttribute(ATTRIBUTE_REQUIRED);
1443             }
1444         }
1445     }
1446 
1447     /**
1448      * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1449      *
1450      * @param returnNullIfFixed if position is 'fixed' return null
1451      * @return the offset parent {@link HtmlElement}
1452      */
1453     public HtmlElement getOffsetParentInternal(final boolean returnNullIfFixed) {
1454         if (getParentNode() == null) {
1455             return null;
1456         }
1457 
1458         final WebWindow webWindow = getPage().getEnclosingWindow();
1459         final ComputedCssStyleDeclaration style = webWindow.getComputedStyle(this, null);
1460         final String position = style.getPositionWithInheritance();
1461 
1462         if (returnNullIfFixed && FIXED.equals(position)) {
1463             return null;
1464         }
1465 
1466         final boolean staticPos = STATIC.equals(position);
1467 
1468         DomNode currentElement = this;
1469         while (currentElement != null) {
1470 
1471             final DomNode parentNode = currentElement.getParentNode();
1472             if (parentNode instanceof HtmlBody
1473                 || (staticPos && parentNode instanceof HtmlTableDataCell)
1474                 || (staticPos && parentNode instanceof HtmlTable)) {
1475                 return (HtmlElement) parentNode;
1476             }
1477 
1478             if (parentNode instanceof HtmlElement element) {
1479                 final ComputedCssStyleDeclaration parentStyle =
1480                         webWindow.getComputedStyle(element, null);
1481                 final String parentPosition = parentStyle.getPositionWithInheritance();
1482                 if (!STATIC.equals(parentPosition)) {
1483                     return element;
1484                 }
1485             }
1486 
1487             currentElement = currentElement.getParentNode();
1488         }
1489 
1490         return null;
1491     }
1492 
1493     /**
1494      * Returns this element's top offset, which is the calculated left position of this.
1495      *
1496      * @return this element's top offset, which is the calculated left position of this
1497      *         element relative to the <code>offsetParent</code>.
1498      */
1499     public int getOffsetTop() {
1500         if (this instanceof HtmlBody) {
1501             return 0;
1502         }
1503 
1504         int top = 0;
1505 
1506         // Add the offset for this node.
1507         final WebWindow webWindow = getPage().getEnclosingWindow();
1508         ComputedCssStyleDeclaration style = webWindow.getComputedStyle(this, null);
1509         top += style.getTop(true, false, false);
1510 
1511         // If this node is absolutely positioned, we're done.
1512         final String position = style.getPositionWithInheritance();
1513         if (ABSOLUTE.equals(position) || FIXED.equals(position)) {
1514             return top;
1515         }
1516 
1517         final HtmlElement offsetParent = getOffsetParentInternal(false);
1518 
1519         // Add the offset for the ancestor nodes.
1520         DomNode parentNode = getParentNode();
1521         while (parentNode != null && parentNode != offsetParent) {
1522             if (parentNode instanceof HtmlElement element) {
1523                 style = webWindow.getComputedStyle(element, null);
1524                 top += style.getTop(false, true, true);
1525             }
1526             parentNode = parentNode.getParentNode();
1527         }
1528 
1529         if (offsetParent != null) {
1530             style = webWindow.getComputedStyle(this, null);
1531             final boolean thisElementHasTopMargin = style.getMarginTopValue() != 0;
1532 
1533             style = webWindow.getComputedStyle(offsetParent, null);
1534             if (!thisElementHasTopMargin) {
1535                 top += style.getMarginTopValue();
1536             }
1537             top += style.getPaddingTopValue();
1538         }
1539 
1540         return top;
1541     }
1542 
1543     /**
1544      * Returns this element's left offset, which is the calculated left position of this.
1545      *
1546      * @return this element's left offset, which is the calculated left position of this
1547      *         element relative to the <code>offsetParent</code>.
1548      */
1549     public int getOffsetLeft() {
1550         if (this instanceof HtmlBody) {
1551             return 0;
1552         }
1553 
1554         int left = 0;
1555 
1556         // Add the offset for this node.
1557         final WebWindow webWindow = getPage().getEnclosingWindow();
1558         ComputedCssStyleDeclaration style = webWindow.getComputedStyle(this, null);
1559         left += style.getLeft(true, false, false);
1560 
1561         // If this node is absolutely positioned, we're done.
1562         final String position = style.getPositionWithInheritance();
1563         if (ABSOLUTE.equals(position) || FIXED.equals(position)) {
1564             return left;
1565         }
1566 
1567         final HtmlElement offsetParent = getOffsetParentInternal(false);
1568 
1569         DomNode parentNode = getParentNode();
1570         while (parentNode != null && parentNode != offsetParent) {
1571             if (parentNode instanceof HtmlElement element) {
1572                 style = webWindow.getComputedStyle(element, null);
1573                 left += style.getLeft(true, true, true);
1574             }
1575             parentNode = parentNode.getParentNode();
1576         }
1577 
1578         if (offsetParent != null) {
1579             style = webWindow.getComputedStyle(offsetParent, null);
1580             left += style.getMarginLeftValue();
1581             left += style.getPaddingLeftValue();
1582         }
1583 
1584         return left;
1585     }
1586 
1587     /**
1588      * Returns this element's X position.
1589      * @return this element's X position
1590      */
1591     public int getPosX() {
1592         int cumulativeOffset = 0;
1593         final WebWindow webWindow = getPage().getEnclosingWindow();
1594 
1595         HtmlElement element = this;
1596         while (element != null) {
1597             cumulativeOffset += element.getOffsetLeft();
1598             if (element != this) {
1599                 final ComputedCssStyleDeclaration style =
1600                         webWindow.getComputedStyle(element, null);
1601                 cumulativeOffset += style.getBorderLeftValue();
1602             }
1603             element = element.getOffsetParentInternal(false);
1604         }
1605 
1606         return cumulativeOffset;
1607     }
1608 
1609     /**
1610      * Returns this element's Y position.
1611      * @return this element's Y position
1612      */
1613     public int getPosY() {
1614         int cumulativeOffset = 0;
1615         final WebWindow webWindow = getPage().getEnclosingWindow();
1616 
1617         HtmlElement element = this;
1618         while (element != null) {
1619             cumulativeOffset += element.getOffsetTop();
1620             if (element != this) {
1621                 final ComputedCssStyleDeclaration style =
1622                         webWindow.getComputedStyle(element, null);
1623                 cumulativeOffset += style.getBorderTopValue();
1624             }
1625             element = element.getOffsetParentInternal(false);
1626         }
1627 
1628         return cumulativeOffset;
1629     }
1630 
1631     /**
1632      * {@inheritDoc}
1633      */
1634     @Override
1635     public DomNode cloneNode(final boolean deep) {
1636         final HtmlElement newNode = (HtmlElement) super.cloneNode(deep);
1637         if (!deep) {
1638             synchronized (attributeListeners_) {
1639                 newNode.attributeListeners_.clear();
1640                 newNode.attributeListeners_.addAll(attributeListeners_);
1641             }
1642         }
1643 
1644         return newNode;
1645     }
1646 }