rect.js 14 KB

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