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