selection.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. 'use strict';
  7. import Selection from '/ckeditor5/engine/view/selection.js';
  8. import Range from '/ckeditor5/engine/view/range.js';
  9. import Document from '/ckeditor5/engine/view/document.js';
  10. import Element from '/ckeditor5/engine/view/element.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. describe( 'Selection', () => {
  13. let selection;
  14. let el;
  15. let range1, range2, range3;
  16. beforeEach( () => {
  17. selection = new Selection();
  18. el = new Element( 'p' );
  19. range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  20. range2 = Range.createFromParentsAndOffsets( el, 1, el, 2 );
  21. range3 = Range.createFromParentsAndOffsets( el, 12, el, 14 );
  22. } );
  23. describe( 'anchor', () => {
  24. it( 'should return null if no ranges in selection', () => {
  25. expect( selection.anchor ).to.be.null;
  26. } );
  27. it( 'should return start of single range in selection', () => {
  28. selection.addRange( range1 );
  29. const anchor = selection.anchor;
  30. expect( anchor.isEqual( range1.start ) ).to.be.true;
  31. expect( anchor ).to.not.equal( range1.start );
  32. } );
  33. it( 'should return end of single range in selection when added as backward', () => {
  34. selection.addRange( range1, true );
  35. const anchor = selection.anchor;
  36. expect( anchor.isEqual( range1.end ) ).to.be.true;
  37. expect( anchor ).to.not.equal( range1.end );
  38. } );
  39. it( 'should get anchor from last inserted range', () => {
  40. selection.addRange( range1 );
  41. selection.addRange( range2 );
  42. expect( selection.anchor.isEqual( range2.start ) ).to.be.true;
  43. } );
  44. } );
  45. describe( 'focus', () => {
  46. it( 'should return null if no ranges in selection', () => {
  47. expect( selection.focus ).to.be.null;
  48. } );
  49. it( 'should return end of single range in selection', () => {
  50. selection.addRange( range1 );
  51. const focus = selection.focus;
  52. expect( focus.isEqual( range1.end ) ).to.be.true;
  53. } );
  54. it( 'should return start of single range in selection when added as backward', () => {
  55. selection.addRange( range1, true );
  56. const focus = selection.focus;
  57. expect( focus.isEqual( range1.start ) ).to.be.true;
  58. expect( focus ).to.not.equal( range1.start );
  59. } );
  60. it( 'should get focus from last inserted range', () => {
  61. selection.addRange( range1 );
  62. selection.addRange( range2 );
  63. expect( selection.focus.isEqual( range2.end ) ).to.be.true;
  64. } );
  65. } );
  66. describe( 'isCollapsed', () => {
  67. it( 'should return true when there is single collapsed range', () => {
  68. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  69. selection.addRange( range );
  70. expect( selection.isCollapsed ).to.be.true;
  71. } );
  72. it( 'should return false when there are multiple ranges', () => {
  73. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  74. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
  75. selection.addRange( range1 );
  76. selection.addRange( range2 );
  77. expect( selection.isCollapsed ).to.be.false;
  78. } );
  79. it( 'should return false when there is not collapsed range', () => {
  80. const range = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  81. selection.addRange( range );
  82. expect( selection.isCollapsed ).to.be.false;
  83. } );
  84. } );
  85. describe( 'rangeCount', () => {
  86. it( 'should return proper range count', () => {
  87. expect( selection.rangeCount ).to.equal( 0 );
  88. selection.addRange( range1 );
  89. expect( selection.rangeCount ).to.equal( 1 );
  90. selection.addRange( range2 );
  91. expect( selection.rangeCount ).to.equal( 2 );
  92. } );
  93. } );
  94. describe( 'isBackward', () => {
  95. it( 'is defined by the last added range', () => {
  96. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  97. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  98. selection.addRange( range1, true );
  99. expect( selection ).to.have.property( 'isBackward', true );
  100. selection.addRange( range2 );
  101. expect( selection ).to.have.property( 'isBackward', false );
  102. } );
  103. it( 'is false when last range is collapsed', () => {
  104. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  105. selection.addRange( range, true );
  106. expect( selection.isBackward ).to.be.false;
  107. } );
  108. } );
  109. describe( 'addRange', () => {
  110. it( 'should throw an error when range is invalid', () => {
  111. expect( () => {
  112. selection.addRange( { invalid: 'range' } );
  113. } ).to.throw( CKEditorError, 'selection-invalid-range: Invalid Range.' );
  114. } );
  115. it( 'should add range to selection ranges', () => {
  116. selection.addRange( range1 );
  117. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  118. } );
  119. it( 'should fire change event', ( done ) => {
  120. selection.once( 'change', () => {
  121. expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  122. done();
  123. } );
  124. selection.addRange( range1 );
  125. } );
  126. it( 'should throw when range is intersecting with already added range', () => {
  127. const range2 = Range.createFromParentsAndOffsets( el, 7, el, 15 );
  128. selection.addRange( range1 );
  129. expect( () => {
  130. selection.addRange( range2 );
  131. } ).to.throw( CKEditorError, 'selection-range-intersects' );
  132. expect( () => {
  133. selection.addRange( range1 );
  134. } ).to.throw( CKEditorError, 'selection-range-intersects' );
  135. } );
  136. } );
  137. describe( 'getRanges', () => {
  138. it( 'should return iterator with copies of all ranges', () => {
  139. selection.addRange( range1 );
  140. selection.addRange( range2 );
  141. const iterable = selection.getRanges();
  142. const ranges = Array.from( iterable );
  143. expect( ranges.length ).to.equal( 2 );
  144. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  145. expect( ranges[ 0 ] ).to.not.equal( range1 );
  146. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  147. expect( ranges[ 1 ] ).to.not.equal( range2 );
  148. } );
  149. } );
  150. describe( 'getFirstRange', () => {
  151. it( 'should return copy of range with first position', () => {
  152. selection.addRange( range1 );
  153. selection.addRange( range2 );
  154. selection.addRange( range3 );
  155. const range = selection.getFirstRange();
  156. expect( range.isEqual( range2 ) ).to.be.true;
  157. expect( range ).to.not.equal( range2 );
  158. } );
  159. it( 'should return null if no ranges are present', () => {
  160. expect( selection.getFirstRange() ).to.be.null;
  161. } );
  162. } );
  163. describe( 'getLastRange', () => {
  164. it( 'should return copy of range with last position', () => {
  165. selection.addRange( range1 );
  166. selection.addRange( range2 );
  167. selection.addRange( range3 );
  168. const range = selection.getLastRange();
  169. expect( range.isEqual( range3 ) ).to.be.true;
  170. expect( range ).to.not.equal( range3 );
  171. } );
  172. it( 'should return null if no ranges are present', () => {
  173. expect( selection.getLastRange() ).to.be.null;
  174. } );
  175. } );
  176. describe( 'getFirstPosition', () => {
  177. it( 'should return copy of first position', () => {
  178. selection.addRange( range1 );
  179. selection.addRange( range2 );
  180. selection.addRange( range3 );
  181. const position = selection.getFirstPosition();
  182. expect( position.isEqual( range2.start ) ).to.be.true;
  183. expect( position ).to.not.equal( range2.start );
  184. } );
  185. it( 'should return null if no ranges are present', () => {
  186. expect( selection.getFirstPosition() ).to.be.null;
  187. } );
  188. } );
  189. describe( 'getLastPosition', () => {
  190. it( 'should return copy of range with last position', () => {
  191. selection.addRange( range1 );
  192. selection.addRange( range2 );
  193. selection.addRange( range3 );
  194. const position = selection.getLastPosition();
  195. expect( position.isEqual( range3.end ) ).to.be.true;
  196. expect( position ).to.not.equal( range3.end );
  197. } );
  198. it( 'should return null if no ranges are present', () => {
  199. expect( selection.getLastPosition() ).to.be.null;
  200. } );
  201. } );
  202. describe( 'isEqual', () => {
  203. it( 'should return true if selections equal', () => {
  204. selection.addRange( range1 );
  205. selection.addRange( range2 );
  206. const otherSelection = new Selection();
  207. otherSelection.addRange( range1 );
  208. otherSelection.addRange( range2 );
  209. expect( selection.isEqual( otherSelection ) ).to.be.true;
  210. } );
  211. it( 'should return true if backward selections equal', () => {
  212. selection.addRange( range1, true );
  213. const otherSelection = new Selection();
  214. otherSelection.addRange( range1, true );
  215. expect( selection.isEqual( otherSelection ) ).to.be.true;
  216. } );
  217. it( 'should return false if ranges count does not equal', () => {
  218. selection.addRange( range1 );
  219. selection.addRange( range2 );
  220. const otherSelection = new Selection();
  221. otherSelection.addRange( range1 );
  222. expect( selection.isEqual( otherSelection ) ).to.be.false;
  223. } );
  224. it( 'should return false if ranges do not equal', () => {
  225. selection.addRange( range1 );
  226. const otherSelection = new Selection();
  227. otherSelection.addRange( range2 );
  228. expect( selection.isEqual( otherSelection ) ).to.be.false;
  229. } );
  230. it( 'should return false if directions do not equal', () => {
  231. selection.addRange( range1 );
  232. const otherSelection = new Selection();
  233. otherSelection.addRange( range2, true );
  234. expect( selection.isEqual( otherSelection ) ).to.be.false;
  235. } );
  236. } );
  237. describe( 'removeAllRanges', () => {
  238. it( 'should remove all ranges and fire change event', ( done ) => {
  239. selection.addRange( range1 );
  240. selection.addRange( range2 );
  241. selection.once( 'change', () => {
  242. expect( selection.rangeCount ).to.equal( 0 );
  243. done();
  244. } );
  245. selection.removeAllRanges();
  246. } );
  247. it( 'should do nothing when no ranges are present', () => {
  248. const fireSpy = sinon.spy( selection, 'fire' );
  249. selection.removeAllRanges();
  250. fireSpy.restore();
  251. expect( fireSpy.notCalled ).to.be.true;
  252. } );
  253. } );
  254. describe( 'setRanges', () => {
  255. it( 'should throw an error when range is invalid', () => {
  256. expect( () => {
  257. selection.setRanges( [ { invalid: 'range' } ] );
  258. } ).to.throw( CKEditorError, 'selection-invalid-range: Invalid Range.' );
  259. } );
  260. it( 'should add ranges and fire change event', ( done ) => {
  261. selection.addRange( range1 );
  262. selection.once( 'change', () => {
  263. expect( selection.rangeCount ).to.equal( 2 );
  264. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  265. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  266. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  267. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  268. done();
  269. } );
  270. selection.setRanges( [ range2, range3 ] );
  271. } );
  272. } );
  273. describe( 'setTo', () => {
  274. it( 'should return true if selections equal', () => {
  275. selection.addRange( range1 );
  276. const otherSelection = new Selection();
  277. otherSelection.addRange( range2 );
  278. otherSelection.addRange( range3, true );
  279. selection.setTo( otherSelection );
  280. expect( selection.rangeCount ).to.equal( 2 );
  281. expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  282. expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
  283. expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  284. expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
  285. expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
  286. } );
  287. it( 'should fire change event', ( done ) => {
  288. selection.on( 'change', () => {
  289. expect( selection.rangeCount ).to.equal( 1 );
  290. expect( selection.getFirstRange().isEqual( range1 ) ).to.be.true;
  291. done();
  292. } );
  293. const otherSelection = new Selection();
  294. otherSelection.addRange( range1 );
  295. selection.setTo( otherSelection );
  296. } );
  297. } );
  298. describe( 'collapseToStart', () => {
  299. it( 'should collapse to start position and fire change event', ( done ) => {
  300. selection.setRanges( [ range1, range2, range3 ] );
  301. selection.once( 'change', () => {
  302. expect( selection.rangeCount ).to.equal( 1 );
  303. expect( selection.isCollapsed ).to.be.true;
  304. expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  305. done();
  306. } );
  307. selection.collapseToStart();
  308. } );
  309. it( 'should do nothing if no ranges present', () => {
  310. const fireSpy = sinon.spy( selection, 'fire' );
  311. selection.collapseToStart();
  312. fireSpy.restore();
  313. expect( fireSpy.notCalled ).to.be.true;
  314. } );
  315. } );
  316. describe( 'collapseToEnd', () => {
  317. it( 'should collapse to end position and fire change event', ( done ) => {
  318. selection.setRanges( [ range1, range2, range3 ] );
  319. selection.once( 'change', () => {
  320. expect( selection.rangeCount ).to.equal( 1 );
  321. expect( selection.isCollapsed ).to.be.true;
  322. expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  323. done();
  324. } );
  325. selection.collapseToEnd();
  326. } );
  327. it( 'should do nothing if no ranges present', () => {
  328. const fireSpy = sinon.spy( selection, 'fire' );
  329. selection.collapseToEnd();
  330. fireSpy.restore();
  331. expect( fireSpy.notCalled ).to.be.true;
  332. } );
  333. } );
  334. describe( 'getEditableElement', () => {
  335. it( 'should return null if no ranges in selection', () => {
  336. expect( selection.getEditableElement() ).to.be.null;
  337. } );
  338. it( 'should return null if selection is placed in container that is not EditableElement', () => {
  339. selection.addRange( range1 );
  340. expect( selection.getEditableElement() ).to.be.null;
  341. } );
  342. it( 'should return EditableElement when selection is placed inside', () => {
  343. const viewDocument = new Document();
  344. const selection = viewDocument.selection;
  345. const root = viewDocument.createRoot( 'div' );
  346. const element = new Element( 'p' );
  347. root.appendChildren( element );
  348. selection.addRange( Range.createFromParentsAndOffsets( element, 0, element, 0 ) );
  349. expect( selection.getEditableElement() ).to.equal( root );
  350. } );
  351. } );
  352. } );