scroll.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, Text */
  6. import global from '../../src/dom/global';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import isRange from '../../src/dom/isrange';
  9. import {
  10. scrollViewportToShowTarget,
  11. scrollAncestorsToShowTarget
  12. } from '../../src/dom/scroll';
  13. testUtils.createSinonSandbox();
  14. describe( 'scrollAncestorsToShowTarget()', () => {
  15. let target, element, firstAncestor, secondAncestor, body;
  16. beforeEach( () => {
  17. element = document.createElement( 'p' );
  18. firstAncestor = document.createElement( 'blockquote' );
  19. secondAncestor = document.createElement( 'div' );
  20. body = document.createElement( 'div' );
  21. testUtils.sinon.stub( global, 'document', { body } );
  22. document.body.appendChild( body );
  23. body.appendChild( secondAncestor );
  24. secondAncestor.appendChild( firstAncestor );
  25. firstAncestor.appendChild( element );
  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( 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. body.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( 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( global, 'window', {
  125. innerWidth: 1000,
  126. innerHeight: 500,
  127. scrollX: 100,
  128. scrollY: 100,
  129. scrollTo: sinon.spy(),
  130. getComputedStyle: () => ( {
  131. borderTopWidth: '0px',
  132. borderRightWidth: '0px',
  133. borderBottomWidth: '0px',
  134. borderLeftWidth: '0px'
  135. } )
  136. } );
  137. // Assuming 20px v- and h-scrollbars here.
  138. testUtils.sinon.stub( global, 'document', {
  139. body: document.body,
  140. documentElement: {
  141. clientWidth: 980,
  142. clientHeight: 480
  143. }
  144. } );
  145. } );
  146. afterEach( () => {
  147. firstAncestor.remove();
  148. } );
  149. describe( 'for an HTMLElement', () => {
  150. beforeEach( () => {
  151. target = element;
  152. } );
  153. testNoOffset();
  154. describe( 'with a viewportOffset', () => {
  155. testWithOffset();
  156. } );
  157. } );
  158. describe( 'for a DOM Range', () => {
  159. beforeEach( () => {
  160. target = document.createRange();
  161. target.setStart( firstAncestor, 0 );
  162. target.setEnd( firstAncestor, 0 );
  163. } );
  164. testNoOffset();
  165. describe( 'with a viewportOffset', () => {
  166. testWithOffset();
  167. } );
  168. } );
  169. // Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
  170. // the getBoundingClientRect geometry of the target. That's why scrolling the viewport
  171. // works like the target remained in the original position. It's tricky but much faster
  172. // and still shows that the whole thing works as expected.
  173. //
  174. // Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
  175. // initial geometry and scroll position they give the right, relative picture of what's going on.
  176. function testNoOffset() {
  177. it( 'does not scroll the viewport when the target is fully visible', () => {
  178. stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
  179. scrollViewportToShowTarget( { target } );
  180. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
  181. sinon.assert.notCalled( global.window.scrollTo );
  182. } );
  183. it( 'scrolls the viewport to show the target (above)', () => {
  184. stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
  185. scrollViewportToShowTarget( { target } );
  186. assertScrollPosition( firstAncestor, { scrollTop: -100, scrollLeft: 200 } );
  187. sinon.assert.calledWithExactly( global.window.scrollTo, 100, -100 );
  188. } );
  189. it( 'scrolls the viewport to show the target (partially above)', () => {
  190. stubRect( target, { top: -50, right: 200, bottom: 50, left: 100, width: 100, height: 100 } );
  191. scrollViewportToShowTarget( { target } );
  192. assertScrollPosition( firstAncestor, { scrollTop: 50, scrollLeft: 200 } );
  193. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 50 );
  194. } );
  195. it( 'scrolls the viewport to show the target (below)', () => {
  196. stubRect( target, { top: 600, right: 200, bottom: 700, left: 100, width: 100, height: 100 } );
  197. scrollViewportToShowTarget( { target } );
  198. assertScrollPosition( firstAncestor, { scrollTop: 700, scrollLeft: 200 } );
  199. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 320 );
  200. } );
  201. it( 'scrolls the viewport to show the target (partially below)', () => {
  202. stubRect( target, { top: 450, right: 200, bottom: 550, left: 100, width: 100, height: 100 } );
  203. scrollViewportToShowTarget( { target } );
  204. assertScrollPosition( firstAncestor, { scrollTop: 550, scrollLeft: 200 } );
  205. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 170 );
  206. } );
  207. it( 'scrolls the viewport to show the target (to the left)', () => {
  208. stubRect( target, { top: 0, right: -100, bottom: 100, left: -200, width: 100, height: 100 } );
  209. scrollViewportToShowTarget( { target } );
  210. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: -100 } );
  211. sinon.assert.calledWithExactly( global.window.scrollTo, -100, 100 );
  212. } );
  213. it( 'scrolls the viewport to show the target (partially to the left)', () => {
  214. stubRect( target, { top: 0, right: 50, bottom: 100, left: -50, width: 100, height: 100 } );
  215. scrollViewportToShowTarget( { target } );
  216. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 50 } );
  217. sinon.assert.calledWithExactly( global.window.scrollTo, 50, 100 );
  218. } );
  219. it( 'scrolls the viewport to show the target (to the right)', () => {
  220. stubRect( target, { top: 0, right: 1200, bottom: 100, left: 1100, width: 100, height: 100 } );
  221. scrollViewportToShowTarget( { target } );
  222. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1200 } );
  223. sinon.assert.calledWithExactly( global.window.scrollTo, 320, 100 );
  224. } );
  225. it( 'scrolls the viewport to show the target (partially to the right)', () => {
  226. stubRect( target, { top: 0, right: 1050, bottom: 100, left: 950, width: 100, height: 100 } );
  227. scrollViewportToShowTarget( { target } );
  228. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1050 } );
  229. sinon.assert.calledWithExactly( global.window.scrollTo, 170, 100 );
  230. } );
  231. }
  232. // Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
  233. // the getBoundingClientRect geometry of the target. That's why scrolling the viewport
  234. // works like the target remained in the original position. It's tricky but much faster
  235. // and still shows that the whole thing works as expected.
  236. //
  237. // Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
  238. // initial geometry and scroll position they give the right, relative picture of what's going on.
  239. function testWithOffset() {
  240. it( 'does not scroll the viewport when the target is fully visible', () => {
  241. stubRect( target, { top: 50, right: 200, bottom: 150, left: 100, width: 100, height: 100 } );
  242. scrollViewportToShowTarget( { target, viewportOffset } );
  243. assertScrollPosition( firstAncestor, { scrollTop: 150, scrollLeft: 200 } );
  244. sinon.assert.notCalled( global.window.scrollTo );
  245. } );
  246. it( 'scrolls the viewport to show the target (above)', () => {
  247. stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
  248. scrollViewportToShowTarget( { target, viewportOffset } );
  249. assertScrollPosition( firstAncestor, { scrollTop: -100, scrollLeft: 200 } );
  250. sinon.assert.calledWithExactly( global.window.scrollTo, 100, -130 );
  251. } );
  252. it( 'scrolls the viewport to show the target (partially above)', () => {
  253. stubRect( target, { top: -50, right: 200, bottom: 50, left: 100, width: 100, height: 100 } );
  254. scrollViewportToShowTarget( { target, viewportOffset } );
  255. assertScrollPosition( firstAncestor, { scrollTop: 50, scrollLeft: 200 } );
  256. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 20 );
  257. } );
  258. it( 'scrolls the viewport to show the target (below)', () => {
  259. stubRect( target, { top: 600, right: 200, bottom: 700, left: 100, width: 100, height: 100 } );
  260. scrollViewportToShowTarget( { target, viewportOffset } );
  261. assertScrollPosition( firstAncestor, { scrollTop: 700, scrollLeft: 200 } );
  262. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 350 );
  263. } );
  264. it( 'scrolls the viewport to show the target (partially below)', () => {
  265. stubRect( target, { top: 450, right: 200, bottom: 550, left: 100, width: 100, height: 100 } );
  266. scrollViewportToShowTarget( { target, viewportOffset } );
  267. assertScrollPosition( firstAncestor, { scrollTop: 550, scrollLeft: 200 } );
  268. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 200 );
  269. } );
  270. it( 'scrolls the viewport to show the target (to the left)', () => {
  271. stubRect( target, { top: 0, right: -100, bottom: 100, left: -200, width: 100, height: 100 } );
  272. scrollViewportToShowTarget( { target, viewportOffset } );
  273. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: -100 } );
  274. sinon.assert.calledWithExactly( global.window.scrollTo, -130, 70 );
  275. } );
  276. it( 'scrolls the viewport to show the target (partially to the left)', () => {
  277. stubRect( target, { top: 0, right: 50, bottom: 100, left: -50, width: 100, height: 100 } );
  278. scrollViewportToShowTarget( { target, viewportOffset } );
  279. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 50 } );
  280. sinon.assert.calledWithExactly( global.window.scrollTo, 20, 70 );
  281. } );
  282. it( 'scrolls the viewport to show the target (to the right)', () => {
  283. stubRect( target, { top: 0, right: 1200, bottom: 100, left: 1100, width: 100, height: 100 } );
  284. scrollViewportToShowTarget( { target, viewportOffset } );
  285. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1200 } );
  286. sinon.assert.calledWithExactly( global.window.scrollTo, 350, 70 );
  287. } );
  288. it( 'scrolls the viewport to show the target (partially to the right)', () => {
  289. stubRect( target, { top: 0, right: 1050, bottom: 100, left: 950, width: 100, height: 100 } );
  290. scrollViewportToShowTarget( { target, viewportOffset } );
  291. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1050 } );
  292. sinon.assert.calledWithExactly( global.window.scrollTo, 200, 70 );
  293. } );
  294. }
  295. } );
  296. function stubRect( target, geometryStub, scrollStub ) {
  297. if ( isRange( target ) ) {
  298. testUtils.sinon.stub( target, 'getClientRects' ).returns( [ geometryStub ] );
  299. } else {
  300. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( geometryStub );
  301. }
  302. if ( scrollStub ) {
  303. let { scrollLeft, scrollTop } = scrollStub;
  304. // There's no way to stub scrollLeft|Top with Sinon. defineProperties
  305. // must be used instead.
  306. Object.defineProperties( target, {
  307. scrollLeft: {
  308. get() {
  309. return scrollLeft;
  310. },
  311. set( value ) {
  312. scrollLeft = value;
  313. }
  314. },
  315. scrollTop: {
  316. get() {
  317. return scrollTop;
  318. },
  319. set( value ) {
  320. scrollTop = value;
  321. }
  322. }
  323. } );
  324. }
  325. }
  326. function assertScrollPosition( element, expected ) {
  327. expect( element.scrollTop ).to.equal( expected.scrollTop, 'scrollTop' );
  328. expect( element.scrollLeft ).to.equal( expected.scrollLeft, 'scrollLeft' );
  329. }