rect.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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 log from '../../src/log';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. testUtils.createSinonSandbox();
  11. describe( 'Rect', () => {
  12. let geometry;
  13. beforeEach( () => {
  14. geometry = {
  15. top: 10,
  16. right: 40,
  17. bottom: 30,
  18. left: 20,
  19. width: 20,
  20. height: 20
  21. };
  22. testUtils.sinon.stub( log, 'warn' );
  23. } );
  24. describe( 'constructor()', () => {
  25. it( 'should store passed object in #_source property', () => {
  26. const obj = {};
  27. const rect = new Rect( obj );
  28. expect( rect._source ).to.equal( obj );
  29. } );
  30. it( 'should accept HTMLElement', () => {
  31. const element = document.createElement( 'div' );
  32. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  33. assertRect( new Rect( element ), geometry );
  34. } );
  35. it( 'should accept Range (non–collapsed)', () => {
  36. const range = document.createRange();
  37. range.selectNode( document.body );
  38. testUtils.sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  39. assertRect( new Rect( range ), geometry );
  40. } );
  41. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  42. it( 'should accept Range (collapsed)', () => {
  43. const range = document.createRange();
  44. range.collapse();
  45. testUtils.sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  46. assertRect( new Rect( range ), geometry );
  47. } );
  48. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  49. it( 'should accept Range (collapsed, no Range rects available)', () => {
  50. const range = document.createRange();
  51. const element = document.createElement( 'div' );
  52. range.setStart( element, 0 );
  53. range.collapse();
  54. testUtils.sinon.stub( range, 'getClientRects' ).returns( [] );
  55. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  56. const expectedGeometry = Object.assign( {}, geometry );
  57. expectedGeometry.right = expectedGeometry.left;
  58. expectedGeometry.width = 0;
  59. assertRect( new Rect( range ), expectedGeometry );
  60. } );
  61. it( 'should accept the window (viewport)', () => {
  62. testUtils.sinon.stub( global, 'window' ).value( {
  63. innerWidth: 1000,
  64. innerHeight: 500,
  65. scrollX: 100,
  66. scrollY: 200
  67. } );
  68. assertRect( new Rect( global.window ), {
  69. top: 0,
  70. right: 1000,
  71. bottom: 500,
  72. left: 0,
  73. width: 1000,
  74. height: 500
  75. } );
  76. } );
  77. it( 'should accept Rect', () => {
  78. const sourceRect = new Rect( geometry );
  79. const rect = new Rect( sourceRect );
  80. expect( rect ).to.not.equal( sourceRect );
  81. assertRect( rect, geometry );
  82. } );
  83. it( 'should accept ClientRect', () => {
  84. const clientRect = document.body.getBoundingClientRect();
  85. const { top, right, bottom, left, width, height } = clientRect;
  86. const rect = new Rect( clientRect );
  87. assertRect( rect, { top, right, bottom, left, width, height } );
  88. } );
  89. it( 'should accept geometry object', () => {
  90. assertRect( new Rect( geometry ), geometry );
  91. } );
  92. it( 'should copy the properties (Rect)', () => {
  93. const sourceGeometry = Object.assign( {}, geometry );
  94. const sourceRect = new Rect( geometry );
  95. const rect = new Rect( sourceRect );
  96. assertRect( rect, geometry );
  97. rect.top = 100;
  98. rect.width = 200;
  99. assertRect( sourceRect, sourceGeometry );
  100. } );
  101. it( 'should copy the properties (geomerty object)', () => {
  102. const sourceGeometry = Object.assign( {}, geometry );
  103. const rect = new Rect( geometry );
  104. assertRect( rect, geometry );
  105. rect.top = 100;
  106. rect.width = 200;
  107. assertRect( geometry, sourceGeometry );
  108. } );
  109. it( 'should warn if the source does not belong to rendered DOM tree (HTML element)', () => {
  110. const element = document.createElement( 'div' );
  111. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  112. const rect = new Rect( element );
  113. sinon.assert.calledOnce( log.warn );
  114. sinon.assert.calledWithExactly( log.warn, sinon.match( /^rect-source-not-in-dom/ ), { source: element } );
  115. assertRect( rect, geometry );
  116. } );
  117. it( 'should warn if the source does not belong to rendered DOM tree (DOM Range)', () => {
  118. const range = document.createRange();
  119. range.collapse();
  120. testUtils.sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  121. const rect = new Rect( range );
  122. sinon.assert.calledOnce( log.warn );
  123. sinon.assert.calledWithExactly( log.warn, sinon.match( /^rect-source-not-in-dom/ ), { source: range } );
  124. assertRect( rect, geometry );
  125. } );
  126. } );
  127. describe( 'clone()', () => {
  128. it( 'should clone the source rect', () => {
  129. const rect = new Rect( geometry );
  130. const clone = rect.clone();
  131. expect( clone ).to.be.instanceOf( Rect );
  132. expect( clone ).not.equal( rect );
  133. assertRect( clone, rect );
  134. } );
  135. it( 'should preserve #_source', () => {
  136. const rect = new Rect( geometry );
  137. const clone = rect.clone();
  138. expect( clone._source ).to.equal( rect._source );
  139. assertRect( clone, rect );
  140. } );
  141. } );
  142. describe( 'moveTo()', () => {
  143. it( 'should return the source rect', () => {
  144. const rect = new Rect( geometry );
  145. const returned = rect.moveTo( 100, 200 );
  146. expect( returned ).to.equal( rect );
  147. } );
  148. it( 'should move the rect', () => {
  149. const rect = new Rect( geometry );
  150. rect.moveTo( 100, 200 );
  151. assertRect( rect, {
  152. top: 200,
  153. right: 120,
  154. bottom: 220,
  155. left: 100,
  156. width: 20,
  157. height: 20
  158. } );
  159. } );
  160. } );
  161. describe( 'moveBy()', () => {
  162. it( 'should return the source rect', () => {
  163. const rect = new Rect( geometry );
  164. const returned = rect.moveBy( 100, 200 );
  165. expect( returned ).to.equal( rect );
  166. } );
  167. it( 'should move the rect', () => {
  168. const rect = new Rect( geometry );
  169. rect.moveBy( 100, 200 );
  170. assertRect( rect, {
  171. top: 210,
  172. right: 140,
  173. bottom: 230,
  174. left: 120,
  175. width: 20,
  176. height: 20
  177. } );
  178. } );
  179. } );
  180. describe( 'getIntersection()', () => {
  181. it( 'should return a new rect', () => {
  182. const rect = new Rect( geometry );
  183. const insersect = rect.getIntersection( new Rect( geometry ) );
  184. expect( insersect ).to.be.instanceOf( Rect );
  185. expect( insersect ).to.not.equal( rect );
  186. } );
  187. it( 'should calculate the geometry (#1)', () => {
  188. const rectA = new Rect( {
  189. top: 0,
  190. right: 100,
  191. bottom: 100,
  192. left: 0,
  193. width: 100,
  194. height: 100
  195. } );
  196. const rectB = new Rect( {
  197. top: 50,
  198. right: 150,
  199. bottom: 150,
  200. left: 50,
  201. width: 100,
  202. height: 100
  203. } );
  204. const insersect = rectA.getIntersection( rectB );
  205. assertRect( insersect, {
  206. top: 50,
  207. right: 100,
  208. bottom: 100,
  209. left: 50,
  210. width: 50,
  211. height: 50
  212. } );
  213. } );
  214. it( 'should calculate the geometry (#2)', () => {
  215. const rectA = new Rect( {
  216. top: 0,
  217. right: 100,
  218. bottom: 100,
  219. left: 0,
  220. width: 100,
  221. height: 100
  222. } );
  223. const rectB = new Rect( {
  224. top: 0,
  225. right: 200,
  226. bottom: 100,
  227. left: 100,
  228. width: 100,
  229. height: 100
  230. } );
  231. const insersect = rectA.getIntersection( rectB );
  232. assertRect( insersect, {
  233. top: 0,
  234. right: 100,
  235. bottom: 100,
  236. left: 100,
  237. width: 0,
  238. height: 100
  239. } );
  240. } );
  241. it( 'should calculate the geometry (#3)', () => {
  242. const rectA = new Rect( {
  243. top: 0,
  244. right: 100,
  245. bottom: 100,
  246. left: 0,
  247. width: 100,
  248. height: 100
  249. } );
  250. const rectB = new Rect( {
  251. top: 100,
  252. right: 300,
  253. bottom: 200,
  254. left: 200,
  255. width: 100,
  256. height: 100
  257. } );
  258. expect( rectA.getIntersection( rectB ) ).to.be.null;
  259. } );
  260. } );
  261. describe( 'getIntersectionArea()', () => {
  262. it( 'should calculate the area (#1)', () => {
  263. const rectA = new Rect( {
  264. top: 0,
  265. right: 100,
  266. bottom: 100,
  267. left: 0,
  268. width: 100,
  269. height: 100
  270. } );
  271. const rectB = new Rect( {
  272. top: 50,
  273. right: 150,
  274. bottom: 150,
  275. left: 50,
  276. width: 100,
  277. height: 100
  278. } );
  279. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 2500 );
  280. } );
  281. it( 'should calculate the area (#2)', () => {
  282. const rectA = new Rect( {
  283. top: 0,
  284. right: 100,
  285. bottom: 100,
  286. left: 0,
  287. width: 100,
  288. height: 100
  289. } );
  290. const rectB = new Rect( {
  291. top: 0,
  292. right: 200,
  293. bottom: 100,
  294. left: 100,
  295. width: 100,
  296. height: 100
  297. } );
  298. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 0 );
  299. } );
  300. it( 'should calculate the area (#3)', () => {
  301. const rectA = new Rect( {
  302. top: 0,
  303. right: 100,
  304. bottom: 100,
  305. left: 0,
  306. width: 100,
  307. height: 100
  308. } );
  309. const rectB = new Rect( {
  310. top: 100,
  311. right: 300,
  312. bottom: 200,
  313. left: 200,
  314. width: 100,
  315. height: 100
  316. } );
  317. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 0 );
  318. } );
  319. } );
  320. describe( 'getArea()', () => {
  321. it( 'should calculate the area', () => {
  322. const rect = new Rect( {
  323. width: 100,
  324. height: 50
  325. } );
  326. expect( rect.getArea() ).to.equal( 5000 );
  327. } );
  328. } );
  329. describe( 'getVisible()', () => {
  330. let element, range, ancestorA, ancestorB;
  331. beforeEach( () => {
  332. element = document.createElement( 'div' );
  333. range = document.createRange();
  334. ancestorA = document.createElement( 'div' );
  335. ancestorB = document.createElement( 'div' );
  336. ancestorA.append( element );
  337. document.body.appendChild( ancestorA );
  338. } );
  339. afterEach( () => {
  340. ancestorA.remove();
  341. ancestorB.remove();
  342. } );
  343. it( 'should return a new rect', () => {
  344. const rect = new Rect( {} );
  345. const visible = rect.getVisible();
  346. expect( visible ).to.not.equal( rect );
  347. } );
  348. it( 'should not fail when the rect is for document#body', () => {
  349. testUtils.sinon.stub( document.body, 'getBoundingClientRect' ).returns( {
  350. top: 0,
  351. right: 100,
  352. bottom: 100,
  353. left: 0,
  354. width: 100,
  355. height: 100
  356. } );
  357. assertRect( new Rect( document.body ).getVisible(), {
  358. top: 0,
  359. right: 100,
  360. bottom: 100,
  361. left: 0,
  362. width: 100,
  363. height: 100
  364. } );
  365. } );
  366. it( 'should return the visible rect (HTMLElement), partially cropped', () => {
  367. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
  368. top: 0,
  369. right: 100,
  370. bottom: 100,
  371. left: 0,
  372. width: 100,
  373. height: 100
  374. } );
  375. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  376. top: 50,
  377. right: 150,
  378. bottom: 150,
  379. left: 50,
  380. width: 100,
  381. height: 100
  382. } );
  383. assertRect( new Rect( element ).getVisible(), {
  384. top: 50,
  385. right: 100,
  386. bottom: 100,
  387. left: 50,
  388. width: 50,
  389. height: 50
  390. } );
  391. } );
  392. it( 'should return the visible rect (HTMLElement), fully visible', () => {
  393. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
  394. top: 0,
  395. right: 100,
  396. bottom: 100,
  397. left: 0,
  398. width: 100,
  399. height: 100
  400. } );
  401. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  402. top: 0,
  403. right: 150,
  404. bottom: 150,
  405. left: 0,
  406. width: 150,
  407. height: 150
  408. } );
  409. assertRect( new Rect( element ).getVisible(), {
  410. top: 0,
  411. right: 100,
  412. bottom: 100,
  413. left: 0,
  414. width: 100,
  415. height: 100
  416. } );
  417. } );
  418. it( 'should return the visible rect (HTMLElement), partially cropped, deep ancestor overflow', () => {
  419. ancestorB.append( ancestorA );
  420. document.body.appendChild( ancestorB );
  421. testUtils.sinon.stub( element, '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: 100,
  432. bottom: 100,
  433. left: 0,
  434. width: 50,
  435. height: 50
  436. } );
  437. testUtils.sinon.stub( ancestorB, 'getBoundingClientRect' ).returns( {
  438. top: 0,
  439. right: 150,
  440. bottom: 100,
  441. left: 50,
  442. width: 100,
  443. height: 100
  444. } );
  445. assertRect( new Rect( element ).getVisible(), {
  446. top: 50,
  447. right: 100,
  448. bottom: 100,
  449. left: 50,
  450. width: 50,
  451. height: 50
  452. } );
  453. } );
  454. it( 'should return the visible rect (Range), partially cropped', () => {
  455. range.setStart( ancestorA, 0 );
  456. range.setEnd( ancestorA, 1 );
  457. testUtils.sinon.stub( range, 'getClientRects' ).returns( [ {
  458. top: 0,
  459. right: 100,
  460. bottom: 100,
  461. left: 0,
  462. width: 100,
  463. height: 100
  464. } ] );
  465. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  466. top: 50,
  467. right: 150,
  468. bottom: 150,
  469. left: 50,
  470. width: 100,
  471. height: 100
  472. } );
  473. assertRect( new Rect( range ).getVisible(), {
  474. top: 50,
  475. right: 100,
  476. bottom: 100,
  477. left: 50,
  478. width: 50,
  479. height: 50
  480. } );
  481. } );
  482. it( 'should return null if there\'s no visible rect', () => {
  483. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
  484. top: 0,
  485. right: 100,
  486. bottom: 100,
  487. left: 0,
  488. width: 100,
  489. height: 100
  490. } );
  491. testUtils.sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  492. top: 150,
  493. right: 200,
  494. bottom: 200,
  495. left: 150,
  496. width: 50,
  497. height: 50
  498. } );
  499. expect( new Rect( element ).getVisible() ).to.equal( null );
  500. } );
  501. } );
  502. describe( 'isEqual()', () => {
  503. it( 'returns `true` when rects are equal', () => {
  504. const rectA = new Rect( {
  505. top: 10,
  506. right: 20,
  507. bottom: 30,
  508. left: 40,
  509. width: 10,
  510. height: 20
  511. } );
  512. const rectB = new Rect( {
  513. top: 10,
  514. right: 20,
  515. bottom: 30,
  516. left: 40,
  517. width: 10,
  518. height: 20
  519. } );
  520. expect( rectA.isEqual( rectB ) ).to.be.true;
  521. expect( rectB.isEqual( rectA ) ).to.be.true;
  522. expect( rectA.isEqual( rectA ) ).to.be.true;
  523. } );
  524. it( 'returns `false` when rects are different', () => {
  525. const rectA = new Rect( {
  526. top: 10,
  527. right: 20,
  528. bottom: 30,
  529. left: 40,
  530. width: 10,
  531. height: 20
  532. } );
  533. const rectB = new Rect( {
  534. top: 10,
  535. right: 20,
  536. bottom: 30,
  537. left: 40,
  538. width: 10,
  539. height: 30 // !
  540. } );
  541. expect( rectA.isEqual( rectB ) ).to.be.false;
  542. expect( rectB.isEqual( rectA ) ).to.be.false;
  543. } );
  544. } );
  545. describe( 'contains()', () => {
  546. it( 'should return true if rects are the same', () => {
  547. const rectA = new Rect( {
  548. top: 0,
  549. right: 100,
  550. bottom: 100,
  551. left: 0,
  552. width: 100,
  553. height: 100
  554. } );
  555. const rectB = new Rect( {
  556. top: 0,
  557. right: 100,
  558. bottom: 100,
  559. left: 0,
  560. width: 100,
  561. height: 100
  562. } );
  563. expect( rectA.isEqual( rectB ) ).to.be.true;
  564. expect( rectA.contains( rectB ) ).to.be.true;
  565. expect( rectB.contains( rectA ) ).to.be.true;
  566. } );
  567. it( 'should return true if rect is within', () => {
  568. const rectA = new Rect( {
  569. top: 0,
  570. right: 100,
  571. bottom: 100,
  572. left: 0,
  573. width: 100,
  574. height: 100
  575. } );
  576. const rectB = new Rect( {
  577. top: 10,
  578. right: 90,
  579. bottom: 90,
  580. left: 10,
  581. width: 80,
  582. height: 80
  583. } );
  584. expect( rectA.contains( rectB ) ).to.be.true;
  585. expect( rectB.contains( rectA ) ).to.be.false;
  586. } );
  587. it( 'should return false if rect extends beyond the boundaries', () => {
  588. const rectA = new Rect( {
  589. top: 0,
  590. right: 100,
  591. bottom: 100,
  592. left: 0,
  593. width: 100,
  594. height: 100
  595. } );
  596. const rectB = new Rect( {
  597. top: 10,
  598. right: 100,
  599. bottom: 110,
  600. left: 0,
  601. width: 100,
  602. height: 100
  603. } );
  604. expect( rectA.contains( rectB ) ).to.be.false;
  605. expect( rectB.contains( rectA ) ).to.be.false;
  606. } );
  607. it( 'should return false if rect is completely beyond the boundaries', () => {
  608. const rectA = new Rect( {
  609. top: 0,
  610. right: 100,
  611. bottom: 100,
  612. left: 0,
  613. width: 100,
  614. height: 100
  615. } );
  616. const rectB = new Rect( {
  617. top: 110,
  618. right: 100,
  619. bottom: 210,
  620. left: 0,
  621. width: 100,
  622. height: 100
  623. } );
  624. expect( rectA.contains( rectB ) ).to.be.false;
  625. expect( rectB.contains( rectA ) ).to.be.false;
  626. } );
  627. } );
  628. describe( 'excludeScrollbarsAndBorders()', () => {
  629. it( 'should exclude scrollbars and borders of a HTMLElement', () => {
  630. const element = document.createElement( 'div' );
  631. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  632. testUtils.sinon.stub( global.window, 'getComputedStyle' ).returns( {
  633. borderTopWidth: '5px',
  634. borderRightWidth: '10px',
  635. borderLeftWidth: '5px',
  636. borderBottomWidth: '10px'
  637. } );
  638. // Simulate 5px srollbars.
  639. Object.defineProperties( element, {
  640. offsetWidth: {
  641. value: 20
  642. },
  643. offsetHeight: {
  644. value: 20
  645. },
  646. clientWidth: {
  647. value: 10
  648. },
  649. clientHeight: {
  650. value: 10
  651. }
  652. } );
  653. assertRect( new Rect( element ).excludeScrollbarsAndBorders(), {
  654. top: 15,
  655. right: 35,
  656. bottom: 25,
  657. left: 25,
  658. width: 10,
  659. height: 10
  660. } );
  661. } );
  662. it( 'should exclude scrollbars from viewport\'s rect', () => {
  663. testUtils.sinon.stub( global, 'window' ).value( {
  664. innerWidth: 1000,
  665. innerHeight: 500,
  666. scrollX: 100,
  667. scrollY: 200
  668. } );
  669. testUtils.sinon.stub( global, 'document' ).value( {
  670. documentElement: {
  671. clientWidth: 990,
  672. clientHeight: 490
  673. }
  674. } );
  675. assertRect( new Rect( global.window ).excludeScrollbarsAndBorders(), {
  676. top: 0,
  677. right: 990,
  678. bottom: 490,
  679. left: 0,
  680. width: 990,
  681. height: 490
  682. } );
  683. } );
  684. } );
  685. describe( 'getDomRangeRects() ', () => {
  686. it( 'should return rects for a Range (non–collapsed)', () => {
  687. const range = document.createRange();
  688. range.selectNode( document.body );
  689. testUtils.sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  690. const rects = Rect.getDomRangeRects( range );
  691. expect( rects ).to.have.length( 1 );
  692. assertRect( rects[ 0 ], geometry );
  693. } );
  694. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  695. it( 'should return rects for a Range (collapsed)', () => {
  696. const range = document.createRange();
  697. const secondGeometry = { top: 20, right: 80, bottom: 60, left: 40, width: 40, height: 40 };
  698. range.collapse();
  699. testUtils.sinon.stub( range, 'getClientRects' ).returns( [ geometry, secondGeometry ] );
  700. const rects = Rect.getDomRangeRects( range );
  701. expect( rects ).to.have.length( 2 );
  702. assertRect( rects[ 0 ], geometry );
  703. assertRect( rects[ 1 ], secondGeometry );
  704. } );
  705. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  706. it( 'should return rects for a Range (collapsed, no Range rects available)', () => {
  707. const range = document.createRange();
  708. const element = document.createElement( 'div' );
  709. range.setStart( element, 0 );
  710. range.collapse();
  711. testUtils.sinon.stub( range, 'getClientRects' ).returns( [] );
  712. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  713. const expectedGeometry = Object.assign( {}, geometry );
  714. expectedGeometry.right = expectedGeometry.left;
  715. expectedGeometry.width = 0;
  716. const rects = Rect.getDomRangeRects( range );
  717. expect( rects ).to.have.length( 1 );
  718. assertRect( rects[ 0 ], expectedGeometry );
  719. } );
  720. } );
  721. } );
  722. function assertRect( rect, expected ) {
  723. expect( rect ).to.deep.equal( expected );
  724. }