scroll.js 15 KB

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