selection.js 13 KB

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