scroll.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. // Make the element immune to the border-width-* styles in the test environment.
  28. testUtils.sinon.stub( global.window, 'getComputedStyle', () => ( {
  29. borderTopWidth: '0px',
  30. borderRightWidth: '0px',
  31. borderBottomWidth: '0px',
  32. borderLeftWidth: '0px'
  33. } ) );
  34. stubRect( firstAncestor, {
  35. top: 0, right: 100, bottom: 100, left: 0, width: 100, height: 100
  36. }, {
  37. scrollLeft: 100, scrollTop: 100
  38. } );
  39. stubRect( secondAncestor, {
  40. top: -100, right: 0, bottom: 0, left: -100, width: 100, height: 100
  41. }, {
  42. scrollLeft: 100, scrollTop: 100
  43. } );
  44. stubRect( body, {
  45. top: 1000, right: 2000, bottom: 1000, left: 1000, width: 1000, height: 1000
  46. }, {
  47. scrollLeft: 1000, scrollTop: 1000
  48. } );
  49. } );
  50. afterEach( () => {
  51. body.remove();
  52. } );
  53. describe( 'for an HTMLElement', () => {
  54. beforeEach( () => {
  55. target = element;
  56. } );
  57. test();
  58. } );
  59. describe( 'for a DOM Range', () => {
  60. beforeEach( () => {
  61. target = document.createRange();
  62. target.setStart( firstAncestor, 0 );
  63. target.setEnd( firstAncestor, 0 );
  64. } );
  65. test();
  66. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (above, attached to the Text)', () => {
  67. const text = new Text( 'foo' );
  68. firstAncestor.appendChild( text );
  69. target.setStart( text, 1 );
  70. target.setEnd( text, 2 );
  71. stubRect( target, { top: -100, right: 75, bottom: 0, left: 25, width: 50, height: 100 } );
  72. scrollAncestorsToShowTarget( target );
  73. assertScrollPosition( firstAncestor, { scrollTop: 0, scrollLeft: 100 } );
  74. } );
  75. } );
  76. function test() {
  77. it( 'should not touch the #scrollTop #scrollLeft of the ancestor if target is visible', () => {
  78. stubRect( target, { top: 25, right: 75, bottom: 75, left: 25, width: 50, height: 50 } );
  79. scrollAncestorsToShowTarget( target );
  80. assertScrollPosition( firstAncestor, { scrollLeft: 100, scrollTop: 100 } );
  81. } );
  82. it( 'should not touch the #scrollTop #scrollLeft of the document.body', () => {
  83. stubRect( target, { top: 25, right: 75, bottom: 75, left: 25, width: 50, height: 50 } );
  84. scrollAncestorsToShowTarget( target );
  85. assertScrollPosition( body, { scrollLeft: 1000, scrollTop: 1000 } );
  86. } );
  87. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (above)', () => {
  88. stubRect( target, { top: -100, right: 75, bottom: 0, left: 25, width: 50, height: 100 } );
  89. scrollAncestorsToShowTarget( target );
  90. assertScrollPosition( firstAncestor, { scrollTop: 0, scrollLeft: 100 } );
  91. } );
  92. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (below)', () => {
  93. stubRect( target, { top: 200, right: 75, bottom: 300, left: 25, width: 50, height: 100 } );
  94. scrollAncestorsToShowTarget( target );
  95. assertScrollPosition( firstAncestor, { scrollTop: 300, scrollLeft: 100 } );
  96. } );
  97. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (left of)', () => {
  98. stubRect( target, { top: 0, right: 0, bottom: 100, left: -100, width: 100, height: 100 } );
  99. scrollAncestorsToShowTarget( target );
  100. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 0 } );
  101. } );
  102. it( 'should set #scrollTop and #scrollLeft of the ancestor to show the target (right of)', () => {
  103. stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
  104. scrollAncestorsToShowTarget( target );
  105. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
  106. } );
  107. it( 'should set #scrollTop and #scrollLeft of all the ancestors', () => {
  108. stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
  109. scrollAncestorsToShowTarget( target );
  110. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
  111. // Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
  112. // the getBoundingClientRect geometry of the target. That's why scrolling secondAncestor
  113. // works like the target remained in the original position and hence scrollLeft is 300 instead
  114. // of 200.
  115. assertScrollPosition( secondAncestor, { scrollTop: 200, scrollLeft: 300 } );
  116. } );
  117. }
  118. } );
  119. describe( 'scrollViewportToShowTarget()', () => {
  120. let target, firstAncestor, element;
  121. const viewportOffset = 30;
  122. beforeEach( () => {
  123. element = document.createElement( 'p' );
  124. firstAncestor = document.createElement( 'blockquote' );
  125. document.body.appendChild( firstAncestor );
  126. firstAncestor.appendChild( element );
  127. stubRect( firstAncestor, {
  128. top: 0, right: 100, bottom: 100, left: 0, width: 100, height: 100
  129. }, {
  130. scrollLeft: 100, scrollTop: 100
  131. } );
  132. testUtils.sinon.stub( global, 'window', {
  133. innerWidth: 1000,
  134. innerHeight: 500,
  135. scrollX: 100,
  136. scrollY: 100,
  137. scrollTo: sinon.spy(),
  138. getComputedStyle: () => ( {
  139. borderTopWidth: '0px',
  140. borderRightWidth: '0px',
  141. borderBottomWidth: '0px',
  142. borderLeftWidth: '0px'
  143. } )
  144. } );
  145. // Assuming 20px v- and h-scrollbars here.
  146. testUtils.sinon.stub( global, 'document', {
  147. body: document.body,
  148. documentElement: {
  149. clientWidth: 980,
  150. clientHeight: 480
  151. }
  152. } );
  153. } );
  154. afterEach( () => {
  155. firstAncestor.remove();
  156. } );
  157. describe( 'for an HTMLElement', () => {
  158. beforeEach( () => {
  159. target = element;
  160. } );
  161. testNoOffset();
  162. describe( 'with a viewportOffset', () => {
  163. testWithOffset();
  164. } );
  165. } );
  166. describe( 'for a DOM Range', () => {
  167. beforeEach( () => {
  168. target = document.createRange();
  169. target.setStart( firstAncestor, 0 );
  170. target.setEnd( firstAncestor, 0 );
  171. } );
  172. testNoOffset();
  173. describe( 'with a viewportOffset', () => {
  174. testWithOffset();
  175. } );
  176. } );
  177. // Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
  178. // the getBoundingClientRect geometry of the target. That's why scrolling the viewport
  179. // works like the target remained in the original position. It's tricky but much faster
  180. // and still shows that the whole thing works as expected.
  181. //
  182. // Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
  183. // initial geometry and scroll position they give the right, relative picture of what's going on.
  184. function testNoOffset() {
  185. it( 'does not scroll the viewport when the target is fully visible', () => {
  186. stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
  187. scrollViewportToShowTarget( { target } );
  188. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 200 } );
  189. sinon.assert.notCalled( global.window.scrollTo );
  190. } );
  191. it( 'scrolls the viewport to show the target (above)', () => {
  192. stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
  193. scrollViewportToShowTarget( { target } );
  194. assertScrollPosition( firstAncestor, { scrollTop: -100, scrollLeft: 200 } );
  195. sinon.assert.calledWithExactly( global.window.scrollTo, 100, -100 );
  196. } );
  197. it( 'scrolls the viewport to show the target (partially above)', () => {
  198. stubRect( target, { top: -50, right: 200, bottom: 50, left: 100, width: 100, height: 100 } );
  199. scrollViewportToShowTarget( { target } );
  200. assertScrollPosition( firstAncestor, { scrollTop: 50, scrollLeft: 200 } );
  201. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 50 );
  202. } );
  203. it( 'scrolls the viewport to show the target (below)', () => {
  204. stubRect( target, { top: 600, right: 200, bottom: 700, left: 100, width: 100, height: 100 } );
  205. scrollViewportToShowTarget( { target } );
  206. assertScrollPosition( firstAncestor, { scrollTop: 700, scrollLeft: 200 } );
  207. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 320 );
  208. } );
  209. it( 'scrolls the viewport to show the target (partially below)', () => {
  210. stubRect( target, { top: 450, right: 200, bottom: 550, left: 100, width: 100, height: 100 } );
  211. scrollViewportToShowTarget( { target } );
  212. assertScrollPosition( firstAncestor, { scrollTop: 550, scrollLeft: 200 } );
  213. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 170 );
  214. } );
  215. it( 'scrolls the viewport to show the target (to the left)', () => {
  216. stubRect( target, { top: 0, right: -100, bottom: 100, left: -200, width: 100, height: 100 } );
  217. scrollViewportToShowTarget( { target } );
  218. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: -100 } );
  219. sinon.assert.calledWithExactly( global.window.scrollTo, -100, 100 );
  220. } );
  221. it( 'scrolls the viewport to show the target (partially to the left)', () => {
  222. stubRect( target, { top: 0, right: 50, bottom: 100, left: -50, width: 100, height: 100 } );
  223. scrollViewportToShowTarget( { target } );
  224. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 50 } );
  225. sinon.assert.calledWithExactly( global.window.scrollTo, 50, 100 );
  226. } );
  227. it( 'scrolls the viewport to show the target (to the right)', () => {
  228. stubRect( target, { top: 0, right: 1200, bottom: 100, left: 1100, width: 100, height: 100 } );
  229. scrollViewportToShowTarget( { target } );
  230. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1200 } );
  231. sinon.assert.calledWithExactly( global.window.scrollTo, 320, 100 );
  232. } );
  233. it( 'scrolls the viewport to show the target (partially to the right)', () => {
  234. stubRect( target, { top: 0, right: 1050, bottom: 100, left: 950, width: 100, height: 100 } );
  235. scrollViewportToShowTarget( { target } );
  236. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1050 } );
  237. sinon.assert.calledWithExactly( global.window.scrollTo, 170, 100 );
  238. } );
  239. }
  240. // Note: Because everything is a mock, scrolling the firstAncestor doesn't really change
  241. // the getBoundingClientRect geometry of the target. That's why scrolling the viewport
  242. // works like the target remained in the original position. It's tricky but much faster
  243. // and still shows that the whole thing works as expected.
  244. //
  245. // Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
  246. // initial geometry and scroll position they give the right, relative picture of what's going on.
  247. function testWithOffset() {
  248. it( 'does not scroll the viewport when the target is fully visible', () => {
  249. stubRect( target, { top: 50, right: 200, bottom: 150, left: 100, width: 100, height: 100 } );
  250. scrollViewportToShowTarget( { target, viewportOffset } );
  251. assertScrollPosition( firstAncestor, { scrollTop: 150, scrollLeft: 200 } );
  252. sinon.assert.notCalled( global.window.scrollTo );
  253. } );
  254. it( 'scrolls the viewport to show the target (above)', () => {
  255. stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
  256. scrollViewportToShowTarget( { target, viewportOffset } );
  257. assertScrollPosition( firstAncestor, { scrollTop: -100, scrollLeft: 200 } );
  258. sinon.assert.calledWithExactly( global.window.scrollTo, 100, -130 );
  259. } );
  260. it( 'scrolls the viewport to show the target (partially above)', () => {
  261. stubRect( target, { top: -50, right: 200, bottom: 50, left: 100, width: 100, height: 100 } );
  262. scrollViewportToShowTarget( { target, viewportOffset } );
  263. assertScrollPosition( firstAncestor, { scrollTop: 50, scrollLeft: 200 } );
  264. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 20 );
  265. } );
  266. it( 'scrolls the viewport to show the target (below)', () => {
  267. stubRect( target, { top: 600, right: 200, bottom: 700, left: 100, width: 100, height: 100 } );
  268. scrollViewportToShowTarget( { target, viewportOffset } );
  269. assertScrollPosition( firstAncestor, { scrollTop: 700, scrollLeft: 200 } );
  270. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 350 );
  271. } );
  272. it( 'scrolls the viewport to show the target (partially below)', () => {
  273. stubRect( target, { top: 450, right: 200, bottom: 550, left: 100, width: 100, height: 100 } );
  274. scrollViewportToShowTarget( { target, viewportOffset } );
  275. assertScrollPosition( firstAncestor, { scrollTop: 550, scrollLeft: 200 } );
  276. sinon.assert.calledWithExactly( global.window.scrollTo, 100, 200 );
  277. } );
  278. it( 'scrolls the viewport to show the target (to the left)', () => {
  279. stubRect( target, { top: 0, right: -100, bottom: 100, left: -200, width: 100, height: 100 } );
  280. scrollViewportToShowTarget( { target, viewportOffset } );
  281. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: -100 } );
  282. sinon.assert.calledWithExactly( global.window.scrollTo, -130, 70 );
  283. } );
  284. it( 'scrolls the viewport to show the target (partially to the left)', () => {
  285. stubRect( target, { top: 0, right: 50, bottom: 100, left: -50, width: 100, height: 100 } );
  286. scrollViewportToShowTarget( { target, viewportOffset } );
  287. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 50 } );
  288. sinon.assert.calledWithExactly( global.window.scrollTo, 20, 70 );
  289. } );
  290. it( 'scrolls the viewport to show the target (to the right)', () => {
  291. stubRect( target, { top: 0, right: 1200, bottom: 100, left: 1100, width: 100, height: 100 } );
  292. scrollViewportToShowTarget( { target, viewportOffset } );
  293. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1200 } );
  294. sinon.assert.calledWithExactly( global.window.scrollTo, 350, 70 );
  295. } );
  296. it( 'scrolls the viewport to show the target (partially to the right)', () => {
  297. stubRect( target, { top: 0, right: 1050, bottom: 100, left: 950, width: 100, height: 100 } );
  298. scrollViewportToShowTarget( { target, viewportOffset } );
  299. assertScrollPosition( firstAncestor, { scrollTop: 100, scrollLeft: 1050 } );
  300. sinon.assert.calledWithExactly( global.window.scrollTo, 200, 70 );
  301. } );
  302. }
  303. } );
  304. function stubRect( target, geometryStub, scrollStub ) {
  305. if ( isRange( target ) ) {
  306. testUtils.sinon.stub( target, 'getClientRects' ).returns( [ geometryStub ] );
  307. } else {
  308. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( geometryStub );
  309. // Make the element immune to the border-width-* styles in the test environment.
  310. Object.defineProperties( target, {
  311. offsetWidth: {
  312. value: geometryStub.width
  313. },
  314. clientWidth: {
  315. value: geometryStub.width
  316. },
  317. offsetHeight: {
  318. value: geometryStub.height
  319. },
  320. clientHeight: {
  321. value: geometryStub.height
  322. }
  323. } );
  324. }
  325. if ( scrollStub ) {
  326. let { scrollLeft, scrollTop } = scrollStub;
  327. // There's no way to stub scrollLeft|Top with Sinon. defineProperties
  328. // must be used instead.
  329. Object.defineProperties( target, {
  330. scrollLeft: {
  331. get() {
  332. return scrollLeft;
  333. },
  334. set( value ) {
  335. scrollLeft = value;
  336. }
  337. },
  338. scrollTop: {
  339. get() {
  340. return scrollTop;
  341. },
  342. set( value ) {
  343. scrollTop = value;
  344. }
  345. }
  346. } );
  347. }
  348. }
  349. function assertScrollPosition( element, expected ) {
  350. expect( element.scrollTop ).to.equal( expected.scrollTop, 'scrollTop' );
  351. expect( element.scrollLeft ).to.equal( expected.scrollLeft, 'scrollLeft' );
  352. }