preventScroll.html (2173B)
1 <!doctype html> 2 <title>focus(options) - preventScroll</title> 3 <script src=/resources/testharness.js></script> 4 <script src=/resources/testharnessreport.js></script> 5 <style> 6 #iframe { width: 500px; height: 500px; border: none } 7 </style> 8 <iframe id=iframe src="support/preventScroll-helper.html"></iframe> 9 <script> 10 function isEntirelyInView(elm, win) { 11 const inViewHorizontal = (elm.offsetLeft >= win.scrollX) && 12 ((elm.offsetLeft + elm.clientWidth) <= (win.scrollX + win.innerWidth)); 13 const inViewVertical = (elm.offsetTop >= win.scrollY) && 14 ((elm.offsetTop + elm.clientHeight) <= (win.scrollY + win.innerHeight)); 15 return inViewHorizontal && inViewVertical; 16 } 17 18 setup({explicit_done: true}); 19 20 function resetState(win) { 21 win.scrollTo(0, 0); 22 win.document.activeElement.blur(); 23 } 24 25 onload = () => { 26 const win = document.getElementById('iframe').contentWindow; 27 const elm = win.document.getElementById('button'); 28 29 test(() => { 30 assert_false(isEntirelyInView(elm, win), 'initial state'); 31 elm.scrollIntoView(); 32 assert_true(isEntirelyInView(elm, win), 'after elm.scrollIntoView()'); 33 resetState(win); 34 assert_false(isEntirelyInView(elm, win), 'after resetScrollPosition(win)'); 35 }, 'Sanity test'); 36 37 test(() => { 38 resetState(win); 39 elm.focus(); 40 assert_true(isEntirelyInView(elm, win)); 41 }, 'elm.focus() without arguments'); 42 43 test(() => { 44 resetState(win); 45 elm.focus(undefined); 46 assert_true(isEntirelyInView(elm, win)); 47 }, 'elm.focus(undefined)'); 48 49 test(() => { 50 resetState(win); 51 elm.focus(null); 52 assert_true(isEntirelyInView(elm, win)); 53 }, 'elm.focus(null)'); 54 55 test(() => { 56 resetState(win); 57 elm.focus({}); 58 assert_true(isEntirelyInView(elm, win)); 59 }, 'elm.focus({})'); 60 61 test(() => { 62 resetState(win); 63 elm.focus({preventScroll: false}); 64 assert_true(isEntirelyInView(elm, win)); 65 }, 'elm.focus({preventScroll: false})'); 66 67 test(() => { 68 resetState(win); 69 elm.focus({preventScroll: true}); 70 assert_equals(win.scrollX, 0); 71 assert_equals(win.scrollY, 0); 72 }, 'elm.focus({preventScroll: true})'); 73 74 done(); 75 } 76 </script>