8
0

rect.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import global from '../../src/dom/global';
  7. import Rect from '../../src/dom/rect';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. testUtils.createSinonSandbox();
  10. describe( 'Rect', () => {
  11. let geometry;
  12. beforeEach( () => {
  13. geometry = {
  14. top: 10,
  15. right: 40,
  16. bottom: 30,
  17. left: 20,
  18. width: 20,
  19. height: 20
  20. };
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'should store passed object in #_source property', () => {
  24. const obj = {};
  25. const rect = new Rect( obj );
  26. expect( rect._source ).to.equal( obj );
  27. } );
  28. it( 'should accept HTMLElement', () => {
  29. const element = document.createElement( 'div' );
  30. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  31. assertRect( new Rect( element ), geometry );
  32. } );
  33. it( 'should accept Range (non–collapsed)', () => {
  34. const range = document.createRange();
  35. range.selectNode( document.body );
  36. testUtils.sinon.stub( range, 'getBoundingClientRect' ).returns( geometry );
  37. assertRect( new Rect( range ), geometry );
  38. } );
  39. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  40. it( 'should accept Range (collapsed)', () => {
  41. const range = document.createRange();
  42. range.collapse();
  43. testUtils.sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  44. assertRect( new Rect( range ), geometry );
  45. } );
  46. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  47. it( 'should accept Range (collapsed, no Range rects available)', () => {
  48. const range = document.createRange();
  49. const element = document.createElement( 'div' );
  50. range.setStart( element, 0 );
  51. range.collapse();
  52. testUtils.sinon.stub( range, 'getClientRects' ).returns( [] );
  53. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  54. const expectedGeometry = Object.assign( {}, geometry );
  55. expectedGeometry.width = 0;
  56. assertRect( new Rect( range ), expectedGeometry );
  57. } );
  58. it( 'should accept Rect', () => {
  59. const sourceRect = new Rect( geometry );
  60. const rect = new Rect( sourceRect );
  61. expect( rect ).to.not.equal( sourceRect );
  62. assertRect( rect, geometry );
  63. } );
  64. it( 'should accept ClientRect', () => {
  65. const clientRect = document.body.getBoundingClientRect();
  66. const { top, right, bottom, left, width, height } = clientRect;
  67. const rect = new Rect( clientRect );
  68. assertRect( rect, { top, right, bottom, left, width, height } );
  69. } );
  70. it( 'should accept geometry object', () => {
  71. assertRect( new Rect( geometry ), geometry );
  72. } );
  73. it( 'should copy the properties (Rect)', () => {
  74. const sourceGeometry = Object.assign( {}, geometry );
  75. const sourceRect = new Rect( geometry );
  76. const rect = new Rect( sourceRect );
  77. assertRect( rect, geometry );
  78. rect.top = 100;
  79. rect.width = 200;
  80. assertRect( sourceRect, sourceGeometry );
  81. } );
  82. it( 'should copy the properties (geomerty object)', () => {
  83. const sourceGeometry = Object.assign( {}, geometry );
  84. const rect = new Rect( geometry );
  85. assertRect( rect, geometry );
  86. rect.top = 100;
  87. rect.width = 200;
  88. assertRect( geometry, sourceGeometry );
  89. } );
  90. } );
  91. describe( 'clone()', () => {
  92. it( 'should clone the source rect', () => {
  93. const rect = new Rect( geometry );
  94. const clone = rect.clone();
  95. expect( clone ).to.be.instanceOf( Rect );
  96. expect( clone ).not.equal( rect );
  97. assertRect( clone, rect );
  98. } );
  99. it( 'should preserve #_source', () => {
  100. const rect = new Rect( geometry );
  101. const clone = rect.clone();
  102. expect( clone._source ).to.equal( rect._source );
  103. assertRect( clone, rect );
  104. } );
  105. } );
  106. describe( 'moveTo()', () => {
  107. it( 'should return the source rect', () => {
  108. const rect = new Rect( geometry );
  109. const returned = rect.moveTo( 100, 200 );
  110. expect( returned ).to.equal( rect );
  111. } );
  112. it( 'should move the rect', () => {
  113. const rect = new Rect( geometry );
  114. rect.moveTo( 100, 200 );
  115. assertRect( rect, {
  116. top: 200,
  117. right: 120,
  118. bottom: 220,
  119. left: 100,
  120. width: 20,
  121. height: 20
  122. } );
  123. } );
  124. } );
  125. describe( 'moveBy()', () => {
  126. it( 'should return the source rect', () => {
  127. const rect = new Rect( geometry );
  128. const returned = rect.moveBy( 100, 200 );
  129. expect( returned ).to.equal( rect );
  130. } );
  131. it( 'should move the rect', () => {
  132. const rect = new Rect( geometry );
  133. rect.moveBy( 100, 200 );
  134. assertRect( rect, {
  135. top: 210,
  136. right: 140,
  137. bottom: 230,
  138. left: 120,
  139. width: 20,
  140. height: 20
  141. } );
  142. } );
  143. } );
  144. describe( 'getIntersection()', () => {
  145. it( 'should return a new rect', () => {
  146. const rect = new Rect( geometry );
  147. const insersect = rect.getIntersection( new Rect( geometry ) );
  148. expect( insersect ).to.be.instanceOf( Rect );
  149. expect( insersect ).to.not.equal( rect );
  150. } );
  151. it( 'should calculate the geometry (#1)', () => {
  152. const rectA = new Rect( {
  153. top: 0,
  154. right: 100,
  155. bottom: 100,
  156. left: 0,
  157. width: 100,
  158. height: 100
  159. } );
  160. const rectB = new Rect( {
  161. top: 50,
  162. right: 150,
  163. bottom: 150,
  164. left: 50,
  165. width: 100,
  166. height: 100
  167. } );
  168. const insersect = rectA.getIntersection( rectB );
  169. assertRect( insersect, {
  170. top: 50,
  171. right: 100,
  172. bottom: 100,
  173. left: 50,
  174. width: 50,
  175. height: 50
  176. } );
  177. } );
  178. it( 'should calculate the geometry (#2)', () => {
  179. const rectA = new Rect( {
  180. top: 0,
  181. right: 100,
  182. bottom: 100,
  183. left: 0,
  184. width: 100,
  185. height: 100
  186. } );
  187. const rectB = new Rect( {
  188. top: 0,
  189. right: 200,
  190. bottom: 100,
  191. left: 100,
  192. width: 100,
  193. height: 100
  194. } );
  195. const insersect = rectA.getIntersection( rectB );
  196. assertRect( insersect, {
  197. top: 0,
  198. right: 100,
  199. bottom: 100,
  200. left: 100,
  201. width: 0,
  202. height: 100
  203. } );
  204. } );
  205. it( 'should calculate the geometry (#3)', () => {
  206. const rectA = new Rect( {
  207. top: 0,
  208. right: 100,
  209. bottom: 100,
  210. left: 0,
  211. width: 100,
  212. height: 100
  213. } );
  214. const rectB = new Rect( {
  215. top: 100,
  216. right: 300,
  217. bottom: 200,
  218. left: 200,
  219. width: 100,
  220. height: 100
  221. } );
  222. expect( rectA.getIntersection( rectB ) ).to.be.null;
  223. } );
  224. } );
  225. describe( 'getIntersectionArea()', () => {
  226. it( 'should calculate the area (#1)', () => {
  227. const rectA = new Rect( {
  228. top: 0,
  229. right: 100,
  230. bottom: 100,
  231. left: 0,
  232. width: 100,
  233. height: 100
  234. } );
  235. const rectB = new Rect( {
  236. top: 50,
  237. right: 150,
  238. bottom: 150,
  239. left: 50,
  240. width: 100,
  241. height: 100
  242. } );
  243. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 2500 );
  244. } );
  245. it( 'should calculate the area (#2)', () => {
  246. const rectA = new Rect( {
  247. top: 0,
  248. right: 100,
  249. bottom: 100,
  250. left: 0,
  251. width: 100,
  252. height: 100
  253. } );
  254. const rectB = new Rect( {
  255. top: 0,
  256. right: 200,
  257. bottom: 100,
  258. left: 100,
  259. width: 100,
  260. height: 100
  261. } );
  262. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 0 );
  263. } );
  264. it( 'should calculate the area (#3)', () => {
  265. const rectA = new Rect( {
  266. top: 0,
  267. right: 100,
  268. bottom: 100,
  269. left: 0,
  270. width: 100,
  271. height: 100
  272. } );
  273. const rectB = new Rect( {
  274. top: 100,
  275. right: 300,
  276. bottom: 200,
  277. left: 200,
  278. width: 100,
  279. height: 100
  280. } );
  281. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 0 );
  282. } );
  283. } );
  284. describe( 'getArea()', () => {
  285. it( 'should calculate the area', () => {
  286. const rect = new Rect( {
  287. width: 100,
  288. height: 50
  289. } );
  290. expect( rect.getArea() ).to.equal( 5000 );
  291. } );
  292. } );
  293. describe( 'getVisible()', () => {
  294. let element, range, ancestorA, ancestorB;
  295. beforeEach( () => {
  296. element = document.createElement( 'div' );
  297. range = document.createRange();
  298. ancestorA = document.createElement( 'div' );
  299. ancestorB = document.createElement( 'div' );
  300. ancestorA.append( element );
  301. document.body.appendChild( ancestorA );
  302. } );
  303. afterEach( () => {
  304. ancestorA.remove();
  305. ancestorB.remove();
  306. } );
  307. it( 'should return a new rect', () => {
  308. const rect = new Rect( {} );
  309. const visible = rect.getVisible();
  310. expect( visible ).to.not.equal( rect );
  311. } );
  312. it( 'should not fail when the rect is for document#body', () => {
  313. testUtils.sinon.stub( document.body, 'getBoundingClientRect' ).returns( {
  314. top: 0,
  315. right: 100,
  316. bottom: 100,
  317. left: 0,
  318. width: 100,
  319. height: 100
  320. } );
  321. assertRect( new Rect( document.body ).getVisible(), {
  322. top: 0,
  323. right: 100,
  324. bottom: 100,
  325. left: 0,
  326. width: 100,
  327. height: 100
  328. } );
  329. } );
  330. it( 'should return the visible rect (HTMLElement), partially cropped', () => {
  331. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
  332. top: 0,
  333. right: 100,
  334. bottom: 100,
  335. left: 0,
  336. width: 100,
  337. height: 100
  338. } );
  339. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  340. top: 50,
  341. right: 150,
  342. bottom: 150,
  343. left: 50,
  344. width: 100,
  345. height: 100
  346. } );
  347. assertRect( new Rect( element ).getVisible(), {
  348. top: 50,
  349. right: 100,
  350. bottom: 100,
  351. left: 50,
  352. width: 50,
  353. height: 50
  354. } );
  355. } );
  356. it( 'should return the visible rect (HTMLElement), fully visible', () => {
  357. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
  358. top: 0,
  359. right: 100,
  360. bottom: 100,
  361. left: 0,
  362. width: 100,
  363. height: 100
  364. } );
  365. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  366. top: 0,
  367. right: 150,
  368. bottom: 150,
  369. left: 0,
  370. width: 150,
  371. height: 150
  372. } );
  373. assertRect( new Rect( element ).getVisible(), {
  374. top: 0,
  375. right: 100,
  376. bottom: 100,
  377. left: 0,
  378. width: 100,
  379. height: 100
  380. } );
  381. } );
  382. it( 'should return the visible rect (HTMLElement), partially cropped, deep ancestor overflow', () => {
  383. ancestorB.append( ancestorA );
  384. document.body.appendChild( ancestorB );
  385. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
  386. top: 0,
  387. right: 100,
  388. bottom: 100,
  389. left: 0,
  390. width: 100,
  391. height: 100
  392. } );
  393. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  394. top: 50,
  395. right: 100,
  396. bottom: 100,
  397. left: 0,
  398. width: 50,
  399. height: 50
  400. } );
  401. testUtils.sinon.stub( ancestorB, 'getBoundingClientRect' ).returns( {
  402. top: 0,
  403. right: 150,
  404. bottom: 100,
  405. left: 50,
  406. width: 100,
  407. height: 100
  408. } );
  409. assertRect( new Rect( element ).getVisible(), {
  410. top: 50,
  411. right: 100,
  412. bottom: 100,
  413. left: 50,
  414. width: 50,
  415. height: 50
  416. } );
  417. } );
  418. it( 'should return the visible rect (Range), partially cropped', () => {
  419. range.setStart( ancestorA, 0 );
  420. range.setEnd( ancestorA, 1 );
  421. testUtils.sinon.stub( range, 'getBoundingClientRect' ).returns( {
  422. top: 0,
  423. right: 100,
  424. bottom: 100,
  425. left: 0,
  426. width: 100,
  427. height: 100
  428. } );
  429. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  430. top: 50,
  431. right: 150,
  432. bottom: 150,
  433. left: 50,
  434. width: 100,
  435. height: 100
  436. } );
  437. assertRect( new Rect( range ).getVisible(), {
  438. top: 50,
  439. right: 100,
  440. bottom: 100,
  441. left: 50,
  442. width: 50,
  443. height: 50
  444. } );
  445. } );
  446. it( 'should return null if there\'s no visible rect', () => {
  447. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
  448. top: 0,
  449. right: 100,
  450. bottom: 100,
  451. left: 0,
  452. width: 100,
  453. height: 100
  454. } );
  455. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  456. top: 150,
  457. right: 200,
  458. bottom: 200,
  459. left: 150,
  460. width: 50,
  461. height: 50
  462. } );
  463. expect( new Rect( element ).getVisible() ).to.equal( null );
  464. } );
  465. } );
  466. describe( 'getViewportRect()', () => {
  467. it( 'should reaturn a rect', () => {
  468. expect( Rect.getViewportRect() ).to.be.instanceOf( Rect );
  469. } );
  470. it( 'should return the viewport\'s rect', () => {
  471. testUtils.sinon.stub( global, 'window', {
  472. innerWidth: 1000,
  473. innerHeight: 500,
  474. scrollX: 100,
  475. scrollY: 200
  476. } );
  477. assertRect( Rect.getViewportRect(), {
  478. top: 0,
  479. right: 1000,
  480. bottom: 500,
  481. left: 0,
  482. width: 1000,
  483. height: 500
  484. } );
  485. } );
  486. } );
  487. } );
  488. function assertRect( rect, expected ) {
  489. expect( rect ).to.deep.equal( expected );
  490. }