rect.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 accept HTMLElement', () => {
  24. const element = document.createElement( 'div' );
  25. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
  26. assertRect( new Rect( element ), geometry );
  27. } );
  28. it( 'should accept Range', () => {
  29. const range = document.createRange();
  30. testUtils.sinon.stub( range, 'getBoundingClientRect' ).returns( geometry );
  31. assertRect( new Rect( range ), geometry );
  32. } );
  33. it( 'should accept Rect', () => {
  34. const sourceRect = new Rect( geometry );
  35. const rect = new Rect( sourceRect );
  36. expect( rect ).to.not.equal( sourceRect );
  37. assertRect( rect, geometry );
  38. } );
  39. it( 'should accept ClientRect', () => {
  40. const clientRect = document.body.getBoundingClientRect();
  41. const { top, right, bottom, left, width, height } = clientRect;
  42. const rect = new Rect( clientRect );
  43. assertRect( rect, { top, right, bottom, left, width, height } );
  44. } );
  45. it( 'should accept geometry object', () => {
  46. assertRect( new Rect( geometry ), geometry );
  47. } );
  48. it( 'should copy the properties (Rect)', () => {
  49. const sourceGeometry = Object.assign( {}, geometry );
  50. const sourceRect = new Rect( geometry );
  51. const rect = new Rect( sourceRect );
  52. assertRect( rect, geometry );
  53. rect.top = 100;
  54. rect.width = 200;
  55. assertRect( sourceRect, sourceGeometry );
  56. } );
  57. it( 'should copy the properties (geomerty object)', () => {
  58. const sourceGeometry = Object.assign( {}, geometry );
  59. const rect = new Rect( geometry );
  60. assertRect( rect, geometry );
  61. rect.top = 100;
  62. rect.width = 200;
  63. assertRect( geometry, sourceGeometry );
  64. } );
  65. } );
  66. describe( 'clone()', () => {
  67. it( 'should clone the source rect', () => {
  68. const rect = new Rect( geometry );
  69. const clone = rect.clone();
  70. expect( clone ).to.be.instanceOf( Rect );
  71. expect( clone ).not.equal( rect );
  72. assertRect( clone, rect );
  73. } );
  74. } );
  75. describe( 'moveTo()', () => {
  76. it( 'should return the source rect', () => {
  77. const rect = new Rect( geometry );
  78. const returned = rect.moveTo( 100, 200 );
  79. expect( returned ).to.equal( rect );
  80. } );
  81. it( 'should move the rect', () => {
  82. const rect = new Rect( geometry );
  83. rect.moveTo( 100, 200 );
  84. assertRect( rect, {
  85. top: 200,
  86. right: 120,
  87. bottom: 220,
  88. left: 100,
  89. width: 20,
  90. height: 20
  91. } );
  92. } );
  93. } );
  94. describe( 'moveBy()', () => {
  95. it( 'should return the source rect', () => {
  96. const rect = new Rect( geometry );
  97. const returned = rect.moveBy( 100, 200 );
  98. expect( returned ).to.equal( rect );
  99. } );
  100. it( 'should move the rect', () => {
  101. const rect = new Rect( geometry );
  102. rect.moveBy( 100, 200 );
  103. assertRect( rect, {
  104. top: 210,
  105. right: 140,
  106. bottom: 230,
  107. left: 120,
  108. width: 20,
  109. height: 20
  110. } );
  111. } );
  112. } );
  113. describe( 'getIntersection()', () => {
  114. it( 'should return a new rect', () => {
  115. const rect = new Rect( geometry );
  116. const insersect = rect.getIntersection( new Rect( geometry ) );
  117. expect( insersect ).to.be.instanceOf( Rect );
  118. expect( insersect ).to.not.equal( rect );
  119. } );
  120. it( 'should calculate the geometry (#1)', () => {
  121. const rectA = new Rect( {
  122. top: 0,
  123. right: 100,
  124. bottom: 100,
  125. left: 0,
  126. width: 100,
  127. height: 100
  128. } );
  129. const rectB = new Rect( {
  130. top: 50,
  131. right: 150,
  132. bottom: 150,
  133. left: 50,
  134. width: 100,
  135. height: 100
  136. } );
  137. const insersect = rectA.getIntersection( rectB );
  138. assertRect( insersect, {
  139. top: 50,
  140. right: 100,
  141. bottom: 100,
  142. left: 50,
  143. width: 50,
  144. height: 50
  145. } );
  146. } );
  147. it( 'should calculate the geometry (#2)', () => {
  148. const rectA = new Rect( {
  149. top: 0,
  150. right: 100,
  151. bottom: 100,
  152. left: 0,
  153. width: 100,
  154. height: 100
  155. } );
  156. const rectB = new Rect( {
  157. top: 0,
  158. right: 200,
  159. bottom: 100,
  160. left: 100,
  161. width: 100,
  162. height: 100
  163. } );
  164. const insersect = rectA.getIntersection( rectB );
  165. assertRect( insersect, {
  166. top: 0,
  167. right: 100,
  168. bottom: 100,
  169. left: 100,
  170. width: 0,
  171. height: 100
  172. } );
  173. } );
  174. it( 'should calculate the geometry (#3)', () => {
  175. const rectA = new Rect( {
  176. top: 0,
  177. right: 100,
  178. bottom: 100,
  179. left: 0,
  180. width: 100,
  181. height: 100
  182. } );
  183. const rectB = new Rect( {
  184. top: 100,
  185. right: 300,
  186. bottom: 200,
  187. left: 200,
  188. width: 100,
  189. height: 100
  190. } );
  191. expect( rectA.getIntersection( rectB ) ).to.be.null;
  192. } );
  193. } );
  194. describe( 'getIntersectionArea()', () => {
  195. it( 'should calculate the area (#1)', () => {
  196. const rectA = new Rect( {
  197. top: 0,
  198. right: 100,
  199. bottom: 100,
  200. left: 0,
  201. width: 100,
  202. height: 100
  203. } );
  204. const rectB = new Rect( {
  205. top: 50,
  206. right: 150,
  207. bottom: 150,
  208. left: 50,
  209. width: 100,
  210. height: 100
  211. } );
  212. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 2500 );
  213. } );
  214. it( 'should calculate the area (#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. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 0 );
  232. } );
  233. it( 'should calculate the area (#3)', () => {
  234. const rectA = new Rect( {
  235. top: 0,
  236. right: 100,
  237. bottom: 100,
  238. left: 0,
  239. width: 100,
  240. height: 100
  241. } );
  242. const rectB = new Rect( {
  243. top: 100,
  244. right: 300,
  245. bottom: 200,
  246. left: 200,
  247. width: 100,
  248. height: 100
  249. } );
  250. expect( rectA.getIntersectionArea( rectB ) ).to.equal( 0 );
  251. } );
  252. } );
  253. describe( 'getArea()', () => {
  254. it( 'should calculate the area', () => {
  255. const rect = new Rect( {
  256. width: 100,
  257. height: 50
  258. } );
  259. expect( rect.getArea() ).to.equal( 5000 );
  260. } );
  261. } );
  262. describe( 'getViewportRect()', () => {
  263. it( 'should reaturn a rect', () => {
  264. expect( Rect.getViewportRect() ).to.be.instanceOf( Rect );
  265. } );
  266. it( 'should return the viewport\'s rect', () => {
  267. testUtils.sinon.stub( global, 'window', {
  268. innerWidth: 1000,
  269. innerHeight: 500,
  270. scrollX: 100,
  271. scrollY: 200
  272. } );
  273. assertRect( Rect.getViewportRect(), {
  274. top: 0,
  275. right: 1000,
  276. bottom: 500,
  277. left: 0,
  278. width: 1000,
  279. height: 500
  280. } );
  281. } );
  282. } );
  283. } );
  284. function assertRect( rect, expected ) {
  285. expect( rect ).to.deep.equal( expected );
  286. }