8
0

resizeobserver.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, setTimeout, Event, console */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import Rect from '../../src/dom/rect';
  8. import global from '../../src/dom/global';
  9. import DomEmitterMixin from '../../src/dom/emittermixin';
  10. import ResizeObserver from '../../src/dom/resizeobserver';
  11. describe( 'ResizeObserver()', () => {
  12. let elementA, elementB;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. // Make sure other tests of the editor do not affect tests that follow.
  16. // Without it, if an instance of ResizeObserver already exists somewhere undestroyed
  17. // in DOM, any DOM mock will have no effect.
  18. ResizeObserver._observerInstance = null;
  19. elementA = document.createElement( 'div' );
  20. elementA.id = 'A';
  21. elementB = document.createElement( 'div' );
  22. elementB.id = 'B';
  23. document.body.appendChild( elementA );
  24. document.body.appendChild( elementB );
  25. } );
  26. afterEach( () => {
  27. // Make it look like the module was loaded from scratch.
  28. ResizeObserver._observerInstance = null;
  29. ResizeObserver._elementCallbacks = null;
  30. elementA.remove();
  31. elementB.remove();
  32. } );
  33. describe( 'constructor()', () => {
  34. it( 'should use the native implementation if available', () => {
  35. const spy = sinon.spy();
  36. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( () => {
  37. return {
  38. observe: spy,
  39. unobserve: sinon.spy()
  40. };
  41. } );
  42. const observer = new ResizeObserver( elementA, () => {} );
  43. sinon.assert.calledOnce( spy );
  44. observer.destroy();
  45. } );
  46. it( 'should use the polyfill when no native implementation available', () => {
  47. testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( null );
  48. const observer = new ResizeObserver( elementA, () => {} );
  49. expect( ResizeObserver._observerInstance.constructor.name ).to.equal( 'ResizeObserverPolyfill' );
  50. observer.destroy();
  51. } );
  52. it( 'should re-use the same native observer instance over and over again', () => {
  53. const elementA = document.createElement( 'div' );
  54. const elementB = document.createElement( 'div' );
  55. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( () => {
  56. return {
  57. observe() {},
  58. unobserve() {}
  59. };
  60. } );
  61. const observerA = new ResizeObserver( elementA, () => {} );
  62. const observerB = new ResizeObserver( elementB, () => {} );
  63. sinon.assert.calledOnce( global.window.ResizeObserver );
  64. observerA.destroy();
  65. observerB.destroy();
  66. } );
  67. it( 'should react to resizing of an element', () => {
  68. const callbackA = sinon.spy();
  69. let resizeCallback;
  70. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  71. resizeCallback = callback;
  72. return {
  73. observe() {},
  74. unobserve() {}
  75. };
  76. } );
  77. const observerA = new ResizeObserver( elementA, callbackA );
  78. resizeCallback( [
  79. { target: elementA }
  80. ] );
  81. sinon.assert.calledOnce( callbackA );
  82. sinon.assert.calledWithExactly( callbackA.firstCall, { target: elementA } );
  83. observerA.destroy();
  84. } );
  85. it( 'should not react to resizing of an element if element is invisible', () => {
  86. const callbackA = sinon.spy();
  87. let resizeCallback;
  88. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  89. resizeCallback = callback;
  90. return {
  91. observe() {},
  92. unobserve() {}
  93. };
  94. } );
  95. const observerA = new ResizeObserver( elementA, callbackA );
  96. elementA.style.display = 'none';
  97. resizeCallback( [
  98. { target: elementA }
  99. ] );
  100. sinon.assert.notCalled( callbackA );
  101. observerA.destroy();
  102. } );
  103. it( 'should not react to resizing of an element if element\'s parent is invisible', () => {
  104. const callbackA = sinon.spy();
  105. let resizeCallback;
  106. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  107. resizeCallback = callback;
  108. return {
  109. observe() {},
  110. unobserve() {}
  111. };
  112. } );
  113. const observerA = new ResizeObserver( elementA, callbackA );
  114. const parent = document.createElement( 'div' );
  115. document.body.appendChild( parent );
  116. parent.appendChild( elementA );
  117. parent.style.display = 'none';
  118. resizeCallback( [
  119. { target: elementA }
  120. ] );
  121. sinon.assert.notCalled( callbackA );
  122. parent.remove();
  123. observerA.destroy();
  124. } );
  125. it( 'should be able to observe the same element along with other observers', () => {
  126. const callbackA = sinon.spy();
  127. const callbackB = sinon.spy();
  128. let resizeCallback;
  129. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  130. resizeCallback = callback;
  131. return {
  132. observe() {},
  133. unobserve() {}
  134. };
  135. } );
  136. const observerA = new ResizeObserver( elementA, callbackA );
  137. const observerB = new ResizeObserver( elementA, callbackB );
  138. resizeCallback( [
  139. { target: elementA }
  140. ] );
  141. sinon.assert.calledOnce( callbackA );
  142. sinon.assert.calledWithExactly( callbackA, { target: elementA } );
  143. sinon.assert.calledOnce( callbackB );
  144. sinon.assert.calledWithExactly( callbackB, { target: elementA } );
  145. observerA.destroy();
  146. observerB.destroy();
  147. } );
  148. it( 'should not be affected by other observers being destroyed', () => {
  149. const callbackA = sinon.spy();
  150. const callbackB = sinon.spy();
  151. let resizeCallback;
  152. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  153. resizeCallback = callback;
  154. return {
  155. observe() {},
  156. unobserve() {}
  157. };
  158. } );
  159. const observerA = new ResizeObserver( elementA, callbackA );
  160. const observerB = new ResizeObserver( elementA, callbackB );
  161. resizeCallback( [
  162. { target: elementA }
  163. ] );
  164. sinon.assert.calledOnce( callbackA );
  165. sinon.assert.calledWithExactly( callbackA, { target: elementA } );
  166. sinon.assert.calledOnce( callbackB );
  167. sinon.assert.calledWithExactly( callbackB, { target: elementA } );
  168. observerB.destroy();
  169. resizeCallback( [
  170. { target: elementA }
  171. ] );
  172. sinon.assert.calledTwice( callbackA );
  173. sinon.assert.calledWithExactly( callbackA.secondCall, { target: elementA } );
  174. sinon.assert.calledOnce( callbackB );
  175. sinon.assert.calledWithExactly( callbackB, { target: elementA } );
  176. observerA.destroy();
  177. } );
  178. } );
  179. describe( 'destroy()', () => {
  180. it( 'should make the observer stop responding to resize of an element', () => {
  181. const callbackA = sinon.spy();
  182. let resizeCallback;
  183. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  184. resizeCallback = callback;
  185. return {
  186. observe() {},
  187. unobserve() {}
  188. };
  189. } );
  190. const observerA = new ResizeObserver( elementA, callbackA );
  191. resizeCallback( [
  192. { target: elementA }
  193. ] );
  194. sinon.assert.calledOnce( callbackA );
  195. observerA.destroy();
  196. resizeCallback( [
  197. { target: elementA }
  198. ] );
  199. sinon.assert.calledOnce( callbackA );
  200. } );
  201. it( 'should not throw if called multiple times', () => {
  202. const callbackA = sinon.spy();
  203. const observerA = new ResizeObserver( elementA, callbackA );
  204. expect( () => {
  205. observerA.destroy();
  206. observerA.destroy();
  207. } ).to.not.throw();
  208. } );
  209. } );
  210. describe( 'ResizeObserverPolyfill', () => {
  211. let callback, elementRectA, elementRectB;
  212. beforeEach( () => {
  213. testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( null );
  214. callback = sinon.spy();
  215. elementRectA = {
  216. top: 10,
  217. right: 20,
  218. bottom: 20,
  219. left: 0,
  220. height: 10,
  221. width: 20
  222. };
  223. elementRectB = {
  224. top: 0,
  225. right: 10,
  226. bottom: 10,
  227. left: 0,
  228. height: 10,
  229. width: 10
  230. };
  231. elementA.getBoundingClientRect = () => elementRectA;
  232. elementB.getBoundingClientRect = () => elementRectB;
  233. document.body.appendChild( elementA );
  234. document.body.appendChild( elementB );
  235. } );
  236. afterEach( () => {
  237. elementA.remove();
  238. elementB.remove();
  239. } );
  240. it( 'mixes DomEmitterMixin', () => {
  241. const observer = new ResizeObserver( elementA, () => {} );
  242. expect( testUtils.isMixed( ResizeObserver._observerInstance.constructor, DomEmitterMixin ) ).to.be.true;
  243. observer.destroy();
  244. } );
  245. describe( 'observe()', () => {
  246. it( 'calls the callback immediatelly', () => {
  247. const observer = new ResizeObserver( elementA, callback );
  248. sinon.assert.calledOnce( callback );
  249. const { target, contentRect } = callback.firstCall.args[ 0 ];
  250. expect( target ).to.equal( elementA );
  251. expect( contentRect ).to.be.instanceOf( Rect );
  252. expect( contentRect ).to.deep.equal( elementRectA );
  253. observer.destroy();
  254. } );
  255. it( 'does not execute the callback if element has no parent in DOM', () => {
  256. const warnSpy = testUtils.sinon.spy( console, 'warn' );
  257. elementA.remove();
  258. const observer = new ResizeObserver( elementA, callback );
  259. sinon.assert.notCalled( callback );
  260. sinon.assert.notCalled( warnSpy );
  261. observer.destroy();
  262. } );
  263. it( 'starts periodic check and asynchronously does not execute the callback if the element rect is the same', done => {
  264. const observer = new ResizeObserver( elementA, callback );
  265. setTimeout( () => {
  266. sinon.assert.calledOnce( callback );
  267. observer.destroy();
  268. done();
  269. }, 200 );
  270. } );
  271. it( 'starts periodic check and asynchronously executes the callback if the element rect changed', done => {
  272. const observer = new ResizeObserver( elementA, callback );
  273. sinon.assert.calledOnce( callback );
  274. const newRect = {
  275. top: 30,
  276. right: 10,
  277. bottom: 40,
  278. left: 0,
  279. height: 10,
  280. width: 10
  281. };
  282. elementA.getBoundingClientRect = () => newRect;
  283. setTimeout( () => {
  284. sinon.assert.calledTwice( callback );
  285. const { target, contentRect } = callback.secondCall.args[ 0 ];
  286. expect( target ).to.equal( elementA );
  287. expect( contentRect ).to.deep.equal( newRect );
  288. observer.destroy();
  289. done();
  290. }, 200 );
  291. } );
  292. it( 'starts periodic check and asynchronously executes the callback if multiple element rects changed', done => {
  293. const callbackA = sinon.spy();
  294. const callbackB = sinon.spy();
  295. const observerA = new ResizeObserver( elementA, callbackA );
  296. const observerB = new ResizeObserver( elementB, callbackB );
  297. sinon.assert.calledOnce( callbackA );
  298. sinon.assert.calledOnce( callbackB );
  299. const newRectA = {
  300. top: 30,
  301. right: 10,
  302. bottom: 40,
  303. left: 0,
  304. height: 10,
  305. width: 10
  306. };
  307. const newRectB = {
  308. top: 30,
  309. right: 100,
  310. bottom: 40,
  311. left: 0,
  312. height: 10,
  313. width: 100
  314. };
  315. elementA.getBoundingClientRect = () => newRectA;
  316. elementB.getBoundingClientRect = () => newRectB;
  317. setTimeout( () => {
  318. sinon.assert.calledTwice( callbackA );
  319. sinon.assert.calledTwice( callbackB );
  320. const { target: targetA, contentRect: contentRectA } = callbackA.secondCall.args[ 0 ];
  321. const { target: targetB, contentRect: contentRectB } = callbackB.secondCall.args[ 0 ];
  322. expect( targetA ).to.equal( elementA );
  323. expect( contentRectA ).to.deep.equal( newRectA );
  324. expect( targetB ).to.equal( elementB );
  325. expect( contentRectB ).to.deep.equal( newRectB );
  326. observerA.destroy();
  327. observerB.destroy();
  328. done();
  329. }, 200 );
  330. } );
  331. it( 'starts periodic check and synchronously responds to window resize', () => {
  332. const observer = new ResizeObserver( elementA, callback );
  333. sinon.assert.calledOnce( callback );
  334. const newRectA = {
  335. top: 30,
  336. right: 10,
  337. bottom: 40,
  338. left: 0,
  339. height: 10,
  340. width: 10
  341. };
  342. elementA.getBoundingClientRect = () => newRectA;
  343. global.window.dispatchEvent( new Event( 'resize' ) );
  344. sinon.assert.calledTwice( callback );
  345. const { target: targetA, contentRect: contentRectA } = callback.secondCall.args[ 0 ];
  346. expect( targetA ).to.equal( elementA );
  347. expect( contentRectA ).to.deep.equal( newRectA );
  348. observer.destroy();
  349. } );
  350. } );
  351. describe( 'unobserve()', () => {
  352. it( 'removes the element from the observer so no future changes to the element execute the callback', done => {
  353. const observer = new ResizeObserver( elementA, callback );
  354. sinon.assert.calledOnce( callback );
  355. const newRect = {
  356. top: 30,
  357. right: 10,
  358. bottom: 40,
  359. left: 0,
  360. height: 10,
  361. width: 10
  362. };
  363. observer.destroy();
  364. elementA.getBoundingClientRect = () => newRect;
  365. setTimeout( () => {
  366. sinon.assert.calledOnce( callback );
  367. done();
  368. }, 200 );
  369. } );
  370. it( 'disables the Emitter when no elements left in the observer', () => {
  371. const observer = new ResizeObserver( elementA, callback );
  372. const stopCheckSpy = testUtils.sinon.spy( ResizeObserver._observerInstance, '_stopPeriodicCheck' );
  373. sinon.assert.calledOnce( callback );
  374. observer.destroy();
  375. sinon.assert.calledOnce( stopCheckSpy );
  376. } );
  377. } );
  378. } );
  379. } );