8
0

selection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Selection from '/ckeditor5/engine/treeview/selection.js';
  8. import Range from '/ckeditor5/engine/treeview/range.js';
  9. import Element from '/ckeditor5/engine/treeview/element.js';
  10. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. describe( 'Selection', () => {
  12. let selection;
  13. let el;
  14. let range1, range2, range3;
  15. beforeEach( () => {
  16. selection = new Selection();
  17. el = new Element( 'p' );
  18. range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  19. range2 = Range.createFromParentsAndOffsets( el, 1, el, 2 );
  20. range3 = Range.createFromParentsAndOffsets( el, 12, el, 14 );
  21. } );
  22. describe( 'anchor', () => {
  23. it( 'should return null if no ranges in selection', () => {
  24. expect( selection.anchor ).to.be.null;
  25. } );
  26. it( 'should return start of single range in selection', () => {
  27. selection.addRange( range1 );
  28. const anchor = selection.anchor;
  29. expect( anchor.isEqual( range1.start ) ).to.be.true;
  30. expect( anchor ).to.not.equal( range1.start );
  31. } );
  32. it( 'should return end of single range in selection when added as backward', () => {
  33. selection.addRange( range1, true );
  34. const anchor = selection.anchor;
  35. expect( anchor.isEqual( range1.end ) ).to.be.true;
  36. expect( anchor ).to.not.equal( range1.end );
  37. } );
  38. it( 'should get anchor from last inserted range', () => {
  39. selection.addRange( range1 );
  40. selection.addRange( range2 );
  41. expect( selection.anchor.isEqual( range2.start ) ).to.be.true;
  42. } );
  43. } );
  44. describe( 'focus', () => {
  45. it( 'should return null if no ranges in selection', () => {
  46. expect( selection.focus ).to.be.null;
  47. } );
  48. it( 'should return end of single range in selection', () => {
  49. selection.addRange( range1 );
  50. const focus = selection.focus;
  51. expect( focus.isEqual( range1.end ) ).to.be.true;
  52. } );
  53. it( 'should return start of single range in selection when added as backward', () => {
  54. selection.addRange( range1, true );
  55. const focus = selection.focus;
  56. expect( focus.isEqual( range1.start ) ).to.be.true;
  57. expect( focus ).to.not.equal( range1.start );
  58. } );
  59. it( 'should get focus from last inserted range', () => {
  60. selection.addRange( range1 );
  61. selection.addRange( range2 );
  62. expect( selection.focus.isEqual( range2.end ) ).to.be.true;
  63. } );
  64. } );
  65. describe( 'isCollapsed', () => {
  66. it( 'should return true when there is single collapsed range', () => {
  67. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  68. selection.addRange( range );
  69. expect( selection.isCollapsed ).to.be.true;
  70. } );
  71. it( 'should return false when there are multiple ranges', () => {
  72. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  73. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
  74. selection.addRange( range1 );
  75. selection.addRange( range2 );
  76. expect( selection.isCollapsed ).to.be.false;
  77. } );
  78. it( 'should return false when there is not collapsed range', () => {
  79. const range = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  80. selection.addRange( range );
  81. expect( selection.isCollapsed ).to.be.false;
  82. } );
  83. } );
  84. describe( 'rangeCount', () => {
  85. it( 'should return proper range count', () => {
  86. expect( selection.rangeCount ).to.equal( 0 );
  87. selection.addRange( range1 );
  88. expect( selection.rangeCount ).to.equal( 1 );
  89. selection.addRange( range2 );
  90. expect( selection.rangeCount ).to.equal( 2 );
  91. } );
  92. } );
  93. describe( 'addRange', () => {
  94. it( 'should add range to selection ranges', () => {
  95. selection.addRange( range1 );
  96. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  97. } );
  98. it( 'should fire change event', ( done ) => {
  99. selection.once( 'change', () => {
  100. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  101. done();
  102. } );
  103. selection.addRange( range1 );
  104. } );
  105. it( 'should throw when range is intersecting with already added range', () => {
  106. const range2 = Range.createFromParentsAndOffsets( el, 7, el, 15 );
  107. selection.addRange( range1 );
  108. expect( () => {
  109. selection.addRange( range2 );
  110. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  111. expect( () => {
  112. selection.addRange( range1 );
  113. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  114. } );
  115. } );
  116. describe( 'getRanges', () => {
  117. it( 'should return iterator with copies of all ranges', () => {
  118. selection.addRange( range1 );
  119. selection.addRange( range2 );
  120. const iterable = selection.getRanges();
  121. const ranges = Array.from( iterable );
  122. expect( ranges.length ).to.equal( 2 );
  123. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  124. expect( ranges[ 0 ] ).to.not.equal( range1 );
  125. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  126. expect( ranges[ 1 ] ).to.not.equal( range2 );
  127. } );
  128. } );
  129. describe( 'getFirstRange', () => {
  130. it( 'should return copy of range with first position', () => {
  131. selection.addRange( range1 );
  132. selection.addRange( range2 );
  133. selection.addRange( range3 );
  134. const range = selection.getFirstRange();
  135. expect( range.isEqual( range2 ) ).to.be.true;
  136. expect( range ).to.not.equal( range2 );
  137. } );
  138. it( 'should return null if no ranges are present', () => {
  139. expect( selection.getFirstRange() ).to.be.null;
  140. } );
  141. } );
  142. describe( 'getLastRange', () => {
  143. it( 'should return copy of range with last position', () => {
  144. selection.addRange( range1 );
  145. selection.addRange( range2 );
  146. selection.addRange( range3 );
  147. const range = selection.getLastRange();
  148. expect( range.isEqual( range3 ) ).to.be.true;
  149. expect( range ).to.not.equal( range3 );
  150. } );
  151. it( 'should return null if no ranges are present', () => {
  152. expect( selection.getLastRange() ).to.be.null;
  153. } );
  154. } );
  155. describe( 'getFirstPosition', () => {
  156. it( 'should return copy of first position', () => {
  157. selection.addRange( range1 );
  158. selection.addRange( range2 );
  159. selection.addRange( range3 );
  160. const position = selection.getFirstPosition();
  161. expect( position.isEqual( range2.start ) ).to.be.true;
  162. expect( position ).to.not.equal( range2.start );
  163. } );
  164. it( 'should return null if no ranges are present', () => {
  165. expect( selection.getFirstPosition() ).to.be.null;
  166. } );
  167. } );
  168. describe( 'getLastPosition', () => {
  169. it( 'should return copy of range with last position', () => {
  170. selection.addRange( range1 );
  171. selection.addRange( range2 );
  172. selection.addRange( range3 );
  173. const position = selection.getLastPosition();
  174. expect( position.isEqual( range3.end ) ).to.be.true;
  175. expect( position ).to.not.equal( range3.end );
  176. } );
  177. it( 'should return null if no ranges are present', () => {
  178. expect( selection.getLastPosition() ).to.be.null;
  179. } );
  180. } );
  181. describe( 'isEqual', () => {
  182. it( 'should return true if selections equal', () => {
  183. selection.addRange( range1 );
  184. selection.addRange( range2 );
  185. const otherSelection = new Selection();
  186. otherSelection.addRange( range1 );
  187. otherSelection.addRange( range2 );
  188. expect( selection.isEqual( otherSelection ) ).to.be.true;
  189. } );
  190. it( 'should return true if backward selections equal', () => {
  191. selection.addRange( range1, true );
  192. const otherSelection = new Selection();
  193. otherSelection.addRange( range1, true );
  194. expect( selection.isEqual( otherSelection ) ).to.be.true;
  195. } );
  196. it( 'should return false if ranges count does not equal', () => {
  197. selection.addRange( range1 );
  198. selection.addRange( range2 );
  199. const otherSelection = new Selection();
  200. otherSelection.addRange( range1 );
  201. expect( selection.isEqual( otherSelection ) ).to.be.false;
  202. } );
  203. it( 'should return false if ranges do not equal', () => {
  204. selection.addRange( range1 );
  205. const otherSelection = new Selection();
  206. otherSelection.addRange( range2 );
  207. expect( selection.isEqual( otherSelection ) ).to.be.false;
  208. } );
  209. it( 'should return false if directions do not equal', () => {
  210. selection.addRange( range1 );
  211. const otherSelection = new Selection();
  212. otherSelection.addRange( range2, true );
  213. expect( selection.isEqual( otherSelection ) ).to.be.false;
  214. } );
  215. } );
  216. describe( 'removeAllRanges', () => {
  217. it( 'should remove all ranges and fire change event', ( done ) => {
  218. selection.addRange( range1 );
  219. selection.addRange( range2 );
  220. selection.once( 'change', () => {
  221. expect( selection.rangeCount ).to.equal( 0 );
  222. done();
  223. } );
  224. selection.removeAllRanges();
  225. } );
  226. it( 'should do nothing when no ranges are present', () => {
  227. const fireSpy = sinon.spy( selection, 'fire' );
  228. selection.removeAllRanges();
  229. fireSpy.restore();
  230. expect( fireSpy.notCalled ).to.be.true;
  231. } );
  232. } );
  233. describe( 'setRanges', () => {
  234. it( 'should add ranges and fire change event', ( done ) => {
  235. selection.addRange( range1 );
  236. selection.once( 'change', () => {
  237. expect( selection.rangeCount ).to.equal( 2 );
  238. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  239. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  240. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  241. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  242. done();
  243. } );
  244. selection.setRanges( [ range2, range3 ] );
  245. } );
  246. } );
  247. describe( 'setTo', () => {
  248. it( 'should return true if selections equal', () => {
  249. selection.addRange( range1 );
  250. const otherSelection = new Selection();
  251. otherSelection.addRange( range2 );
  252. otherSelection.addRange( range3, true );
  253. selection.setTo( otherSelection );
  254. expect( selection.rangeCount ).to.equal( 2 );
  255. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  256. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  257. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  258. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  259. expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
  260. } );
  261. } );
  262. describe( 'collapseToStart', () => {
  263. it( 'should collapse to start position and fire change event', ( done ) => {
  264. selection.setRanges( [ range1, range2, range3 ] );
  265. selection.once( 'change', () => {
  266. expect( selection.rangeCount ).to.equal( 1 );
  267. expect( selection.isCollapsed ).to.be.true;
  268. expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  269. done();
  270. } );
  271. selection.collapseToStart();
  272. } );
  273. it( 'should do nothing if no ranges present', () => {
  274. const fireSpy = sinon.spy( selection, 'fire' );
  275. selection.collapseToStart();
  276. fireSpy.restore();
  277. expect( fireSpy.notCalled ).to.be.true;
  278. } );
  279. } );
  280. describe( 'collapseToEnd', () => {
  281. it( 'should collapse to end position and fire change event', ( done ) => {
  282. selection.setRanges( [ range1, range2, range3 ] );
  283. selection.once( 'change', () => {
  284. expect( selection.rangeCount ).to.equal( 1 );
  285. expect( selection.isCollapsed ).to.be.true;
  286. expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  287. done();
  288. } );
  289. selection.collapseToEnd();
  290. } );
  291. it( 'should do nothing if no ranges present', () => {
  292. const fireSpy = sinon.spy( selection, 'fire' );
  293. selection.collapseToEnd();
  294. fireSpy.restore();
  295. expect( fireSpy.notCalled ).to.be.true;
  296. } );
  297. } );
  298. } );