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.html;
16  
17  import static org.htmlunit.BrowserVersionFeatures.HTMLDOCUMENT_ELEMENTS_BY_NAME_EMPTY;
18  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
19  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
20  
21  import java.io.IOException;
22  import java.io.Serializable;
23  import java.net.URL;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.function.Supplier;
27  
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.htmlunit.ScriptResult;
32  import org.htmlunit.StringWebResponse;
33  import org.htmlunit.WebClient;
34  import org.htmlunit.WebWindow;
35  import org.htmlunit.corejs.javascript.Context;
36  import org.htmlunit.corejs.javascript.Function;
37  import org.htmlunit.corejs.javascript.Scriptable;
38  import org.htmlunit.corejs.javascript.VarScope;
39  import org.htmlunit.html.BaseFrameElement;
40  import org.htmlunit.html.DomDocumentFragment;
41  import org.htmlunit.html.DomElement;
42  import org.htmlunit.html.DomNode;
43  import org.htmlunit.html.FrameWindow;
44  import org.htmlunit.html.HtmlAttributeChangeEvent;
45  import org.htmlunit.html.HtmlElement;
46  import org.htmlunit.html.HtmlForm;
47  import org.htmlunit.html.HtmlImage;
48  import org.htmlunit.html.HtmlPage;
49  import org.htmlunit.html.HtmlScript;
50  import org.htmlunit.javascript.HtmlUnitScriptable;
51  import org.htmlunit.javascript.JavaScriptEngine;
52  import org.htmlunit.javascript.PostponedAction;
53  import org.htmlunit.javascript.configuration.JsxClass;
54  import org.htmlunit.javascript.configuration.JsxConstructor;
55  import org.htmlunit.javascript.configuration.JsxFunction;
56  import org.htmlunit.javascript.configuration.JsxGetter;
57  import org.htmlunit.javascript.configuration.JsxStaticFunction;
58  import org.htmlunit.javascript.host.Element;
59  import org.htmlunit.javascript.host.dom.AbstractList.EffectOnCache;
60  import org.htmlunit.javascript.host.dom.Attr;
61  import org.htmlunit.javascript.host.dom.Document;
62  import org.htmlunit.javascript.host.dom.Node;
63  import org.htmlunit.javascript.host.dom.NodeList;
64  import org.htmlunit.javascript.host.dom.Selection;
65  import org.htmlunit.javascript.host.event.Event;
66  import org.htmlunit.javascript.host.event.EventTarget;
67  import org.htmlunit.util.UrlUtils;
68  
69  /**
70   * A JavaScript object for {@code HTMLDocument}.
71   *
72   * @author Mike Bowler
73   * @author David K. Taylor
74   * @author Chen Jun
75   * @author Christian Sell
76   * @author Chris Erskine
77   * @author Marc Guillemot
78   * @author Daniel Gredler
79   * @author Michael Ottati
80   * @author George Murnock
81   * @author Ahmed Ashour
82   * @author Rob Di Marco
83   * @author Sudhan Moghe
84   * @author Mike Dirolf
85   * @author Ronald Brill
86   * @author Frank Danek
87   * @author Sven Strickroth
88   *
89   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document">MDN Documentation</a>
90   * @see <a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-7068919">
91   *     W3C DOM Level 1</a>
92   */
93  @JsxClass
94  public class HTMLDocument extends Document {
95  
96      private static final Log LOG = LogFactory.getLog(HTMLDocument.class);
97  
98      private enum ParsingStatus { OUTSIDE, START, IN_NAME, INSIDE, IN_STRING }
99  
100     /** The buffer that will be used for calls to document.write(). */
101     private final StringBuilder writeBuilder_ = new StringBuilder();
102     private boolean writeInCurrentDocument_ = true;
103 
104     private boolean closePostponedAction_;
105     private boolean executionExternalPostponed_;
106 
107     /**
108      * JavaScript constructor.
109      */
110     @Override
111     @JsxConstructor
112     public void jsConstructor() {
113         super.jsConstructor();
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public DomNode getDomNodeOrDie() {
121         try {
122             return super.getDomNodeOrDie();
123         }
124         catch (final IllegalStateException e) {
125             throw JavaScriptEngine.typeError("No node attached to this object");
126         }
127     }
128 
129     /**
130      * Returns the HTML page that this document is modeling.
131      * @return the HTML page that this document is modeling
132      */
133     @Override
134     public HtmlPage getPage() {
135         return (HtmlPage) getDomNodeOrDie();
136     }
137 
138     /**
139      * Parses the given string of HTML without sanitizing it and returns a new HTMLDocument.
140      *
141      * @param cx the current context
142      * @param scope the scope
143      * @param thisObj the scriptable this object
144      * @param args the arguments
145      * @param funObj the function object
146      * @return a newly created {@link HTMLDocument}
147      *
148      * @see <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsehtmlunsafe">
149      *     HTML spec - parseHTMLUnsafe</a>
150      */
151     @JsxStaticFunction
152     public static HTMLDocument parseHTMLUnsafe(final Context cx, final VarScope scope,
153             final Scriptable thisObj, final Object[] args, final Function funObj) {
154         return (HTMLDocument) Document.parseHTMLUnsafe(cx, scope, thisObj, args, funObj);
155     }
156 
157     /**
158      * JavaScript function "write" may accept a variable number of arguments.
159      * @param context the JavaScript context
160      * @param scope the scope
161      * @param thisObj the scriptable
162      * @param args the arguments passed into the method
163      * @param function the function
164      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/write">MDN Documentation</a>
165      */
166     @JsxFunction
167     public static void write(final Context context, final VarScope scope,
168             final Scriptable thisObj, final Object[] args, final Function function) {
169         final HTMLDocument thisAsDocument = getDocument(thisObj);
170         thisAsDocument.write(concatArgsAsString(args));
171     }
172 
173     /**
174      * Converts the arguments to strings and concatenate them.
175      * @param args the JavaScript arguments
176      * @return the string concatenation
177      */
178     private static String concatArgsAsString(final Object[] args) {
179         final StringBuilder builder = new StringBuilder();
180         for (final Object arg : args) {
181             builder.append(JavaScriptEngine.toString(arg));
182         }
183         return builder.toString();
184     }
185 
186     /**
187      * Moves a given Node inside the invoking node as a direct child, before a given reference node.
188      *
189      * @param context the JavaScript context
190      * @param scope the scope
191      * @param thisObj the scriptable
192      * @param args the arguments passed into the method
193      * @param function the function
194      */
195     @JsxFunction
196     public static void moveBefore(final Context context, final VarScope scope,
197             final Scriptable thisObj, final Object[] args, final Function function) {
198         moveBeforeImpl(thisObj, args, "Document");
199     }
200 
201     /**
202      * JavaScript function "writeln" may accept a variable number of arguments.
203      * @param context the JavaScript context
204      * @param scope the scope
205      * @param thisObj the scriptable
206      * @param args the arguments passed into the method
207      * @param function the function
208      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/writeln">MDN Documentation</a>
209      */
210     @JsxFunction
211     public static void writeln(final Context context, final VarScope scope,
212             final Scriptable thisObj, final Object[] args, final Function function) {
213         final HTMLDocument thisAsDocument = getDocument(thisObj);
214         thisAsDocument.write(concatArgsAsString(args) + "\n");
215     }
216 
217     /**
218      * Returns the current document instance, using <code>thisObj</code> as a hint.
219      * @param thisObj a hint as to the current document (maybe the prototype when function is used without "this")
220      * @return the current document instance
221      */
222     private static HTMLDocument getDocument(final Scriptable thisObj) {
223         // if function is used "detached", then thisObj is the top scope (ie Window), not the real object
224         // cf unit test DocumentTest#testDocumentWrite_AssignedToVar
225         // may be the prototype too
226         // cf DocumentTest#testDocumentWrite_AssignedToVar2
227         if (thisObj instanceof HTMLDocument document && thisObj.getPrototype() instanceof HTMLDocument) {
228             return document;
229         }
230         if (thisObj instanceof DocumentProxy proxy && thisObj.getPrototype() instanceof HTMLDocument) {
231             return (HTMLDocument) proxy.getDelegee();
232         }
233 
234         throw JavaScriptEngine.reportRuntimeError("Function can't be used detached from document");
235     }
236 
237     /**
238      * This a hack!!! A cleaner way is welcome.
239      * Handle a case where document.write() is simply ignored.
240      * See HTMLDocumentWrite2Test.write_fromScriptAddedWithAppendChild_external.
241      * @param executing indicates if executing or not
242      */
243     public void setExecutingDynamicExternalPosponed(final boolean executing) {
244         executionExternalPostponed_ = executing;
245     }
246 
247     /**
248      * JavaScript function "write".
249      * <p>
250      * See http://www.whatwg.org/specs/web-apps/current-work/multipage/section-dynamic.html for
251      * a good description of the semantics of open(), write(), writeln() and close().
252      * </p>
253      *
254      * @param content the content to write
255      */
256     protected void write(final String content) {
257         // really strange: if called from an external script loaded as postponed action, write is ignored!!!
258         if (executionExternalPostponed_) {
259             if (LOG.isDebugEnabled()) {
260                 LOG.debug("skipping write for external posponed: " + content);
261             }
262             return;
263         }
264 
265         if (LOG.isDebugEnabled()) {
266             LOG.debug("write: " + content);
267         }
268 
269         final HtmlPage page = (HtmlPage) getDomNodeOrDie();
270         if (!page.isBeingParsed()) {
271             writeInCurrentDocument_ = false;
272         }
273 
274         // Add content to the content buffer.
275         writeBuilder_.append(content);
276 
277         // If open() was called; don't write to doc yet -- wait for call to close().
278         if (!writeInCurrentDocument_) {
279             LOG.debug("wrote content to buffer");
280             scheduleImplicitClose();
281             return;
282         }
283         final String bufferedContent = writeBuilder_.toString();
284         if (!canAlreadyBeParsed(bufferedContent)) {
285             LOG.debug("write: not enough content to parse it now");
286             return;
287         }
288 
289         writeBuilder_.setLength(0);
290         page.writeInParsedStream(bufferedContent);
291     }
292 
293     private void scheduleImplicitClose() {
294         if (!closePostponedAction_) {
295             closePostponedAction_ = true;
296             final HtmlPage page = (HtmlPage) getDomNodeOrDie();
297             final WebWindow enclosingWindow = page.getEnclosingWindow();
298             page.getWebClient().getJavaScriptEngine().addPostponedAction(
299                     new PostponedAction(page, "HTMLDocument.scheduleImplicitClose") {
300                         @Override
301                         public void execute() throws Exception {
302                             if (writeBuilder_.length() != 0) {
303                                 close();
304                             }
305                             closePostponedAction_ = false;
306                         }
307 
308                         @Override
309                         public boolean isStillAlive() {
310                             return !enclosingWindow.isClosed();
311                         }
312                     });
313         }
314     }
315 
316     /**
317      * Indicates if the content is a well-formed HTML snippet that can already be parsed to be added to the DOM.
318      *
319      * @param content the HTML snippet
320      * @return {@code false} if it is not well-formed
321      */
322     static boolean canAlreadyBeParsed(final String content) {
323         // all <script> must have their </script> because the parser doesn't close automatically this tag
324         // All tags must be complete, that is from '<' to '>'.
325         ParsingStatus tagState = ParsingStatus.OUTSIDE;
326         int tagNameBeginIndex = 0;
327         int scriptTagCount = 0;
328         boolean tagIsOpen = true;
329         char stringBoundary = 0;
330         boolean stringSkipNextChar = false;
331         int index = 0;
332         char openingQuote = 0;
333         for (final char currentChar : content.toCharArray()) {
334             switch (tagState) {
335                 case OUTSIDE:
336                     if (currentChar == '<') {
337                         tagState = ParsingStatus.START;
338                         tagIsOpen = true;
339                     }
340                     else if (scriptTagCount > 0 && (currentChar == '\'' || currentChar == '"')) {
341                         tagState = ParsingStatus.IN_STRING;
342                         stringBoundary = currentChar;
343                         stringSkipNextChar = false;
344                     }
345                     break;
346                 case START:
347                     if (currentChar == '/') {
348                         tagIsOpen = false;
349                         tagNameBeginIndex = index + 1;
350                     }
351                     else {
352                         tagNameBeginIndex = index;
353                     }
354                     tagState = ParsingStatus.IN_NAME;
355                     break;
356                 case IN_NAME:
357                     if (Character.isWhitespace(currentChar) || currentChar == '>') {
358                         final String tagName = content.substring(tagNameBeginIndex, index);
359                         if ("script".equalsIgnoreCase(tagName)) {
360                             if (tagIsOpen) {
361                                 scriptTagCount++;
362                             }
363                             else if (scriptTagCount > 0) {
364                                 // Ignore extra close tags for now. Let the parser deal with them.
365                                 scriptTagCount--;
366                             }
367                         }
368                         if (currentChar == '>') {
369                             tagState = ParsingStatus.OUTSIDE;
370                         }
371                         else {
372                             tagState = ParsingStatus.INSIDE;
373                         }
374                     }
375                     else if (!Character.isLetter(currentChar)) {
376                         tagState = ParsingStatus.OUTSIDE;
377                     }
378                     break;
379                 case INSIDE:
380                     if (currentChar == openingQuote) {
381                         openingQuote = 0;
382                     }
383                     else if (openingQuote == 0) {
384                         if (currentChar == '\'' || currentChar == '"') {
385                             openingQuote = currentChar;
386                         }
387                         else if (currentChar == '>' && openingQuote == 0) {
388                             tagState = ParsingStatus.OUTSIDE;
389                         }
390                     }
391                     break;
392                 case IN_STRING:
393                     if (stringSkipNextChar) {
394                         stringSkipNextChar = false;
395                     }
396                     else {
397                         if (currentChar == stringBoundary) {
398                             tagState = ParsingStatus.OUTSIDE;
399                         }
400                         else if (currentChar == '\\') {
401                             stringSkipNextChar = true;
402                         }
403                     }
404                     break;
405                 default:
406                     // nothing
407             }
408             index++;
409         }
410         if (scriptTagCount > 0 || tagState != ParsingStatus.OUTSIDE) {
411             if (LOG.isDebugEnabled()) {
412                 final StringBuilder message = new StringBuilder()
413                     .append("canAlreadyBeParsed() returns false for content: '")
414                     .append(StringUtils.abbreviateMiddle(content, ".", 100))
415                     .append("' (scriptTagCount: ")
416                         .append(scriptTagCount)
417                     .append(" tagState: ")
418                         .append(tagState)
419                     .append(')');
420                 LOG.debug(message.toString());
421             }
422             return false;
423         }
424 
425         return true;
426     }
427 
428     /**
429      * Gets the node that is the last one when exploring following nodes, depth-first.
430      * @param node the node to search
431      * @return the searched node
432      */
433     HtmlElement getLastHtmlElement(final HtmlElement node) {
434         final DomNode lastChild = node.getLastChild();
435         if (!(lastChild instanceof HtmlElement)
436                 || lastChild instanceof HtmlScript) {
437             return node;
438         }
439 
440         return getLastHtmlElement((HtmlElement) lastChild);
441     }
442 
443     /**
444      * JavaScript function "open".
445      * <p>
446      * See http://www.whatwg.org/specs/web-apps/current-work/multipage/section-dynamic.html for
447      * a good description of the semantics of open(), write(), writeln() and close().
448      * </p>
449      *
450      * @param url when a new document is opened, <i>url</i> is a String that specifies a MIME type for the document.
451      *        When a new window is opened, <i>url</i> is a String that specifies the URL to render in the new window
452      * @param name the name
453      * @param features the features
454      * @param replace whether to replace in the history list or not
455      * @return a reference to the new document object
456      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/open">MDN Documentation</a>
457      */
458     @JsxFunction
459     public HTMLDocument open(final Object url, final Object name, final Object features,
460             final Object replace) {
461         // Any open() invocations are ignored during the parsing stage, because write() and
462         // writeln() invocations will directly append content to the current insertion point.
463         final HtmlPage page = getPage();
464         if (page.isBeingParsed()) {
465             LOG.warn("Ignoring call to open() during the parsing stage.");
466             return null;
467         }
468 
469         // We're not in the parsing stage; OK to continue.
470         if (!writeInCurrentDocument_) {
471             LOG.warn("Function open() called when document is already open.");
472         }
473         writeInCurrentDocument_ = false;
474 
475         // like the browsers, document.open() removes all event listeners and handlers
476         // of the document, all its nodes and the window
477         for (final DomNode node : page.getDescendants()) {
478             if (node.getScriptableObjectIfCreated() instanceof EventTarget target) {
479                 target.removeAllEventListeners();
480             }
481         }
482         removeAllEventListeners();
483         getWindow().removeAllEventListeners();
484 
485         final WebWindow ww = getWindow().getWebWindow();
486         if (ww instanceof FrameWindow window
487                 && UrlUtils.ABOUT_BLANK.equals(getPage().getUrl().toExternalForm())) {
488             final URL enclosingUrl = window.getEnclosingPage().getUrl();
489             getPage().getWebResponse().getWebRequest().setUrl(enclosingUrl);
490         }
491         return this;
492     }
493 
494     /**
495      * {@inheritDoc}
496      */
497     @Override
498     @JsxFunction({FF, FF_ESR})
499     public void close() throws IOException {
500         if (writeInCurrentDocument_) {
501             LOG.warn("close() called when document is not open.");
502         }
503         else {
504             final HtmlPage page = getPage();
505             final URL url = page.getUrl();
506             final StringWebResponse webResponse = new StringWebResponse(writeBuilder_.toString(), url);
507             webResponse.setFromJavascript(true);
508             writeInCurrentDocument_ = true;
509             writeBuilder_.setLength(0);
510 
511             final WebClient webClient = page.getWebClient();
512             final WebWindow window = page.getEnclosingWindow();
513             // reset isAttachedToPageDuringOnload_ to trigger the onload event for chrome also
514             if (window instanceof FrameWindow frameWindow) {
515                 final BaseFrameElement frame = frameWindow.getFrameElement();
516                 final HtmlUnitScriptable scriptable = frame.getScriptableObject();
517                 if (scriptable instanceof HTMLIFrameElement element) {
518                     element.onRefresh();
519                 }
520             }
521             webClient.loadWebResponseInto(webResponse, window);
522         }
523     }
524 
525     /**
526      * {@inheritDoc}
527      */
528     @JsxGetter
529     @Override
530     public Element getDocumentElement() {
531         implicitCloseIfNecessary();
532         return super.getDocumentElement();
533     }
534 
535     /**
536      * Closes the document implicitly, i.e. flushes the <code>document.write</code> buffer (IE only).
537      */
538     private void implicitCloseIfNecessary() {
539         if (!writeInCurrentDocument_) {
540             try {
541                 close();
542             }
543             catch (final IOException e) {
544                 throw JavaScriptEngine.throwAsScriptRuntimeEx(e);
545             }
546         }
547     }
548 
549     /**
550      * {@inheritDoc}
551      */
552     @Override
553     public Node appendChild(final Object childObject) {
554         if (childObject instanceof Node childNode
555                 && !isInsertableAtEnd(getPage(), childNode.getDomNodeOrDie())) {
556             throw JavaScriptEngine.asJavaScriptException(
557                     getWindow(),
558                     "Node cannot be inserted at the specified point in the hierarchy.",
559                     org.htmlunit.javascript.host.dom.DOMException.HIERARCHY_REQUEST_ERR);
560         }
561         return super.appendChild(childObject);
562     }
563 
564     /**
565      * Pre-insertion validity of a node that is appended to a document (DOM standard, "ensure
566      * pre-insertion validity"): at most one element, at most one doctype and the doctype has to be
567      * in front of the element; text is not allowed; comments and processing instructions are always fine.
568      */
569     private static boolean isInsertableAtEnd(final HtmlPage page, final DomNode node) {
570         if (node instanceof DomDocumentFragment fragment) {
571             int elements = 0;
572             for (final DomNode child : fragment.getChildren()) {
573                 if (child instanceof org.w3c.dom.Element) {
574                     elements++;
575                 }
576                 else if (child instanceof org.w3c.dom.Text) {
577                     return false;
578                 }
579             }
580             return elements == 0 || (elements == 1 && page.getDocumentElement() == null);
581         }
582         if (node instanceof org.w3c.dom.Element) {
583             return page.getDocumentElement() == null;
584         }
585         if (node instanceof org.w3c.dom.DocumentType) {
586             return page.getDoctype() == null && page.getDocumentElement() == null;
587         }
588         return node instanceof org.w3c.dom.Comment || node instanceof org.w3c.dom.ProcessingInstruction;
589     }
590 
591     /**
592      * Returns the element with the specified ID, or {@code null} if that element could not be found.
593      * @param id the ID to search for
594      * @return the element, or {@code null} if it could not be found
595      */
596     @JsxFunction
597     @Override
598     public HtmlUnitScriptable getElementById(final String id) {
599         implicitCloseIfNecessary();
600         final DomElement domElement = getPage().getElementById(id);
601         if (domElement == null) {
602             // Just fall through - result is already set to null
603             if (LOG.isDebugEnabled()) {
604                 LOG.debug("getElementById(" + id + "): no DOM node found with this id");
605             }
606             return null;
607         }
608 
609         final HtmlUnitScriptable jsElement = getScriptableFor(domElement);
610         if (jsElement == NOT_FOUND) {
611             if (LOG.isDebugEnabled()) {
612                 LOG.debug("getElementById(" + id
613                         + ") cannot return a result as there isn't a JavaScript object for the HTML element "
614                         + domElement.getClass().getName());
615             }
616             return null;
617         }
618         return jsElement;
619     }
620 
621     /**
622      * {@inheritDoc}
623      */
624     @Override
625     public HTMLCollection getElementsByClassName(final String className) {
626         return getDocumentElement().getElementsByClassName(className);
627     }
628 
629     /**
630      * {@inheritDoc}
631      */
632     @Override
633     public NodeList getElementsByName(final String elementName) {
634         implicitCloseIfNecessary();
635 
636         if ("null".equals(elementName)
637                 || (elementName.isEmpty()
638                     && getBrowserVersion().hasFeature(HTMLDOCUMENT_ELEMENTS_BY_NAME_EMPTY))) {
639             return NodeList.staticNodeList(getParentScope(), new ArrayList<>());
640         }
641 
642         final HtmlPage page = getPage();
643         final NodeList elements = new NodeList(page, true);
644         elements.setElementsSupplier(
645                 (Supplier<List<DomNode>> & Serializable)
646                 () -> new ArrayList<>(page.getElementsByName(elementName)));
647 
648         elements.setEffectOnCacheFunction(
649                 (java.util.function.Function<HtmlAttributeChangeEvent, EffectOnCache> & Serializable)
650                 event -> {
651                     if ("name".equals(event.getName())) {
652                         return EffectOnCache.RESET;
653                     }
654                     return EffectOnCache.NONE;
655                 });
656 
657         return elements;
658     }
659 
660     /**
661      * Calls to <code>document.XYZ</code> should first look at elements named <code>XYZ</code> before
662      * using standard functions.
663      *
664      * {@inheritDoc}
665      */
666     @Override
667     protected Object getWithPreemption(final String name) {
668         final HtmlPage page = (HtmlPage) getDomNodeOrNull();
669         if (page == null) {
670             final Object response = getPrototype().get(name, this);
671             if (response != NOT_FOUND) {
672                 return response;
673             }
674         }
675         return getIt(name);
676     }
677 
678     private Object getIt(final String name) {
679         final HtmlPage page = (HtmlPage) getDomNodeOrNull();
680         if (page == null) {
681             return NOT_FOUND;
682         }
683 
684         // for performance,
685         // we will calculate the elements to decide if we really have
686         // to really create a HTMLCollection or not
687         final List<DomNode> matchingElements = getItComputeElements(page, name);
688         final int size = matchingElements.size();
689         if (size == 0) {
690             return NOT_FOUND;
691         }
692         if (size == 1) {
693             final DomNode object = matchingElements.get(0);
694             if (object instanceof BaseFrameElement element) {
695                 return element.getEnclosedWindow().getScriptableObject();
696             }
697             return super.getScriptableFor(object);
698         }
699 
700         final HTMLCollection coll = new HTMLCollection(page, matchingElements) {
701             @Override
702             protected HtmlUnitScriptable getScriptableFor(final Object object) {
703                 if (object instanceof BaseFrameElement element) {
704                     return element.getEnclosedWindow().getScriptableObject();
705                 }
706                 return super.getScriptableFor(object);
707             }
708         };
709 
710         coll.setElementsSupplier(
711                 (Supplier<List<DomNode>> & Serializable)
712                 () -> getItComputeElements(page, name));
713 
714         coll.setEffectOnCacheFunction(
715                 (java.util.function.Function<HtmlAttributeChangeEvent, EffectOnCache> & Serializable)
716                 event -> {
717                     final String attributeName = event.getName();
718                     if (DomElement.NAME_ATTRIBUTE.equals(attributeName)) {
719                         return EffectOnCache.RESET;
720                     }
721 
722                     return EffectOnCache.NONE;
723                 });
724 
725         return coll;
726     }
727 
728     static List<DomNode> getItComputeElements(final HtmlPage page, final String name) {
729         final List<DomElement> elements = page.getElementsByName(name);
730         final List<DomNode> matchingElements = new ArrayList<>();
731         for (final DomElement elt : elements) {
732             if (elt instanceof HtmlForm || elt instanceof HtmlImage || elt instanceof BaseFrameElement) {
733                 matchingElements.add(elt);
734             }
735         }
736         return matchingElements;
737     }
738 
739     /**
740      * {@inheritDoc}
741      */
742     @Override
743     public HTMLElement getHead() {
744         final HtmlElement head = getPage().getHead();
745         if (head == null) {
746             return null;
747         }
748         return head.getScriptableObject();
749     }
750 
751     /**
752      * {@inheritDoc}
753      */
754     @Override
755     public String getTitle() {
756         return getPage().getDocumentTitle();
757     }
758 
759     /**
760      * {@inheritDoc}
761      */
762     @Override
763     public void setTitle(final String title) {
764         getPage().setTitleText(title);
765     }
766 
767     /**
768      * {@inheritDoc}
769      */
770     @Override
771     public HTMLElement getActiveElement() {
772         final HtmlElement activeElement = getPage().getActiveElement();
773         if (activeElement != null) {
774             return activeElement.getScriptableObject();
775         }
776         return null;
777     }
778 
779     /**
780      * {@inheritDoc}
781      */
782     @Override
783     public boolean hasFocus() {
784         return getPage().getFocusedElement() != null;
785     }
786 
787     /**
788      * Dispatches an event into the event system (standards-conformant browsers only). See
789      * <a href="https://developer.mozilla.org/en-US/docs/DOM/element.dispatchEvent">the Gecko
790      * DOM reference</a> for more information.
791      *
792      * @param event the event to be dispatched
793      * @return {@code false} if at least one of the event handlers which handled the event
794      *         called <code>preventDefault</code>; {@code true} otherwise
795      */
796     @Override
797     @JsxFunction
798     public boolean dispatchEvent(final Event event) {
799         event.setTarget(this);
800         final ScriptResult result = fireEvent(event);
801         return !event.isAborted(result);
802     }
803 
804     /**
805      * {@inheritDoc}
806      */
807     @Override
808     public Selection getSelection() {
809         return getWindow().getSelectionImpl();
810     }
811 
812     /**
813      * Creates a new HTML attribute with the specified name.
814      *
815      * @param attributeName the name of the attribute to create
816      * @return an attribute with the specified name
817      */
818     @Override
819     public Attr createAttribute(final String attributeName) {
820         String name = attributeName;
821         if (!org.htmlunit.util.StringUtils.isEmptyOrNull(name)) {
822             name = org.htmlunit.util.StringUtils.toRootLowerCase(name);
823         }
824 
825         return super.createAttribute(name);
826     }
827 
828     /**
829      * {@inheritDoc}
830      */
831     @Override
832     public String getBaseURI() {
833         return getPage().getBaseURL().toString();
834     }
835 
836     /**
837      * {@inheritDoc}
838      */
839     @Override
840     public HtmlUnitScriptable elementFromPoint(final int x, final int y) {
841         final HtmlElement element = getPage().getElementFromPoint(x, y);
842         return element == null ? null : element.getScriptableObject();
843     }
844 }