8
0

selection.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view */
  6. import Selection from '/ckeditor5/engine/view/selection.js';
  7. import Range from '/ckeditor5/engine/view/range.js';
  8. import Document from '/ckeditor5/engine/view/document.js';
  9. import Element from '/ckeditor5/engine/view/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( 'isBackward', () => {
  94. it( 'is defined by the last added range', () => {
  95. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  96. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  97. selection.addRange( range1, true );
  98. expect( selection ).to.have.property( 'isBackward', true );
  99. selection.addRange( range2 );
  100. expect( selection ).to.have.property( 'isBackward', false );
  101. } );
  102. it( 'is false when last range is collapsed', () => {
  103. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  104. selection.addRange( range, true );
  105. expect( selection.isBackward ).to.be.false;
  106. } );
  107. } );
  108. describe( 'addRange', () => {
  109. it( 'should throw an error when range is invalid', () => {
  110. expect( () => {
  111. selection.addRange( { invalid: 'range' } );
  112. } ).to.throw( CKEditorError, 'selection-invalid-range: Invalid Range.' );
  113. } );
  114. it( 'should add range to selection ranges', () => {
  115. selection.addRange( range1 );
  116. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  117. } );
  118. it( 'should fire change event', ( done ) => {
  119. selection.once( 'change', () => {
  120. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  121. done();
  122. } );
  123. selection.addRange( range1 );
  124. } );
  125. it( 'should throw when range is intersecting with already added range', () => {
  126. const range2 = Range.createFromParentsAndOffsets( el, 7, el, 15 );
  127. selection.addRange( range1 );
  128. expect( () => {
  129. selection.addRange( range2 );
  130. } ).to.throw( CKEditorError, 'selection-range-intersects' );
  131. expect( () => {
  132. selection.addRange( range1 );
  133. } ).to.throw( CKEditorError, 'selection-range-intersects' );
  134. } );
  135. } );
  136. describe( 'getRanges', () => {
  137. it( 'should return iterator with copies of all ranges', () => {
  138. selection.addRange( range1 );
  139. selection.addRange( range2 );
  140. const iterable = selection.getRanges();
  141. const ranges = Array.from( iterable );
  142. expect( ranges.length ).to.equal( 2 );
  143. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  144. expect( ranges[ 0 ] ).to.not.equal( range1 );
  145. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  146. expect( ranges[ 1 ] ).to.not.equal( range2 );
  147. } );
  148. } );
  149. describe( 'getFirstRange', () => {
  150. it( 'should return copy of range with first position', () => {
  151. selection.addRange( range1 );
  152. selection.addRange( range2 );
  153. selection.addRange( range3 );
  154. const range = selection.getFirstRange();
  155. expect( range.isEqual( range2 ) ).to.be.true;
  156. expect( range ).to.not.equal( range2 );
  157. } );
  158. it( 'should return null if no ranges are present', () => {
  159. expect( selection.getFirstRange() ).to.be.null;
  160. } );
  161. } );
  162. describe( 'getLastRange', () => {
  163. it( 'should return copy of range with last position', () => {
  164. selection.addRange( range1 );
  165. selection.addRange( range2 );
  166. selection.addRange( range3 );
  167. const range = selection.getLastRange();
  168. expect( range.isEqual( range3 ) ).to.be.true;
  169. expect( range ).to.not.equal( range3 );
  170. } );
  171. it( 'should return null if no ranges are present', () => {
  172. expect( selection.getLastRange() ).to.be.null;
  173. } );
  174. } );
  175. describe( 'getFirstPosition', () => {
  176. it( 'should return copy of first position', () => {
  177. selection.addRange( range1 );
  178. selection.addRange( range2 );
  179. selection.addRange( range3 );
  180. const position = selection.getFirstPosition();
  181. expect( position.isEqual( range2.start ) ).to.be.true;
  182. expect( position ).to.not.equal( range2.start );
  183. } );
  184. it( 'should return null if no ranges are present', () => {
  185. expect( selection.getFirstPosition() ).to.be.null;
  186. } );
  187. } );
  188. describe( 'getLastPosition', () => {
  189. it( 'should return copy of range with last position', () => {
  190. selection.addRange( range1 );
  191. selection.addRange( range2 );
  192. selection.addRange( range3 );
  193. const position = selection.getLastPosition();
  194. expect( position.isEqual( range3.end ) ).to.be.true;
  195. expect( position ).to.not.equal( range3.end );
  196. } );
  197. it( 'should return null if no ranges are present', () => {
  198. expect( selection.getLastPosition() ).to.be.null;
  199. } );
  200. } );
  201. describe( 'isEqual', () => {
  202. it( 'should return true if selections equal', () => {
  203. selection.addRange( range1 );
  204. selection.addRange( range2 );
  205. const otherSelection = new Selection();
  206. otherSelection.addRange( range1 );
  207. otherSelection.addRange( range2 );
  208. expect( selection.isEqual( otherSelection ) ).to.be.true;
  209. } );
  210. it( 'should return true if backward selections equal', () => {
  211. selection.addRange( range1, true );
  212. const otherSelection = new Selection();
  213. otherSelection.addRange( range1, true );
  214. expect( selection.isEqual( otherSelection ) ).to.be.true;
  215. } );
  216. it( 'should return false if ranges count does not equal', () => {
  217. selection.addRange( range1 );
  218. selection.addRange( range2 );
  219. const otherSelection = new Selection();
  220. otherSelection.addRange( range1 );
  221. expect( selection.isEqual( otherSelection ) ).to.be.false;
  222. } );
  223. it( 'should return false if ranges do not equal', () => {
  224. selection.addRange( range1 );
  225. const otherSelection = new Selection();
  226. otherSelection.addRange( range2 );
  227. expect( selection.isEqual( otherSelection ) ).to.be.false;
  228. } );
  229. it( 'should return false if directions do not equal', () => {
  230. selection.addRange( range1 );
  231. const otherSelection = new Selection();
  232. otherSelection.addRange( range2, true );
  233. expect( selection.isEqual( otherSelection ) ).to.be.false;
  234. } );
  235. } );
  236. describe( 'removeAllRanges', () => {
  237. it( 'should remove all ranges and fire change event', ( done ) => {
  238. selection.addRange( range1 );
  239. selection.addRange( range2 );
  240. selection.once( 'change', () => {
  241. expect( selection.rangeCount ).to.equal( 0 );
  242. done();
  243. } );
  244. selection.removeAllRanges();
  245. } );
  246. it( 'should do nothing when no ranges are present', () => {
  247. const fireSpy = sinon.spy( selection, 'fire' );
  248. selection.removeAllRanges();
  249. fireSpy.restore();
  250. expect( fireSpy.notCalled ).to.be.true;
  251. } );
  252. } );
  253. describe( 'setRanges', () => {
  254. it( 'should throw an error when range is invalid', () => {
  255. expect( () => {
  256. selection.setRanges( [ { invalid: 'range' } ] );
  257. } ).to.throw( CKEditorError, 'selection-invalid-range: Invalid Range.' );
  258. } );
  259. it( 'should add ranges and fire change event', ( done ) => {
  260. selection.addRange( range1 );
  261. selection.once( 'change', () => {
  262. expect( selection.rangeCount ).to.equal( 2 );
  263. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  264. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  265. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  266. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  267. done();
  268. } );
  269. selection.setRanges( [ range2, range3 ] );
  270. } );
  271. } );
  272. describe( 'setTo', () => {
  273. it( 'should return true if selections equal', () => {
  274. selection.addRange( range1 );
  275. const otherSelection = new Selection();
  276. otherSelection.addRange( range2 );
  277. otherSelection.addRange( range3, true );
  278. selection.setTo( otherSelection );
  279. expect( selection.rangeCount ).to.equal( 2 );
  280. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  281. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  282. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  283. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  284. expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
  285. } );
  286. it( 'should fire change event', ( done ) => {
  287. selection.on( 'change', () => {
  288. expect( selection.rangeCount ).to.equal( 1 );
  289. expect( selection.getFirstRange().isEqual( range1 ) ).to.be.true;
  290. done();
  291. } );
  292. const otherSelection = new Selection();
  293. otherSelection.addRange( range1 );
  294. selection.setTo( otherSelection );
  295. } );
  296. } );
  297. describe( 'collapseToStart', () => {
  298. it( 'should collapse to start position and fire change event', ( done ) => {
  299. selection.setRanges( [ range1, range2, range3 ] );
  300. selection.once( 'change', () => {
  301. expect( selection.rangeCount ).to.equal( 1 );
  302. expect( selection.isCollapsed ).to.be.true;
  303. expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  304. done();
  305. } );
  306. selection.collapseToStart();
  307. } );
  308. it( 'should do nothing if no ranges present', () => {
  309. const fireSpy = sinon.spy( selection, 'fire' );
  310. selection.collapseToStart();
  311. fireSpy.restore();
  312. expect( fireSpy.notCalled ).to.be.true;
  313. } );
  314. } );
  315. describe( 'collapseToEnd', () => {
  316. it( 'should collapse to end position and fire change event', ( done ) => {
  317. selection.setRanges( [ range1, range2, range3 ] );
  318. selection.once( 'change', () => {
  319. expect( selection.rangeCount ).to.equal( 1 );
  320. expect( selection.isCollapsed ).to.be.true;
  321. expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  322. done();
  323. } );
  324. selection.collapseToEnd();
  325. } );
  326. it( 'should do nothing if no ranges present', () => {
  327. const fireSpy = sinon.spy( selection, 'fire' );
  328. selection.collapseToEnd();
  329. fireSpy.restore();
  330. expect( fireSpy.notCalled ).to.be.true;
  331. } );
  332. } );
  333. describe( 'getEditableElement', () => {
  334. it( 'should return null if no ranges in selection', () => {
  335. expect( selection.getEditableElement() ).to.be.null;
  336. } );
  337. it( 'should return null if selection is placed in container that is not EditableElement', () => {
  338. selection.addRange( range1 );
  339. expect( selection.getEditableElement() ).to.be.null;
  340. } );
  341. it( 'should return EditableElement when selection is placed inside', () => {
  342. const viewDocument = new Document();
  343. const selection = viewDocument.selection;
  344. const root = viewDocument.createRoot( 'div' );
  345. const element = new Element( 'p' );
  346. root.appendChildren( element );
  347. selection.addRange( Range.createFromParentsAndOffsets( element, 0, element, 0 ) );
  348. expect( selection.getEditableElement() ).to.equal( root );
  349. } );
  350. } );
  351. } );