forked from kadirselcuk/jquery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.js
3366 lines (2682 loc) · 98.9 KB
/
event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
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
555
556
557
558
559
560
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
QUnit.module( "event", {
beforeEach: function() {
document.body.focus();
},
afterEach: moduleTeardown
} );
QUnit.test( "null or undefined handler", function( assert ) {
assert.expect( 4 );
// Supports Fixes bug #7229
try {
jQuery( "#firstp" ).on( "click", null );
assert.ok( true, "Passing a null handler will not throw an exception" );
} catch ( e ) {}
try {
jQuery( "#firstp" ).on( "click", undefined );
assert.ok( true, "Passing an undefined handler will not throw an exception" );
} catch ( e ) {}
var expectedElem = jQuery( "#firstp" );
var actualElem = expectedElem.on( "click", null );
assert.equal( actualElem, expectedElem, "Passing a null handler should return the original element" );
actualElem = expectedElem.on( "click", undefined );
assert.equal( actualElem, expectedElem, "Passing a null handler should return the original element" );
} );
QUnit.test( "on() with non-null,defined data", function( assert ) {
assert.expect( 2 );
var handler = function( event, data ) {
assert.equal( data, 0, "non-null, defined data (zero) is correctly passed" );
};
jQuery( "#foo" ).on( "foo.on", handler );
jQuery( "div" ).on( "foo.delegate", "#foo", handler );
jQuery( "#foo" ).trigger( "foo", 0 );
jQuery( "#foo" ).off( "foo.on", handler );
jQuery( "div" ).off( "foo.delegate", "#foo" );
} );
QUnit.test( "Handler changes and .trigger() order", function( assert ) {
assert.expect( 1 );
var markup = jQuery(
"<div><div><p><span><b class=\"a\">b</b></span></p></div></div>"
),
path = "";
markup
.find( "*" ).addBack().on( "click", function() {
path += this.nodeName.toLowerCase() + " ";
} )
.filter( "b" ).on( "click", function( e ) {
// Removing span should not stop propagation to original parents
if ( e.target === this ) {
jQuery( this ).parent().remove();
}
} );
markup.find( "b" ).trigger( "click" );
assert.equal( path, "b p div div ", "Delivered all events" );
markup.remove();
} );
QUnit.test( "on(), with data", function( assert ) {
assert.expect( 4 );
var test, handler, handler2;
handler = function( event ) {
assert.ok( event.data, "on() with data, check passed data exists" );
assert.equal( event.data.foo, "bar", "on() with data, Check value of passed data" );
};
jQuery( "#firstp" ).on( "click", { "foo": "bar" }, handler ).trigger( "click" ).off( "click", handler );
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using data." );
test = function() {};
handler2 = function( event ) {
assert.equal( event.data, test, "on() with function data, Check value of passed data" );
};
jQuery( "#firstp" ).on( "click", test, handler2 ).trigger( "click" ).off( "click", handler2 );
} );
QUnit.test( "click(), with data", function( assert ) {
assert.expect( 3 );
var handler = function( event ) {
assert.ok( event.data, "on() with data, check passed data exists" );
assert.equal( event.data.foo, "bar", "on() with data, Check value of passed data" );
};
jQuery( "#firstp" ).on( "click", { "foo": "bar" }, handler ).trigger( "click" ).off( "click", handler );
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using data." );
} );
QUnit.test( "on(), with data, trigger with data", function( assert ) {
assert.expect( 4 );
var handler = function( event, data ) {
assert.ok( event.data, "check passed data exists" );
assert.equal( event.data.foo, "bar", "Check value of passed data" );
assert.ok( data, "Check trigger data" );
assert.equal( data.bar, "foo", "Check value of trigger data" );
};
jQuery( "#firstp" ).on( "click", { foo: "bar" }, handler ).trigger( "click", [ { bar: "foo" } ] ).off( "click", handler );
} );
QUnit.test( "on(), multiple events at once", function( assert ) {
assert.expect( 2 );
var handler,
clickCounter = 0,
mouseoverCounter = 0;
handler = function( event ) {
if ( event.type === "click" ) {
clickCounter += 1;
} else if ( event.type === "mouseover" ) {
mouseoverCounter += 1;
}
};
jQuery( "#firstp" ).on( "click mouseover", handler ).trigger( "click" ).trigger( "mouseover" );
assert.equal( clickCounter, 1, "on() with multiple events at once" );
assert.equal( mouseoverCounter, 1, "on() with multiple events at once" );
} );
QUnit.test( "on(), five events at once", function( assert ) {
assert.expect( 1 );
var count = 0,
handler = function() {
count++;
};
jQuery( "#firstp" ).on( "click mouseover foo bar baz", handler )
.trigger( "click" ).trigger( "mouseover" )
.trigger( "foo" ).trigger( "bar" )
.trigger( "baz" );
assert.equal( count, 5, "on() five events at once" );
} );
QUnit.test( "on(), multiple events at once and namespaces", function( assert ) {
assert.expect( 7 );
var cur, div,
obj = {};
div = jQuery( "<div></div>" ).on( "focusin.a", function( e ) {
assert.equal( e.type, cur, "Verify right single event was fired." );
} );
cur = "focusin";
div.trigger( "focusin.a" );
// manually clean up detached elements
div.remove();
div = jQuery( "<div></div>" ).on( "click mouseover", obj, function( e ) {
assert.equal( e.type, cur, "Verify right multi event was fired." );
assert.equal( e.data, obj, "Make sure the data came in correctly." );
} );
cur = "click";
div.trigger( "click" );
cur = "mouseover";
div.trigger( "mouseover" );
// manually clean up detached elements
div.remove();
div = jQuery( "<div></div>" ).on( "focusin.a focusout.b", function( e ) {
assert.equal( e.type, cur, "Verify right multi event was fired." );
} );
cur = "focusin";
div.trigger( "focusin.a" );
cur = "focusout";
div.trigger( "focusout.b" );
// manually clean up detached elements
div.remove();
} );
QUnit.test( "on(), namespace with special add", function( assert ) {
assert.expect( 27 );
var i = 0,
div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "test", function() {
assert.ok( true, "Test event fired." );
} );
jQuery.event.special.test = {
_default: function( e, data ) {
assert.equal( e.type, "test", "Make sure we're dealing with a test event." );
assert.ok( data, "And that trigger data was passed." );
assert.strictEqual( e.target, div[ 0 ], "And that the target is correct." );
assert.equal( this, window, "And that the context is correct." );
},
setup: function() {},
teardown: function() {
assert.ok( true, "Teardown called." );
},
add: function( handleObj ) {
var handler = handleObj.handler;
handleObj.handler = function( e ) {
e.xyz = ++i;
handler.apply( this, arguments );
};
},
remove: function() {
assert.ok( true, "Remove called." );
}
};
div.on( "test.a", { x: 1 }, function( e ) {
assert.ok( !!e.xyz, "Make sure that the data is getting passed through." );
assert.equal( e.data[ "x" ], 1, "Make sure data is attached properly." );
} );
div.on( "test.b", { x: 2 }, function( e ) {
assert.ok( !!e.xyz, "Make sure that the data is getting passed through." );
assert.equal( e.data[ "x" ], 2, "Make sure data is attached properly." );
} );
// Should trigger 5
div.trigger( "test", 33.33 );
// Should trigger 2
div.trigger( "test.a", "George Harrison" );
// Should trigger 2
div.trigger( "test.b", { year: 1982 } );
// Should trigger 4
div.off( "test" );
div = jQuery( "<div></div>" ).on( "test", function() {
assert.ok( true, "Test event fired." );
} );
// Should trigger 2
div.appendTo( "#qunit-fixture" ).remove();
delete jQuery.event.special.test;
} );
QUnit.test( "on(), no data", function( assert ) {
assert.expect( 1 );
var handler = function( event ) {
assert.ok( !event.data, "Check that no data is added to the event object" );
};
jQuery( "#firstp" ).on( "click", handler ).trigger( "click" );
} );
QUnit.test( "on/one/off(Object)", function( assert ) {
assert.expect( 6 );
var $elem,
clickCounter = 0,
mouseoverCounter = 0;
function handler( event ) {
if ( event.type === "click" ) {
clickCounter++;
} else if ( event.type === "mouseover" ) {
mouseoverCounter++;
}
}
function handlerWithData( event ) {
if ( event.type === "click" ) {
clickCounter += event.data;
} else if ( event.type === "mouseover" ) {
mouseoverCounter += event.data;
}
}
function trigger() {
$elem.trigger( "click" ).trigger( "mouseover" );
}
$elem = jQuery( "#firstp" )
// Regular bind
.on( {
"click":handler,
"mouseover":handler
} )
// Bind with data
.one( {
"click":handlerWithData,
"mouseover":handlerWithData
}, 2 );
trigger();
assert.equal( clickCounter, 3, "on(Object)" );
assert.equal( mouseoverCounter, 3, "on(Object)" );
trigger();
assert.equal( clickCounter, 4, "on(Object)" );
assert.equal( mouseoverCounter, 4, "on(Object)" );
jQuery( "#firstp" ).off( {
"click":handler,
"mouseover":handler
} );
trigger();
assert.equal( clickCounter, 4, "on(Object)" );
assert.equal( mouseoverCounter, 4, "on(Object)" );
} );
QUnit.test( "on/off(Object), on/off(Object, String)", function( assert ) {
assert.expect( 6 );
var events,
clickCounter = 0,
mouseoverCounter = 0,
$p = jQuery( "#firstp" ),
$a = $p.find( "a" ).eq( 0 );
events = {
"click": function( event ) {
clickCounter += ( event.data || 1 );
},
"mouseover": function( event ) {
mouseoverCounter += ( event.data || 1 );
}
};
function trigger() {
$a.trigger( "click" ).trigger( "mouseover" );
}
jQuery( document ).on( events, "#firstp a" );
$p.on( events, "a", 2 );
trigger();
assert.equal( clickCounter, 3, "on" );
assert.equal( mouseoverCounter, 3, "on" );
$p.off( events, "a" );
trigger();
assert.equal( clickCounter, 4, "off" );
assert.equal( mouseoverCounter, 4, "off" );
jQuery( document ).off( events, "#firstp a" );
trigger();
assert.equal( clickCounter, 4, "off" );
assert.equal( mouseoverCounter, 4, "off" );
} );
QUnit.test( "on immediate propagation", function( assert ) {
assert.expect( 2 );
var lastClick,
$p = jQuery( "#firstp" ),
$a = $p.find( "a" ).eq( 0 );
lastClick = "";
jQuery( document ).on( "click", "#firstp a", function( e ) {
lastClick = "click1";
e.stopImmediatePropagation();
} );
jQuery( document ).on( "click", "#firstp a", function() {
lastClick = "click2";
} );
$a.trigger( "click" );
assert.equal( lastClick, "click1", "on stopImmediatePropagation" );
jQuery( document ).off( "click", "#firstp a" );
lastClick = "";
$p.on( "click", "a", function( e ) {
lastClick = "click1";
e.stopImmediatePropagation();
} );
$p.on( "click", "a", function() {
lastClick = "click2";
} );
$a.trigger( "click" );
assert.equal( lastClick, "click1", "on stopImmediatePropagation" );
$p.off( "click", "**" );
} );
QUnit.test( "on bubbling, isDefaultPrevented, stopImmediatePropagation", function( assert ) {
assert.expect( 3 );
var $anchor2 = jQuery( "#anchor2" ),
$main = jQuery( "#qunit-fixture" ),
neverCallMe = function() {
assert.ok( false, "immediate propagation should have been stopped" );
},
fakeClick = function( $jq ) {
// Use a native click so we don't get jQuery simulated bubbling
var e = document.createEvent( "MouseEvents" );
e.initEvent( "click", true, true );
$jq[ 0 ].dispatchEvent( e );
};
$anchor2.on( "click", function( e ) {
e.preventDefault();
} );
$main.on( "click", "#foo", function( e ) {
assert.equal( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
} );
fakeClick( $anchor2 );
$anchor2.off( "click" );
$main.off( "click", "**" );
$anchor2.on( "click", function() {
// Let the default action occur
} );
$main.on( "click", "#foo", function( e ) {
assert.equal( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
} );
fakeClick( $anchor2 );
$anchor2.off( "click" );
$main.off( "click", "**" );
$anchor2.on( "click", function( e ) {
e.stopImmediatePropagation();
assert.ok( true, "anchor was clicked and prop stopped" );
} );
$anchor2[ 0 ].addEventListener( "click", neverCallMe, false );
fakeClick( $anchor2 );
$anchor2[ 0 ].removeEventListener( "click", neverCallMe );
} );
QUnit.test( "triggered events stopPropagation() for natively-bound events", function( assert ) {
assert.expect( 1 );
var $button = jQuery( "#button" ),
$parent = $button.parent(),
neverCallMe = function() {
assert.ok( false, "propagation should have been stopped" );
},
stopPropagationCallback = function( e ) {
assert.ok( true, "propagation is stopped" );
e.stopPropagation();
};
$parent[ 0 ].addEventListener( "click", neverCallMe );
$button.on( "click", stopPropagationCallback );
$button.trigger( "click" );
$parent[ 0 ].removeEventListener( "click", neverCallMe );
$button.off( "click", stopPropagationCallback );
} );
QUnit.test( "trigger() works with events that were previously stopped", function( assert ) {
assert.expect( 0 );
var $button = jQuery( "#button" ),
$parent = $button.parent(),
neverCallMe = function() {
assert.ok( false, "propagation should have been stopped" );
};
$parent[ 0 ].addEventListener( "click", neverCallMe );
$button.on( "click", neverCallMe );
var clickEvent = jQuery.Event( "click" );
clickEvent.stopPropagation();
$button.trigger( clickEvent );
$parent[ 0 ].removeEventListener( "click", neverCallMe );
$button.off( "click", neverCallMe );
} );
QUnit.test( "on(), iframes", function( assert ) {
assert.expect( 1 );
// events don't work with iframes, see #939 - this test fails in IE because of contentDocument
var doc = jQuery( "#loadediframe" ).contents();
jQuery( "div", doc ).on( "click", function() {
assert.ok( true, "Binding to element inside iframe" );
} ).trigger( "click" ).off( "click" );
} );
QUnit.test( "on(), trigger change on select", function( assert ) {
assert.expect( 5 );
var counter = 0;
function selectOnChange( event ) {
assert.equal( event.data, counter++, "Event.data is not a global event object" );
}
jQuery( "#form select" ).each( function( i ) {
jQuery( this ).on( "change", i, selectOnChange );
} ).trigger( "change" );
} );
QUnit.test( "on(), namespaced events, cloned events", function( assert ) {
assert.expect( 18 );
var firstp = jQuery( "#firstp" );
firstp.on( "custom.test", function() {
assert.ok( false, "Custom event triggered" );
} );
firstp.on( "click", function( e ) {
assert.ok( true, "Normal click triggered" );
assert.equal( e.type + e.namespace, "click", "Check that only click events trigger this fn" );
} );
firstp.on( "click.test", function( e ) {
var check = "click";
assert.ok( true, "Namespaced click triggered" );
if ( e.namespace ) {
check += "test";
}
assert.equal( e.type + e.namespace, check, "Check that only click/click.test events trigger this fn" );
} );
//clone(true) element to verify events are cloned correctly
firstp = firstp.add( firstp.clone( true ).attr( "id", "firstp2" ).insertBefore( firstp ) );
// Trigger both bound fn (8)
firstp.trigger( "click" );
// Trigger one bound fn (4)
firstp.trigger( "click.test" );
// Remove only the one fn
firstp.off( "click.test" );
// Trigger the remaining fn (4)
firstp.trigger( "click" );
// Remove the remaining namespaced fn
firstp.off( ".test" );
// Try triggering the custom event (0)
firstp.trigger( "custom" );
// using contents will get comments regular, text, and comment nodes
jQuery( "#nonnodes" ).contents().on( "tester", function() {
assert.equal( this.nodeType, 1, "Check node,textnode,comment on just does real nodes" );
} ).trigger( "tester" );
// Make sure events stick with appendTo'd elements (which are cloned) #2027
jQuery( "<a href='#fail' class='test'>test</a>" ).on( "click", function() { return false; } ).appendTo( "#qunit-fixture" );
assert.ok( jQuery( "a.test" ).eq( 0 ).triggerHandler( "click" ) === false, "Handler is bound to appendTo'd elements" );
} );
QUnit.test( "on(), multi-namespaced events", function( assert ) {
assert.expect( 6 );
var order = [
"click.test.abc",
"click.test.abc",
"click.test",
"click.test.abc",
"click.test",
"custom.test2"
];
function check( name, msg ) {
assert.deepEqual( name, order.shift(), msg );
}
jQuery( "#firstp" ).on( "custom.test", function() {
check( "custom.test", "Custom event triggered" );
} );
jQuery( "#firstp" ).on( "custom.test2", function() {
check( "custom.test2", "Custom event triggered" );
} );
jQuery( "#firstp" ).on( "click.test", function() {
check( "click.test", "Normal click triggered" );
} );
jQuery( "#firstp" ).on( "click.test.abc", function() {
check( "click.test.abc", "Namespaced click triggered" );
} );
// Those would not trigger/off (#5303)
jQuery( "#firstp" ).trigger( "click.a.test" );
jQuery( "#firstp" ).off( "click.a.test" );
// Trigger both bound fn (1)
jQuery( "#firstp" ).trigger( "click.test.abc" );
// Trigger one bound fn (1)
jQuery( "#firstp" ).trigger( "click.abc" );
// Trigger two bound fn (2)
jQuery( "#firstp" ).trigger( "click.test" );
// Remove only the one fn
jQuery( "#firstp" ).off( "click.abc" );
// Trigger the remaining fn (1)
jQuery( "#firstp" ).trigger( "click" );
// Remove the remaining fn
jQuery( "#firstp" ).off( ".test" );
// Trigger the remaining fn (1)
jQuery( "#firstp" ).trigger( "custom" );
} );
QUnit.test( "namespace-only event binding is a no-op", function( assert ) {
assert.expect( 2 );
jQuery( "#firstp" )
.on( ".whoops", function() {
assert.ok( false, "called a namespace-only event" );
} )
.on( "whoops", function() {
assert.ok( true, "called whoops" );
} )
.trigger( "whoops" ) // 1
.off( ".whoops" )
.trigger( "whoops" ) // 2
.off( "whoops" );
} );
QUnit.test( "Empty namespace is ignored", function( assert ) {
assert.expect( 1 );
jQuery( "#firstp" )
.on( "meow.", function( e ) {
assert.equal( e.namespace, "", "triggered a namespace-less meow event" );
} )
.trigger( "meow." )
.off( "meow." );
} );
QUnit.test( "on(), with same function", function( assert ) {
assert.expect( 2 );
var count = 0, func = function() {
count++;
};
jQuery( "#liveHandlerOrder" ).on( "foo.bar", func ).on( "foo.zar", func );
jQuery( "#liveHandlerOrder" ).trigger( "foo.bar" );
assert.equal( count, 1, "Verify binding function with multiple namespaces." );
jQuery( "#liveHandlerOrder" ).off( "foo.bar", func ).off( "foo.zar", func );
jQuery( "#liveHandlerOrder" ).trigger( "foo.bar" );
assert.equal( count, 1, "Verify that removing events still work." );
} );
QUnit.test( "on(), make sure order is maintained", function( assert ) {
assert.expect( 1 );
var elem = jQuery( "#firstp" ), log = [], check = [];
jQuery.each( new Array( 100 ), function( i ) {
elem.on( "click", function() {
log.push( i );
} );
check.push( i );
} );
elem.trigger( "click" );
assert.equal( log.join( "," ), check.join( "," ), "Make sure order was maintained." );
elem.off( "click" );
} );
QUnit.test( "on(), with different this object", function( assert ) {
assert.expect( 4 );
var thisObject = { myThis: true },
data = { myData: true },
handler1 = function() {
assert.equal( this, thisObject, "on() with different this object" );
}.bind( thisObject ),
handler2 = function( event ) {
assert.equal( this, thisObject, "on() with different this object and data" );
assert.equal( event.data, data, "on() with different this object and data" );
}.bind( thisObject );
jQuery( "#firstp" )
.on( "click", handler1 ).trigger( "click" ).off( "click", handler1 )
.on( "click", data, handler2 ).trigger( "click" ).off( "click", handler2 );
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using different this object and data." );
} );
QUnit.test( "on(name, false), off(name, false)", function( assert ) {
assert.expect( 3 );
var main = 0;
jQuery( "#qunit-fixture" ).on( "click", function() { main++; } );
jQuery( "#ap" ).trigger( "click" );
assert.equal( main, 1, "Verify that the trigger happened correctly." );
main = 0;
jQuery( "#ap" ).on( "click", false );
jQuery( "#ap" ).trigger( "click" );
assert.equal( main, 0, "Verify that no bubble happened." );
main = 0;
jQuery( "#ap" ).off( "click", false );
jQuery( "#ap" ).trigger( "click" );
assert.equal( main, 1, "Verify that the trigger happened correctly." );
// manually clean up events from elements outside the fixture
jQuery( "#qunit-fixture" ).off( "click" );
} );
QUnit.test( "on(name, selector, false), off(name, selector, false)", function( assert ) {
assert.expect( 3 );
var main = 0;
jQuery( "#qunit-fixture" ).on( "click", "#ap", function() { main++; } );
jQuery( "#ap" ).trigger( "click" );
assert.equal( main, 1, "Verify that the trigger happened correctly." );
main = 0;
jQuery( "#ap" ).on( "click", "#groups", false );
jQuery( "#groups" ).trigger( "click" );
assert.equal( main, 0, "Verify that no bubble happened." );
main = 0;
jQuery( "#ap" ).off( "click", "#groups", false );
jQuery( "#groups" ).trigger( "click" );
assert.equal( main, 1, "Verify that the trigger happened correctly." );
jQuery( "#qunit-fixture" ).off( "click", "#ap" );
} );
QUnit.test( "on()/trigger()/off() on plain object", function( assert ) {
assert.expect( 7 );
var events,
obj = {};
// Make sure it doesn't complain when no events are found
jQuery( obj ).trigger( "test" );
// Make sure it doesn't complain when no events are found
jQuery( obj ).off( "test" );
jQuery( obj ).on( {
"test": function() {
assert.ok( true, "Custom event run." );
},
"submit": function() {
assert.ok( true, "Custom submit event run." );
}
} );
events = jQuery._data( obj, "events" );
assert.ok( events, "Object has events bound." );
assert.equal( obj[ "events" ], undefined, "Events object on plain objects is not events" );
assert.equal( obj.test, undefined, "Make sure that test event is not on the plain object." );
assert.equal( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
// Should trigger 1
jQuery( obj ).trigger( "test" );
jQuery( obj ).trigger( "submit" );
jQuery( obj ).off( "test" );
jQuery( obj ).off( "submit" );
// Should trigger 0
jQuery( obj ).trigger( "test" );
// Make sure it doesn't complain when no events are found
jQuery( obj ).off( "test" );
assert.equal( obj && obj[ jQuery.expando ] &&
obj[ jQuery.expando ][ jQuery.expando ] &&
obj[ jQuery.expando ][ jQuery.expando ][ "events" ], undefined, "Make sure events object is removed" );
} );
QUnit.test( "off(type)", function( assert ) {
assert.expect( 1 );
var message, func,
$elem = jQuery( "#firstp" );
function error() {
assert.ok( false, message );
}
message = "unbind passing function";
$elem.on( "error1", error ).off( "error1", error ).triggerHandler( "error1" );
message = "unbind all from event";
$elem.on( "error1", error ).off( "error1" ).triggerHandler( "error1" );
message = "unbind all";
$elem.on( "error1", error ).off().triggerHandler( "error1" );
message = "unbind many with function";
$elem.on( "error1 error2", error )
.off( "error1 error2", error )
.trigger( "error1" ).triggerHandler( "error2" );
message = "unbind many"; // #3538
$elem.on( "error1 error2", error )
.off( "error1 error2" )
.trigger( "error1" ).triggerHandler( "error2" );
message = "unbind without a type or handler";
$elem.on( "error1 error2.test", error )
.off()
.trigger( "error1" ).triggerHandler( "error2" );
// Should only unbind the specified function
jQuery( document ).on( "click", function() {
assert.ok( true, "called handler after selective removal" );
} );
func = function() {};
jQuery( document )
.on( "click", func )
.off( "click", func )
.trigger( "click" )
.off( "click" );
} );
QUnit.test( "off(eventObject)", function( assert ) {
assert.expect( 4 );
var $elem = jQuery( "#firstp" ),
num;
function check( expected ) {
num = 0;
$elem.trigger( "foo" ).triggerHandler( "bar" );
assert.equal( num, expected, "Check the right handlers are triggered" );
}
$elem
// This handler shouldn't be unbound
.on( "foo", function() {
num += 1;
} )
.on( "foo", function( e ) {
$elem.off( e );
num += 2;
} )
// Neither this one
.on( "bar", function() {
num += 4;
} );
check( 7 );
check( 5 );
$elem.off( "bar" );
check( 1 );
$elem.off();
check( 0 );
} );
QUnit.test( "mouseover triggers mouseenter", function( assert ) {
assert.expect( 1 );
var count = 0,
elem = jQuery( "<a></a>" );
elem.on( "mouseenter", function() {
count++;
} );
elem.trigger( "mouseover" );
assert.equal( count, 1, "make sure mouseover triggers a mouseenter" );
elem.remove();
} );
QUnit.test( "pointerover triggers pointerenter", function( assert ) {
assert.expect( 1 );
var count = 0,
elem = jQuery( "<a></a>" );
elem.on( "pointerenter", function() {
count++;
} );
elem.trigger( "pointerover" );
assert.equal( count, 1, "make sure pointerover triggers a pointerenter" );
elem.remove();
} );
QUnit.test( "withinElement implemented with jQuery.contains()", function( assert ) {
assert.expect( 1 );
jQuery( "#qunit-fixture" ).append( "<div id='jc-outer'><div id='jc-inner'></div></div>" );
jQuery( "#jc-outer" ).on( "mouseenter mouseleave", function( event ) {
assert.equal( this.id, "jc-outer", this.id + " " + event.type );
} );
jQuery( "#jc-inner" ).trigger( "mouseenter" );
} );
QUnit.test( "mouseenter, mouseleave don't catch exceptions", function( assert ) {
assert.expect( 2 );
var elem = jQuery( "#firstp" ).on( "mouseenter mouseleave", function() {
throw "an Exception";
} );
try {
elem.trigger( "mouseenter" );
} catch ( e ) {
assert.equal( e, "an Exception", "mouseenter doesn't catch exceptions" );
}
try {
elem.trigger( "mouseleave" );
} catch ( e ) {
assert.equal( e, "an Exception", "mouseleave doesn't catch exceptions" );
}
} );
QUnit.test( "trigger() bubbling", function( assert ) {
assert.expect( 18 );
var win = 0, doc = 0, html = 0, body = 0, main = 0, ap = 0;
jQuery( window ).on( "click", function() { win++; } );
jQuery( document ).on( "click", function( e ) { if ( e.target !== document ) { doc++; } } );
jQuery( "html" ).on( "click", function() { html++; } );
jQuery( "body" ).on( "click", function() { body++; } );
jQuery( "#qunit-fixture" ).on( "click", function() { main++; } );
jQuery( "#ap" ).on( "click", function() { ap++; return false; } );
jQuery( "html" ).trigger( "click" );
assert.equal( win, 1, "HTML bubble" );
assert.equal( doc, 1, "HTML bubble" );
assert.equal( html, 1, "HTML bubble" );
jQuery( "body" ).trigger( "click" );
assert.equal( win, 2, "Body bubble" );
assert.equal( doc, 2, "Body bubble" );
assert.equal( html, 2, "Body bubble" );
assert.equal( body, 1, "Body bubble" );
jQuery( "#qunit-fixture" ).trigger( "click" );
assert.equal( win, 3, "Main bubble" );
assert.equal( doc, 3, "Main bubble" );
assert.equal( html, 3, "Main bubble" );
assert.equal( body, 2, "Main bubble" );
assert.equal( main, 1, "Main bubble" );
jQuery( "#ap" ).trigger( "click" );
assert.equal( doc, 3, "ap bubble" );
assert.equal( html, 3, "ap bubble" );
assert.equal( body, 2, "ap bubble" );
assert.equal( main, 1, "ap bubble" );
assert.equal( ap, 1, "ap bubble" );
jQuery( document ).trigger( "click" );
assert.equal( win, 4, "doc bubble" );
// manually clean up events from elements outside the fixture
jQuery( window ).off( "click" );
jQuery( document ).off( "click" );
jQuery( "html, body, #qunit-fixture" ).off( "click" );
} );
QUnit.test( "trigger(type, [data], [fn])", function( assert ) {
assert.expect( 16 );
var $elem, pass, form, elem2,
handler = function( event, a, b, c ) {
assert.equal( event.type, "click", "check passed data" );
assert.equal( a, 1, "check passed data" );
assert.equal( b, "2", "check passed data" );
assert.equal( c, "abc", "check passed data" );
return "test";
};
$elem = jQuery( "#firstp" );
// Simulate a "native" click
$elem[ 0 ].click = function() {
assert.ok( true, "Native call was triggered" );
};