scroll.js 19 KB

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