1
2
3
4
5
6
7
8
9
10
11
12
13
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
101 private final StringBuilder writeBuilder_ = new StringBuilder();
102 private boolean writeInCurrentDocument_ = true;
103
104 private boolean closePostponedAction_;
105 private boolean executionExternalPostponed_;
106
107
108
109
110 @Override
111 @JsxConstructor
112 public void jsConstructor() {
113 super.jsConstructor();
114 }
115
116
117
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
131
132
133 @Override
134 public HtmlPage getPage() {
135 return (HtmlPage) getDomNodeOrDie();
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
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
159
160
161
162
163
164
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
175
176
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
188
189
190
191
192
193
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
203
204
205
206
207
208
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
219
220
221
222 private static HTMLDocument getDocument(final Scriptable thisObj) {
223
224
225
226
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
239
240
241
242
243 public void setExecutingDynamicExternalPosponed(final boolean executing) {
244 executionExternalPostponed_ = executing;
245 }
246
247
248
249
250
251
252
253
254
255
256 protected void write(final String content) {
257
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
275 writeBuilder_.append(content);
276
277
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
318
319
320
321
322 static boolean canAlreadyBeParsed(final String content) {
323
324
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
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
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
430
431
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458 @JsxFunction
459 public HTMLDocument open(final Object url, final Object name, final Object features,
460 final Object replace) {
461
462
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
470 if (!writeInCurrentDocument_) {
471 LOG.warn("Function open() called when document is already open.");
472 }
473 writeInCurrentDocument_ = false;
474
475
476
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
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
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
527
528 @JsxGetter
529 @Override
530 public Element getDocumentElement() {
531 implicitCloseIfNecessary();
532 return super.getDocumentElement();
533 }
534
535
536
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
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
566
567
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
593
594
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
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
623
624 @Override
625 public HTMLCollection getElementsByClassName(final String className) {
626 return getDocumentElement().getElementsByClassName(className);
627 }
628
629
630
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
662
663
664
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
685
686
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
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
753
754 @Override
755 public String getTitle() {
756 return getPage().getDocumentTitle();
757 }
758
759
760
761
762 @Override
763 public void setTitle(final String title) {
764 getPage().setTitleText(title);
765 }
766
767
768
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
781
782 @Override
783 public boolean hasFocus() {
784 return getPage().getFocusedElement() != null;
785 }
786
787
788
789
790
791
792
793
794
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
806
807 @Override
808 public Selection getSelection() {
809 return getWindow().getSelectionImpl();
810 }
811
812
813
814
815
816
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
830
831 @Override
832 public String getBaseURI() {
833 return getPage().getBaseURL().toString();
834 }
835
836
837
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 }