1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.event;
16
17 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
18 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
19
20 import java.util.ArrayList;
21
22 import org.htmlunit.ScriptResult;
23 import org.htmlunit.corejs.javascript.Context;
24 import org.htmlunit.corejs.javascript.Scriptable;
25 import org.htmlunit.corejs.javascript.ScriptableObject;
26 import org.htmlunit.html.DomNode;
27 import org.htmlunit.javascript.HtmlUnitScriptable;
28 import org.htmlunit.javascript.JavaScriptEngine;
29 import org.htmlunit.javascript.configuration.JsxClass;
30 import org.htmlunit.javascript.configuration.JsxConstant;
31 import org.htmlunit.javascript.configuration.JsxConstructor;
32 import org.htmlunit.javascript.configuration.JsxFunction;
33 import org.htmlunit.javascript.configuration.JsxGetter;
34 import org.htmlunit.javascript.configuration.JsxSetter;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 @JsxClass
58 public class Event extends HtmlUnitScriptable {
59
60
61
62
63
64 protected static final String KEY_CURRENT_EVENT = "Event#current";
65
66
67 public static final String TYPE_SUBMIT = "submit";
68
69
70 public static final String TYPE_CHANGE = "change";
71
72
73 public static final String TYPE_LOAD = "load";
74
75
76 public static final String TYPE_UNLOAD = "unload";
77
78
79 public static final String TYPE_POPSTATE = "popstate";
80
81
82 public static final String TYPE_FOCUS = "focus";
83
84
85 public static final String TYPE_FOCUS_IN = "focusin";
86
87
88 public static final String TYPE_FOCUS_OUT = "focusout";
89
90
91 public static final String TYPE_BLUR = "blur";
92
93
94 public static final String TYPE_KEY_DOWN = "keydown";
95
96
97 public static final String TYPE_KEY_PRESS = "keypress";
98
99
100 public static final String TYPE_INPUT = "input";
101
102
103 public static final String TYPE_KEY_UP = "keyup";
104
105
106 public static final String TYPE_RESET = "reset";
107
108
109 public static final String TYPE_BEFORE_UNLOAD = "beforeunload";
110
111
112 public static final String TYPE_DOM_DOCUMENT_LOADED = "DOMContentLoaded";
113
114
115 public static final String TYPE_PROPERTY_CHANGE = "propertychange";
116
117
118 public static final String TYPE_HASH_CHANGE = "hashchange";
119
120
121 public static final String TYPE_READY_STATE_CHANGE = "readystatechange";
122
123
124 public static final String TYPE_ERROR = "error";
125
126
127 public static final String TYPE_MESSAGE = "message";
128
129
130 public static final String TYPE_CLOSE = "close";
131
132
133 public static final String TYPE_OPEN = "open";
134
135
136 public static final String TYPE_LOAD_START = "loadstart";
137
138
139 public static final String TYPE_LOAD_END = "loadend";
140
141
142 public static final String TYPE_PROGRESS = "progress";
143
144
145 public static final String TYPE_ABORT = "abort";
146
147
148 public static final String TYPE_TIMEOUT = "timeout";
149
150
151 public static final String TYPE_SCROLL = "scroll";
152
153
154 public static final String TYPE_SCROLLEND = "scrollend";
155
156
157 public static final String TYPE_SEARCH = "search";
158
159
160 public static final String TYPE_DRAGEND = "dragend";
161
162
163 public static final String TYPE_INVALID = "invalid";
164
165
166 public static final String TYPE_POINTEROUT = "pointerout";
167
168
169 public static final String TYPE_RATECHANGE = "ratechange";
170
171
172 public static final String TYPE_ANIMATIONITERATION = "animationiteration";
173
174
175 public static final String TYPE_CANPLAYTHROUGH = "canplaythrough";
176
177
178 public static final String TYPE_CANCEL = "cancel";
179
180
181 public static final String TYPE_POINTERENTER = "pointerenter";
182
183
184 public static final String TYPE_SELECT = "select";
185
186
187 public static final String TYPE_AUXCLICK = "auxclick";
188
189
190 public static final String TYPE_WEBANIMATIONSTART = "webkitanimationstart";
191
192
193 public static final String TYPE_DRAG = "drag";
194
195
196 public static final String TYPE_SEEKED = "seeked";
197
198
199 public static final String TYPE_OFFLINE = "offline";
200
201
202 public static final String TYPE_DEVICEORIENTATION = "deviceorientation";
203
204
205 public static final String TYPE_TOGGLE = "toggle";
206
207
208 public static final String TYPE_PLAY = "play";
209
210
211 public static final String TYPE_LOADEDMETADATA = "loadedmetadata";
212
213
214 public static final String TYPE_POINTERMOVE = "pointermove";
215
216
217 public static final String TYPE_LOSTPOINTERCAPTURE = "lostpointercapture";
218
219
220 public static final String TYPE_POINTEROVER = "pointerover";
221
222
223 public static final String TYPE_ANIMATIONCANCEL = "animationcancel";
224
225
226 public static final String TYPE_ANIMATIONEND = "animationend";
227
228
229 public static final String TYPE_DRAGENTER = "dragenter";
230
231
232 public static final String TYPE_AFTERPRINT = "afterprint";
233
234
235 public static final String TYPE_MOZFULLSCREENERROR = "mozfullscreenerror";
236
237
238 public static final String TYPE_MOUSELEAVE = "mouseleave";
239
240
241 public static final String TYPE_MOUSEWHEEL = "mousewheel";
242
243
244 public static final String TYPE_SEEKING = "seeking";
245
246
247 public static final String TYPE_CUECHANGE = "cuechange";
248
249
250 public static final String TYPE_PAGESHOW = "pageshow";
251
252
253 public static final String TYPE_MOZFULLSCREENCHANGE = "mozfullscreenchange";
254
255
256 public static final String TYPE_DURATIONCHANGE = "durationchange";
257
258
259 public static final String TYPE_PLAYING = "playing";
260
261
262 public static final String TYPE_ENDED = "ended";
263
264
265 public static final String TYPE_LOADEDDATA = "loadeddata";
266
267
268 public static final String TYPE_UNHANDLEDREJECTION = "unhandledrejection";
269
270
271 public static final String TYPE_SUSPEND = "suspend";
272
273
274 public static final String TYPE_WAITING = "waiting";
275
276
277 public static final String TYPE_CANPLAY = "canplay";
278
279
280 public static final String TYPE_LANGUAGECHANGE = "languagechange";
281
282
283 public static final String TYPE_EMPTIED = "emptied";
284
285
286 public static final String TYPE_REJECTIONHANDLED = "rejectionhandled";
287
288
289 public static final String TYPE_POINTERCANCEL = "pointercancel";
290
291
292 public static final String TYPE_RESIZE = "resize";
293
294
295 public static final String TYPE_PAUSE = "pause";
296
297
298 public static final String TYPE_POINTERUP = "pointerup";
299
300
301 public static final String TYPE_WHEEL = "wheel";
302
303
304 public static final String TYPE_POINTERLEAVE = "pointerleave";
305
306
307 public static final String TYPE_BEFOREPRINT = "beforeprint";
308
309
310 public static final String TYPE_STORAGE = "storage";
311
312
313 public static final String TYPE_ANIMATIONSTART = "animationstart";
314
315
316 public static final String TYPE_TIMEUPDATE = "timeupdate";
317
318
319 public static final String TYPE_PAGEHIDE = "pagehide";
320
321
322 public static final String TYPE_WEBKITANIMATIONITERATION = "webkitanimationiteration";
323
324
325 public static final String TYPE_DRAGOVER = "dragover";
326
327
328 public static final String TYPE_ONLINE = "online";
329
330
331 public static final String TYPE_VOLUMECHANGE = "volumechange";
332
333
334 public static final String TYPE_GOTPOINTERCAPTURE = "gotpointercapture";
335
336
337 public static final String TYPE_WEBKITTRANSITIONEND = "webkittransitionend";
338
339
340 public static final String TYPE_WEBKITANIMATIONEND = "webkitanimationend";
341
342
343 public static final String TYPE_DEVICEMOTION = "devicemotion";
344
345
346 public static final String TYPE_STALLED = "stalled";
347
348
349 public static final String TYPE_MOUDEENTER = "mouseenter";
350
351
352 public static final String TYPE_DRAGLEAVE = "dragleave";
353
354
355 public static final String TYPE_POINTERDOWN = "pointerdown";
356
357
358 public static final String TYPE_DROP = "drop";
359
360
361 public static final String TYPE_DRAGSTART = "dragstart";
362
363
364 public static final String TYPE_TRANSITIONEND = "transitionend";
365
366
367 public static final String TYPE_DEVICEORIENTATIONABSOLUTE = "deviceorientationabsolute";
368
369
370 public static final String TYPE_BEFORECOPY = "beforecopy";
371
372
373 public static final String TYPE_BEFORECUT = "beforecut";
374
375
376 public static final String TYPE_BEFOREPASTE = "beforepaste";
377
378
379 public static final String TYPE_SELECTSTART = "selectstart";
380
381
382 public static final String TYPE_WEBKITFULLSCREENCHANGE = "webkitfullscreenchange";
383
384
385 public static final String TYPE_WEBKITFULLSCREENERROR = "webkitfullscreenerror";
386
387
388 public static final String TYPE_COPY = "copy";
389
390
391 public static final String TYPE_CUT = "cut";
392
393
394 public static final String TYPE_PASTE = "paste";
395
396
397 public static final String TYPE_ONMESSAGEERROR = "onmessageerror";
398
399
400 public static final String TYPE_POINTERLOCKCHANGE = "pointerlockchange";
401
402
403 public static final String TYPE_POINTERLOCKERROR = "pointerlockerror";
404
405
406 public static final String TYPE_SELECTIONCHANGE = "selectionchange";
407
408
409 public static final String TYPE_AFTERSCRIPTEXECUTE = "afterscriptexecute";
410
411
412 public static final String TYPE_BEFORESCRIPTEXECUTE = "beforescriptexecute";
413
414
415 public static final String TYPE_ONTRANSITIONCANCEL = "ontransitioncancel";
416
417
418 public static final String TYPE_ONTRANSITIONEND = "ontransitionend";
419
420
421 public static final String TYPE_ONTRANSITIONRUN = "ontransitionrun";
422
423
424 public static final String TYPE_ONTRANSITIONSTART = "ontransitionstart";
425
426
427 public static final String TYPE_GAMEPAD_CONNECTED = "ongamepadconnected";
428
429
430 public static final String TYPE_GAMEPAD_DISCONNECTED = "ongamepaddisconnected";
431
432
433 @JsxConstant
434 public static final int NONE = 0;
435
436
437 @JsxConstant
438 public static final int CAPTURING_PHASE = 1;
439
440
441 @JsxConstant
442 public static final int AT_TARGET = 2;
443
444
445 @JsxConstant
446 public static final int BUBBLING_PHASE = 3;
447
448
449 @JsxConstant({FF, FF_ESR})
450 public static final int ALT_MASK = 0x1;
451
452
453 @JsxConstant({FF, FF_ESR})
454 public static final int CONTROL_MASK = 0x2;
455
456
457 @JsxConstant({FF, FF_ESR})
458 public static final int SHIFT_MASK = 0x4;
459
460
461 @JsxConstant({FF, FF_ESR})
462 public static final int META_MASK = 0x8;
463
464 private Object srcElement_;
465 private EventTarget target_;
466 private Scriptable currentTarget_;
467 private String type_ = "";
468 private int keyCode_;
469 private boolean shiftKey_;
470 private boolean ctrlKey_;
471 private boolean altKey_;
472 private String propertyName_;
473 private boolean stopPropagation_;
474 private boolean stopImmediatePropagation_;
475 private boolean preventDefault_;
476
477
478
479
480
481 private int eventPhase_;
482
483
484
485
486
487
488
489
490 private boolean bubbles_ = true;
491
492
493
494
495
496
497
498
499
500 private boolean cancelable_ = true;
501
502
503
504
505 private final long timeStamp_ = System.currentTimeMillis();
506
507
508
509
510
511
512 public Event(final DomNode domNode, final String type) {
513 this((EventTarget) domNode.getScriptableObject(), type);
514 setDomNode(domNode, false);
515 }
516
517
518
519
520
521
522 public Event(final EventTarget target, final String type) {
523 this(type);
524
525 srcElement_ = target;
526 target_ = target;
527 currentTarget_ = target;
528
529 setParentScope(target);
530 setPrototype(getPrototype(getClass()));
531
532 if (TYPE_CHANGE.equals(type)) {
533 cancelable_ = false;
534 }
535 else if (TYPE_LOAD.equals(type)
536 || TYPE_CLOSE.equals(type)) {
537 bubbles_ = false;
538 cancelable_ = false;
539 }
540 else if (TYPE_ERROR.equals(type)) {
541
542 bubbles_ = false;
543 }
544 else if (
545 TYPE_FOCUS.equals(type)
546 || TYPE_BLUR.equals(type)
547 || TYPE_BEFOREPRINT.equals(type)
548 || TYPE_AFTERPRINT.equals(type)) {
549 bubbles_ = false;
550 cancelable_ = false;
551 }
552 }
553
554
555
556
557
558 public Event(final String type) {
559 super();
560 type_ = type;
561 }
562
563
564
565
566
567
568
569 public static Event createPropertyChangeEvent(final DomNode domNode, final String propertyName) {
570 final Event event = new Event(domNode, TYPE_PROPERTY_CHANGE);
571 event.propertyName_ = propertyName;
572 return event;
573 }
574
575
576
577
578 public Event() {
579 super();
580 }
581
582
583
584
585
586 public void eventCreated() {
587 setBubbles(false);
588 setCancelable(false);
589 }
590
591
592
593
594
595
596
597 @JsxConstructor
598 public void jsConstructor(final String type, final ScriptableObject details) {
599 boolean bubbles = false;
600 boolean cancelable = false;
601
602 if (details != null && !JavaScriptEngine.isUndefined(details)) {
603 bubbles = JavaScriptEngine.toBoolean(details.get("bubbles"));
604 cancelable = JavaScriptEngine.toBoolean(details.get("cancelable"));
605 }
606 initEvent(type, bubbles, cancelable);
607 }
608
609
610
611
612 @SuppressWarnings("unchecked")
613 public void startFire() {
614 final Context context = Context.getCurrentContext();
615 ArrayList<Event> events = (ArrayList<Event>) context.getThreadLocal(KEY_CURRENT_EVENT);
616 if (events == null) {
617 events = new ArrayList<>();
618 context.putThreadLocal(KEY_CURRENT_EVENT, events);
619 }
620 events.add(this);
621 }
622
623
624
625
626 @SuppressWarnings("unchecked")
627 public void endFire() {
628 final Context context = Context.getCurrentContext();
629 final ArrayList<Event> events = (ArrayList<Event>) context.getThreadLocal(KEY_CURRENT_EVENT);
630 if (events != null && !events.isEmpty()) {
631 events.remove(events.size() - 1);
632 }
633 }
634
635
636
637
638
639 @JsxGetter
640 public Object getSrcElement() {
641 return srcElement_;
642 }
643
644
645
646
647
648 public void setSrcElement(final Object srcElement) {
649 srcElement_ = srcElement;
650 }
651
652
653
654
655
656 @JsxGetter
657 public Object getTarget() {
658 return target_;
659 }
660
661
662
663
664
665 public void setTarget(final EventTarget target) {
666 target_ = target;
667 }
668
669
670
671
672
673
674 @JsxGetter
675 public Scriptable getCurrentTarget() {
676 return currentTarget_;
677 }
678
679
680
681
682
683 public void setCurrentTarget(final Scriptable target) {
684 currentTarget_ = target;
685 }
686
687
688
689
690
691 @JsxGetter
692 public String getType() {
693 return type_;
694 }
695
696
697
698
699
700 public void setType(final String type) {
701 type_ = type;
702 }
703
704
705
706
707
708 public void setEventType(final String eventType) {
709 type_ = eventType;
710 }
711
712
713
714
715
716 @JsxGetter
717 public long getTimeStamp() {
718 return timeStamp_;
719 }
720
721
722
723
724
725 protected void setKeyCode(final int keyCode) {
726 keyCode_ = keyCode;
727 }
728
729
730
731
732
733 public int getKeyCode() {
734 return keyCode_;
735 }
736
737
738
739
740
741 public boolean isShiftKey() {
742 return shiftKey_;
743 }
744
745
746
747
748
749 protected void setShiftKey(final boolean shiftKey) {
750 shiftKey_ = shiftKey;
751 }
752
753
754
755
756
757 public boolean isCtrlKey() {
758 return ctrlKey_;
759 }
760
761
762
763
764
765 protected void setCtrlKey(final boolean ctrlKey) {
766 ctrlKey_ = ctrlKey;
767 }
768
769
770
771
772
773 public boolean isAltKey() {
774 return altKey_;
775 }
776
777
778
779
780
781 protected void setAltKey(final boolean altKey) {
782 altKey_ = altKey;
783 }
784
785
786
787
788
789 @JsxGetter
790 public int getEventPhase() {
791 return eventPhase_;
792 }
793
794
795
796
797
798
799
800 public void setEventPhase(final int phase) {
801 if (phase != CAPTURING_PHASE && phase != AT_TARGET && phase != BUBBLING_PHASE) {
802 throw new IllegalArgumentException("Illegal phase specified: " + phase);
803 }
804 eventPhase_ = phase;
805 }
806
807
808
809
810 @JsxGetter
811 public boolean isBubbles() {
812 return bubbles_;
813 }
814
815
816
817
818 public void setBubbles(final boolean bubbles) {
819 bubbles_ = bubbles;
820 }
821
822
823
824
825 @JsxGetter
826 public boolean isCancelable() {
827 return cancelable_;
828 }
829
830
831
832
833 public void setCancelable(final boolean cancelable) {
834 cancelable_ = cancelable;
835 }
836
837
838
839
840
841
842 @JsxGetter
843 public boolean isDefaultPrevented() {
844 return cancelable_ && preventDefault_;
845 }
846
847
848
849
850 @JsxGetter
851 public boolean isCancelBubble() {
852 return stopPropagation_;
853 }
854
855
856
857
858 @JsxSetter
859 public void setCancelBubble(final boolean newValue) {
860 stopPropagation_ = newValue;
861 }
862
863
864
865
866 @JsxFunction
867 public void stopPropagation() {
868 stopPropagation_ = true;
869 }
870
871
872
873
874
875 public boolean isPropagationStopped() {
876 return stopPropagation_;
877 }
878
879
880
881
882 @JsxFunction
883 public void stopImmediatePropagation() {
884 stopImmediatePropagation_ = true;
885 stopPropagation();
886 }
887
888
889
890
891
892 public boolean isImmediatePropagationStopped() {
893 return stopImmediatePropagation_;
894 }
895
896
897
898
899
900 void handlePropertyHandlerReturnValue(final Object returnValue) {
901 if (Boolean.FALSE.equals(returnValue)) {
902 preventDefault();
903 }
904 }
905
906
907
908
909
910 public String getPropertyName() {
911 return propertyName_;
912 }
913
914
915
916
917
918
919
920 @JsxFunction
921 public void initEvent(final String type, final boolean bubbles, final boolean cancelable) {
922 type_ = type;
923 bubbles_ = bubbles;
924 cancelable_ = cancelable;
925 }
926
927
928
929
930
931
932 @JsxFunction
933 public void preventDefault() {
934 if (isCancelable()) {
935 preventDefault_ = true;
936 }
937 }
938
939
940
941
942
943
944
945
946 public boolean isAborted(final ScriptResult result) {
947 return ScriptResult.isFalse(result) || preventDefault_;
948 }
949
950
951
952
953 @Override
954 public String toString() {
955 final StringBuilder builder = new StringBuilder(40);
956 builder.append("Event ")
957 .append(getType())
958 .append(" (Current Target: ")
959 .append(currentTarget_)
960 .append(");");
961 return builder.toString();
962 }
963
964
965
966
967
968
969
970
971 public boolean processLabelAfterBubbling() {
972 return false;
973 }
974
975
976
977
978 @JsxGetter
979 public Object getReturnValue() {
980 return !preventDefault_;
981 }
982
983
984
985
986 @JsxSetter
987 public void setReturnValue(final Object newValue) {
988 if (isCancelable()) {
989 final boolean bool = !JavaScriptEngine.toBoolean(newValue);
990 if (bool) {
991 preventDefault_ = bool;
992 }
993 }
994 }
995
996
997
998
999 @JsxGetter
1000 public boolean isComposed() {
1001 return false;
1002 }
1003
1004
1005
1006
1007
1008
1009 protected static boolean isMissingOrUndefined(final Object value) {
1010 return value == Scriptable.NOT_FOUND || JavaScriptEngine.isUndefined(value);
1011 }
1012 }