Debian Codegraph @

tests/unit/position/core.js

raw symbols SCIP
1 define( [
2 "qunit",
3 "jquery",
4 "lib/common",
5 "lib/helper",
6 "ui/position"
7 ], function( QUnit, $, common, helper ) {
8 "use strict";
9
10 var win = $( window ),
11 scrollTopSupport = function() {
12 var support = win.scrollTop( 1 ).scrollTop() === 1;
13 win.scrollTop( 0 );
14 scrollTopSupport = function() {
15 return support;
16 };
17 return support;
18 };
19
20 QUnit.module( "position", {
21 beforeEach: function() {
22 win.scrollTop( 0 ).scrollLeft( 0 );
23 },
24 afterEach: helper.moduleAfterEach
25 } );
26
27 QUnit.test( "my, at, of", function( assert ) {
28 assert.expect( 4 );
29
30 $( "#elx" ).position( {
31 my: "left top",
32 at: "left top",
33 of: "#parentx",
34 collision: "none"
35 } );
36 assert.deepEqual( $( "#elx" ).offset(), { top: 40, left: 40 }, "left top, left top" );
37
38 $( "#elx" ).position( {
39 my: "left top",
40 at: "left bottom",
41 of: "#parentx",
42 collision: "none"
43 } );
44 assert.deepEqual( $( "#elx" ).offset(), { top: 60, left: 40 }, "left top, left bottom" );
45
46 $( "#elx" ).position( {
47 my: "left",
48 at: "bottom",
49 of: "#parentx",
50 collision: "none"
51 } );
52 assert.deepEqual( $( "#elx" ).offset(), { top: 55, left: 50 }, "left, bottom" );
53
54 $( "#elx" ).position( {
55 my: "left foo",
56 at: "bar baz",
57 of: "#parentx",
58 collision: "none"
59 } );
60 assert.deepEqual( $( "#elx" ).offset(), { top: 45, left: 50 }, "left foo, bar baz" );
61 } );
62
63 QUnit.test( "multiple elements", function( assert ) {
64 assert.expect( 3 );
65
66 var elements = $( "#el1, #el2" ),
67 result = elements.position( {
68 my: "left top",
69 at: "left bottom",
70 of: "#parent",
71 collision: "none"
72 } ),
73 expected = { top: 10, left: 4 };
74
75 assert.deepEqual( result, elements );
76 elements.each( function() {
77 assert.deepEqual( $( this ).offset(), expected );
78 } );
79 } );
80
81 QUnit.test( "positions", function( assert ) {
82 assert.expect( 18 );
83
84 var offsets = {
85 left: 0,
86 center: 3,
87 right: 6,
88 top: 0,
89 bottom: 6
90 },
91 start = { left: 4, top: 4 },
92 el = $( "#el1" );
93
94 $.each( [ 0, 1 ], function( my ) {
95 $.each( [ "top", "center", "bottom" ], function( vindex, vertical ) {
96 $.each( [ "left", "center", "right" ], function( hindex, horizontal ) {
97 var _my = my ? horizontal + " " + vertical : "left top",
98 _at = !my ? horizontal + " " + vertical : "left top";
99 el.position( {
100 my: _my,
101 at: _at,
102 of: "#parent",
103 collision: "none"
104 } );
105 assert.deepEqual( el.offset(), {
106 top: start.top + offsets[ vertical ] * ( my ? -1 : 1 ),
107 left: start.left + offsets[ horizontal ] * ( my ? -1 : 1 )
108 }, "Position via " + QUnit.dump.parse( { my: _my, at: _at } ) );
109 } );
110 } );
111 } );
112 } );
113
114 QUnit.test( "of", function( assert ) {
115 assert.expect( 10 + ( scrollTopSupport() ? 1 : 0 ) );
116
117 var done = assert.async();
118
119 var event;
120
121 $( "#elx" ).position( {
122 my: "left top",
123 at: "left top",
124 of: "#parentx",
125 collision: "none"
126 } );
127 assert.deepEqual( $( "#elx" ).offset(), { top: 40, left: 40 }, "selector" );
128
129 $( "#elx" ).position( {
130 my: "left top",
131 at: "left bottom",
132 of: $( "#parentx" ),
133 collision: "none"
134 } );
135 assert.deepEqual( $( "#elx" ).offset(), { top: 60, left: 40 }, "jQuery object" );
136
137 $( "#elx" ).position( {
138 my: "left top",
139 at: "left top",
140 of: $( "#parentx" )[ 0 ],
141 collision: "none"
142 } );
143 assert.deepEqual( $( "#elx" ).offset(), { top: 40, left: 40 }, "DOM element" );
144
145 $( "#elx" ).position( {
146 my: "right bottom",
147 at: "right bottom",
148 of: document,
149 collision: "none"
150 } );
151 assert.deepEqual( $( "#elx" ).offset(), {
152 top: $( document ).height() - 10,
153 left: $( document ).width() - 10
154 }, "document" );
155
156 $( "#elx" ).position( {
157 my: "right bottom",
158 at: "right bottom",
159 of: $( document ),
160 collision: "none"
161 } );
162 assert.deepEqual( $( "#elx" ).offset(), {
163 top: $( document ).height() - 10,
164 left: $( document ).width() - 10
165 }, "document as jQuery object" );
166
167 win.scrollTop( 0 );
168
169 $( "#elx" ).position( {
170 my: "right bottom",
171 at: "right bottom",
172 of: window,
173 collision: "none"
174 } );
175 assert.deepEqual( $( "#elx" ).offset(), {
176 top: win.height() - 10,
177 left: win.width() - 10
178 }, "window" );
179
180 $( "#elx" ).position( {
181 my: "right bottom",
182 at: "right bottom",
183 of: win,
184 collision: "none"
185 } );
186 assert.deepEqual( $( "#elx" ).offset(), {
187 top: win.height() - 10,
188 left: win.width() - 10
189 }, "window as jQuery object" );
190
191 if ( scrollTopSupport() ) {
192 win.scrollTop( 500 ).scrollLeft( 200 );
193 $( "#elx" ).position( {
194 my: "right bottom",
195 at: "right bottom",
196 of: window,
197 collision: "none"
198 } );
199 assert.deepEqual( $( "#elx" ).offset(), {
200 top: win.height() + 500 - 10,
201 left: win.width() + 200 - 10
202 }, "window, scrolled" );
203 win.scrollTop( 0 ).scrollLeft( 0 );
204 }
205
206 event = $.extend( $.Event( "someEvent" ), { pageX: 200, pageY: 300 } );
207 $( "#elx" ).position( {
208 my: "left top",
209 at: "left top",
210 of: event,
211 collision: "none"
212 } );
213 assert.deepEqual( $( "#elx" ).offset(), {
214 top: 300,
215 left: 200
216 }, "event - left top, left top" );
217
218 event = $.extend( $.Event( "someEvent" ), { pageX: 400, pageY: 600 } );
219 $( "#elx" ).position( {
220 my: "left top",
221 at: "right bottom",
222 of: event,
223 collision: "none"
224 } );
225 assert.deepEqual( $( "#elx" ).offset(), {
226 top: 600,
227 left: 400
228 }, "event - left top, right bottom" );
229
230 try {
231 $( "#elx" ).position( {
232 my: "left top",
233 at: "right bottom",
234 of: "<img onerror='window.globalOf=true' src='/404' />",
235 collision: "none"
236 } );
237 } catch ( e ) {}
238
239 setTimeout( function() {
240 assert.equal( window.globalOf, undefined, "of treated as a selector" );
241 delete window.globalOf;
242 done();
243 }, 500 );
244 } );
245
246 QUnit.test( "offsets", function( assert ) {
247 assert.expect( 9 );
248
249 var offset;
250
251 $( "#elx" ).position( {
252 my: "left top",
253 at: "left+10 bottom+10",
254 of: "#parentx",
255 collision: "none"
256 } );
257 assert.deepEqual( $( "#elx" ).offset(), { top: 70, left: 50 }, "offsets in at" );
258
259 $( "#elx" ).position( {
260 my: "left+10 top-10",
261 at: "left bottom",
262 of: "#parentx",
263 collision: "none"
264 } );
265 assert.deepEqual( $( "#elx" ).offset(), { top: 50, left: 50 }, "offsets in my" );
266
267 $( "#elx" ).position( {
268 my: "left top",
269 at: "left+50% bottom-10%",
270 of: "#parentx",
271 collision: "none"
272 } );
273 assert.deepEqual( $( "#elx" ).offset(), { top: 58, left: 50 }, "percentage offsets in at" );
274
275 $( "#elx" ).position( {
276 my: "left-30% top+50%",
277 at: "left bottom",
278 of: "#parentx",
279 collision: "none"
280 } );
281 assert.deepEqual( $( "#elx" ).offset(), { top: 65, left: 37 }, "percentage offsets in my" );
282
283 $( "#elx" ).position( {
284 my: "left-30.001% top+50.0%",
285 at: "left bottom",
286 of: "#parentx",
287 collision: "none"
288 } );
289 offset = $( "#elx" ).offset();
290 assert.equal( Math.round( offset.top ), 65, "decimal percentage offsets in my" );
291 assert.equal( Math.round( offset.left ), 37, "decimal percentage offsets in my" );
292
293 $( "#elx" ).position( {
294 my: "left+10.4 top-10.6",
295 at: "left bottom",
296 of: "#parentx",
297 collision: "none"
298 } );
299 offset = $( "#elx" ).offset();
300 assert.equal( Math.round( offset.top ), 49, "decimal offsets in my" );
301 assert.equal( Math.round( offset.left ), 50, "decimal offsets in my" );
302
303 $( "#elx" ).position( {
304 my: "left+right top-left",
305 at: "left-top bottom-bottom",
306 of: "#parentx",
307 collision: "none"
308 } );
309 assert.deepEqual( $( "#elx" ).offset(), { top: 60, left: 40 }, "invalid offsets" );
310 } );
311
312 QUnit.test( "using", function( assert ) {
313 assert.expect( 10 );
314
315 var count = 0,
316 elems = $( "#el1, #el2" ),
317 of = $( "#parentx" ),
318 expectedPosition = { top: 60, left: 60 },
319 expectedFeedback = {
320 target: {
321 element: of,
322 width: 20,
323 height: 20,
324 left: 40,
325 top: 40
326 },
327 element: {
328 width: 6,
329 height: 6,
330 left: 60,
331 top: 60
332 },
333 horizontal: "left",
334 vertical: "top",
335 important: "vertical"
336 },
337 originalPosition = elems.position( {
338 my: "right bottom",
339 at: "rigt bottom",
340 of: "#parentx",
341 collision: "none"
342 } ).offset();
343
344 elems.position( {
345 my: "left top",
346 at: "center+10 bottom",
347 of: "#parentx",
348 using: function( position, feedback ) {
349 assert.deepEqual( this, elems[ count ], "correct context for call #" + count );
350 assert.deepEqual( position, expectedPosition, "correct position for call #" + count );
351 assert.deepEqual( feedback.element.element[ 0 ], elems[ count ] );
352 delete feedback.element.element;
353 delete feedback.target.element.prevObject;
354 assert.deepEqual( feedback, expectedFeedback );
355 count++;
356 }
357 } );
358
359 elems.each( function() {
360 assert.deepEqual( $( this ).offset(), originalPosition, "elements not moved" );
361 } );
362 } );
363
364 function collisionTest( assert, config, result, msg ) {
365 var elem = $( "#elx" ).position( $.extend( {
366 my: "left top",
367 at: "right bottom",
368 of: "#parent"
369 }, config ) );
370 assert.deepEqual( elem.offset(), result, msg );
371 }
372
373 function collisionTest2( assert, config, result, msg ) {
374 collisionTest( assert, $.extend( {
375 my: "right bottom",
376 at: "left top"
377 }, config ), result, msg );
378 }
379
380 QUnit.test( "collision: fit, no collision", function( assert ) {
381 assert.expect( 2 );
382
383 collisionTest( assert, {
384 collision: "fit"
385 }, {
386 top: 10,
387 left: 10
388 }, "no offset" );
389
390 collisionTest( assert, {
391 collision: "fit",
392 at: "right+2 bottom+3"
393 }, {
394 top: 13,
395 left: 12
396 }, "with offset" );
397 } );
398
399 QUnit.test( "collision: fit, collision", function( assert ) {
400 assert.expect( 2 + ( scrollTopSupport() ? 1 : 0 ) );
401
402 collisionTest2( assert, {
403 collision: "fit"
404 }, {
405 top: 0,
406 left: 0
407 }, "no offset" );
408
409 collisionTest2( assert, {
410 collision: "fit",
411 at: "left+2 top+3"
412 }, {
413 top: 0,
414 left: 0
415 }, "with offset" );
416
417 if ( scrollTopSupport() ) {
418 win.scrollTop( 300 ).scrollLeft( 200 );
419 collisionTest( assert, {
420 collision: "fit"
421 }, {
422 top: 300,
423 left: 200
424 }, "window scrolled" );
425
426 win.scrollTop( 0 ).scrollLeft( 0 );
427 }
428 } );
429
430 QUnit.test( "collision: flip, no collision", function( assert ) {
431 assert.expect( 2 );
432
433 collisionTest( assert, {
434 collision: "flip"
435 }, {
436 top: 10,
437 left: 10
438 }, "no offset" );
439
440 collisionTest( assert, {
441 collision: "flip",
442 at: "right+2 bottom+3"
443 }, {
444 top: 13,
445 left: 12
446 }, "with offset" );
447 } );
448
449 QUnit.test( "collision: flip, collision", function( assert ) {
450 assert.expect( 2 );
451
452 collisionTest2( assert, {
453 collision: "flip"
454 }, {
455 top: 10,
456 left: 10
457 }, "no offset" );
458
459 collisionTest2( assert, {
460 collision: "flip",
461 at: "left+2 top+3"
462 }, {
463 top: 7,
464 left: 8
465 }, "with offset" );
466 } );
467
468 QUnit.test( "collision: flipfit, no collision", function( assert ) {
469 assert.expect( 2 );
470
471 collisionTest( assert, {
472 collision: "flipfit"
473 }, {
474 top: 10,
475 left: 10
476 }, "no offset" );
477
478 collisionTest( assert, {
479 collision: "flipfit",
480 at: "right+2 bottom+3"
481 }, {
482 top: 13,
483 left: 12
484 }, "with offset" );
485 } );
486
487 QUnit.test( "collision: flipfit, collision", function( assert ) {
488 assert.expect( 2 );
489
490 collisionTest2( assert, {
491 collision: "flipfit"
492 }, {
493 top: 10,
494 left: 10
495 }, "no offset" );
496
497 collisionTest2( assert, {
498 collision: "flipfit",
499 at: "left+2 top+3"
500 }, {
501 top: 7,
502 left: 8
503 }, "with offset" );
504 } );
505
506 QUnit.test( "collision: none, no collision", function( assert ) {
507 assert.expect( 2 );
508
509 collisionTest( assert, {
510 collision: "none"
511 }, {
512 top: 10,
513 left: 10
514 }, "no offset" );
515
516 collisionTest( assert, {
517 collision: "none",
518 at: "right+2 bottom+3"
519 }, {
520 top: 13,
521 left: 12
522 }, "with offset" );
523 } );
524
525 QUnit.test( "collision: none, collision", function( assert ) {
526 assert.expect( 2 );
527
528 collisionTest2( assert, {
529 collision: "none"
530 }, {
531 top: -6,
532 left: -6
533 }, "no offset" );
534
535 collisionTest2( assert, {
536 collision: "none",
537 at: "left+2 top+3"
538 }, {
539 top: -3,
540 left: -4
541 }, "with offset" );
542 } );
543
544 QUnit.test( "collision: fit, with margin", function( assert ) {
545 assert.expect( 2 );
546
547 $( "#elx" ).css( {
548 marginTop: 6,
549 marginLeft: 4
550 } );
551
552 collisionTest( assert, {
553 collision: "fit"
554 }, {
555 top: 10,
556 left: 10
557 }, "right bottom" );
558
559 collisionTest2( assert, {
560 collision: "fit"
561 }, {
562 top: 6,
563 left: 4
564 }, "left top" );
565 } );
566
567 QUnit.test( "collision: flip, with margin", function( assert ) {
568 assert.expect( 3 );
569
570 $( "#elx" ).css( {
571 marginTop: 6,
572 marginLeft: 4
573 } );
574
575 collisionTest( assert, {
576 collision: "flip"
577 }, {
578 top: 10,
579 left: 10
580 }, "left top" );
581
582 collisionTest2( assert, {
583 collision: "flip"
584 }, {
585 top: 10,
586 left: 10
587 }, "right bottom" );
588
589 collisionTest2( assert, {
590 collision: "flip",
591 my: "left top"
592 }, {
593 top: 0,
594 left: 4
595 }, "right bottom" );
596 } );
597
598 QUnit.test( "within", function( assert ) {
599 assert.expect( 7 );
600
601 collisionTest( assert, {
602 within: document
603 }, {
604 top: 10,
605 left: 10
606 }, "within document" );
607
608 collisionTest( assert, {
609 within: "#within",
610 collision: "fit"
611 }, {
612 top: 4,
613 left: 2
614 }, "fit - right bottom" );
615
616 collisionTest2( assert, {
617 within: "#within",
618 collision: "fit"
619 }, {
620 top: 2,
621 left: 0
622 }, "fit - left top" );
623
624 collisionTest( assert, {
625 within: "#within",
626 collision: "flip"
627 }, {
628 top: 10,
629 left: -6
630 }, "flip - right bottom" );
631
632 collisionTest2( assert, {
633 within: "#within",
634 collision: "flip"
635 }, {
636 top: 10,
637 left: -6
638 }, "flip - left top" );
639
640 collisionTest( assert, {
641 within: "#within",
642 collision: "flipfit"
643 }, {
644 top: 4,
645 left: 0
646 }, "flipfit - right bottom" );
647
648 collisionTest2( assert, {
649 within: "#within",
650 collision: "flipfit"
651 }, {
652 top: 4,
653 left: 0
654 }, "flipfit - left top" );
655 } );
656
657 // jQuery 3.2 incorrectly handle scrollbars in WebKit/Blink-based browsers.
658 // This is fixed in version 3.3, see https://github.com/jquery/jquery/issues/3589.
659 // As the data here comes from jQuery directly and the changes to fix it
660 // are non-trivial: https://github.com/jquery/jquery/pull/3656, just accept
661 // that scrollbar data in this jQuery version is inaccurate.
662 QUnit[ jQuery.fn.jquery.substring( 0, 4 ) === "3.2." ? "skip" : "test" ](
663 "with scrollbars", function( assert ) {
664 assert.expect( 4 );
665
666 $( "#scrollx" ).css( {
667 width: 100,
668 height: 100,
669 left: 0,
670 top: 0
671 } );
672
673 collisionTest( assert, {
674 of: "#scrollx",
675 collision: "fit",
676 within: "#scrollx"
677 }, {
678 top: 90,
679 left: 90
680 }, "visible" );
681
682 $( "#scrollx" ).css( {
683 overflow: "scroll"
684 } );
685
686 var scrollbarInfo = $.position.getScrollInfo( $.position.getWithinInfo( $( "#scrollx" ) ) );
687
688 collisionTest( assert, {
689 of: "#scrollx",
690 collision: "fit",
691 within: "#scrollx"
692 }, {
693 top: 90 - scrollbarInfo.height,
694 left: 90 - scrollbarInfo.width
695 }, "scroll" );
696
697 $( "#scrollx" ).css( {
698 overflow: "auto"
699 } );
700
701 collisionTest( assert, {
702 of: "#scrollx",
703 collision: "fit",
704 within: "#scrollx"
705 }, {
706 top: 90,
707 left: 90
708 }, "auto, no scroll" );
709
710 $( "#scrollx" ).css( {
711 overflow: "auto"
712 } ).append( $( "<div>" ).height( 300 ).width( 300 ) );
713
714 collisionTest( assert, {
715 of: "#scrollx",
716 collision: "fit",
717 within: "#scrollx"
718 }, {
719 top: 90 - scrollbarInfo.height,
720 left: 90 - scrollbarInfo.width
721 }, "auto, with scroll" );
722 } );
723
724 QUnit.test( "fractions", function( assert ) {
725 assert.expect( 1 );
726
727 $( "#fractions-element" ).position( {
728 my: "left top",
729 at: "left top",
730 of: "#fractions-parent",
731 collision: "none"
732 } );
733 assert.deepEqual( $( "#fractions-element" ).offset(), $( "#fractions-parent" ).offset(), "left top, left top" );
734 } );
735
736 QUnit.test( "bug #5280: consistent results (avoid fractional values)", function( assert ) {
737 assert.expect( 1 );
738
739 var wrapper = $( "#bug-5280" ),
740 elem = wrapper.children(),
741 offset1 = elem.position( {
742 my: "center",
743 at: "center",
744 of: wrapper,
745 collision: "none"
746 } ).offset(),
747 offset2 = elem.position( {
748 my: "center",
749 at: "center",
750 of: wrapper,
751 collision: "none"
752 } ).offset();
753 assert.deepEqual( offset1, offset2 );
754 } );
755
756 QUnit.test( "bug #8710: flip if flipped position fits more", function( assert ) {
757 assert.expect( 3 );
758
759 // Positions a 10px tall element within 99px height at top 90px.
760 collisionTest( assert, {
761 within: "#bug-8710-within-smaller",
762 of: "#parentx",
763 collision: "flip",
764 at: "right bottom+30"
765 }, {
766 top: 0,
767 left: 60
768 }, "flip - top fits all" );
769
770 // Positions a 10px tall element within 99px height at top 92px.
771 collisionTest( assert, {
772 within: "#bug-8710-within-smaller",
773 of: "#parentx",
774 collision: "flip",
775 at: "right bottom+32"
776 }, {
777 top: -2,
778 left: 60
779 }, "flip - top fits more" );
780
781 // Positions a 10px tall element within 101px height at top 92px.
782 collisionTest( assert, {
783 within: "#bug-8710-within-bigger",
784 of: "#parentx",
785 collision: "flip",
786 at: "right bottom+32"
787 }, {
788 top: 92,
789 left: 60
790 }, "no flip - top fits less" );
791 } );
792
793 } );