1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host;
16
17 import static org.htmlunit.BrowserVersionFeatures.EVENT_SCROLL_UIEVENT;
18 import static org.htmlunit.BrowserVersionFeatures.JS_WINDOW_SELECTION_NULL_IF_INVISIBLE;
19 import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
20 import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
21 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
22 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
23
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.io.Serializable;
27 import java.lang.reflect.Method;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.util.ArrayList;
31 import java.util.EnumMap;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.function.Predicate;
36 import java.util.function.Supplier;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.htmlunit.AlertHandler;
41 import org.htmlunit.BrowserVersion;
42 import org.htmlunit.ConfirmHandler;
43 import org.htmlunit.ElementNotFoundException;
44 import org.htmlunit.Page;
45 import org.htmlunit.PrintHandler;
46 import org.htmlunit.PromptHandler;
47 import org.htmlunit.ScriptException;
48 import org.htmlunit.ScriptResult;
49 import org.htmlunit.SgmlPage;
50 import org.htmlunit.StatusHandler;
51 import org.htmlunit.StorageHolder.Type;
52 import org.htmlunit.TopLevelWindow;
53 import org.htmlunit.WebAssert;
54 import org.htmlunit.WebClient;
55 import org.htmlunit.WebConsole;
56 import org.htmlunit.WebWindow;
57 import org.htmlunit.WebWindowNotFoundException;
58 import org.htmlunit.corejs.javascript.AccessorSlot;
59 import org.htmlunit.corejs.javascript.Context;
60 import org.htmlunit.corejs.javascript.EcmaError;
61 import org.htmlunit.corejs.javascript.Function;
62 import org.htmlunit.corejs.javascript.JavaScriptException;
63 import org.htmlunit.corejs.javascript.NativeConsole.Level;
64 import org.htmlunit.corejs.javascript.NativeObject;
65 import org.htmlunit.corejs.javascript.Scriptable;
66 import org.htmlunit.corejs.javascript.ScriptableObject;
67 import org.htmlunit.corejs.javascript.Slot;
68 import org.htmlunit.corejs.javascript.TopLevel;
69 import org.htmlunit.corejs.javascript.VarScope;
70 import org.htmlunit.css.ComputedCssStyleDeclaration;
71 import org.htmlunit.html.BaseFrameElement;
72 import org.htmlunit.html.DomElement;
73 import org.htmlunit.html.DomNode;
74 import org.htmlunit.html.FrameWindow;
75 import org.htmlunit.html.HtmlAnchor;
76 import org.htmlunit.html.HtmlAttributeChangeEvent;
77 import org.htmlunit.html.HtmlButton;
78 import org.htmlunit.html.HtmlElement;
79 import org.htmlunit.html.HtmlEmbed;
80 import org.htmlunit.html.HtmlForm;
81 import org.htmlunit.html.HtmlImage;
82 import org.htmlunit.html.HtmlInput;
83 import org.htmlunit.html.HtmlMap;
84 import org.htmlunit.html.HtmlObject;
85 import org.htmlunit.html.HtmlPage;
86 import org.htmlunit.html.HtmlSelect;
87 import org.htmlunit.html.HtmlTextArea;
88 import org.htmlunit.javascript.AbstractJavaScriptEngine;
89 import org.htmlunit.javascript.HtmlUnitContextFactory;
90 import org.htmlunit.javascript.HtmlUnitScriptable;
91 import org.htmlunit.javascript.JavaScriptEngine;
92 import org.htmlunit.javascript.PostponedAction;
93 import org.htmlunit.javascript.configuration.JsxClass;
94 import org.htmlunit.javascript.configuration.JsxConstant;
95 import org.htmlunit.javascript.configuration.JsxConstructor;
96 import org.htmlunit.javascript.configuration.JsxFunction;
97 import org.htmlunit.javascript.configuration.JsxGetter;
98 import org.htmlunit.javascript.configuration.JsxSetter;
99 import org.htmlunit.javascript.host.crypto.Crypto;
100 import org.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration;
101 import org.htmlunit.javascript.host.css.MediaQueryList;
102 import org.htmlunit.javascript.host.css.StyleMedia;
103 import org.htmlunit.javascript.host.dom.AbstractList.EffectOnCache;
104 import org.htmlunit.javascript.host.dom.DOMException;
105 import org.htmlunit.javascript.host.dom.Document;
106 import org.htmlunit.javascript.host.dom.Node;
107 import org.htmlunit.javascript.host.dom.Selection;
108 import org.htmlunit.javascript.host.event.Event;
109 import org.htmlunit.javascript.host.event.EventTarget;
110 import org.htmlunit.javascript.host.event.MessageEvent;
111 import org.htmlunit.javascript.host.event.MouseEvent;
112 import org.htmlunit.javascript.host.event.UIEvent;
113 import org.htmlunit.javascript.host.html.DocumentProxy;
114 import org.htmlunit.javascript.host.html.HTMLCollection;
115 import org.htmlunit.javascript.host.html.HTMLDocument;
116 import org.htmlunit.javascript.host.html.HTMLElement;
117 import org.htmlunit.javascript.host.performance.Performance;
118 import org.htmlunit.javascript.host.speech.SpeechSynthesis;
119 import org.htmlunit.javascript.host.xml.XMLDocument;
120 import org.htmlunit.util.StringUtils;
121 import org.htmlunit.util.UrlUtils;
122 import org.htmlunit.xml.XmlPage;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 @JsxClass
149 @SuppressWarnings("PMD.TooManyFields")
150 public class Window extends EventTarget implements WindowOrWorkerGlobalScope, AutoCloseable {
151
152 private static final Log LOG = LogFactory.getLog(Window.class);
153
154
155 @JsxConstant({CHROME, EDGE})
156 public static final int TEMPORARY = 0;
157
158
159 @JsxConstant({CHROME, EDGE})
160 public static final int PERSISTENT = 1;
161
162 private static final Method GETTER_LENGTH;
163 private static final Method SETTER_LENGTH;
164 private static final Method GETTER_SELF;
165 private static final Method SETTER_SELF;
166 private static final Method GETTER_PARENT;
167 private static final Method SETTER_PARENT;
168 private static final Method GETTER_FRAMES;
169 private static final Method SETTER_FRAMES;
170
171 static {
172 try {
173 GETTER_LENGTH = Window.class.getDeclaredMethod("jsGetLength");
174 SETTER_LENGTH = Window.class.getDeclaredMethod("jsSetLength", Scriptable.class);
175
176 GETTER_SELF = Window.class.getDeclaredMethod("jsGetSelf");
177 SETTER_SELF = Window.class.getDeclaredMethod("jsSetSelf", Scriptable.class);
178
179 GETTER_PARENT = Window.class.getDeclaredMethod("jsGetParent");
180 SETTER_PARENT = Window.class.getDeclaredMethod("jsSetParent", Scriptable.class);
181
182 GETTER_FRAMES = Window.class.getDeclaredMethod("jsGetFrames");
183 SETTER_FRAMES = Window.class.getDeclaredMethod("jsSetFrames", Scriptable.class);
184 }
185 catch (NoSuchMethodException | SecurityException e) {
186 throw new RuntimeException(e);
187 }
188 }
189
190 private Scriptable lengthShadow_;
191 private Scriptable selfShadow_;
192 private Scriptable parentShadow_;
193 private Scriptable framesShadow_;
194
195 private Document document_;
196 private DocumentProxy documentProxy_;
197 private Navigator navigator_;
198 private Object clientInformation_;
199 private WebWindow webWindow_;
200 private WindowProxy windowProxy_;
201 private Screen screen_;
202 private History history_;
203 private Location location_;
204 private Selection selection_;
205 private Event currentEvent_;
206 private String status_ = "";
207 private Map<Class<? extends Scriptable>, Scriptable> prototypes_ = new HashMap<>();
208 private Object controllers_;
209 private Object opener_;
210 private Crypto crypto_;
211 private Scriptable performance_;
212
213 private final EnumMap<Type, Storage> storages_ = new EnumMap<>(Type.class);
214
215 private transient List<AnimationFrame> animationFrames_ = new ArrayList<>();
216
217 private record AnimationFrame(long id_, Function callback_) {
218 }
219
220
221
222
223
224
225
226
227
228
229
230 @JsxConstructor
231 public static Scriptable jsConstructor(final Context cx, final VarScope scope,
232 final Object[] args, final Function ctorObj, final boolean inNewExpr) {
233 throw JavaScriptEngine.typeError("Illegal constructor");
234 }
235
236
237
238
239
240
241
242 private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
243 stream.defaultReadObject();
244 animationFrames_ = new ArrayList<>();
245 }
246
247
248
249
250
251
252 @Override
253 public Scriptable getPrototype(final Class<? extends HtmlUnitScriptable> jsClass) {
254 return prototypes_.get(jsClass);
255 }
256
257
258
259
260
261 public void setPrototypes(final Map<Class<? extends Scriptable>, Scriptable> map) {
262 prototypes_ = map;
263 }
264
265
266
267
268
269 @JsxFunction
270 public void alert(final Object message) {
271
272
273 final String stringMessage = JavaScriptEngine.toString(message);
274 final AlertHandler handler = getWebWindow().getWebClient().getAlertHandler();
275 if (handler == null) {
276 if (LOG.isWarnEnabled()) {
277 LOG.warn("window.alert(\"" + stringMessage + "\") no alert handler installed");
278 }
279 }
280 else {
281 handler.handleAlert(document_.getPage(), stringMessage);
282 }
283 }
284
285
286
287
288
289
290 @JsxFunction
291 @Override
292 public String btoa(final String stringToEncode) {
293 return WindowOrWorkerGlobalScopeMixin.btoa(stringToEncode, this);
294 }
295
296
297
298
299
300
301 @JsxFunction
302 @Override
303 public String atob(final String encodedData) {
304 return WindowOrWorkerGlobalScopeMixin.atob(encodedData, this);
305 }
306
307
308
309
310
311
312 @JsxFunction
313 public boolean confirm(final String message) {
314 final ConfirmHandler handler = getWebWindow().getWebClient().getConfirmHandler();
315 if (handler == null) {
316 if (LOG.isWarnEnabled()) {
317 LOG.warn("window.confirm(\""
318 + message + "\") no confirm handler installed, simulating the OK button");
319 }
320 return true;
321 }
322 return handler.handleConfirm(document_.getPage(), message);
323 }
324
325
326
327
328
329
330
331 @JsxFunction
332 public String prompt(final String message, final Object defaultValue) {
333 final PromptHandler handler = getWebWindow().getWebClient().getPromptHandler();
334 if (handler == null) {
335 if (LOG.isWarnEnabled()) {
336 LOG.warn("window.prompt(\"" + message + "\") no prompt handler installed");
337 }
338 return null;
339 }
340 final String defaultText = JavaScriptEngine.isUndefined(defaultValue)
341 ? null
342 : JavaScriptEngine.toString(defaultValue);
343 return handler.handlePrompt(document_.getPage(), message, defaultText);
344 }
345
346
347
348
349
350 @JsxGetter(propertyName = "document")
351 public DocumentProxy getDocument_js() {
352 return documentProxy_;
353 }
354
355
356
357
358
359 public Document getDocument() {
360 return document_;
361 }
362
363
364
365
366
367 @JsxGetter
368 public Object getEvent() {
369 if (currentEvent_ == null) {
370 return JavaScriptEngine.UNDEFINED;
371 }
372 return currentEvent_;
373 }
374
375
376
377
378
379 public Event getCurrentEvent() {
380 return currentEvent_;
381 }
382
383
384
385
386
387 public void setCurrentEvent(final Event event) {
388 currentEvent_ = event;
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402
403 @JsxFunction
404 public WindowProxy open(final Object url, final Object name, final Object features,
405 final Object replace) {
406 String urlString = null;
407 if (!JavaScriptEngine.isUndefined(url)) {
408 urlString = JavaScriptEngine.toString(url);
409 }
410 String windowName = "";
411 if (!JavaScriptEngine.isUndefined(name)) {
412 windowName = JavaScriptEngine.toString(name);
413 }
414 String featuresString = null;
415 if (!JavaScriptEngine.isUndefined(features)) {
416 featuresString = JavaScriptEngine.toString(features);
417 }
418 final WebClient webClient = getWebWindow().getWebClient();
419
420 if (webClient.getOptions().isPopupBlockerEnabled()) {
421 LOG.debug("Ignoring window.open() invocation because popups are blocked.");
422 return null;
423 }
424
425 boolean replaceCurrentEntryInBrowsingHistory = false;
426 if (!JavaScriptEngine.isUndefined(replace)) {
427 replaceCurrentEntryInBrowsingHistory = JavaScriptEngine.toBoolean(replace);
428 }
429 if ((featuresString != null || replaceCurrentEntryInBrowsingHistory) && LOG.isDebugEnabled()) {
430 LOG.debug(
431 "window.open: features and replaceCurrentEntryInBrowsingHistory "
432 + "not implemented: url=[" + urlString
433 + "] windowName=[" + windowName
434 + "] features=[" + featuresString
435 + "] replaceCurrentEntry=[" + replaceCurrentEntryInBrowsingHistory
436 + "]");
437 }
438
439
440 if (StringUtils.isEmptyOrNull(urlString) && !StringUtils.isEmptyString(windowName)) {
441 try {
442 final WebWindow webWindow = webClient.getWebWindowByName(windowName);
443 return getProxy(webWindow);
444 }
445 catch (final WebWindowNotFoundException ignored) {
446
447 }
448 }
449 final URL newUrl = makeUrlForOpenWindow(urlString);
450 final WebWindow newWebWindow = webClient.openWindow(newUrl, windowName, webWindow_);
451 return getProxy(newWebWindow);
452 }
453
454 private URL makeUrlForOpenWindow(final String urlString) {
455 if (urlString.isEmpty()) {
456 return UrlUtils.URL_ABOUT_BLANK;
457 }
458
459 try {
460 final Page page = getWebWindow().getEnclosedPage();
461 if (page != null && page.isHtmlPage()) {
462 return ((HtmlPage) page).getFullyQualifiedUrl(urlString);
463 }
464 return new URL(urlString);
465 }
466 catch (final MalformedURLException e) {
467 if (LOG.isWarnEnabled()) {
468 LOG.error("Unable to create URL for openWindow: relativeUrl=[" + urlString + "]", e);
469 }
470 return null;
471 }
472 }
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489 @JsxFunction
490 public static Object setTimeout(final Context context, final VarScope scope,
491 final Scriptable thisObj, final Object[] args, final Function function) {
492 return WindowOrWorkerGlobalScopeMixin.setTimeout(context, thisObj, args, function);
493 }
494
495
496
497
498
499
500
501
502
503
504
505
506
507 @JsxFunction
508 public static Object queueMicrotask(final Context context, final VarScope scope,
509 final Scriptable thisObj, final Object[] args, final Function function) {
510 return WindowOrWorkerGlobalScopeMixin.queueMicrotask(thisObj, args);
511 }
512
513
514
515
516
517
518
519
520
521
522
523
524
525 @JsxFunction
526 public static Object setInterval(final Context context, final VarScope scope,
527 final Scriptable thisObj, final Object[] args, final Function function) {
528 return WindowOrWorkerGlobalScopeMixin.setInterval(context, thisObj, args, function);
529 }
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554 @JsxFunction
555 public void clearTimeout(final int timeoutId) {
556 if (timeoutId > 0) {
557 if (LOG.isDebugEnabled()) {
558 LOG.debug("clearTimeout(" + timeoutId + ")");
559 }
560 getWebWindow().getJobManager().removeJob(timeoutId);
561 }
562 }
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587 @JsxFunction
588 public void clearInterval(final int intervalID) {
589 if (intervalID > 0) {
590 if (LOG.isDebugEnabled()) {
591 LOG.debug("clearInterval(" + intervalID + ")");
592 }
593 getWebWindow().getJobManager().removeJob(intervalID);
594 }
595 }
596
597
598
599
600
601 @JsxGetter
602 public Navigator getNavigator() {
603 return navigator_;
604 }
605
606
607
608
609
610 @JsxGetter
611 public Object getClientInformation() {
612 if (clientInformation_ != null) {
613 return clientInformation_;
614 }
615 return navigator_;
616 }
617
618
619
620
621
622
623 @JsxSetter
624 public void setClientInformation(final Object clientInformation) {
625 clientInformation_ = clientInformation;
626 }
627
628
629
630
631
632 @JsxGetter(propertyName = "window")
633 public Window getWindow_js() {
634 return this;
635 }
636
637
638
639
640
641 @JsxGetter
642 public Storage getLocalStorage() {
643 return getStorage(Type.LOCAL_STORAGE);
644 }
645
646
647
648
649
650 @JsxGetter
651 public Storage getSessionStorage() {
652 return getStorage(Type.SESSION_STORAGE);
653 }
654
655
656
657
658
659
660 public Storage getStorage(final Type storageType) {
661 return storages_.computeIfAbsent(storageType,
662 k -> {
663 final WebWindow webWindow = getWebWindow();
664 final Map<String, String> store = webWindow.getWebClient().getStorageHolder().
665 getStore(storageType, webWindow.getEnclosedPage());
666 return new Storage(this, store, storageType);
667 }
668 );
669 }
670
671
672
673
674
675 @JsxGetter
676 public Location getLocation() {
677 return location_;
678 }
679
680
681
682
683
684
685 @JsxSetter
686 public void setLocation(final String newLocation) throws IOException {
687 location_.setHref(newLocation);
688 }
689
690
691
692
693
694
695
696
697
698
699
700 @JsxFunction({FF, FF_ESR})
701 public void dump(final String message) {
702 final WebConsole console = getWebWindow().getWebClient().getWebConsole();
703 console.print(Context.getCurrentContext(), getParentScope(), Level.INFO, new String[] {message}, null);
704 }
705
706
707
708
709
710
711 public int animateAnimationsFrames() {
712 final List<AnimationFrame> animationFrames = new ArrayList<>(animationFrames_);
713 animationFrames_.clear();
714
715 final double now = System.nanoTime() / 1_000_000d;
716 final Object[] args = {now};
717
718 final WebWindow ww = getWindow().getWebWindow();
719 final JavaScriptEngine jsEngine = (JavaScriptEngine) ww.getWebClient().getJavaScriptEngine();
720
721 for (final AnimationFrame animationFrame : animationFrames) {
722 jsEngine.callFunction((HtmlPage) ww.getEnclosedPage(),
723 animationFrame.callback_, getParentScope(), this, args);
724 }
725 return animationFrames_.size();
726 }
727
728
729
730
731
732
733
734 @JsxFunction
735 public int requestAnimationFrame(final Object callback) {
736 if (callback instanceof Function function) {
737 final int id = animationFrames_.size();
738 final AnimationFrame animationFrame = new AnimationFrame(id, function);
739 animationFrames_.add(animationFrame);
740 return id;
741 }
742 return -1;
743 }
744
745
746
747
748
749
750 @JsxFunction
751 public void cancelAnimationFrame(final Object requestId) {
752 final int id = (int) JavaScriptEngine.toNumber(requestId);
753
754 animationFrames_.removeIf(animationFrame -> animationFrame.id_ == id);
755 }
756
757
758
759
760
761 @JsxGetter
762 public Screen getScreen() {
763 return screen_;
764 }
765
766
767
768
769
770 @JsxGetter
771 public History getHistory() {
772 return history_;
773 }
774
775
776
777
778
779 @JsxGetter
780 public External getExternal() {
781 final External external = new External();
782 external.setParentScope(getParentScope());
783 external.setPrototype(getPrototype(external.getClass()));
784 return external;
785 }
786
787
788
789
790
791
792
793 public void initialize(final TopLevel scope, final WebWindow webWindow, final Page pageToEnclose) {
794 webWindow_ = webWindow;
795 webWindow_.setScriptableObject(this);
796
797 defineProperty(scope, "length", null, GETTER_LENGTH, SETTER_LENGTH, ScriptableObject.READONLY);
798 defineProperty(scope, "self", null, GETTER_SELF, SETTER_SELF, ScriptableObject.READONLY);
799 defineProperty(scope, "parent", null, GETTER_PARENT, SETTER_PARENT, ScriptableObject.READONLY);
800 defineProperty(scope, "frames", null, GETTER_FRAMES, SETTER_FRAMES, ScriptableObject.READONLY);
801
802 windowProxy_ = new WindowProxy(webWindow_);
803
804 if (pageToEnclose instanceof XmlPage) {
805 document_ = new XMLDocument();
806 }
807 else {
808 document_ = new HTMLDocument();
809 }
810 document_.setParentScope(scope);
811 document_.setPrototype(getPrototype(document_.getClass()));
812 document_.setWindow(this);
813
814 if (pageToEnclose instanceof SgmlPage page) {
815 document_.setDomNode(page);
816
817 if (page.isHtmlPage()) {
818 final HtmlPage htmlPage = (HtmlPage) page;
819
820 htmlPage.addAutoCloseable(this);
821 }
822 }
823
824 documentProxy_ = new DocumentProxy(webWindow_);
825
826 navigator_ = new Navigator();
827 navigator_.setParentScope(scope);
828 navigator_.setPrototype(getPrototype(navigator_.getClass()));
829
830 screen_ = new Screen(getWebWindow().getScreen());
831 screen_.setParentScope(scope);
832 screen_.setPrototype(getPrototype(screen_.getClass()));
833
834 history_ = new History();
835 history_.setParentScope(scope);
836 history_.setPrototype(getPrototype(history_.getClass()));
837
838 location_ = new Location();
839 location_.setParentScope(scope);
840 location_.setPrototype(getPrototype(location_.getClass()));
841 location_.initialize(scope, this, pageToEnclose);
842
843
844 controllers_ = JavaScriptEngine.newObject(scope);
845
846 if (webWindow_ instanceof TopLevelWindow window) {
847 final WebWindow opener = window.getOpener();
848 if (opener != null) {
849 opener_ = opener.getScriptableObject();
850 }
851 }
852 }
853
854
855
856
857
858 public void initialize(final Page enclosedPage) {
859 if (enclosedPage != null && enclosedPage.isHtmlPage()) {
860 final HtmlPage htmlPage = (HtmlPage) enclosedPage;
861
862
863
864
865 setDomNode(htmlPage);
866 clearEventListenersContainer();
867
868 WebAssert.notNull("document_", document_);
869 document_.setDomNode(htmlPage);
870 }
871 }
872
873
874
875
876 public void initialize() {
877
878 }
879
880
881
882
883
884 @JsxGetter
885 public Object getTop() {
886 final WebWindow top = getWebWindow().getTopWindow();
887 return top.getScriptableObject();
888 }
889
890
891
892
893
894 @JsxGetter
895 public Object getOpener() {
896 Object opener = opener_;
897 if (opener instanceof Window window) {
898 opener = window.windowProxy_;
899 }
900 return opener;
901 }
902
903
904
905
906
907 @JsxSetter
908 public void setOpener(final Object newValue) {
909 opener_ = newValue;
910 }
911
912
913
914
915
916 @JsxGetter
917 public HtmlUnitScriptable getFrameElement() {
918 final WebWindow window = getWebWindow();
919 if (window instanceof FrameWindow frameWindow) {
920 return frameWindow.getFrameElement().getScriptableObject();
921 }
922 return null;
923 }
924
925
926
927
928
929 @JsxGetter
930 public Object getLength() {
931 return JavaScriptEngine.UNDEFINED;
932 }
933
934
935
936
937
938
939 public Object jsGetLength() {
940 if (lengthShadow_ != null) {
941 return lengthShadow_;
942 }
943
944 final HTMLCollection frames = getFrames();
945 if (frames != null) {
946 return frames.getLength();
947 }
948 return 0;
949 }
950
951
952
953
954
955
956 public void jsSetLength(final Scriptable lengthShadow) {
957 lengthShadow_ = lengthShadow;
958 }
959
960
961
962
963
964 @JsxGetter
965 public Object getSelf() {
966 return JavaScriptEngine.UNDEFINED;
967 }
968
969
970
971
972
973
974 public Object jsGetSelf() {
975 if (selfShadow_ != null) {
976 return selfShadow_;
977 }
978
979 return this;
980 }
981
982
983
984
985
986
987 public void jsSetSelf(final Scriptable selfShadow) {
988 selfShadow_ = selfShadow;
989 }
990
991
992
993
994
995 @JsxGetter
996 public Object getParent() {
997 return JavaScriptEngine.UNDEFINED;
998 }
999
1000
1001
1002
1003
1004
1005 public Object jsGetParent() {
1006 if (parentShadow_ != null) {
1007 return parentShadow_;
1008 }
1009
1010 final WebWindow parent = getWebWindow().getParentWindow();
1011 return parent.getScriptableObject();
1012 }
1013
1014
1015
1016
1017
1018
1019 public void jsSetParent(final Scriptable parentShadow) {
1020 parentShadow_ = parentShadow;
1021 }
1022
1023
1024
1025
1026
1027 @JsxGetter(propertyName = "frames")
1028 public Object getFrames_js() {
1029 return JavaScriptEngine.UNDEFINED;
1030 }
1031
1032
1033
1034
1035
1036
1037 public Object jsGetFrames() {
1038 if (framesShadow_ != null) {
1039 return framesShadow_;
1040 }
1041
1042 return this;
1043 }
1044
1045
1046
1047
1048
1049
1050 public void jsSetFrames(final Scriptable framesShadow) {
1051 framesShadow_ = framesShadow;
1052 }
1053
1054
1055
1056
1057
1058 private HTMLCollection getFrames() {
1059 final Page page = getWebWindow().getEnclosedPage();
1060 if (page instanceof HtmlPage htmlPage) {
1061 return new HTMLCollectionFrames(htmlPage);
1062 }
1063 return null;
1064 }
1065
1066
1067
1068
1069
1070 public WebWindow getWebWindow() {
1071 return webWindow_;
1072 }
1073
1074
1075
1076
1077 @JsxFunction
1078 public void focus() {
1079 final WebWindow window = getWebWindow();
1080 window.getWebClient().setCurrentWindow(window);
1081 }
1082
1083
1084
1085
1086 @JsxFunction
1087 public void blur() {
1088 LOG.debug("window.blur() not implemented");
1089 }
1090
1091
1092
1093
1094 @JsxFunction(functionName = "close")
1095 public void close_js() {
1096 final WebWindow webWindow = getWebWindow();
1097 if (webWindow instanceof TopLevelWindow window) {
1098 window.close();
1099 }
1100 else {
1101 webWindow.getWebClient().deregisterWebWindow(webWindow);
1102 }
1103 }
1104
1105
1106
1107
1108
1109 @JsxGetter
1110 public boolean isClosed() {
1111 final WebWindow webWindow = getWebWindow();
1112 return !webWindow.getWebClient().containsWebWindow(webWindow);
1113 }
1114
1115
1116
1117
1118
1119
1120 @JsxFunction
1121 public void moveTo(final int x, final int y) {
1122 LOG.debug("window.moveTo() not implemented");
1123 }
1124
1125
1126
1127
1128
1129
1130 @JsxFunction
1131 public void moveBy(final int x, final int y) {
1132 LOG.debug("window.moveBy() not implemented");
1133 }
1134
1135
1136
1137
1138
1139
1140 @JsxFunction
1141 public void resizeBy(final int width, final int height) {
1142 LOG.debug("window.resizeBy() not implemented");
1143 }
1144
1145
1146
1147
1148
1149
1150 @JsxFunction
1151 public void resizeTo(final int width, final int height) {
1152 LOG.debug("window.resizeTo() not implemented");
1153 }
1154
1155
1156
1157
1158
1159
1160 @JsxFunction
1161 public void scroll(final Scriptable x, final Scriptable y) {
1162 scrollTo(x, y);
1163 }
1164
1165
1166
1167
1168
1169
1170 @JsxFunction
1171 public void scrollBy(final Scriptable x, final Scriptable y) {
1172 final HTMLElement body = document_.getBody();
1173 if (body != null) {
1174 int xOff = 0;
1175 int yOff = 0;
1176 if (y != null) {
1177 xOff = JavaScriptEngine.toInt32(x);
1178 yOff = JavaScriptEngine.toInt32(y);
1179 }
1180 else {
1181 if (!(x instanceof NativeObject)) {
1182 throw JavaScriptEngine.typeError("eee");
1183 }
1184 if (x.has("left", x)) {
1185 xOff = JavaScriptEngine.toInt32(x.get("left", x));
1186 }
1187 if (x.has("top", x)) {
1188 yOff = JavaScriptEngine.toInt32(x.get("top", x));
1189 }
1190 }
1191
1192 body.setScrollLeft(body.getScrollLeft() + xOff);
1193 body.setScrollTop(body.getScrollTop() + yOff);
1194
1195 fireScrollEvent(body);
1196 }
1197
1198 fireScrollEvent(document_);
1199 }
1200
1201 private void fireScrollEvent(final Node node) {
1202 final Event event;
1203 if (getBrowserVersion().hasFeature(EVENT_SCROLL_UIEVENT)) {
1204 event = new UIEvent(node, Event.TYPE_SCROLL);
1205 }
1206 else {
1207 event = new Event(node, Event.TYPE_SCROLL);
1208 event.setCancelable(false);
1209 }
1210 node.fireEvent(event);
1211 }
1212
1213
1214
1215
1216
1217 @JsxFunction({FF, FF_ESR})
1218 public void scrollByLines(final int lines) {
1219 final HTMLElement body = document_.getBody();
1220 if (body != null) {
1221 body.setScrollTop(body.getScrollTop() + (19 * lines));
1222
1223 fireScrollEvent(body);
1224 }
1225
1226 fireScrollEvent(document_);
1227 }
1228
1229
1230
1231
1232
1233 @JsxFunction({FF, FF_ESR})
1234 public void scrollByPages(final int pages) {
1235 final HTMLElement body = document_.getBody();
1236 if (body != null) {
1237 body.setScrollTop(body.getScrollTop() + (getInnerHeight() * pages));
1238
1239 fireScrollEvent(body);
1240 }
1241
1242 fireScrollEvent(document_);
1243 }
1244
1245
1246
1247
1248
1249
1250 @JsxFunction
1251 public void scrollTo(final Scriptable x, final Scriptable y) {
1252 final HTMLElement body = document_.getBody();
1253 if (body != null) {
1254 int xOff;
1255 int yOff;
1256 if (y != null) {
1257 xOff = JavaScriptEngine.toInt32(x);
1258 yOff = JavaScriptEngine.toInt32(y);
1259 }
1260 else {
1261 if (!(x instanceof NativeObject)) {
1262 throw JavaScriptEngine.typeError("eee");
1263 }
1264
1265 xOff = body.getScrollLeft();
1266 yOff = body.getScrollTop();
1267
1268 if (x.has("left", x)) {
1269 xOff = JavaScriptEngine.toInt32(x.get("left", x));
1270 }
1271 if (x.has("top", x)) {
1272 yOff = JavaScriptEngine.toInt32(x.get("top", x));
1273 }
1274 }
1275 body.setScrollLeft(xOff);
1276 body.setScrollTop(yOff);
1277
1278 fireScrollEvent(body);
1279 }
1280
1281 fireScrollEvent(document_);
1282 }
1283
1284
1285
1286
1287
1288 @JsxGetter
1289 public Function getOnload() {
1290 return getEventHandler(Event.TYPE_LOAD);
1291 }
1292
1293
1294
1295
1296
1297 @JsxSetter
1298 public void setOnload(final Object onload) {
1299 setHandlerForJavaScript(Event.TYPE_LOAD, onload);
1300 }
1301
1302
1303
1304
1305
1306 @JsxSetter
1307 public void setOnblur(final Object onblur) {
1308 setHandlerForJavaScript(Event.TYPE_BLUR, onblur);
1309 }
1310
1311
1312
1313
1314
1315 @JsxGetter
1316 public Function getOnblur() {
1317 return getEventHandler(Event.TYPE_BLUR);
1318 }
1319
1320
1321
1322
1323
1324 @JsxGetter
1325 public Function getOnclick() {
1326 return getEventHandler(MouseEvent.TYPE_CLICK);
1327 }
1328
1329
1330
1331
1332
1333 @JsxSetter
1334 public void setOnclick(final Object onclick) {
1335 setHandlerForJavaScript(MouseEvent.TYPE_CLICK, onclick);
1336 }
1337
1338
1339
1340
1341
1342 @JsxGetter
1343 public Function getOndblclick() {
1344 return getEventHandler(MouseEvent.TYPE_DBL_CLICK);
1345 }
1346
1347
1348
1349
1350
1351 @JsxSetter
1352 public void setOndblclick(final Object ondblclick) {
1353 setHandlerForJavaScript(MouseEvent.TYPE_DBL_CLICK, ondblclick);
1354 }
1355
1356
1357
1358
1359
1360 @JsxGetter
1361 public Function getOnhashchange() {
1362 return getEventHandler(Event.TYPE_HASH_CHANGE);
1363 }
1364
1365
1366
1367
1368
1369 @JsxSetter
1370 public void setOnhashchange(final Object onhashchange) {
1371 setHandlerForJavaScript(Event.TYPE_HASH_CHANGE, onhashchange);
1372 }
1373
1374
1375
1376
1377
1378 @JsxGetter
1379 public String getName() {
1380 return getWebWindow().getName();
1381 }
1382
1383
1384
1385
1386
1387 @JsxSetter
1388 public void setName(final String name) {
1389 getWebWindow().setName(name);
1390 }
1391
1392
1393
1394
1395
1396 @JsxGetter
1397 public Function getOnbeforeunload() {
1398 return getEventHandler(Event.TYPE_BEFORE_UNLOAD);
1399 }
1400
1401
1402
1403
1404
1405 @JsxSetter
1406 public void setOnbeforeunload(final Object onbeforeunload) {
1407 setHandlerForJavaScript(Event.TYPE_BEFORE_UNLOAD, onbeforeunload);
1408 }
1409
1410
1411
1412
1413
1414 @JsxGetter
1415 public Function getOnerror() {
1416 return getEventHandler(Event.TYPE_ERROR);
1417 }
1418
1419
1420
1421
1422
1423 @JsxSetter
1424 public void setOnerror(final Object onerror) {
1425 setHandlerForJavaScript(Event.TYPE_ERROR, onerror);
1426 }
1427
1428
1429
1430
1431
1432 @JsxGetter
1433 public Function getOnmessage() {
1434 return getEventHandler(Event.TYPE_MESSAGE);
1435 }
1436
1437
1438
1439
1440
1441 @JsxSetter
1442 public void setOnmessage(final Object onmessage) {
1443 setHandlerForJavaScript(Event.TYPE_MESSAGE, onmessage);
1444 }
1445
1446
1447
1448
1449
1450 public void triggerOnError(final ScriptException e) {
1451 final Function f = getOnerror();
1452 if (f != null) {
1453 String msg = e.getMessage();
1454 final String url = e.getPage().getUrl().toExternalForm();
1455
1456 final int line = e.getFailingLineNumber();
1457 final int column = e.getFailingColumnNumber();
1458
1459 final TopLevel scope = getTopLevelScope(getParentScope());
1460
1461 Object jsError = e.getMessage();
1462 if (e.getCause() instanceof JavaScriptException) {
1463 msg = "uncaught exception: " + e.getCause().getMessage();
1464 jsError = ((JavaScriptException) e.getCause()).getValue();
1465 }
1466 else if (e.getCause() instanceof EcmaError ecmaError) {
1467 msg = "uncaught " + e.getCause().getMessage();
1468
1469 final Scriptable err = JavaScriptEngine.newObject(scope, "Error", JavaScriptEngine.EMPTY_ARGS);
1470 ScriptableObject.putProperty(err, "message", ecmaError.getMessage());
1471 ScriptableObject.putProperty(err, "fileName", ecmaError.sourceName());
1472 ScriptableObject.putProperty(err, "lineNumber", Integer.valueOf(ecmaError.lineNumber()));
1473 jsError = err;
1474 }
1475
1476 final Object[] args = {msg, url, Integer.valueOf(line), Integer.valueOf(column), jsError};
1477 f.call(Context.getCurrentContext(), scope, this, args);
1478 }
1479 }
1480
1481 private void setHandlerForJavaScript(final String eventName, final Object handler) {
1482 getEventListenersContainer().setEventHandler(eventName, handler);
1483 }
1484
1485
1486
1487
1488 @Override
1489 protected Object getWithPreemption(final String name) {
1490 final DomNode domNode = getDomNodeOrNull();
1491 if (domNode == null) {
1492 return NOT_FOUND;
1493 }
1494
1495
1496 final HtmlPage page = (HtmlPage) domNode.getPage();
1497 Object result = getFrameWindowByName(page, name);
1498
1499 if (result == NOT_FOUND) {
1500 result = getElementsByName(page, name);
1501
1502 if (result == NOT_FOUND) {
1503
1504 try {
1505 final HtmlElement htmlElement = page.getHtmlElementById(name);
1506 result = getScriptableFor(htmlElement);
1507 }
1508 catch (final ElementNotFoundException e) {
1509 result = NOT_FOUND;
1510 }
1511 }
1512 }
1513
1514 if (result instanceof Window window) {
1515 final WebWindow webWindow = window.getWebWindow();
1516 result = getProxy(webWindow);
1517 }
1518
1519 return result;
1520 }
1521
1522
1523
1524
1525 @Override
1526 public Object get(final int index, final Scriptable start) {
1527 if (index < 0 || getWebWindow() == null) {
1528 return JavaScriptEngine.UNDEFINED;
1529 }
1530
1531 final HTMLCollection frames = getFrames();
1532 if (frames == null || index >= frames.getLength()) {
1533 return JavaScriptEngine.UNDEFINED;
1534 }
1535 return frames.item(Integer.valueOf(index));
1536 }
1537
1538 private static Object getFrameWindowByName(final HtmlPage page, final String name) {
1539 try {
1540 return page.getFrameByName(name).getScriptableObject();
1541 }
1542 catch (final ElementNotFoundException e) {
1543 return NOT_FOUND;
1544 }
1545 }
1546
1547 private Object getElementsByName(final HtmlPage page, final String name) {
1548
1549
1550
1551
1552
1553
1554 final List<DomElement> elements = page.getElementsByName(name);
1555
1556 final Filter filter = new Filter(false);
1557
1558 elements.removeIf(domElement -> !filter.matches(domElement));
1559
1560 if (elements.isEmpty()) {
1561 return NOT_FOUND;
1562 }
1563
1564 if (elements.size() == 1) {
1565 return getScriptableFor(elements.get(0));
1566 }
1567
1568
1569 final String expElementName = "null".equals(name) ? "" : name;
1570
1571 final HTMLCollection coll = new HTMLCollection(page, true);
1572 coll.setElementsSupplier(
1573 (Supplier<List<DomNode>> & Serializable)
1574 () -> {
1575 final List<DomElement> expElements = page.getElementsByName(expElementName);
1576 final List<DomNode> result = new ArrayList<>(expElements.size());
1577
1578 for (final DomElement domElement : expElements) {
1579 if (filter.matches(domElement)) {
1580 result.add(domElement);
1581 }
1582 }
1583 return result;
1584 });
1585
1586 coll.setEffectOnCacheFunction(
1587 (java.util.function.Function<HtmlAttributeChangeEvent, EffectOnCache> & Serializable)
1588 event -> {
1589 if ("name".equals(event.getName())) {
1590 return EffectOnCache.RESET;
1591 }
1592 return EffectOnCache.NONE;
1593 });
1594
1595 return coll;
1596 }
1597
1598
1599
1600
1601
1602
1603 public static WindowProxy getProxy(final WebWindow w) {
1604 return ((Window) w.getScriptableObject()).windowProxy_;
1605 }
1606
1607
1608
1609
1610
1611 @JsxGetter
1612 public String getStatus() {
1613 return status_;
1614 }
1615
1616
1617
1618
1619
1620 @JsxSetter
1621 public void setStatus(final String message) {
1622 status_ = message;
1623
1624 final StatusHandler statusHandler = webWindow_.getWebClient().getStatusHandler();
1625 if (statusHandler != null) {
1626 statusHandler.statusMessageChanged(webWindow_.getEnclosedPage(), message);
1627 }
1628 }
1629
1630
1631
1632
1633
1634
1635 @JsxGetter
1636 public int getInnerWidth() {
1637 return getWebWindow().getInnerWidth();
1638 }
1639
1640
1641
1642
1643
1644 @JsxSetter
1645 public void setInnerWidth(final int width) {
1646 getWebWindow().setInnerWidth(width);
1647 }
1648
1649
1650
1651
1652
1653
1654 @JsxGetter
1655 public int getOuterWidth() {
1656 return getWebWindow().getOuterWidth();
1657 }
1658
1659
1660
1661
1662
1663 @JsxSetter
1664 public void setOuterWidth(final int width) {
1665 getWebWindow().setOuterWidth(width);
1666 }
1667
1668
1669
1670
1671
1672
1673 @JsxGetter
1674 public int getInnerHeight() {
1675 return getWebWindow().getInnerHeight();
1676 }
1677
1678
1679
1680
1681
1682 @JsxSetter
1683 public void setInnerHeight(final int height) {
1684 getWebWindow().setInnerHeight(height);
1685 }
1686
1687
1688
1689
1690
1691
1692 @JsxGetter
1693 public int getOuterHeight() {
1694 return getWebWindow().getOuterHeight();
1695 }
1696
1697
1698
1699
1700
1701 @JsxSetter
1702 public void setOuterHeight(final int height) {
1703 getWebWindow().setOuterHeight(height);
1704 }
1705
1706
1707
1708
1709
1710
1711 @JsxFunction
1712 public void print() {
1713 final PrintHandler handler = getWebWindow().getWebClient().getPrintHandler();
1714 if (handler == null) {
1715 LOG.info("No PrintHandler installed - window.print() ignored");
1716 return;
1717 }
1718
1719 final SgmlPage sgmlPage = getDocument().getPage();
1720 if (!(sgmlPage instanceof HtmlPage page)) {
1721 LOG.debug("Page is not an HtmlPage - window.print() ignored");
1722 return;
1723 }
1724
1725 Event event = new Event(this, Event.TYPE_BEFOREPRINT);
1726 fireEvent(event);
1727
1728 page.setPrinting(true);
1729 try {
1730 handler.handlePrint(page);
1731 }
1732 finally {
1733 page.setPrinting(false);
1734 }
1735 event = new Event(this, Event.TYPE_AFTERPRINT);
1736 fireEvent(event);
1737 }
1738
1739
1740
1741
1742
1743
1744 @JsxFunction
1745 public void captureEvents(final String type) {
1746
1747 }
1748
1749
1750
1751
1752
1753
1754 @JsxFunction
1755 public void releaseEvents(final String type) {
1756
1757 }
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769 @JsxFunction
1770 public ComputedCSSStyleDeclaration getComputedStyle(final Object element, final String pseudoElement) {
1771 if (!(element instanceof Element e)) {
1772 throw JavaScriptEngine.typeError("parameter 1 is not of type 'Element'");
1773 }
1774
1775 final ComputedCssStyleDeclaration style = getWebWindow().getComputedStyle(e.getDomNodeOrDie(), pseudoElement);
1776 return new ComputedCSSStyleDeclaration(e, style);
1777 }
1778
1779
1780
1781
1782
1783 @JsxFunction
1784 public Selection getSelection() {
1785 final WebWindow webWindow = getWebWindow();
1786
1787 if (webWindow instanceof FrameWindow frameWindow) {
1788 if (getBrowserVersion().hasFeature(JS_WINDOW_SELECTION_NULL_IF_INVISIBLE)
1789 && !frameWindow.getFrameElement().isDisplayed()) {
1790 return null;
1791 }
1792 }
1793 return getSelectionImpl();
1794 }
1795
1796
1797
1798
1799
1800 public Selection getSelectionImpl() {
1801 if (selection_ == null) {
1802 selection_ = new Selection();
1803 selection_.setParentScope(getParentScope());
1804 selection_.setPrototype(getPrototype(selection_.getClass()));
1805 }
1806 return selection_;
1807 }
1808
1809
1810
1811
1812
1813
1814
1815 @JsxGetter({FF, FF_ESR})
1816 public Object getControllers() {
1817 return controllers_;
1818 }
1819
1820
1821
1822
1823
1824 @JsxSetter({FF, FF_ESR})
1825 public void setControllers(final Object value) {
1826 controllers_ = value;
1827 }
1828
1829
1830
1831
1832
1833 @JsxGetter({FF, FF_ESR})
1834 public int getMozInnerScreenX() {
1835 return 12;
1836 }
1837
1838
1839
1840
1841
1842 @JsxGetter({FF, FF_ESR})
1843 public int getMozInnerScreenY() {
1844 return 90;
1845 }
1846
1847 private static final class Filter {
1848 private final boolean includeFormFields_;
1849
1850 Filter(final boolean includeFormFields) {
1851 includeFormFields_ = includeFormFields;
1852 }
1853
1854 boolean matches(final Object object) {
1855 if (object instanceof HtmlEmbed
1856 || object instanceof HtmlForm
1857 || object instanceof HtmlImage
1858 || object instanceof HtmlObject) {
1859 return true;
1860 }
1861
1862 return includeFormFields_
1863 && (object instanceof HtmlAnchor
1864 || object instanceof HtmlButton
1865 || object instanceof HtmlInput
1866 || object instanceof HtmlMap
1867 || object instanceof HtmlSelect
1868 || object instanceof HtmlTextArea);
1869 }
1870 }
1871
1872
1873
1874
1875
1876
1877 @JsxFunction
1878 public void stop() {
1879
1880 }
1881
1882
1883
1884
1885
1886 @JsxGetter
1887 public int getPageXOffset() {
1888 return 0;
1889 }
1890
1891
1892
1893
1894
1895 @JsxGetter
1896 public int getPageYOffset() {
1897 return 0;
1898 }
1899
1900
1901
1902
1903
1904 @JsxGetter
1905 public int getScrollX() {
1906 return 0;
1907 }
1908
1909
1910
1911
1912
1913 @JsxGetter
1914 public int getScrollY() {
1915 return 0;
1916 }
1917
1918
1919
1920
1921
1922
1923 @JsxGetter({FF, FF_ESR})
1924 public Netscape getNetscape() {
1925 return new Netscape(getParentScope());
1926 }
1927
1928
1929
1930
1931
1932 @Override
1933 public boolean isConst(final Context cx, final String name) {
1934 if ("undefined".equals(name) || "Infinity".equals(name) || "NaN".equals(name)) {
1935 return false;
1936 }
1937
1938 return super.isConst(cx, name);
1939 }
1940
1941
1942
1943
1944 @Override
1945 public boolean dispatchEvent(final Event event) {
1946 event.setTarget(this);
1947 final ScriptResult result = fireEvent(event);
1948 return !event.isAborted(result);
1949 }
1950
1951
1952
1953
1954
1955 @JsxGetter
1956 public Function getOnchange() {
1957 return getEventHandler(Event.TYPE_CHANGE);
1958 }
1959
1960
1961
1962
1963
1964 @JsxSetter
1965 public void setOnchange(final Object onchange) {
1966 setHandlerForJavaScript(Event.TYPE_CHANGE, onchange);
1967 }
1968
1969
1970
1971
1972
1973 @JsxGetter
1974 public Function getOnsubmit() {
1975 return getEventHandler(Event.TYPE_SUBMIT);
1976 }
1977
1978
1979
1980
1981
1982 @JsxSetter
1983 public void setOnsubmit(final Object onsubmit) {
1984 setHandlerForJavaScript(Event.TYPE_SUBMIT, onsubmit);
1985 }
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996 @JsxFunction
1997 public static void postMessage(final Context context, final VarScope scope,
1998 final Scriptable thisObj, final Object[] args, final Function funObj) {
1999
2000
2001 if (args.length < 1) {
2002 throw JavaScriptEngine.typeError("message not provided");
2003 }
2004 final Object message = args[0];
2005
2006 String targetOrigin = "*";
2007 Object transfer = JavaScriptEngine.UNDEFINED;
2008
2009 if (args.length > 1) {
2010 if (JavaScriptEngine.isArray(args[1])) {
2011 transfer = args[1];
2012 }
2013 else {
2014 targetOrigin = JavaScriptEngine.toString(args[1]);
2015 }
2016 }
2017
2018 if (args.length > 2) {
2019 if (JavaScriptEngine.isArray(args[2])) {
2020 transfer = args[2];
2021 }
2022 }
2023
2024 final TopLevel topLevel = ScriptableObject.getTopLevelScope(scope);
2025 final Window sender = (Window) topLevel.getGlobalThis();
2026 final Window receiver = (Window) thisObj;
2027 final URL receiverURL = receiver.getWebWindow().getEnclosedPage().getUrl();
2028
2029 final WebWindow webWindow = sender.getWebWindow();
2030 final Page page = webWindow.getEnclosedPage();
2031 final URL senderURL = page.getUrl();
2032
2033 if (!StringUtils.equalsChar('*', targetOrigin)) {
2034 final URL targetURL;
2035 if (StringUtils.equalsChar('/', targetOrigin)) {
2036 targetURL = senderURL;
2037 }
2038 else {
2039 try {
2040 targetURL = new URL(targetOrigin);
2041 }
2042 catch (final Exception e) {
2043 throw JavaScriptEngine.asJavaScriptException(
2044 sender,
2045 "Failed to execute 'postMessage' on 'Window': Invalid target origin '"
2046 + targetOrigin + "' was specified (reason: " + e.getMessage() + ".",
2047 DOMException.SYNTAX_ERR);
2048 }
2049 }
2050
2051 if (getPort(targetURL) != getPort(receiverURL)) {
2052 return;
2053 }
2054 if (!targetURL.getHost().equals(receiverURL.getHost())) {
2055 return;
2056 }
2057 if (!targetURL.getProtocol().equals(receiverURL.getProtocol())) {
2058 return;
2059 }
2060 }
2061
2062 final String origin;
2063 try {
2064 final URL originUrl = UrlUtils.getUrlWithoutPathRefQuery(senderURL);
2065 origin = UrlUtils.removeRedundantPort(originUrl).toExternalForm();
2066 }
2067 catch (final MalformedURLException e) {
2068 throw JavaScriptEngine.throwAsScriptRuntimeEx(e);
2069 }
2070
2071 final MessageEvent event = new MessageEvent();
2072 event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin, "", sender, transfer);
2073 event.setParentScope(scope);
2074 event.setPrototype(receiver.getPrototype(event.getClass()));
2075
2076 final AbstractJavaScriptEngine<?> jsEngine = webWindow.getWebClient().getJavaScriptEngine();
2077 final PostponedAction action = new PostponedAction(page, "Window.postMessage") {
2078 @Override
2079 public void execute() {
2080 final HtmlUnitContextFactory cf = jsEngine.getContextFactory();
2081 cf.call(cx -> receiver.dispatchEvent(event));
2082 }
2083 };
2084 jsEngine.addPostponedAction(action);
2085 }
2086
2087
2088
2089
2090
2091
2092 public static int getPort(final URL url) {
2093 int port = url.getPort();
2094 if (port == -1) {
2095 if ("http".equals(url.getProtocol())) {
2096 port = 80;
2097 }
2098 else {
2099 port = 443;
2100 }
2101 }
2102 return port;
2103 }
2104
2105
2106
2107
2108
2109
2110
2111 @JsxGetter
2112 public Scriptable getPerformance() {
2113 if (performance_ == null) {
2114 final Performance performance = new Performance();
2115 performance.setParentScope(getParentScope());
2116 performance.setPrototype(getPrototype(performance.getClass()));
2117 performance_ = performance;
2118 }
2119 return performance_;
2120 }
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131 @JsxSetter
2132 public void setPerformance(final Scriptable performance) {
2133 performance_ = performance;
2134 }
2135
2136
2137
2138
2139
2140 @JsxGetter
2141 public int getDevicePixelRatio() {
2142 return 1;
2143 }
2144
2145
2146
2147
2148
2149 @JsxGetter({CHROME, EDGE})
2150 public StyleMedia getStyleMedia() {
2151 final StyleMedia styleMedia = new StyleMedia();
2152 styleMedia.setParentScope(getParentScope());
2153 styleMedia.setPrototype(getPrototype(styleMedia.getClass()));
2154 return styleMedia;
2155 }
2156
2157
2158
2159
2160
2161
2162
2163 @JsxFunction
2164 public MediaQueryList matchMedia(final String mediaQueryString) {
2165 final MediaQueryList mediaQueryList = new MediaQueryList(mediaQueryString);
2166 mediaQueryList.setParentScope(getParentScope());
2167 mediaQueryList.setPrototype(getPrototype(mediaQueryList.getClass()));
2168 return mediaQueryList;
2169 }
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182 @JsxFunction
2183 public boolean find(final String search, final boolean caseSensitive,
2184 final boolean backwards, final boolean wrapAround,
2185 final boolean wholeWord, final boolean searchInFrames, final boolean showDialog) {
2186 return false;
2187 }
2188
2189
2190
2191
2192
2193 @JsxGetter({CHROME, EDGE})
2194 public SpeechSynthesis getSpeechSynthesis() {
2195 final SpeechSynthesis speechSynthesis = new SpeechSynthesis();
2196 speechSynthesis.setParentScope(getParentScope());
2197 speechSynthesis.setPrototype(getPrototype(speechSynthesis.getClass()));
2198 return speechSynthesis;
2199 }
2200
2201
2202
2203
2204
2205 @JsxGetter({CHROME, EDGE})
2206 public boolean isOffscreenBuffering() {
2207 return true;
2208 }
2209
2210
2211
2212
2213
2214 @JsxGetter
2215 public Crypto getCrypto() {
2216 if (crypto_ == null) {
2217 crypto_ = new Crypto(this);
2218 }
2219 return crypto_;
2220 }
2221
2222
2223
2224
2225 @Override
2226 public void close() {
2227
2228 }
2229
2230
2231
2232
2233
2234 @JsxGetter
2235 public Function getOnfocus() {
2236 return getEventHandler(Event.TYPE_FOCUS);
2237 }
2238
2239
2240
2241
2242
2243 @JsxSetter
2244 public void setOnfocus(final Object onfocus) {
2245 setHandlerForJavaScript(Event.TYPE_FOCUS, onfocus);
2246 }
2247
2248
2249
2250
2251
2252 @JsxGetter
2253 public Function getOndragend() {
2254 return getEventHandler(Event.TYPE_DRAGEND);
2255 }
2256
2257
2258
2259
2260
2261 @JsxSetter
2262 public void setOndragend(final Object ondragend) {
2263 setHandlerForJavaScript(Event.TYPE_DRAGEND, ondragend);
2264 }
2265
2266
2267
2268
2269
2270 @JsxGetter
2271 public Function getOninvalid() {
2272 return getEventHandler(Event.TYPE_INVALID);
2273 }
2274
2275
2276
2277
2278
2279 @JsxSetter
2280 public void setOninvalid(final Object oninvalid) {
2281 setHandlerForJavaScript(Event.TYPE_INVALID, oninvalid);
2282 }
2283
2284
2285
2286
2287
2288 @JsxGetter({CHROME, EDGE})
2289 public Function getOnpointerout() {
2290 return getEventHandler(Event.TYPE_POINTEROUT);
2291 }
2292
2293
2294
2295
2296
2297 @JsxSetter({CHROME, EDGE})
2298 public void setOnpointerout(final Object onpointerout) {
2299 setHandlerForJavaScript(Event.TYPE_POINTEROUT, onpointerout);
2300 }
2301
2302
2303
2304
2305
2306 @JsxGetter
2307 public Function getOnratechange() {
2308 return getEventHandler(Event.TYPE_RATECHANGE);
2309 }
2310
2311
2312
2313
2314
2315 @JsxSetter
2316 public void setOnratechange(final Object onratechange) {
2317 setHandlerForJavaScript(Event.TYPE_RATECHANGE, onratechange);
2318 }
2319
2320
2321
2322
2323
2324 @JsxGetter
2325 public Function getOnanimationiteration() {
2326 return getEventHandler(Event.TYPE_ANIMATIONITERATION);
2327 }
2328
2329
2330
2331
2332
2333 @JsxSetter
2334 public void setOnanimationiteration(final Object onanimationiteration) {
2335 setHandlerForJavaScript(Event.TYPE_ANIMATIONITERATION, onanimationiteration);
2336 }
2337
2338
2339
2340
2341
2342 @JsxGetter
2343 public Function getOncanplaythrough() {
2344 return getEventHandler(Event.TYPE_CANPLAYTHROUGH);
2345 }
2346
2347
2348
2349
2350
2351 @JsxSetter
2352 public void setOncanplaythrough(final Object oncanplaythrough) {
2353 setHandlerForJavaScript(Event.TYPE_CANPLAYTHROUGH, oncanplaythrough);
2354 }
2355
2356
2357
2358
2359
2360 @JsxGetter({CHROME, EDGE})
2361 public Function getOncancel() {
2362 return getEventHandler(Event.TYPE_CANCEL);
2363 }
2364
2365
2366
2367
2368
2369 @JsxSetter({CHROME, EDGE})
2370 public void setOncancel(final Object oncancel) {
2371 setHandlerForJavaScript(Event.TYPE_CANCEL, oncancel);
2372 }
2373
2374
2375
2376
2377
2378 @JsxGetter({CHROME, EDGE})
2379 public Function getOnpointerenter() {
2380 return getEventHandler(Event.TYPE_POINTERENTER);
2381 }
2382
2383
2384
2385
2386
2387 @JsxSetter({CHROME, EDGE})
2388 public void setOnpointerenter(final Object onpointerenter) {
2389 setHandlerForJavaScript(Event.TYPE_POINTERENTER, onpointerenter);
2390 }
2391
2392
2393
2394
2395
2396 @JsxGetter
2397 public Function getOnselect() {
2398 return getEventHandler(Event.TYPE_SELECT);
2399 }
2400
2401
2402
2403
2404
2405 @JsxSetter
2406 public void setOnselect(final Object onselect) {
2407 setHandlerForJavaScript(Event.TYPE_SELECT, onselect);
2408 }
2409
2410
2411
2412
2413
2414 @JsxGetter({CHROME, EDGE})
2415 public Function getOnauxclick() {
2416 return getEventHandler(Event.TYPE_AUXCLICK);
2417 }
2418
2419
2420
2421
2422
2423 @JsxSetter({CHROME, EDGE})
2424 public void setOnauxclick(final Object onauxclick) {
2425 setHandlerForJavaScript(Event.TYPE_AUXCLICK, onauxclick);
2426 }
2427
2428
2429
2430
2431
2432 @JsxGetter
2433 public Function getOnscroll() {
2434 return getEventHandler(Event.TYPE_SCROLL);
2435 }
2436
2437
2438
2439
2440
2441 @JsxSetter
2442 public void setOnscroll(final Object onscroll) {
2443 setHandlerForJavaScript(Event.TYPE_SCROLL, onscroll);
2444 }
2445
2446
2447
2448
2449
2450 @JsxGetter
2451 public Function getOnkeydown() {
2452 return getEventHandler(Event.TYPE_KEY_DOWN);
2453 }
2454
2455
2456
2457
2458
2459 @JsxSetter
2460 public void setOnkeydown(final Object onkeydown) {
2461 setHandlerForJavaScript(Event.TYPE_KEY_DOWN, onkeydown);
2462 }
2463
2464
2465
2466
2467
2468 @JsxGetter({CHROME, EDGE})
2469 public Function getOnwebkitanimationstart() {
2470 return getEventHandler(Event.TYPE_WEBANIMATIONSTART);
2471 }
2472
2473
2474
2475
2476
2477 @JsxSetter({CHROME, EDGE})
2478 public void setOnwebkitanimationstart(final Object onwebkitanimationstart) {
2479 setHandlerForJavaScript(Event.TYPE_WEBANIMATIONSTART, onwebkitanimationstart);
2480 }
2481
2482
2483
2484
2485
2486 @JsxGetter
2487 public Function getOnkeyup() {
2488 return getEventHandler(Event.TYPE_KEY_UP);
2489 }
2490
2491
2492
2493
2494
2495 @JsxSetter
2496 public void setOnkeyup(final Object onkeyup) {
2497 setHandlerForJavaScript(Event.TYPE_KEY_UP, onkeyup);
2498 }
2499
2500
2501
2502
2503
2504 @JsxGetter
2505 public Function getOnreset() {
2506 return getEventHandler(Event.TYPE_RESET);
2507 }
2508
2509
2510
2511
2512
2513 @JsxSetter
2514 public void setOnreset(final Object onreset) {
2515 setHandlerForJavaScript(Event.TYPE_RESET, onreset);
2516 }
2517
2518
2519
2520
2521
2522 @JsxGetter
2523 public Function getOnkeypress() {
2524 return getEventHandler(Event.TYPE_KEY_PRESS);
2525 }
2526
2527
2528
2529
2530
2531 @JsxSetter
2532 public void setOnkeypress(final Object onkeypress) {
2533 setHandlerForJavaScript(Event.TYPE_KEY_PRESS, onkeypress);
2534 }
2535
2536
2537
2538
2539
2540 @JsxGetter
2541 public Function getOndrag() {
2542 return getEventHandler(Event.TYPE_DRAG);
2543 }
2544
2545
2546
2547
2548
2549 @JsxSetter
2550 public void setOndrag(final Object ondrag) {
2551 setHandlerForJavaScript(Event.TYPE_DRAG, ondrag);
2552 }
2553
2554
2555
2556
2557
2558 @JsxGetter
2559 public Function getOnseeked() {
2560 return getEventHandler(Event.TYPE_SEEKED);
2561 }
2562
2563
2564
2565
2566
2567 @JsxSetter
2568 public void setOnseeked(final Object onseeked) {
2569 setHandlerForJavaScript(Event.TYPE_SEEKED, onseeked);
2570 }
2571
2572
2573
2574
2575
2576 @JsxGetter
2577 public Function getOnoffline() {
2578 return getEventHandler(Event.TYPE_OFFLINE);
2579 }
2580
2581
2582
2583
2584
2585 @JsxSetter
2586 public void setOnoffline(final Object onoffline) {
2587 setHandlerForJavaScript(Event.TYPE_OFFLINE, onoffline);
2588 }
2589
2590
2591
2592
2593
2594 @JsxGetter
2595 public Function getOndeviceorientation() {
2596 return getEventHandler(Event.TYPE_DEVICEORIENTATION);
2597 }
2598
2599
2600
2601
2602
2603 @JsxSetter
2604 public void setOndeviceorientation(final Object ondeviceorientation) {
2605 setHandlerForJavaScript(Event.TYPE_DEVICEORIENTATION, ondeviceorientation);
2606 }
2607
2608
2609
2610
2611
2612 @JsxGetter({CHROME, EDGE})
2613 public Function getOntoggle() {
2614 return getEventHandler(Event.TYPE_TOGGLE);
2615 }
2616
2617
2618
2619
2620
2621 @JsxSetter({CHROME, EDGE})
2622 public void setOntoggle(final Object ontoggle) {
2623 setHandlerForJavaScript(Event.TYPE_TOGGLE, ontoggle);
2624 }
2625
2626
2627
2628
2629
2630 @JsxGetter
2631 public Function getOnplay() {
2632 return getEventHandler(Event.TYPE_PLAY);
2633 }
2634
2635
2636
2637
2638
2639 @JsxSetter
2640 public void setOnplay(final Object onplay) {
2641 setHandlerForJavaScript(Event.TYPE_PLAY, onplay);
2642 }
2643
2644
2645
2646
2647
2648 @JsxGetter
2649 public Function getOncontextmenu() {
2650 return getEventHandler(MouseEvent.TYPE_CONTEXT_MENU);
2651 }
2652
2653
2654
2655
2656
2657 @JsxSetter
2658 public void setOncontextmenu(final Object oncontextmenu) {
2659 setHandlerForJavaScript(MouseEvent.TYPE_CONTEXT_MENU, oncontextmenu);
2660 }
2661
2662
2663
2664
2665
2666 @JsxGetter
2667 public Function getOnmousemove() {
2668 return getEventHandler(MouseEvent.TYPE_MOUSE_MOVE);
2669 }
2670
2671
2672
2673
2674
2675 @JsxSetter
2676 public void setOnmousemove(final Object onmousemove) {
2677 setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_MOVE, onmousemove);
2678 }
2679
2680
2681
2682
2683
2684 @JsxGetter({CHROME, EDGE})
2685 public Function getOnpointermove() {
2686 return getEventHandler(Event.TYPE_POINTERMOVE);
2687 }
2688
2689
2690
2691
2692
2693 @JsxSetter({CHROME, EDGE})
2694 public void setOnpointermove(final Object onpointermove) {
2695 setHandlerForJavaScript(Event.TYPE_POINTERMOVE, onpointermove);
2696 }
2697
2698
2699
2700
2701
2702 @JsxGetter
2703 public Function getOnmouseover() {
2704 return getEventHandler(MouseEvent.TYPE_MOUSE_OVER);
2705 }
2706
2707
2708
2709
2710
2711 @JsxSetter
2712 public void setOnmouseover(final Object onmouseover) {
2713 setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_OVER, onmouseover);
2714 }
2715
2716
2717
2718
2719
2720 @JsxGetter({CHROME, EDGE})
2721 public Function getOnlostpointercapture() {
2722 return getEventHandler(Event.TYPE_LOSTPOINTERCAPTURE);
2723 }
2724
2725
2726
2727
2728
2729 @JsxSetter({CHROME, EDGE})
2730 public void setOnlostpointercapture(final Object onlostpointercapture) {
2731 setHandlerForJavaScript(Event.TYPE_LOSTPOINTERCAPTURE, onlostpointercapture);
2732 }
2733
2734
2735
2736
2737
2738 @JsxGetter({CHROME, EDGE})
2739 public Function getOnpointerover() {
2740 return getEventHandler(Event.TYPE_POINTEROVER);
2741 }
2742
2743
2744
2745
2746
2747 @JsxSetter({CHROME, EDGE})
2748 public void setOnpointerover(final Object onpointerover) {
2749 setHandlerForJavaScript(Event.TYPE_POINTEROVER, onpointerover);
2750 }
2751
2752
2753
2754
2755
2756 @JsxGetter({CHROME, EDGE})
2757 public Function getOnclose() {
2758 return getEventHandler(Event.TYPE_CLOSE);
2759 }
2760
2761
2762
2763
2764
2765 @JsxSetter({CHROME, EDGE})
2766 public void setOnclose(final Object onclose) {
2767 setHandlerForJavaScript(Event.TYPE_CLOSE, onclose);
2768 }
2769
2770
2771
2772
2773
2774 @JsxGetter
2775 public Function getOnanimationend() {
2776 return getEventHandler(Event.TYPE_ANIMATIONEND);
2777 }
2778
2779
2780
2781
2782
2783 @JsxSetter
2784 public void setOnanimationend(final Object onanimationend) {
2785 setHandlerForJavaScript(Event.TYPE_ANIMATIONEND, onanimationend);
2786 }
2787
2788
2789
2790
2791
2792 @JsxGetter
2793 public Function getOndragenter() {
2794 return getEventHandler(Event.TYPE_DRAGENTER);
2795 }
2796
2797
2798
2799
2800
2801 @JsxSetter
2802 public void setOndragenter(final Object ondragenter) {
2803 setHandlerForJavaScript(Event.TYPE_DRAGENTER, ondragenter);
2804 }
2805
2806
2807
2808
2809
2810 @JsxGetter({FF, FF_ESR})
2811 public Function getOnafterprint() {
2812 return getEventHandler(Event.TYPE_AFTERPRINT);
2813 }
2814
2815
2816
2817
2818
2819 @JsxSetter({FF, FF_ESR})
2820 public void setOnafterprint(final Object onafterprint) {
2821 setHandlerForJavaScript(Event.TYPE_AFTERPRINT, onafterprint);
2822 }
2823
2824
2825
2826
2827
2828 @JsxGetter({FF, FF_ESR})
2829 public Function getOnmozfullscreenerror() {
2830 return getEventHandler(Event.TYPE_MOZFULLSCREENERROR);
2831 }
2832
2833
2834
2835
2836
2837 @JsxSetter({FF, FF_ESR})
2838 public void setOnmozfullscreenerror(final Object onmozfullscreenerror) {
2839 setHandlerForJavaScript(Event.TYPE_MOZFULLSCREENERROR, onmozfullscreenerror);
2840 }
2841
2842
2843
2844
2845
2846 @JsxGetter
2847 public Function getOnmouseleave() {
2848 return getEventHandler(Event.TYPE_MOUSELEAVE);
2849 }
2850
2851
2852
2853
2854
2855 @JsxSetter
2856 public void setOnmouseleave(final Object onmouseleave) {
2857 setHandlerForJavaScript(Event.TYPE_MOUSELEAVE, onmouseleave);
2858 }
2859
2860
2861
2862
2863
2864 @JsxGetter({CHROME, EDGE})
2865 public Function getOnmousewheel() {
2866 return getEventHandler(Event.TYPE_MOUSEWHEEL);
2867 }
2868
2869
2870
2871
2872
2873 @JsxSetter({CHROME, EDGE})
2874 public void setOnmousewheel(final Object onmousewheel) {
2875 setHandlerForJavaScript(Event.TYPE_MOUSEWHEEL, onmousewheel);
2876 }
2877
2878
2879
2880
2881
2882 @JsxGetter
2883 public Function getOnseeking() {
2884 return getEventHandler(Event.TYPE_SEEKING);
2885 }
2886
2887
2888
2889
2890
2891 @JsxSetter
2892 public void setOnseeking(final Object onseeking) {
2893 setHandlerForJavaScript(Event.TYPE_SEEKING, onseeking);
2894 }
2895
2896
2897
2898
2899
2900 @JsxGetter({CHROME, EDGE})
2901 public Function getOncuechange() {
2902 return getEventHandler(Event.TYPE_CUECHANGE);
2903 }
2904
2905
2906
2907
2908
2909 @JsxSetter({CHROME, EDGE})
2910 public void setOncuechange(final Object oncuechange) {
2911 setHandlerForJavaScript(Event.TYPE_CUECHANGE, oncuechange);
2912 }
2913
2914
2915
2916
2917
2918 @JsxGetter
2919 public Function getOnpageshow() {
2920 return getEventHandler(Event.TYPE_PAGESHOW);
2921 }
2922
2923
2924
2925
2926
2927 @JsxSetter
2928 public void setOnpageshow(final Object onpageshow) {
2929 setHandlerForJavaScript(Event.TYPE_PAGESHOW, onpageshow);
2930 }
2931
2932
2933
2934
2935
2936 @JsxGetter({FF, FF_ESR})
2937 public Function getOnmozfullscreenchange() {
2938 return getEventHandler(Event.TYPE_MOZFULLSCREENCHANGE);
2939 }
2940
2941
2942
2943
2944
2945 @JsxSetter({FF, FF_ESR})
2946 public void setOnmozfullscreenchange(final Object onmozfullscreenchange) {
2947 setHandlerForJavaScript(Event.TYPE_MOZFULLSCREENCHANGE, onmozfullscreenchange);
2948 }
2949
2950
2951
2952
2953
2954 @JsxGetter
2955 public Function getOndurationchange() {
2956 return getEventHandler(Event.TYPE_DURATIONCHANGE);
2957 }
2958
2959
2960
2961
2962
2963 @JsxSetter
2964 public void setOndurationchange(final Object ondurationchange) {
2965 setHandlerForJavaScript(Event.TYPE_DURATIONCHANGE, ondurationchange);
2966 }
2967
2968
2969
2970
2971
2972 @JsxGetter
2973 public Function getOnplaying() {
2974 return getEventHandler(Event.TYPE_PLAYING);
2975 }
2976
2977
2978
2979
2980
2981 @JsxSetter
2982 public void setOnplaying(final Object onplaying) {
2983 setHandlerForJavaScript(Event.TYPE_PLAYING, onplaying);
2984 }
2985
2986
2987
2988
2989
2990 @JsxGetter
2991 public Function getOnended() {
2992 return getEventHandler(Event.TYPE_ENDED);
2993 }
2994
2995
2996
2997
2998
2999 @JsxSetter
3000 public void setOnended(final Object onended) {
3001 setHandlerForJavaScript(Event.TYPE_ENDED, onended);
3002 }
3003
3004
3005
3006
3007
3008 @JsxGetter
3009 public Function getOnloadeddata() {
3010 return getEventHandler(Event.TYPE_LOADEDDATA);
3011 }
3012
3013
3014
3015
3016
3017 @JsxSetter
3018 public void setOnloadeddata(final Object onloadeddata) {
3019 setHandlerForJavaScript(Event.TYPE_LOADEDDATA, onloadeddata);
3020 }
3021
3022
3023
3024
3025
3026 @JsxGetter({CHROME, EDGE})
3027 public Function getOnunhandledrejection() {
3028 return getEventHandler(Event.TYPE_UNHANDLEDREJECTION);
3029 }
3030
3031
3032
3033
3034
3035 @JsxSetter({CHROME, EDGE})
3036 public void setOnunhandledrejection(final Object onunhandledrejection) {
3037 setHandlerForJavaScript(Event.TYPE_UNHANDLEDREJECTION, onunhandledrejection);
3038 }
3039
3040
3041
3042
3043
3044 @JsxGetter
3045 public Function getOnmouseout() {
3046 return getEventHandler(MouseEvent.TYPE_MOUSE_OUT);
3047 }
3048
3049
3050
3051
3052
3053 @JsxSetter
3054 public void setOnmouseout(final Object onmouseout) {
3055 setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_OUT, onmouseout);
3056 }
3057
3058
3059
3060
3061
3062 @JsxGetter
3063 public Function getOnsuspend() {
3064 return getEventHandler(Event.TYPE_SUSPEND);
3065 }
3066
3067
3068
3069
3070
3071 @JsxSetter
3072 public void setOnsuspend(final Object onsuspend) {
3073 setHandlerForJavaScript(Event.TYPE_SUSPEND, onsuspend);
3074 }
3075
3076
3077
3078
3079
3080 @JsxGetter
3081 public Function getOnwaiting() {
3082 return getEventHandler(Event.TYPE_WAITING);
3083 }
3084
3085
3086
3087
3088
3089 @JsxSetter
3090 public void setOnwaiting(final Object onwaiting) {
3091 setHandlerForJavaScript(Event.TYPE_WAITING, onwaiting);
3092 }
3093
3094
3095
3096
3097
3098 @JsxGetter
3099 public Function getOncanplay() {
3100 return getEventHandler(Event.TYPE_CANPLAY);
3101 }
3102
3103
3104
3105
3106
3107 @JsxSetter
3108 public void setOncanplay(final Object oncanplay) {
3109 setHandlerForJavaScript(Event.TYPE_CANPLAY, oncanplay);
3110 }
3111
3112
3113
3114
3115
3116 @JsxGetter
3117 public Function getOnmousedown() {
3118 return getEventHandler(MouseEvent.TYPE_MOUSE_DOWN);
3119 }
3120
3121
3122
3123
3124
3125 @JsxSetter
3126 public void setOnmousedown(final Object onmousedown) {
3127 setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_DOWN, onmousedown);
3128 }
3129
3130
3131
3132
3133
3134 @JsxGetter
3135 public Function getOnlanguagechange() {
3136 return getEventHandler(Event.TYPE_LANGUAGECHANGE);
3137 }
3138
3139
3140
3141
3142
3143 @JsxSetter
3144 public void setOnlanguagechange(final Object onlanguagechange) {
3145 setHandlerForJavaScript(Event.TYPE_LANGUAGECHANGE, onlanguagechange);
3146 }
3147
3148
3149
3150
3151
3152 @JsxGetter
3153 public Function getOnemptied() {
3154 return getEventHandler(Event.TYPE_EMPTIED);
3155 }
3156
3157
3158
3159
3160
3161 @JsxSetter
3162 public void setOnemptied(final Object onemptied) {
3163 setHandlerForJavaScript(Event.TYPE_EMPTIED, onemptied);
3164 }
3165
3166
3167
3168
3169
3170 @JsxGetter({CHROME, EDGE})
3171 public Function getOnrejectionhandled() {
3172 return getEventHandler(Event.TYPE_REJECTIONHANDLED);
3173 }
3174
3175
3176
3177
3178
3179 @JsxSetter({CHROME, EDGE})
3180 public void setOnrejectionhandled(final Object onrejectionhandled) {
3181 setHandlerForJavaScript(Event.TYPE_REJECTIONHANDLED, onrejectionhandled);
3182 }
3183
3184
3185
3186
3187
3188 @JsxGetter({CHROME, EDGE})
3189 public Function getOnpointercancel() {
3190 return getEventHandler(Event.TYPE_POINTERCANCEL);
3191 }
3192
3193
3194
3195
3196
3197 @JsxSetter({CHROME, EDGE})
3198 public void setOnpointercancel(final Object onpointercancel) {
3199 setHandlerForJavaScript(Event.TYPE_POINTERCANCEL, onpointercancel);
3200 }
3201
3202
3203
3204
3205
3206 @JsxGetter
3207 public Function getOnresize() {
3208 return getEventHandler(Event.TYPE_RESIZE);
3209 }
3210
3211
3212
3213
3214
3215 @JsxSetter
3216 public void setOnresize(final Object onresize) {
3217 setHandlerForJavaScript(Event.TYPE_RESIZE, onresize);
3218 }
3219
3220
3221
3222
3223
3224 @JsxGetter
3225 public Function getOnpause() {
3226 return getEventHandler(Event.TYPE_PAUSE);
3227 }
3228
3229
3230
3231
3232
3233 @JsxSetter
3234 public void setOnpause(final Object onpause) {
3235 setHandlerForJavaScript(Event.TYPE_PAUSE, onpause);
3236 }
3237
3238
3239
3240
3241
3242 @JsxGetter
3243 public Function getOnloadstart() {
3244 return getEventHandler(Event.TYPE_LOAD_START);
3245 }
3246
3247
3248
3249
3250
3251 @JsxSetter
3252 public void setOnloadstart(final Object onloadstart) {
3253 setHandlerForJavaScript(Event.TYPE_LOAD_START, onloadstart);
3254 }
3255
3256
3257
3258
3259
3260 @JsxGetter
3261 public Function getOnprogress() {
3262 return getEventHandler(Event.TYPE_PROGRESS);
3263 }
3264
3265
3266
3267
3268
3269 @JsxSetter
3270 public void setOnprogress(final Object onprogress) {
3271 setHandlerForJavaScript(Event.TYPE_PROGRESS, onprogress);
3272 }
3273
3274
3275
3276
3277
3278 @JsxGetter({CHROME, EDGE})
3279 public Function getOnpointerup() {
3280 return getEventHandler(Event.TYPE_POINTERUP);
3281 }
3282
3283
3284
3285
3286
3287 @JsxSetter({CHROME, EDGE})
3288 public void setOnpointerup(final Object onpointerup) {
3289 setHandlerForJavaScript(Event.TYPE_POINTERUP, onpointerup);
3290 }
3291
3292
3293
3294
3295
3296 @JsxGetter
3297 public Function getOnwheel() {
3298 return getEventHandler(Event.TYPE_WHEEL);
3299 }
3300
3301
3302
3303
3304
3305 @JsxSetter
3306 public void setOnwheel(final Object onwheel) {
3307 setHandlerForJavaScript(Event.TYPE_WHEEL, onwheel);
3308 }
3309
3310
3311
3312
3313
3314 @JsxGetter({CHROME, EDGE})
3315 public Function getOnpointerleave() {
3316 return getEventHandler(Event.TYPE_POINTERLEAVE);
3317 }
3318
3319
3320
3321
3322
3323 @JsxSetter({CHROME, EDGE})
3324 public void setOnpointerleave(final Object onpointerleave) {
3325 setHandlerForJavaScript(Event.TYPE_POINTERLEAVE, onpointerleave);
3326 }
3327
3328
3329
3330
3331
3332 @JsxGetter({FF, FF_ESR})
3333 public Function getOnbeforeprint() {
3334 return getEventHandler(Event.TYPE_BEFOREPRINT);
3335 }
3336
3337
3338
3339
3340
3341 @JsxSetter({FF, FF_ESR})
3342 public void setOnbeforeprint(final Object onbeforeprint) {
3343 setHandlerForJavaScript(Event.TYPE_BEFOREPRINT, onbeforeprint);
3344 }
3345
3346
3347
3348
3349
3350 @JsxGetter
3351 public Function getOnstorage() {
3352 return getEventHandler(Event.TYPE_STORAGE);
3353 }
3354
3355
3356
3357
3358
3359 @JsxSetter
3360 public void setOnstorage(final Object onstorage) {
3361 setHandlerForJavaScript(Event.TYPE_STORAGE, onstorage);
3362 }
3363
3364
3365
3366
3367
3368 @JsxGetter
3369 public Function getOnanimationstart() {
3370 return getEventHandler(Event.TYPE_ANIMATIONSTART);
3371 }
3372
3373
3374
3375
3376
3377 @JsxSetter
3378 public void setOnanimationstart(final Object onanimationstart) {
3379 setHandlerForJavaScript(Event.TYPE_ANIMATIONSTART, onanimationstart);
3380 }
3381
3382
3383
3384
3385
3386 @JsxGetter
3387 public Function getOntimeupdate() {
3388 return getEventHandler(Event.TYPE_TIMEUPDATE);
3389 }
3390
3391
3392
3393
3394
3395 @JsxSetter
3396 public void setOntimeupdate(final Object ontimeupdate) {
3397 setHandlerForJavaScript(Event.TYPE_TIMEUPDATE, ontimeupdate);
3398 }
3399
3400
3401
3402
3403
3404 @JsxGetter
3405 public Function getOnpagehide() {
3406 return getEventHandler(Event.TYPE_PAGEHIDE);
3407 }
3408
3409
3410
3411
3412
3413 @JsxSetter
3414 public void setOnpagehide(final Object onpagehide) {
3415 setHandlerForJavaScript(Event.TYPE_PAGEHIDE, onpagehide);
3416 }
3417
3418
3419
3420
3421
3422 @JsxGetter({CHROME, EDGE})
3423 public Function getOnwebkitanimationiteration() {
3424 return getEventHandler(Event.TYPE_WEBKITANIMATIONITERATION);
3425 }
3426
3427
3428
3429
3430
3431 @JsxSetter({CHROME, EDGE})
3432 public void setOnwebkitanimationiteration(final Object onwebkitanimationiteration) {
3433 setHandlerForJavaScript(Event.TYPE_WEBKITANIMATIONITERATION, onwebkitanimationiteration);
3434 }
3435
3436
3437
3438
3439
3440 @JsxGetter
3441 public Function getOnabort() {
3442 return getEventHandler(Event.TYPE_ABORT);
3443 }
3444
3445
3446
3447
3448
3449 @JsxSetter
3450 public void setOnabort(final Object onabort) {
3451 setHandlerForJavaScript(Event.TYPE_ABORT, onabort);
3452 }
3453
3454
3455
3456
3457
3458 @JsxGetter
3459 public Function getOnloadedmetadata() {
3460 return getEventHandler(Event.TYPE_LOADEDMETADATA);
3461 }
3462
3463
3464
3465
3466
3467 @JsxSetter
3468 public void setOnloadedmetadata(final Object onloadedmetadata) {
3469 setHandlerForJavaScript(Event.TYPE_LOADEDMETADATA, onloadedmetadata);
3470 }
3471
3472
3473
3474
3475
3476 @JsxGetter
3477 public Function getOnmouseup() {
3478 return getEventHandler(MouseEvent.TYPE_MOUSE_UP);
3479 }
3480
3481
3482
3483
3484
3485 @JsxSetter
3486 public void setOnmouseup(final Object onmouseup) {
3487 setHandlerForJavaScript(MouseEvent.TYPE_MOUSE_UP, onmouseup);
3488 }
3489
3490
3491
3492
3493
3494 @JsxGetter
3495 public Function getOndragover() {
3496 return getEventHandler(Event.TYPE_DRAGOVER);
3497 }
3498
3499
3500
3501
3502
3503 @JsxSetter
3504 public void setOndragover(final Object ondragover) {
3505 setHandlerForJavaScript(Event.TYPE_DRAGOVER, ondragover);
3506 }
3507
3508
3509
3510
3511
3512 @JsxGetter
3513 public Function getOnonline() {
3514 return getEventHandler(Event.TYPE_ONLINE);
3515 }
3516
3517
3518
3519
3520
3521 @JsxSetter
3522 public void setOnonline(final Object ononline) {
3523 setHandlerForJavaScript(Event.TYPE_ONLINE, ononline);
3524 }
3525
3526
3527
3528
3529
3530 @JsxGetter({CHROME, EDGE})
3531 public Function getOnsearch() {
3532 return getEventHandler(Event.TYPE_SEARCH);
3533 }
3534
3535
3536
3537
3538
3539 @JsxSetter({CHROME, EDGE})
3540 public void setOnsearch(final Object onsearch) {
3541 setHandlerForJavaScript(Event.TYPE_SEARCH, onsearch);
3542 }
3543
3544
3545
3546
3547
3548 @JsxGetter
3549 public Function getOninput() {
3550 return getEventHandler(Event.TYPE_INPUT);
3551 }
3552
3553
3554
3555
3556
3557 @JsxSetter
3558 public void setOninput(final Object oninput) {
3559 setHandlerForJavaScript(Event.TYPE_INPUT, oninput);
3560 }
3561
3562
3563
3564
3565
3566 @JsxGetter({CHROME, EDGE})
3567 public Function getOnwebkittransitionend() {
3568 return getEventHandler(Event.TYPE_WEBKITTRANSITIONEND);
3569 }
3570
3571
3572
3573
3574
3575 @JsxSetter({CHROME, EDGE})
3576 public void setOnwebkittransitionend(final Object onwebkittransitionend) {
3577 setHandlerForJavaScript(Event.TYPE_WEBKITTRANSITIONEND, onwebkittransitionend);
3578 }
3579
3580
3581
3582
3583
3584 @JsxGetter
3585 public Function getOndevicemotion() {
3586 return getEventHandler(Event.TYPE_DEVICEMOTION);
3587 }
3588
3589
3590
3591
3592
3593 @JsxSetter
3594 public void setOndevicemotion(final Object ondevicemotion) {
3595 setHandlerForJavaScript(Event.TYPE_DEVICEMOTION, ondevicemotion);
3596 }
3597
3598
3599
3600
3601
3602 @JsxGetter
3603 public Function getOnstalled() {
3604 return getEventHandler(Event.TYPE_STALLED);
3605 }
3606
3607
3608
3609
3610
3611 @JsxSetter
3612 public void setOnstalled(final Object onstalled) {
3613 setHandlerForJavaScript(Event.TYPE_STALLED, onstalled);
3614 }
3615
3616
3617
3618
3619
3620 @JsxGetter
3621 public Function getOnmouseenter() {
3622 return getEventHandler(Event.TYPE_MOUDEENTER);
3623 }
3624
3625
3626
3627
3628
3629 @JsxSetter
3630 public void setOnmouseenter(final Object onmouseenter) {
3631 setHandlerForJavaScript(Event.TYPE_MOUDEENTER, onmouseenter);
3632 }
3633
3634
3635
3636
3637
3638 @JsxGetter
3639 public Function getOndragleave() {
3640 return getEventHandler(Event.TYPE_DRAGLEAVE);
3641 }
3642
3643
3644
3645
3646
3647 @JsxSetter
3648 public void setOndragleave(final Object ondragleave) {
3649 setHandlerForJavaScript(Event.TYPE_DRAGLEAVE, ondragleave);
3650 }
3651
3652
3653
3654
3655
3656 @JsxGetter({CHROME, EDGE})
3657 public Function getOnpointerdown() {
3658 return getEventHandler(Event.TYPE_POINTERDOWN);
3659 }
3660
3661
3662
3663
3664
3665 @JsxSetter({CHROME, EDGE})
3666 public void setOnpointerdown(final Object onpointerdown) {
3667 setHandlerForJavaScript(Event.TYPE_POINTERDOWN, onpointerdown);
3668 }
3669
3670
3671
3672
3673
3674 @JsxGetter
3675 public Function getOndrop() {
3676 return getEventHandler(Event.TYPE_DROP);
3677 }
3678
3679
3680
3681
3682
3683 @JsxSetter
3684 public void setOndrop(final Object ondrop) {
3685 setHandlerForJavaScript(Event.TYPE_DROP, ondrop);
3686 }
3687
3688
3689
3690
3691
3692 @JsxGetter
3693 public Function getOnunload() {
3694 return getEventHandler(Event.TYPE_UNLOAD);
3695 }
3696
3697
3698
3699
3700
3701 @JsxSetter
3702 public void setOnunload(final Object onunload) {
3703 setHandlerForJavaScript(Event.TYPE_UNLOAD, onunload);
3704 }
3705
3706
3707
3708
3709
3710 @JsxGetter({CHROME, EDGE})
3711 public Function getOnwebkitanimationend() {
3712 return getEventHandler(Event.TYPE_WEBKITANIMATIONEND);
3713 }
3714
3715
3716
3717
3718
3719 @JsxSetter({CHROME, EDGE})
3720 public void setOnwebkitanimationend(final Object onwebkitanimationend) {
3721 setHandlerForJavaScript(Event.TYPE_WEBKITANIMATIONEND, onwebkitanimationend);
3722 }
3723
3724
3725
3726
3727
3728 @JsxGetter
3729 public Function getOndragstart() {
3730 return getEventHandler(Event.TYPE_DRAGSTART);
3731 }
3732
3733
3734
3735
3736
3737 @JsxSetter
3738 public void setOndragstart(final Object ondragstart) {
3739 setHandlerForJavaScript(Event.TYPE_DRAGSTART, ondragstart);
3740 }
3741
3742
3743
3744
3745
3746 @JsxGetter({CHROME, EDGE})
3747 public Function getOntransitionend() {
3748 return getEventHandler(Event.TYPE_TRANSITIONEND);
3749 }
3750
3751
3752
3753
3754
3755 @JsxSetter({CHROME, EDGE})
3756 public void setOntransitionend(final Object ontransitionend) {
3757 setHandlerForJavaScript(Event.TYPE_TRANSITIONEND, ontransitionend);
3758 }
3759
3760
3761
3762
3763
3764 @JsxGetter({CHROME, EDGE})
3765 public Function getOndeviceorientationabsolute() {
3766 return getEventHandler(Event.TYPE_DEVICEORIENTATIONABSOLUTE);
3767 }
3768
3769
3770
3771
3772
3773 @JsxSetter({CHROME, EDGE})
3774 public void setOndeviceorientationabsolute(final Object ondeviceorientationabsolute) {
3775 setHandlerForJavaScript(Event.TYPE_DEVICEORIENTATIONABSOLUTE, ondeviceorientationabsolute);
3776 }
3777
3778
3779
3780
3781
3782 @JsxGetter
3783 public Function getOnvolumechange() {
3784 return getEventHandler(Event.TYPE_VOLUMECHANGE);
3785 }
3786
3787
3788
3789
3790
3791 @JsxSetter
3792 public void setOnvolumechange(final Object onvolumechange) {
3793 setHandlerForJavaScript(Event.TYPE_VOLUMECHANGE, onvolumechange);
3794 }
3795
3796
3797
3798
3799
3800 @JsxGetter({CHROME, EDGE})
3801 public Function getOngotpointercapture() {
3802 return getEventHandler(Event.TYPE_GOTPOINTERCAPTURE);
3803 }
3804
3805
3806
3807
3808
3809 @JsxSetter({CHROME, EDGE})
3810 public void setOngotpointercapture(final Object ongotpointercapture) {
3811 setHandlerForJavaScript(Event.TYPE_GOTPOINTERCAPTURE, ongotpointercapture);
3812 }
3813
3814
3815
3816
3817
3818 @JsxGetter
3819 public Function getOnpopstate() {
3820 return getEventHandler(Event.TYPE_POPSTATE);
3821 }
3822
3823
3824
3825
3826
3827 @JsxSetter
3828 public void setOnpopstate(final Object onpopstate) {
3829 setHandlerForJavaScript(Event.TYPE_POPSTATE, onpopstate);
3830 }
3831
3832
3833
3834
3835
3836 @Override
3837 public BrowserVersion getBrowserVersion() {
3838 return getWebWindow().getWebClient().getBrowserVersion();
3839 }
3840
3841 @Override
3842 public void put(final String name, final Scriptable start, final Object value) {
3843
3844
3845 if ("event".equals(name)) {
3846 final Slot slot = querySlot(Context.getCurrentContext(), "event");
3847 if (slot instanceof AccessorSlot) {
3848 delete("event");
3849 }
3850 }
3851 super.put(name, start, value);
3852 }
3853
3854
3855
3856
3857
3858 @JsxGetter
3859 public boolean isIsSecureContext() {
3860 final Page page = getWebWindow().getEnclosedPage();
3861 if (page != null) {
3862 final String protocol = page.getUrl().getProtocol();
3863 if ("https".equals(protocol)
3864 || "wss".equals(protocol)
3865 || "file".equals(protocol)) {
3866 return true;
3867 }
3868
3869 final String host = page.getUrl().getHost();
3870 if ("localhost".equals(host)
3871 || "localhost.".equals(host)
3872 || host.endsWith(".localhost")
3873 || host.endsWith(".localhost.")) {
3874 return true;
3875 }
3876 }
3877
3878 return false;
3879 }
3880 }
3881
3882 class HTMLCollectionFrames extends HTMLCollection {
3883 private static final Log LOG = LogFactory.getLog(HTMLCollectionFrames.class);
3884
3885 HTMLCollectionFrames(final HtmlPage page) {
3886 super(page, false);
3887 this.setIsMatchingPredicate((Predicate<DomNode> & Serializable) node -> node instanceof BaseFrameElement);
3888 }
3889
3890 @Override
3891 protected Scriptable getScriptableForElement(final Object obj) {
3892 final WebWindow window;
3893 if (obj instanceof BaseFrameElement element) {
3894 window = element.getEnclosedWindow();
3895 }
3896 else {
3897 window = ((FrameWindow) obj).getFrameElement().getEnclosedWindow();
3898 }
3899
3900 return window.getScriptableObject();
3901 }
3902
3903 @Override
3904 protected Object getWithPreemption(final String name) {
3905 final List<DomNode> elements = getElements();
3906
3907 for (final Object next : elements) {
3908 final BaseFrameElement frameElt = (BaseFrameElement) next;
3909 final WebWindow window = frameElt.getEnclosedWindow();
3910 if (name.equals(window.getName())) {
3911 if (LOG.isDebugEnabled()) {
3912 LOG.debug("Property \"" + name + "\" evaluated (by name) to " + window);
3913 }
3914 return getScriptableForElement(window);
3915 }
3916 }
3917
3918 return NOT_FOUND;
3919 }
3920 }