scroll.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global window, document, Text */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import isRange from '../../src/dom/isrange';
  8. import { scrollViewportToShowTarget, scrollAncestorsToShowTarget } from '../../src/dom/scroll';
  9. testUtils.createSinonSandbox();
  10. describe( 'scrollAncestorsToShowTarget()', () => {
  11. let target, element, firstAncestor, secondAncestor;
  12. beforeEach( () => {
  13. element = document.createElement( 'p' );
  14. firstAncestor = document.createElement( 'blockquote' );
  15. secondAncestor = document.createElement( 'div' );
  16. document.body.appendChild( secondAncestor );
  17. secondAncestor.appendChild( firstAncestor );
  18. firstAncestor.appendChild( element );
  19. // Make the element immune to the border-width-* styles in the test environment.
  20. testUtils.sinon.stub( window, 'getComputedStyle' ).returns( {
  21. borderTopWidth: '0px',
  22. borderRightWidth: '0px',
  23. borderBottomWidth: '0px',
  24. borderLeftWidth: '0px'
  25. } );
  26. stubRect( firstAncestor, {
  27. top: 0, right: 100, bottom: 100, left: 0, width: 100, height: 100
  28. }, {
  29. scrollLeft: 100, scrollTop: 100
  30. } );
  31. stubRect( secondAncestor, {
  32. top: -100, right: 0, bottom: 0, left: -100, width: 100, height: 100
  33. }, {
  34. scrollLeft: 100, scrollTop: 100
  35. } );
  36. stubRect( document.body, {
  37. top: 1000, right: 2000, bottom: 1000, left: 1000, width: 1000, height: 1000
  38. }, {
  39. scrollLeft: 1000, scrollTop: 1000
  40. } );
  41. } );
  42. afterEach( () => {
  43. secondAncestor.remove();
  44. } );
  45. describe( 'for an HTMLElement', () => {
  46. beforeEach( () => {
  47. target = element;
  48. } );
  49. test();
  50. } );
  51. describe( 'for a DOM Range', () => {
  52. beforeEach( () => {
  53. target = document.createRange();
  54. target.setStart( firstAncestor, 0 );
  55. target.setEnd( firstAncestor, 0 );
  56. } );
  57. test();
  58. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (above, attached to the Text)', () => {
  59. const text = new Text( 'foo' );
  60. firstAncestor.appendChild( text );
  61. target.setStart( text, 1 );
  62. target.setEnd( text, 2 );
  63. stubRect( target, { top: -100, right: 75, bottom: 0, left: 25, width: 50, height: 100 } );
  64. scrollAncestorsToShowTarget( target );
  65. assertScrollPosition( firstAncestor, { scrollTop: 0, scrollLeft: 100 } );
  66. } );
  67. } );
  68. function test() {
  69. it( 'should not touch the #scrollTop #scrollLeft of the ancestor if target is visible', () => {
  70. stubRect( target, { top: 25, right: 75, bottom: 75, left: 25, width: 50, height: 50 } );
  71. scrollAncestorsToShowTarget( target );
  72. assertScrollPosition( firstAncestor, { scrollLeft: 100, scrollTop: 100 } );
  73. } );
  74. it( 'should not touch the #scrollTop #scrollLeft of the document.body', () => {
  75. stubRect( target, { top: 25, right: 75, bottom: 75, left: 25, width: 50, height: 50 } );
  76. scrollAncestorsToShowTarget( target );
  77. assertScrollPosition( document.body, { scrollLeft: 1000, scrollTop: 1000 } );
  78. } );
  79. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (above)', () => {
  80. stubRect( target, { top: -100, right: 75, bottom: 0, left: 25, width: 50, height: 100 } );
  81. scrollAncestorsToShowTarget( target );
  82. assertScrollPosition( firstAncestor, { scrollTop: 0, scrollLeft: 100 } );
  83. } );
  84. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (below)', () => {
  85. stubRect( target, { top: 200, right: 75, bottom: 300, left: 25, width: 50, height: 100 } );
  86. scrollAncestorsToShowTarget( target );
  87. assertScrollPosition( firstAncestor, { scrollTop: 300, scrollLeft: 100 } );
  88. } );
  89. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (left of)', () => {
  90. stubRect( target, { top: 0, right: 0, bottom: 100, left: -100, width: 100, height: 100 } );
  91. scrollAncestorsToShowTarget( target );
  92. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 0 } );
  93. } );
  94. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (right of)', () => {
  95. stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
  96. scrollAncestorsToShowTarget( target );
  97. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
  98. } );
  99. it( 'should set #scrollTop and #scrollLeft of all the ancestors', () => {
  100. stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
  101. scrollAncestorsToShowTarget( target );
  102. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
  103. // Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
  104. // the getBoundingClientRect geometry of the target. That's why scrolling secondAncestor
  105. // works like the target remained in the original position and hence scrollLeft is 300 instead
  106. // of 200.
  107. assertScrollPosition( secondAncestor, { scrollTop: 200, scrollLeft: 300 } );
  108. } );
  109. }
  110. } );
  111. describe( 'scrollViewportToShowTarget()', () => {
  112. let target, firstAncestor, element;
  113. const viewportOffset = 30;
  114. beforeEach( () => {
  115. element = document.createElement( 'p' );
  116. firstAncestor = document.createElement( 'blockquote' );
  117. document.body.appendChild( firstAncestor );
  118. firstAncestor.appendChild( element );
  119. stubRect( firstAncestor, {
  120. top: 0, right: 100, bottom: 100, left: 0, width: 100, height: 100
  121. }, {
  122. scrollLeft: 100, scrollTop: 100
  123. } );
  124. testUtils.sinon.stub( window, 'innerWidth' ).value( 1000 );
  125. testUtils.sinon.stub( window, 'innerHeight' ).value( 500 );
  126. testUtils.sinon.stub( window, 'scrollX' ).value( 100 );
  127. testUtils.sinon.stub( window, 'scrollY' ).value( 100 );
  128. testUtils.sinon.stub( window, 'scrollTo' );
  129. testUtils.sinon.stub( window, 'getComputedStyle' ).returns( {
  130. borderTopWidth: '0px',
  131. borderRightWidth: '0px',
  132. borderBottomWidth: '0px',
  133. borderLeftWidth: '0px'
  134. } );
  135. // Assuming 20px v- and h-scrollbars here.
  136. testUtils.sinon.stub( window.document, 'documentElement' ).value( {
  137. clientWidth: 980,
  138. clientHeight: 480
  139. } );
  140. } );
  141. afterEach( () => {
  142. firstAncestor.remove();
  143. } );
  144. describe( 'for an HTMLElement', () => {
  145. beforeEach( () => {
  146. target = element;
  147. } );
  148. testNoOffset();
  149. describe( 'with a viewportOffset', () => {
  150. testWithOffset();
  151. } );
  152. } );
  153. describe( 'for a DOM Range', () => {
  154. beforeEach( () => {
  155. target = document.createRange();
  156. target.setStart( firstAncestor, 0 );
  157. target.setEnd( firstAncestor, 0 );
  158. } );
  159. testNoOffset();
  160. describe( 'with a viewportOffset', () => {
  161. testWithOffset();
  162. } );
  163. } );
  164. describe( 'in an iframe', () => {
  165. let iframe, iframeWindow, iframeAncestor, target, targetAncestor;
  166. beforeEach( done => {
  167. iframe = document.createElement( 'iframe' );
  168. iframeAncestor = document.createElement( 'div' );
  169. iframe.addEventListener( 'load', () => {
  170. iframeWindow = iframe.contentWindow;
  171. testUtils.sinon.stub( iframeWindow, 'innerWidth' ).value( 1000 );
  172. testUtils.sinon.stub( iframeWindow, 'innerHeight' ).value( 500 );
  173. testUtils.sinon.stub( iframeWindow, 'scrollX' ).value( 100 );
  174. testUtils.sinon.stub( iframeWindow, 'scrollY' ).value( 100 );
  175. testUtils.sinon.stub( iframeWindow, 'scrollTo' );
  176. testUtils.sinon.stub( iframeWindow, 'getComputedStyle' ).returns( {
  177. borderTopWidth: '0px',
  178. borderRightWidth: '0px',
  179. borderBottomWidth: '0px',
  180. borderLeftWidth: '0px'
  181. } );
  182. // Assuming 20px v- and h-scrollbars here.
  183. testUtils.sinon.stub( iframeWindow.document, 'documentElement' ).value( {
  184. clientWidth: 980,
  185. clientHeight: 480
  186. } );
  187. target = iframeWindow.document.createElement( 'p' );
  188. targetAncestor = iframeWindow.document.createElement( 'div' );
  189. iframeWindow.document.body.appendChild( targetAncestor );
  190. targetAncestor.appendChild( target );
  191. done();
  192. } );
  193. iframeAncestor.appendChild( iframe );
  194. document.body.appendChild( iframeAncestor );
  195. } );
  196. afterEach( () => {
  197. // Safari fails because of "afterEach()" hook tries to restore values from removed element.
  198. // We need to restore these values manually.
  199. testUtils.sinon.restore();
  200. iframeAncestor.remove();
  201. } );
  202. it( 'does not scroll the viewport when the target is fully visible', () => {
  203. stubRect( target,
  204. { top: 100, right: 200, bottom: 200, left: 100, width: 100, height: 100 } );
  205. stubRect( targetAncestor,
  206. { top: 100, right: 300, bottom: 400, left: 0, width: 300, height: 300 },
  207. { scrollLeft: 200, scrollTop: -100 } );
  208. stubRect( iframe,
  209. { top: 200, right: 400, bottom: 400, left: 200, width: 200, height: 200 } );
  210. stubRect( iframeAncestor,
  211. { top: 0, right: 400, bottom: 400, left: 0, width: 400, height: 400 },
  212. { scrollLeft: 100, scrollTop: 100 } );
  213. scrollViewportToShowTarget( { target } );
  214. assertScrollPosition( targetAncestor, { scrollLeft: 200, scrollTop: -100 } );
  215. assertScrollPosition( iframeAncestor, { scrollTop: 100, scrollLeft: 100 } );
  216. sinon.assert.notCalled( iframeWindow.scrollTo );
  217. sinon.assert.notCalled( window.scrollTo );
  218. } );
  219. it( 'scrolls the viewport to show the target (above)', () => {
  220. stubRect( target,
  221. { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
  222. stubRect( targetAncestor,
  223. { top: 200, right: 300, bottom: 400, left: 0, width: 300, height: 100 },
  224. { scrollLeft: 200, scrollTop: -100 } );
  225. stubRect( iframe,
  226. { top: 2000, right: 2000, bottom: 2500, left: 2500, width: 500, height: 500 } );
  227. stubRect( iframeAncestor,
  228. { top: 0, right: 100, bottom: 100, left: 0, width: 100, height: 100 },
  229. { scrollLeft: 100, scrollTop: 100 } );
  230. scrollViewportToShowTarget( { target } );
  231. assertScrollPosition( targetAncestor, { scrollTop: -500, scrollLeft: 200 } );
  232. assertScrollPosition( iframeAncestor, { scrollTop: 1900, scrollLeft: 2700 } );
  233. sinon.assert.calledWithExactly( iframeWindow.scrollTo, 100, -100 );
  234. sinon.assert.calledWithExactly( window.scrollTo, 1820, 1520 );
  235. } );
  236. } );
  237. // Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
  238. // the getBoundingClientRect geometry of the target. That's why scrolling the viewport
  239. // works like the target remained in the original position. It's tricky but much faster
  240. // and still shows that the whole thing works as expected.
  241. //
  242. // Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
  243. // initial geometry and scroll position they give the right, relative picture of what's going on.
  244. function testNoOffset() {
  245. it( 'does not scroll the viewport when the target is fully visible', () => {
  246. stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
  247. scrollViewportToShowTarget( { target } );
  248. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
  249. sinon.assert.notCalled( window.scrollTo );
  250. } );
  251. it( 'scrolls the viewport to show the target (above)', () => {
  252. stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
  253. scrollViewportToShowTarget( { target } );
  254. assertScrollPosition( firstAncestor, { scrollTop: -100, scrollLeft: 200 } );
  255. sinon.assert.calledWithExactly( window.scrollTo, 100, -100 );
  256. } );
  257. it( 'scrolls the viewport to show the target (partially above)', () => {
  258. stubRect( target, { top: -50, right: 200, bottom: 50, left: 100, width: 100, height: 100 } );
  259. scrollViewportToShowTarget( { target } );
  260. assertScrollPosition( firstAncestor, { scrollTop: 50, scrollLeft: 200 } );
  261. sinon.assert.calledWithExactly( window.scrollTo, 100, 50 );
  262. } );
  263. it( 'scrolls the viewport to show the target (below)', () => {
  264. stubRect( target, { top: 600, right: 200, bottom: 700, left: 100, width: 100, height: 100 } );
  265. scrollViewportToShowTarget( { target } );
  266. assertScrollPosition( firstAncestor, { scrollTop: 700, scrollLeft: 200 } );
  267. sinon.assert.calledWithExactly( window.scrollTo, 100, 320 );
  268. } );
  269. it( 'scrolls the viewport to show the target (partially below)', () => {
  270. stubRect( target, { top: 450, right: 200, bottom: 550, left: 100, width: 100, height: 100 } );
  271. scrollViewportToShowTarget( { target } );
  272. assertScrollPosition( firstAncestor, { scrollTop: 550, scrollLeft: 200 } );
  273. sinon.assert.calledWithExactly( window.scrollTo, 100, 170 );
  274. } );
  275. it( 'scrolls the viewport to show the target (to the left)', () => {
  276. stubRect( target, { top: 0, right: -100, bottom: 100, left: -200, width: 100, height: 100 } );
  277. scrollViewportToShowTarget( { target } );
  278. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: -100 } );
  279. sinon.assert.calledWithExactly( window.scrollTo, -100, 100 );
  280. } );
  281. it( 'scrolls the viewport to show the target (partially to the left)', () => {
  282. stubRect( target, { top: 0, right: 50, bottom: 100, left: -50, width: 100, height: 100 } );
  283. scrollViewportToShowTarget( { target } );
  284. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 50 } );
  285. sinon.assert.calledWithExactly( window.scrollTo, 50, 100 );
  286. } );
  287. it( 'scrolls the viewport to show the target (to the right)', () => {
  288. stubRect( target, { top: 0, right: 1200, bottom: 100, left: 1100, width: 100, height: 100 } );
  289. scrollViewportToShowTarget( { target } );
  290. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1200 } );
  291. sinon.assert.calledWithExactly( window.scrollTo, 320, 100 );
  292. } );
  293. it( 'scrolls the viewport to show the target (partially to the right)', () => {
  294. stubRect( target, { top: 0, right: 1050, bottom: 100, left: 950, width: 100, height: 100 } );
  295. scrollViewportToShowTarget( { target } );
  296. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1050 } );
  297. sinon.assert.calledWithExactly( window.scrollTo, 170, 100 );
  298. } );
  299. }
  300. // Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
  301. // the getBoundingClientRect geometry of the target. That's why scrolling the viewport
  302. // works like the target remained in the original position. It's tricky but much faster
  303. // and still shows that the whole thing works as expected.
  304. //
  305. // Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
  306. // initial geometry and scroll position they give the right, relative picture of what's going on.
  307. function testWithOffset() {
  308. it( 'does not scroll the viewport when the target is fully visible', () => {
  309. stubRect( target, { top: 50, right: 200, bottom: 150, left: 100, width: 100, height: 100 } );
  310. scrollViewportToShowTarget( { target, viewportOffset } );
  311. assertScrollPosition( firstAncestor, { scrollTop: 150, scrollLeft: 200 } );
  312. sinon.assert.notCalled( window.scrollTo );
  313. } );
  314. it( 'scrolls the viewport to show the target (above)', () => {
  315. stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
  316. scrollViewportToShowTarget( { target, viewportOffset } );
  317. assertScrollPosition( firstAncestor, { scrollTop: -100, scrollLeft: 200 } );
  318. sinon.assert.calledWithExactly( window.scrollTo, 100, -130 );
  319. } );
  320. it( 'scrolls the viewport to show the target (partially above)', () => {
  321. stubRect( target, { top: -50, right: 200, bottom: 50, left: 100, width: 100, height: 100 } );
  322. scrollViewportToShowTarget( { target, viewportOffset } );
  323. assertScrollPosition( firstAncestor, { scrollTop: 50, scrollLeft: 200 } );
  324. sinon.assert.calledWithExactly( window.scrollTo, 100, 20 );
  325. } );
  326. it( 'scrolls the viewport to show the target (below)', () => {
  327. stubRect( target, { top: 600, right: 200, bottom: 700, left: 100, width: 100, height: 100 } );
  328. scrollViewportToShowTarget( { target, viewportOffset } );
  329. assertScrollPosition( firstAncestor, { scrollTop: 700, scrollLeft: 200 } );
  330. sinon.assert.calledWithExactly( window.scrollTo, 100, 350 );
  331. } );
  332. it( 'scrolls the viewport to show the target (partially below)', () => {
  333. stubRect( target, { top: 450, right: 200, bottom: 550, left: 100, width: 100, height: 100 } );
  334. scrollViewportToShowTarget( { target, viewportOffset } );
  335. assertScrollPosition( firstAncestor, { scrollTop: 550, scrollLeft: 200 } );
  336. sinon.assert.calledWithExactly( window.scrollTo, 100, 200 );
  337. } );
  338. it( 'scrolls the viewport to show the target (to the left)', () => {
  339. stubRect( target, { top: 0, right: -100, bottom: 100, left: -200, width: 100, height: 100 } );
  340. scrollViewportToShowTarget( { target, viewportOffset } );
  341. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: -100 } );
  342. sinon.assert.calledWithExactly( window.scrollTo, -130, 70 );
  343. } );
  344. it( 'scrolls the viewport to show the target (partially to the left)', () => {
  345. stubRect( target, { top: 0, right: 50, bottom: 100, left: -50, width: 100, height: 100 } );
  346. scrollViewportToShowTarget( { target, viewportOffset } );
  347. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 50 } );
  348. sinon.assert.calledWithExactly( window.scrollTo, 20, 70 );
  349. } );
  350. it( 'scrolls the viewport to show the target (to the right)', () => {
  351. stubRect( target, { top: 0, right: 1200, bottom: 100, left: 1100, width: 100, height: 100 } );
  352. scrollViewportToShowTarget( { target, viewportOffset } );
  353. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1200 } );
  354. sinon.assert.calledWithExactly( window.scrollTo, 350, 70 );
  355. } );
  356. it( 'scrolls the viewport to show the target (partially to the right)', () => {
  357. stubRect( target, { top: 0, right: 1050, bottom: 100, left: 950, width: 100, height: 100 } );
  358. scrollViewportToShowTarget( { target, viewportOffset } );
  359. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1050 } );
  360. sinon.assert.calledWithExactly( window.scrollTo, 200, 70 );
  361. } );
  362. }
  363. } );
  364. function stubRect( target, geometryStub, scrollStub ) {
  365. if ( isRange( target ) ) {
  366. testUtils.sinon.stub( target, 'getClientRects' ).returns( [ geometryStub ] );
  367. } else {
  368. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( geometryStub );
  369. // Make the element immune to the border-width-* styles in the test environment.
  370. Object.defineProperties( target, {
  371. offsetWidth: {
  372. value: geometryStub.width
  373. },
  374. clientWidth: {
  375. value: geometryStub.width
  376. },
  377. offsetHeight: {
  378. value: geometryStub.height
  379. },
  380. clientHeight: {
  381. value: geometryStub.height
  382. }
  383. } );
  384. }
  385. if ( scrollStub ) {
  386. let { scrollLeft, scrollTop } = scrollStub;
  387. // There's no way to stub scrollLeft|Top with Sinon. defineProperties
  388. // must be used instead.
  389. Object.defineProperties( target, {
  390. scrollLeft: {
  391. get() {
  392. return scrollLeft;
  393. },
  394. set( value ) {
  395. scrollLeft = value;
  396. },
  397. configurable: true
  398. },
  399. scrollTop: {
  400. get() {
  401. return scrollTop;
  402. },
  403. set( value ) {
  404. scrollTop = value;
  405. },
  406. configurable: true
  407. }
  408. } );
  409. }
  410. }
  411. function assertScrollPosition( element, expected ) {
  412. expect( element.scrollTop ).to.equal( expected.scrollTop, 'scrollTop' );
  413. expect( element.scrollLeft ).to.equal( expected.scrollLeft, 'scrollLeft' );
  414. }