selection.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  94. } );
  95. it( 'should fire change event', ( done ) => {
  96. selection.once( 'change', () => {
  97. expect( selection._ranges[ 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 iterator with copies of all ranges', () => {
  115. selection.addRange( range1 );
  116. selection.addRange( range2 );
  117. const iterable = selection.getRanges();
  118. const ranges = Array.from( iterable );
  119. expect( ranges.length ).to.equal( 2 );
  120. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  121. expect( ranges[ 0 ] ).to.not.equal( range1 );
  122. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  123. expect( ranges[ 1 ] ).to.not.equal( range2 );
  124. } );
  125. } );
  126. describe( 'getFirstRange', () => {
  127. it( 'should return copy of range with first position', () => {
  128. selection.addRange( range1 );
  129. selection.addRange( range2 );
  130. selection.addRange( range3 );
  131. const range = selection.getFirstRange();
  132. expect( range.isEqual( range2 ) ).to.be.true;
  133. expect( range ).to.not.equal( range2 );
  134. } );
  135. it( 'should return null if no ranges are present', () => {
  136. expect( selection.getFirstRange() ).to.be.null;
  137. } );
  138. } );
  139. describe( 'getLastRange', () => {
  140. it( 'should return copy of range with last position', () => {
  141. selection.addRange( range1 );
  142. selection.addRange( range2 );
  143. selection.addRange( range3 );
  144. const range = selection.getLastRange();
  145. expect( range.isEqual( range3 ) ).to.be.true;
  146. expect( range ).to.not.equal( range3 );
  147. } );
  148. it( 'should return null if no ranges are present', () => {
  149. expect( selection.getLastRange() ).to.be.null;
  150. } );
  151. } );
  152. describe( 'getFirstPosition', () => {
  153. it( 'should return copy of first position', () => {
  154. selection.addRange( range1 );
  155. selection.addRange( range2 );
  156. selection.addRange( range3 );
  157. const position = selection.getFirstPosition();
  158. expect( position.isEqual( range2.start ) ).to.be.true;
  159. expect( position ).to.not.equal( range2.start );
  160. } );
  161. it( 'should return null if no ranges are present', () => {
  162. expect( selection.getFirstPosition() ).to.be.null;
  163. } );
  164. } );
  165. describe( 'getLastPosition', () => {
  166. it( 'should return copy of range with last position', () => {
  167. selection.addRange( range1 );
  168. selection.addRange( range2 );
  169. selection.addRange( range3 );
  170. const position = selection.getLastPosition();
  171. expect( position.isEqual( range3.end ) ).to.be.true;
  172. expect( position ).to.not.equal( range3.end );
  173. } );
  174. it( 'should return null if no ranges are present', () => {
  175. expect( selection.getLastPosition() ).to.be.null;
  176. } );
  177. } );
  178. describe( 'removeAllRanges', () => {
  179. it( 'should remove all ranges and fire change event', ( done ) => {
  180. selection.addRange( range1 );
  181. selection.addRange( range2 );
  182. selection.once( 'change', () => {
  183. expect( selection.rangeCount ).to.equal( 0 );
  184. done();
  185. } );
  186. selection.removeAllRanges();
  187. } );
  188. it( 'should do nothing when no ranges are present', () => {
  189. const fireSpy = sinon.spy( selection, 'fire' );
  190. selection.removeAllRanges();
  191. fireSpy.restore();
  192. expect( fireSpy.notCalled ).to.be.true;
  193. } );
  194. } );
  195. describe( 'setRanges', () => {
  196. it( 'should add ranges and fire change event', ( done ) => {
  197. selection.addRange( range1 );
  198. selection.once( 'change', () => {
  199. expect( selection.rangeCount ).to.equal( 2 );
  200. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  201. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  202. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  203. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  204. done();
  205. } );
  206. selection.setRanges( [ range2, range3 ] );
  207. } );
  208. } );
  209. describe( 'collapseToStart', () => {
  210. it( 'should collapse to start position and fire change event', ( done ) => {
  211. selection.setRanges( [ range1, range2, range3 ] );
  212. selection.once( 'change', () => {
  213. expect( selection.rangeCount ).to.equal( 1 );
  214. expect( selection.isCollapsed ).to.be.true;
  215. expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  216. done();
  217. } );
  218. selection.collapseToStart();
  219. } );
  220. it( 'should do nothing if no ranges present', () => {
  221. const fireSpy = sinon.spy( selection, 'fire' );
  222. selection.collapseToStart();
  223. fireSpy.restore();
  224. expect( fireSpy.notCalled ).to.be.true;
  225. } );
  226. } );
  227. describe( 'collapseToEnd', () => {
  228. it( 'should collapse to end position and fire change event', ( done ) => {
  229. selection.setRanges( [ range1, range2, range3 ] );
  230. selection.once( 'change', () => {
  231. expect( selection.rangeCount ).to.equal( 1 );
  232. expect( selection.isCollapsed ).to.be.true;
  233. expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  234. done();
  235. } );
  236. selection.collapseToEnd();
  237. } );
  238. it( 'should do nothing if no ranges present', () => {
  239. const fireSpy = sinon.spy( selection, 'fire' );
  240. selection.collapseToEnd();
  241. fireSpy.restore();
  242. expect( fireSpy.notCalled ).to.be.true;
  243. } );
  244. } );
  245. } );