scroll.js 16 KB

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