rect.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. /* global window, document, console */
  6. import Rect from '../../src/dom/rect';
  7. describe( 'Rect', () => {
  8. let geometry;
  9. beforeEach( () => {
  10. geometry = {
  11. top: 10,
  12. right: 40,
  13. bottom: 30,
  14. left: 20,
  15. width: 20,
  16. height: 20
  17. };
  18. sinon.stub( console, 'warn' );
  19. } );
  20. afterEach( () => {
  21. sinon.restore();
  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. 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. sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  38. assertRect( new Rect( range ), geometry );
  39. } );
  40. it( 'should accept Range (non–collapsed, sequenced horizontally)', () => {
  41. const firstGeometry = geometry;
  42. const secondGeometry = Object.assign( {}, geometry, {
  43. right: 50,
  44. left: 40,
  45. width: 10
  46. } );
  47. const range = document.createRange();
  48. range.selectNode( document.body );
  49. sinon.stub( range, 'getClientRects' ).returns( [ firstGeometry, secondGeometry ] );
  50. const expectedGeometry = Object.assign( {}, geometry, {
  51. width: 30,
  52. right: 50
  53. } );
  54. assertRect( new Rect( range ), expectedGeometry );
  55. } );
  56. it( 'should accept Range (non–collapsed, sequenced vertically)', () => {
  57. const firstGeometry = geometry;
  58. const secondGeometry = Object.assign( {}, geometry, {
  59. top: 30,
  60. bottom: 40
  61. } );
  62. const range = document.createRange();
  63. range.selectNode( document.body );
  64. sinon.stub( range, 'getClientRects' ).returns( [ firstGeometry, secondGeometry ] );
  65. const expectedGeometry = Object.assign( {}, geometry, {
  66. height: 30,
  67. bottom: 40
  68. } );
  69. assertRect( new Rect( range ), expectedGeometry );
  70. } );
  71. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  72. it( 'should accept Range (collapsed)', () => {
  73. const range = document.createRange();
  74. range.collapse();
  75. sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  76. assertRect( new Rect( range ), geometry );
  77. } );
  78. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  79. it( 'should accept Range (collapsed, no Range rects available)', () => {
  80. const range = document.createRange();
  81. const element = document.createElement( 'div' );
  82. range.setStart( element, 0 );
  83. range.collapse();
  84. sinon.stub( range, 'getClientRects' ).returns( [] );
  85. sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  86. const expectedGeometry = Object.assign( {}, geometry );
  87. expectedGeometry.right = expectedGeometry.left;
  88. expectedGeometry.width = 0;
  89. assertRect( new Rect( range ), expectedGeometry );
  90. } );
  91. it( 'should accept the window (viewport)', () => {
  92. sinon.stub( window, 'innerWidth' ).value( 1000 );
  93. sinon.stub( window, 'innerHeight' ).value( 500 );
  94. sinon.stub( window, 'scrollX' ).value( 100 );
  95. sinon.stub( window, 'scrollY' ).value( 200 );
  96. assertRect( new Rect( window ), {
  97. top: 0,
  98. right: 1000,
  99. bottom: 500,
  100. left: 0,
  101. width: 1000,
  102. height: 500
  103. } );
  104. } );
  105. it( 'should accept Rect', () => {
  106. const sourceRect = new Rect( geometry );
  107. const rect = new Rect( sourceRect );
  108. expect( rect ).to.not.equal( sourceRect );
  109. assertRect( rect, geometry );
  110. } );
  111. it( 'should accept ClientRect', () => {
  112. const clientRect = document.body.getBoundingClientRect();
  113. const { top, right, bottom, left, width, height } = clientRect;
  114. const rect = new Rect( clientRect );
  115. assertRect( rect, { top, right, bottom, left, width, height } );
  116. } );
  117. it( 'should accept geometry object', () => {
  118. assertRect( new Rect( geometry ), geometry );
  119. } );
  120. it( 'should accept objects from another window\'s scope', done => {
  121. const iframe = document.createElement( 'iframe' );
  122. iframe.addEventListener( 'load', () => {
  123. const iframeWindow = iframe.contentWindow;
  124. const element = iframeWindow.document.createElement( 'p' );
  125. const range = document.createRange();
  126. range.selectNode( iframeWindow.document.body );
  127. sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  128. assertRect( new Rect( range ), geometry );
  129. sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  130. assertRect( new Rect( element ), geometry );
  131. iframe.remove();
  132. done();
  133. } );
  134. document.body.appendChild( iframe );
  135. } );
  136. it( 'should copy the properties (Rect)', () => {
  137. const sourceGeometry = Object.assign( {}, geometry );
  138. const sourceRect = new Rect( geometry );
  139. const rect = new Rect( sourceRect );
  140. assertRect( rect, geometry );
  141. rect.top = 100;
  142. rect.width = 200;
  143. assertRect( sourceRect, sourceGeometry );
  144. } );
  145. it( 'should copy the properties (geomerty object)', () => {
  146. const sourceGeometry = Object.assign( {}, geometry );
  147. const rect = new Rect( geometry );
  148. assertRect( rect, geometry );
  149. rect.top = 100;
  150. rect.width = 200;
  151. assertRect( geometry, sourceGeometry );
  152. } );
  153. } );
  154. describe( 'clone()', () => {
  155. it( 'should clone the source rect', () => {
  156. const rect = new Rect( geometry );
  157. const clone = rect.clone();
  158. expect( clone ).to.be.instanceOf( Rect );
  159. expect( clone ).not.equal( rect );
  160. assertRect( clone, rect );
  161. } );
  162. it( 'should preserve #_source', () => {
  163. const rect = new Rect( geometry );
  164. const clone = rect.clone();
  165. expect( clone._source ).to.equal( rect._source );
  166. assertRect( clone, rect );
  167. } );
  168. } );
  169. describe( 'moveTo()', () => {
  170. it( 'should return the source rect', () => {
  171. const rect = new Rect( geometry );
  172. const returned = rect.moveTo( 100, 200 );
  173. expect( returned ).to.equal( rect );
  174. } );
  175. it( 'should move the rect', () => {
  176. const rect = new Rect( geometry );
  177. rect.moveTo( 100, 200 );
  178. assertRect( rect, {
  179. top: 200,
  180. right: 120,
  181. bottom: 220,
  182. left: 100,
  183. width: 20,
  184. height: 20
  185. } );
  186. } );
  187. } );
  188. describe( 'moveBy()', () => {
  189. it( 'should return the source rect', () => {
  190. const rect = new Rect( geometry );
  191. const returned = rect.moveBy( 100, 200 );
  192. expect( returned ).to.equal( rect );
  193. } );
  194. it( 'should move the rect', () => {
  195. const rect = new Rect( geometry );
  196. rect.moveBy( 100, 200 );
  197. assertRect( rect, {
  198. top: 210,
  199. right: 140,
  200. bottom: 230,
  201. left: 120,
  202. width: 20,
  203. height: 20
  204. } );
  205. } );
  206. } );
  207. describe( 'getIntersection()', () => {
  208. it( 'should return a new rect', () => {
  209. const rect = new Rect( geometry );
  210. const insersect = rect.getIntersection( new Rect( geometry ) );
  211. expect( insersect ).to.be.instanceOf( Rect );
  212. expect( insersect ).to.not.equal( rect );
  213. } );
  214. it( 'should calculate the geometry (#1)', () => {
  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: 50,
  225. right: 150,
  226. bottom: 150,
  227. left: 50,
  228. width: 100,
  229. height: 100
  230. } );
  231. const insersect = rectA.getIntersection( rectB );
  232. assertRect( insersect, {
  233. top: 50,
  234. right: 100,
  235. bottom: 100,
  236. left: 50,
  237. width: 50,
  238. height: 50
  239. } );
  240. } );
  241. it( 'should calculate the geometry (#2)', () => {
  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: 0,
  252. right: 200,
  253. bottom: 100,
  254. left: 100,
  255. width: 100,
  256. height: 100
  257. } );
  258. const insersect = rectA.getIntersection( rectB );
  259. assertRect( insersect, {
  260. top: 0,
  261. right: 100,
  262. bottom: 100,
  263. left: 100,
  264. width: 0,
  265. height: 100
  266. } );
  267. } );
  268. it( 'should calculate the geometry (#3)', () => {
  269. const rectA = new Rect( {
  270. top: 0,
  271. right: 100,
  272. bottom: 100,
  273. left: 0,
  274. width: 100,
  275. height: 100
  276. } );
  277. const rectB = new Rect( {
  278. top: 100,
  279. right: 300,
  280. bottom: 200,
  281. left: 200,
  282. width: 100,
  283. height: 100
  284. } );
  285. expect( rectA.getIntersection( rectB ) ).to.be.null;
  286. } );
  287. } );
  288. describe( 'getIntersectionArea()', () => {
  289. it( 'should calculate the area (#1)', () => {
  290. const rectA = new Rect( {
  291. top: 0,
  292. right: 100,
  293. bottom: 100,
  294. left: 0,
  295. width: 100,
  296. height: 100
  297. } );
  298. const rectB = new Rect( {
  299. top: 50,
  300. right: 150,
  301. bottom: 150,
  302. left: 50,
  303. width: 100,
  304. height: 100
  305. } );
  306. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 2500 );
  307. } );
  308. it( 'should calculate the area (#2)', () => {
  309. const rectA = new Rect( {
  310. top: 0,
  311. right: 100,
  312. bottom: 100,
  313. left: 0,
  314. width: 100,
  315. height: 100
  316. } );
  317. const rectB = new Rect( {
  318. top: 0,
  319. right: 200,
  320. bottom: 100,
  321. left: 100,
  322. width: 100,
  323. height: 100
  324. } );
  325. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 0 );
  326. } );
  327. it( 'should calculate the area (#3)', () => {
  328. const rectA = new Rect( {
  329. top: 0,
  330. right: 100,
  331. bottom: 100,
  332. left: 0,
  333. width: 100,
  334. height: 100
  335. } );
  336. const rectB = new Rect( {
  337. top: 100,
  338. right: 300,
  339. bottom: 200,
  340. left: 200,
  341. width: 100,
  342. height: 100
  343. } );
  344. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 0 );
  345. } );
  346. } );
  347. describe( 'getArea()', () => {
  348. it( 'should calculate the area', () => {
  349. const rect = new Rect( {
  350. width: 100,
  351. height: 50
  352. } );
  353. expect( rect.getArea() ).to.equal( 5000 );
  354. } );
  355. } );
  356. describe( 'getVisible()', () => {
  357. let element, range, ancestorA, ancestorB;
  358. beforeEach( () => {
  359. element = document.createElement( 'div' );
  360. range = document.createRange();
  361. ancestorA = document.createElement( 'div' );
  362. ancestorB = document.createElement( 'div' );
  363. ancestorA.appendChild( element );
  364. document.body.appendChild( ancestorA );
  365. } );
  366. afterEach( () => {
  367. ancestorA.remove();
  368. ancestorB.remove();
  369. } );
  370. it( 'should return a new rect', () => {
  371. const rect = new Rect( element );
  372. const visible = rect.getVisible();
  373. expect( visible ).to.not.equal( rect );
  374. } );
  375. it( 'should not fail when the rect is for document#body', () => {
  376. sinon.stub( document.body, 'getBoundingClientRect' ).returns( {
  377. top: 0,
  378. right: 100,
  379. bottom: 100,
  380. left: 0,
  381. width: 100,
  382. height: 100
  383. } );
  384. assertRect( new Rect( document.body ).getVisible(), {
  385. top: 0,
  386. right: 100,
  387. bottom: 100,
  388. left: 0,
  389. width: 100,
  390. height: 100
  391. } );
  392. } );
  393. it( 'should not fail when the rect is for an object in another window\'s scope', done => {
  394. const iframe = document.createElement( 'iframe' );
  395. iframe.addEventListener( 'load', () => {
  396. const iframeWindow = iframe.contentWindow;
  397. const element = iframeWindow.document.createElement( 'p' );
  398. const ancestor = iframeWindow.document.createElement( 'p' );
  399. ancestor.appendChild( element );
  400. iframeWindow.document.body.appendChild( ancestor );
  401. sinon.stub( ancestor, 'getBoundingClientRect' ).returns( {
  402. top: 0,
  403. right: 50,
  404. bottom: 50,
  405. left: 0,
  406. width: 50,
  407. height: 50
  408. } );
  409. sinon.stub( element, 'getBoundingClientRect' ).returns( {
  410. top: 0,
  411. right: 100,
  412. bottom: 100,
  413. left: 0,
  414. width: 100,
  415. height: 100
  416. } );
  417. assertRect( new Rect( element ).getVisible(), {
  418. top: 0,
  419. right: 50,
  420. bottom: 50,
  421. left: 0,
  422. width: 50,
  423. height: 50
  424. } );
  425. iframe.remove();
  426. done();
  427. } );
  428. document.body.appendChild( iframe );
  429. } );
  430. it( 'should return the visible rect (HTMLElement), partially cropped', () => {
  431. sinon.stub( element, 'getBoundingClientRect' ).returns( {
  432. top: 0,
  433. right: 100,
  434. bottom: 100,
  435. left: 0,
  436. width: 100,
  437. height: 100
  438. } );
  439. sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  440. top: 50,
  441. right: 150,
  442. bottom: 150,
  443. left: 50,
  444. width: 100,
  445. height: 100
  446. } );
  447. assertRect( new Rect( element ).getVisible(), {
  448. top: 50,
  449. right: 100,
  450. bottom: 100,
  451. left: 50,
  452. width: 50,
  453. height: 50
  454. } );
  455. } );
  456. it( 'should return the visible rect (HTMLElement), fully visible', () => {
  457. sinon.stub( element, 'getBoundingClientRect' ).returns( {
  458. top: 0,
  459. right: 100,
  460. bottom: 100,
  461. left: 0,
  462. width: 100,
  463. height: 100
  464. } );
  465. sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  466. top: 0,
  467. right: 150,
  468. bottom: 150,
  469. left: 0,
  470. width: 150,
  471. height: 150
  472. } );
  473. assertRect( new Rect( element ).getVisible(), {
  474. top: 0,
  475. right: 100,
  476. bottom: 100,
  477. left: 0,
  478. width: 100,
  479. height: 100
  480. } );
  481. } );
  482. it( 'should return the visible rect (HTMLElement), partially cropped, deep ancestor overflow', () => {
  483. ancestorB.appendChild( ancestorA );
  484. document.body.appendChild( ancestorB );
  485. sinon.stub( element, 'getBoundingClientRect' ).returns( {
  486. top: 0,
  487. right: 100,
  488. bottom: 100,
  489. left: 0,
  490. width: 100,
  491. height: 100
  492. } );
  493. sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  494. top: 50,
  495. right: 100,
  496. bottom: 100,
  497. left: 0,
  498. width: 50,
  499. height: 50
  500. } );
  501. sinon.stub( ancestorB, 'getBoundingClientRect' ).returns( {
  502. top: 0,
  503. right: 150,
  504. bottom: 100,
  505. left: 50,
  506. width: 100,
  507. height: 100
  508. } );
  509. assertRect( new Rect( element ).getVisible(), {
  510. top: 50,
  511. right: 100,
  512. bottom: 100,
  513. left: 50,
  514. width: 50,
  515. height: 50
  516. } );
  517. } );
  518. it( 'should return the visible rect (Range), partially cropped', () => {
  519. range.setStart( ancestorA, 0 );
  520. range.setEnd( ancestorA, 1 );
  521. sinon.stub( range, 'getClientRects' ).returns( [ {
  522. top: 0,
  523. right: 100,
  524. bottom: 100,
  525. left: 0,
  526. width: 100,
  527. height: 100
  528. } ] );
  529. sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  530. top: 50,
  531. right: 150,
  532. bottom: 150,
  533. left: 50,
  534. width: 100,
  535. height: 100
  536. } );
  537. assertRect( new Rect( range ).getVisible(), {
  538. top: 50,
  539. right: 100,
  540. bottom: 100,
  541. left: 50,
  542. width: 50,
  543. height: 50
  544. } );
  545. } );
  546. it( 'should return null if there\'s no visible rect', () => {
  547. sinon.stub( element, 'getBoundingClientRect' ).returns( {
  548. top: 0,
  549. right: 100,
  550. bottom: 100,
  551. left: 0,
  552. width: 100,
  553. height: 100
  554. } );
  555. sinon.stub( ancestorA, 'getBoundingClientRect' ).returns( {
  556. top: 150,
  557. right: 200,
  558. bottom: 200,
  559. left: 150,
  560. width: 50,
  561. height: 50
  562. } );
  563. expect( new Rect( element ).getVisible() ).to.equal( null );
  564. } );
  565. } );
  566. describe( 'isEqual()', () => {
  567. it( 'returns `true` when rects are equal', () => {
  568. const rectA = new Rect( {
  569. top: 10,
  570. right: 20,
  571. bottom: 30,
  572. left: 40,
  573. width: 10,
  574. height: 20
  575. } );
  576. const rectB = new Rect( {
  577. top: 10,
  578. right: 20,
  579. bottom: 30,
  580. left: 40,
  581. width: 10,
  582. height: 20
  583. } );
  584. expect( rectA.isEqual( rectB ) ).to.be.true;
  585. expect( rectB.isEqual( rectA ) ).to.be.true;
  586. expect( rectA.isEqual( rectA ) ).to.be.true;
  587. } );
  588. it( 'returns `false` when rects are different', () => {
  589. const rectA = new Rect( {
  590. top: 10,
  591. right: 20,
  592. bottom: 30,
  593. left: 40,
  594. width: 10,
  595. height: 20
  596. } );
  597. const rectB = new Rect( {
  598. top: 10,
  599. right: 20,
  600. bottom: 30,
  601. left: 40,
  602. width: 10,
  603. height: 30 // !
  604. } );
  605. expect( rectA.isEqual( rectB ) ).to.be.false;
  606. expect( rectB.isEqual( rectA ) ).to.be.false;
  607. } );
  608. } );
  609. describe( 'contains()', () => {
  610. it( 'should return true if rects are the same', () => {
  611. const rectA = new Rect( {
  612. top: 0,
  613. right: 100,
  614. bottom: 100,
  615. left: 0,
  616. width: 100,
  617. height: 100
  618. } );
  619. const rectB = new Rect( {
  620. top: 0,
  621. right: 100,
  622. bottom: 100,
  623. left: 0,
  624. width: 100,
  625. height: 100
  626. } );
  627. expect( rectA.isEqual( rectB ) ).to.be.true;
  628. expect( rectA.contains( rectB ) ).to.be.true;
  629. expect( rectB.contains( rectA ) ).to.be.true;
  630. } );
  631. it( 'should return true if rect is within', () => {
  632. const rectA = new Rect( {
  633. top: 0,
  634. right: 100,
  635. bottom: 100,
  636. left: 0,
  637. width: 100,
  638. height: 100
  639. } );
  640. const rectB = new Rect( {
  641. top: 10,
  642. right: 90,
  643. bottom: 90,
  644. left: 10,
  645. width: 80,
  646. height: 80
  647. } );
  648. expect( rectA.contains( rectB ) ).to.be.true;
  649. expect( rectB.contains( rectA ) ).to.be.false;
  650. } );
  651. it( 'should return false if rect extends beyond the boundaries', () => {
  652. const rectA = new Rect( {
  653. top: 0,
  654. right: 100,
  655. bottom: 100,
  656. left: 0,
  657. width: 100,
  658. height: 100
  659. } );
  660. const rectB = new Rect( {
  661. top: 10,
  662. right: 100,
  663. bottom: 110,
  664. left: 0,
  665. width: 100,
  666. height: 100
  667. } );
  668. expect( rectA.contains( rectB ) ).to.be.false;
  669. expect( rectB.contains( rectA ) ).to.be.false;
  670. } );
  671. it( 'should return false if rect is completely beyond the boundaries', () => {
  672. const rectA = new Rect( {
  673. top: 0,
  674. right: 100,
  675. bottom: 100,
  676. left: 0,
  677. width: 100,
  678. height: 100
  679. } );
  680. const rectB = new Rect( {
  681. top: 110,
  682. right: 100,
  683. bottom: 210,
  684. left: 0,
  685. width: 100,
  686. height: 100
  687. } );
  688. expect( rectA.contains( rectB ) ).to.be.false;
  689. expect( rectB.contains( rectA ) ).to.be.false;
  690. } );
  691. } );
  692. describe( 'excludeScrollbarsAndBorders()', () => {
  693. it( 'should exclude scrollbars and borders of a HTMLElement (dir="ltr")', () => {
  694. const element = document.createElement( 'div' );
  695. sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  696. sinon.stub( window, 'getComputedStyle' )
  697. .withArgs( element )
  698. .returns( {
  699. borderTopWidth: '5px',
  700. borderRightWidth: '10px',
  701. borderLeftWidth: '5px',
  702. borderBottomWidth: '10px',
  703. direction: 'ltr'
  704. } );
  705. // Simulate 5px scrollbars.
  706. Object.defineProperties( element, {
  707. offsetWidth: {
  708. value: 20
  709. },
  710. offsetHeight: {
  711. value: 20
  712. },
  713. clientWidth: {
  714. value: 0
  715. },
  716. clientHeight: {
  717. value: 0
  718. }
  719. } );
  720. assertRect( new Rect( element ).excludeScrollbarsAndBorders(), {
  721. top: 15,
  722. right: 25,
  723. bottom: 15,
  724. left: 25,
  725. width: 0,
  726. height: 0
  727. } );
  728. } );
  729. it( 'should exclude scrollbars and borders of a HTMLElement (dir="rtl")', () => {
  730. const element = document.createElement( 'div' );
  731. element.setAttribute( 'dir', 'rtl' );
  732. sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  733. sinon.stub( window, 'getComputedStyle' )
  734. .withArgs( element )
  735. .returns( {
  736. borderTopWidth: '5px',
  737. borderRightWidth: '10px',
  738. borderLeftWidth: '5px',
  739. borderBottomWidth: '10px',
  740. direction: 'rtl'
  741. } );
  742. // Simulate 5px scrollbars.
  743. Object.defineProperties( element, {
  744. offsetWidth: {
  745. value: 20
  746. },
  747. offsetHeight: {
  748. value: 20
  749. },
  750. clientWidth: {
  751. value: 0
  752. },
  753. clientHeight: {
  754. value: 0
  755. }
  756. } );
  757. assertRect( new Rect( element ).excludeScrollbarsAndBorders(), {
  758. top: 15,
  759. right: 30,
  760. bottom: 15,
  761. left: 30,
  762. width: 0,
  763. height: 0
  764. } );
  765. } );
  766. it( 'should exclude scrollbars from viewport\'s rect (dir="ltr")', () => {
  767. sinon.stub( window, 'innerWidth' ).value( 1000 );
  768. sinon.stub( window, 'innerHeight' ).value( 500 );
  769. sinon.stub( window, 'scrollX' ).value( 100 );
  770. sinon.stub( window, 'scrollY' ).value( 200 );
  771. sinon.stub( document, 'documentElement' ).value( {
  772. clientWidth: 990,
  773. clientHeight: 490
  774. } );
  775. sinon.stub( window, 'getComputedStyle' )
  776. .withArgs( document.documentElement )
  777. .returns( {
  778. direction: 'ltr'
  779. } );
  780. assertRect( new Rect( window ).excludeScrollbarsAndBorders(), {
  781. top: 0,
  782. right: 990,
  783. bottom: 490,
  784. left: 0,
  785. width: 990,
  786. height: 490
  787. } );
  788. } );
  789. it( 'should exclude scrollbars from viewport\'s rect (dir="rtl")', () => {
  790. sinon.stub( window, 'innerWidth' ).value( 1000 );
  791. sinon.stub( window, 'innerHeight' ).value( 500 );
  792. sinon.stub( window, 'scrollX' ).value( 100 );
  793. sinon.stub( window, 'scrollY' ).value( 200 );
  794. sinon.stub( document, 'documentElement' ).value( {
  795. clientWidth: 990,
  796. clientHeight: 490
  797. } );
  798. sinon.stub( window, 'getComputedStyle' )
  799. .withArgs( document.documentElement )
  800. .returns( {
  801. direction: 'rtl'
  802. } );
  803. assertRect( new Rect( window ).excludeScrollbarsAndBorders(), {
  804. top: 0,
  805. right: 1000,
  806. bottom: 490,
  807. left: 10,
  808. width: 990,
  809. height: 490
  810. } );
  811. } );
  812. it( 'should work for a window in an iframe', done => {
  813. const iframe = document.createElement( 'iframe' );
  814. // Mock the properties of the top window. Then make sure the ones
  815. // from the child are used.
  816. sinon.stub( window, 'innerWidth' ).value( 1000 );
  817. sinon.stub( window, 'innerHeight' ).value( 500 );
  818. sinon.stub( window, 'scrollX' ).value( 100 );
  819. sinon.stub( window, 'scrollY' ).value( 200 );
  820. sinon.stub( document, 'documentElement' ).value( {
  821. clientWidth: 990,
  822. clientHeight: 490
  823. } );
  824. sinon.stub( window, 'getComputedStyle' )
  825. .withArgs( document.documentElement )
  826. .returns( {
  827. direction: 'ltr'
  828. } );
  829. iframe.addEventListener( 'load', () => {
  830. const iframeWindow = iframe.contentWindow;
  831. sinon.stub( iframeWindow, 'innerWidth' ).value( 500 );
  832. sinon.stub( iframeWindow, 'innerHeight' ).value( 250 );
  833. sinon.stub( iframeWindow, 'scrollX' ).value( 50 );
  834. sinon.stub( iframeWindow, 'scrollY' ).value( 100 );
  835. sinon.stub( iframeWindow.document, 'documentElement' ).value( {
  836. clientWidth: 480,
  837. clientHeight: 230
  838. } );
  839. sinon.stub( iframeWindow, 'getComputedStyle' )
  840. .withArgs( iframeWindow.document.documentElement )
  841. .returns( {
  842. direction: 'ltr'
  843. } );
  844. assertRect( new Rect( iframeWindow ).excludeScrollbarsAndBorders(), {
  845. top: 0,
  846. right: 480,
  847. bottom: 230,
  848. left: 0,
  849. width: 480,
  850. height: 230
  851. } );
  852. // Safari fails because of "afterEach()" hook tries to restore values from removed element.
  853. // We need to restore these values manually.
  854. sinon.restore();
  855. iframe.remove();
  856. done();
  857. } );
  858. document.body.appendChild( iframe );
  859. } );
  860. } );
  861. describe( 'getDomRangeRects() ', () => {
  862. it( 'should return rects for a Range (non–collapsed)', () => {
  863. const range = document.createRange();
  864. range.selectNode( document.body );
  865. sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );
  866. const rects = Rect.getDomRangeRects( range );
  867. expect( rects ).to.have.length( 1 );
  868. assertRect( rects[ 0 ], geometry );
  869. } );
  870. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  871. it( 'should return rects for a Range (collapsed)', () => {
  872. const range = document.createRange();
  873. const secondGeometry = { top: 20, right: 80, bottom: 60, left: 40, width: 40, height: 40 };
  874. range.collapse();
  875. sinon.stub( range, 'getClientRects' ).returns( [ geometry, secondGeometry ] );
  876. const rects = Rect.getDomRangeRects( range );
  877. expect( rects ).to.have.length( 2 );
  878. assertRect( rects[ 0 ], geometry );
  879. assertRect( rects[ 1 ], secondGeometry );
  880. } );
  881. // https://github.com/ckeditor/ckeditor5-utils/issues/153
  882. it( 'should return rects for a Range (collapsed, no Range rects available)', () => {
  883. const range = document.createRange();
  884. const element = document.createElement( 'div' );
  885. range.setStart( element, 0 );
  886. range.collapse();
  887. sinon.stub( range, 'getClientRects' ).returns( [] );
  888. sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  889. const expectedGeometry = Object.assign( {}, geometry );
  890. expectedGeometry.right = expectedGeometry.left;
  891. expectedGeometry.width = 0;
  892. const rects = Rect.getDomRangeRects( range );
  893. expect( rects ).to.have.length( 1 );
  894. assertRect( rects[ 0 ], expectedGeometry );
  895. } );
  896. // https://github.com/ckeditor/ckeditor5-ui/issues/317
  897. it( 'should return rects for a text node\'s parent (collapsed, no Range rects available)', () => {
  898. const range = document.createRange();
  899. const element = document.createElement( 'div' );
  900. const textNode = document.createTextNode( 'abc' );
  901. element.appendChild( textNode );
  902. range.setStart( textNode, 3 );
  903. range.collapse();
  904. sinon.stub( range, 'getClientRects' ).returns( [] );
  905. sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  906. const expectedGeometry = Object.assign( {}, geometry );
  907. expectedGeometry.right = expectedGeometry.left;
  908. expectedGeometry.width = 0;
  909. const rects = Rect.getDomRangeRects( range );
  910. expect( rects ).to.have.length( 1 );
  911. assertRect( rects[ 0 ], expectedGeometry );
  912. } );
  913. } );
  914. describe( 'getBoundingRect()', () => {
  915. it( 'should not return a rect instance when no rectangles were given', () => {
  916. expect( Rect.getBoundingRect( [] ) ).to.be.null;
  917. } );
  918. it( 'should calculate proper rectangle when multiple rectangles were given', () => {
  919. const rects = [
  920. new Rect( geometry ),
  921. new Rect( {
  922. top: 10,
  923. right: 100,
  924. bottom: 20,
  925. left: 80,
  926. width: 20,
  927. height: 10
  928. } ),
  929. new Rect( {
  930. top: 50,
  931. right: 50,
  932. bottom: 60,
  933. left: 30,
  934. width: 20,
  935. height: 10
  936. } )
  937. ];
  938. assertRect( Rect.getBoundingRect( rects ), {
  939. top: 10,
  940. right: 100,
  941. bottom: 60,
  942. left: 20,
  943. width: 80,
  944. height: 50
  945. } );
  946. } );
  947. it( 'should calculate proper rectangle when a single rectangles was given', () => {
  948. const rectangles = new Set( [ new Rect( geometry ) ] );
  949. assertRect( Rect.getBoundingRect( rectangles ), geometry );
  950. } );
  951. it( 'should return proper type', () => {
  952. const rectangles = new Set( [ new Rect( geometry ) ] );
  953. expect( Rect.getBoundingRect( rectangles ) ).to.be.instanceOf( Rect );
  954. } );
  955. } );
  956. } );
  957. function assertRect( rect, expected ) {
  958. expect( rect ).to.deep.equal( expected );
  959. }