selection.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 all ranges are collapsed', () => {
  67. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  68. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
  69. selection.addRange( range1 );
  70. selection.addRange( range2 );
  71. expect( selection.isCollapsed ).to.be.true;
  72. } );
  73. it( 'should return false when not all ranges are collapsed', () => {
  74. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  75. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  76. selection.addRange( range1 );
  77. selection.addRange( range2 );
  78. expect( selection.isCollapsed ).to.be.false;
  79. } );
  80. } );
  81. describe( 'rangeCount', () => {
  82. it( 'should return proper range count', () => {
  83. expect( selection.rangeCount ).to.equal( 0 );
  84. selection.addRange( range1 );
  85. expect( selection.rangeCount ).to.equal( 1 );
  86. selection.addRange( range2 );
  87. expect( selection.rangeCount ).to.equal( 2 );
  88. } );
  89. } );
  90. describe( 'addRange', () => {
  91. it( 'should add range to selection ranges', () => {
  92. selection.addRange( range1 );
  93. expect( selection.getRangeAt( 0 ).isEqual( range1 ) ).to.be.true;
  94. } );
  95. it( 'should fire change event', ( done ) => {
  96. selection.once( 'change:range', () => {
  97. expect( selection.getRangeAt( 0 ).isEqual( range1 ) ).to.be.true;
  98. done();
  99. } );
  100. selection.addRange( range1 );
  101. } );
  102. it( 'should throw when range is intersecting with already added range', () => {
  103. const range2 = Range.createFromParentsAndOffsets( el, 7, el, 15 );
  104. selection.addRange( range1 );
  105. expect( () => {
  106. selection.addRange( range2 );
  107. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  108. expect( () => {
  109. selection.addRange( range1 );
  110. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  111. } );
  112. } );
  113. describe( 'getRanges', () => {
  114. it( 'should return copy array of all ranges', () => {
  115. selection.addRange( range1 );
  116. selection.addRange( range2 );
  117. const ranges = selection.getRanges();
  118. expect( ranges.length ).to.equal( 2 );
  119. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  120. expect( ranges[ 0 ] ).to.not.equal( range1 );
  121. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  122. expect( ranges[ 1 ] ).to.not.equal( range2 );
  123. } );
  124. } );
  125. describe( 'getRangeAt', () => {
  126. it( 'should return copy of range at specified index', () => {
  127. selection.addRange( range1 );
  128. selection.addRange( range2 );
  129. selection.addRange( range3 );
  130. expect( selection.getRangeAt( 0 ).isEqual( range1 ) ).to.be.true;
  131. expect( selection.getRangeAt( 1 ).isEqual( range2 ) ).to.be.true;
  132. expect( selection.getRangeAt( 2 ).isEqual( range3 ) ).to.be.true;
  133. expect( selection.getRangeAt( 0 ) ).to.not.equal( range1 );
  134. expect( selection.getRangeAt( 1 ) ).to.not.equal( range2 );
  135. expect( selection.getRangeAt( 2 ) ).to.not.equal( range3 );
  136. } );
  137. it( 'should return null if no range at given index', () => {
  138. expect( selection.getRangeAt( 0 ) ).to.be.null;
  139. } );
  140. } );
  141. describe( 'getFirstRange', () => {
  142. it( 'should return copy of range with first position', () => {
  143. selection.addRange( range1 );
  144. selection.addRange( range2 );
  145. selection.addRange( range3 );
  146. const range = selection.getFirstRange();
  147. expect( range.isEqual( range2 ) ).to.be.true;
  148. expect( range ).to.not.equal( range2 );
  149. } );
  150. it( 'should return null if no ranges are present', () => {
  151. expect( selection.getFirstRange() ).to.be.null;
  152. } );
  153. } );
  154. describe( 'getLastRange', () => {
  155. it( 'should return copy of range with last position', () => {
  156. selection.addRange( range1 );
  157. selection.addRange( range2 );
  158. selection.addRange( range3 );
  159. const range = selection.getLastRange();
  160. expect( range.isEqual( range3 ) ).to.be.true;
  161. expect( range ).to.not.equal( range3 );
  162. } );
  163. it( 'should return null if no ranges are present', () => {
  164. expect( selection.getLastRange() ).to.be.null;
  165. } );
  166. } );
  167. describe( 'getFirstPosition', () => {
  168. it( 'should return copy of first position', () => {
  169. selection.addRange( range1 );
  170. selection.addRange( range2 );
  171. selection.addRange( range3 );
  172. const position = selection.getFirstPosition();
  173. expect( position.isEqual( range2.start ) ).to.be.true;
  174. expect( position ).to.not.equal( range2.start );
  175. } );
  176. it( 'should return null if no ranges are present', () => {
  177. expect( selection.getFirstPosition() ).to.be.null;
  178. } );
  179. } );
  180. describe( 'getLastPosition', () => {
  181. it( 'should return copy of range with last position', () => {
  182. selection.addRange( range1 );
  183. selection.addRange( range2 );
  184. selection.addRange( range3 );
  185. const position = selection.getLastPosition();
  186. expect( position.isEqual( range3.end ) ).to.be.true;
  187. expect( position ).to.not.equal( range3.end );
  188. } );
  189. it( 'should return null if no ranges are present', () => {
  190. expect( selection.getLastPosition() ).to.be.null;
  191. } );
  192. } );
  193. describe( 'removeRangeAt', () => {
  194. it( 'should remove range at given index', () => {
  195. selection.addRange( range1 );
  196. selection.addRange( range2 );
  197. selection.addRange( range3 );
  198. const range = selection.removeRangeAt( 1 );
  199. expect( range.isEqual( range2 ) ).to.be.true;
  200. expect( selection.rangeCount ).to.equal( 2 );
  201. } );
  202. it( 'should return null if no range at specified index', () => {
  203. expect( selection.removeRangeAt( 2 ) ).to.be.null;
  204. } );
  205. it( 'should fire change event', ( done ) => {
  206. selection.addRange( range1 );
  207. selection.once( 'change:range', () => {
  208. expect( selection.rangeCount ).to.equal( 0 );
  209. done();
  210. } );
  211. selection.removeRangeAt( 0 );
  212. } );
  213. } );
  214. describe( 'removeAllRanges', () => {
  215. it( 'should remove all ranges and fire change event', ( done ) => {
  216. selection.addRange( range1 );
  217. selection.addRange( range2 );
  218. selection.once( 'change:range', () => {
  219. expect( selection.rangeCount ).to.equal( 0 );
  220. done();
  221. } );
  222. selection.removeAllRanges();
  223. } );
  224. it( 'should do nothing when no ranges are present', () => {
  225. const fireSpy = sinon.spy( selection, 'fire' );
  226. selection.removeAllRanges();
  227. fireSpy.restore();
  228. expect( fireSpy.notCalled ).to.be.true;
  229. } );
  230. } );
  231. describe( 'setRanges', () => {
  232. it( 'should add ranges and fire change event', ( done ) => {
  233. selection.addRange( range1 );
  234. selection.once( 'change:range', () => {
  235. expect( selection.rangeCount ).to.equal( 2 );
  236. expect( selection.getRangeAt( 0 ).isEqual( range2 ) ).to.be.true;
  237. expect( selection.getRangeAt( 0 ) ).is.not.equal( range2 );
  238. expect( selection.getRangeAt( 1 ).isEqual( range3 ) ).to.be.true;
  239. expect( selection.getRangeAt( 1 ) ).is.not.equal( range3 );
  240. done();
  241. } );
  242. selection.setRanges( [ range2, range3 ] );
  243. } );
  244. } );
  245. describe( 'collapseToStart', () => {
  246. it( 'should collapse to start position and fire change event', ( done ) => {
  247. selection.setRanges( [ range1, range2, range3 ] );
  248. selection.once( 'change:range', () => {
  249. expect( selection.rangeCount ).to.equal( 1 );
  250. expect( selection.isCollapsed ).to.be.true;
  251. expect( selection.getRangeAt( 0 ).start.isEqual( range2.start ) ).to.be.true;
  252. done();
  253. } );
  254. selection.collapseToStart();
  255. } );
  256. it( 'should do nothing if no ranges present', () => {
  257. const fireSpy = sinon.spy( selection, 'fire' );
  258. selection.collapseToStart();
  259. fireSpy.restore();
  260. expect( fireSpy.notCalled ).to.be.true;
  261. } );
  262. } );
  263. describe( 'collapseToEnd', () => {
  264. it( 'should collapse to end position and fire change event', ( done ) => {
  265. selection.setRanges( [ range1, range2, range3 ] );
  266. selection.once( 'change:range', () => {
  267. expect( selection.rangeCount ).to.equal( 1 );
  268. expect( selection.isCollapsed ).to.be.true;
  269. expect( selection.getRangeAt( 0 ).end.isEqual( range3.end ) ).to.be.true;
  270. done();
  271. } );
  272. selection.collapseToEnd();
  273. } );
  274. it( 'should do nothing if no ranges present', () => {
  275. const fireSpy = sinon.spy( selection, 'fire' );
  276. selection.collapseToEnd();
  277. fireSpy.restore();
  278. expect( fireSpy.notCalled ).to.be.true;
  279. } );
  280. } );
  281. } );