8
0

scroll.js 19 KB

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