selection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. const modules = bender.amd.require(
  8. 'treemodel/document',
  9. 'treemodel/element',
  10. 'treemodel/range',
  11. 'treemodel/position',
  12. 'treemodel/liverange',
  13. 'treemodel/selection',
  14. 'treemodel/operation/insertoperation',
  15. 'treemodel/operation/moveoperation',
  16. 'ckeditorerror'
  17. );
  18. describe( 'Selection', () => {
  19. let Document, Element, Range, Position, LiveRange, Selection, InsertOperation, MoveOperation, CKEditorError;
  20. before( () => {
  21. Document = modules[ 'treemodel/document' ];
  22. Element = modules[ 'treemodel/element' ];
  23. Range = modules[ 'treemodel/range' ];
  24. Position = modules[ 'treemodel/position' ];
  25. LiveRange = modules[ 'treemodel/liverange' ];
  26. Selection = modules[ 'treemodel/selection' ];
  27. InsertOperation = modules[ 'treemodel/operation/insertoperation' ];
  28. MoveOperation = modules[ 'treemodel/operation/moveoperation' ];
  29. CKEditorError = modules.ckeditorerror;
  30. } );
  31. let doc, root, selection, liveRange, range;
  32. beforeEach( () => {
  33. doc = new Document();
  34. root = doc.createRoot( 'root' );
  35. selection = new Selection();
  36. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  37. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  38. } );
  39. afterEach( () => {
  40. selection.detach();
  41. liveRange.detach();
  42. } );
  43. it( 'should not have any range, anchor or focus position when just created', () => {
  44. let ranges = selection.getRanges();
  45. expect( ranges.length ).to.equal( 0 );
  46. expect( selection.anchor ).to.be.null;
  47. expect( selection.focus ).to.be.null;
  48. } );
  49. it( 'should be collapsed if it has no ranges or all ranges are collapsed', () => {
  50. expect( selection.isCollapsed ).to.be.true;
  51. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  52. expect( selection.isCollapsed ).to.be.true;
  53. } );
  54. it( 'should not be collapsed when it has a range that is not collapsed', () => {
  55. selection.addRange( liveRange );
  56. expect( selection.isCollapsed ).to.be.false;
  57. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  58. expect( selection.isCollapsed ).to.be.false;
  59. } );
  60. it( 'should copy added ranges and store multiple ranges', () => {
  61. selection.addRange( liveRange );
  62. selection.addRange( range );
  63. let ranges = selection.getRanges();
  64. expect( ranges.length ).to.equal( 2 );
  65. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  66. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  67. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  68. expect( ranges[ 1 ] ).not.to.be.equal( range );
  69. } );
  70. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  71. selection.addRange( liveRange );
  72. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  73. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  74. selection.addRange( range );
  75. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  76. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  77. } );
  78. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  79. selection.addRange( liveRange, true );
  80. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  81. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  82. selection.addRange( range, true );
  83. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  84. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  85. } );
  86. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  87. selection.addRange( liveRange );
  88. let ranges = selection.getRanges();
  89. selection.addRange( range );
  90. expect( ranges.length ).to.equal( 1 );
  91. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  92. } );
  93. it( 'should convert added Range to LiveRange', () => {
  94. selection.addRange( range );
  95. let ranges = selection.getRanges();
  96. expect( ranges[ 0 ] ).to.be.instanceof( LiveRange );
  97. } );
  98. it( 'should fire update event when adding a range', () => {
  99. let spy = sinon.spy();
  100. selection.on( 'update', spy );
  101. selection.addRange( range );
  102. expect( spy.called ).to.be.true;
  103. } );
  104. it( 'should unbind all events when detached', () => {
  105. selection.addRange( liveRange );
  106. selection.addRange( range );
  107. let ranges = selection.getRanges();
  108. sinon.spy( ranges[ 0 ], 'detach' );
  109. sinon.spy( ranges[ 1 ], 'detach' );
  110. selection.detach();
  111. expect( ranges[ 0 ].detach.called ).to.be.true;
  112. expect( ranges[ 1 ].detach.called ).to.be.true;
  113. ranges[ 0 ].detach.restore();
  114. ranges[ 1 ].detach.restore();
  115. } );
  116. it( 'should throw an error if added range intersects with already stored range', () => {
  117. selection.addRange( liveRange );
  118. expect( () => {
  119. selection.addRange(
  120. new Range(
  121. new Position( root, [ 0, 4 ] ),
  122. new Position( root, [ 1, 2 ] )
  123. )
  124. );
  125. } ).to.throw( CKEditorError, /selection-range-intersects/ );
  126. } );
  127. describe( 'removeAllRanges', () => {
  128. let spy, ranges;
  129. beforeEach( () => {
  130. selection.addRange( liveRange );
  131. selection.addRange( range );
  132. spy = sinon.spy();
  133. selection.on( 'update', spy );
  134. ranges = selection.getRanges();
  135. sinon.spy( ranges[ 0 ], 'detach' );
  136. sinon.spy( ranges[ 1 ], 'detach' );
  137. selection.removeAllRanges();
  138. } );
  139. afterEach( () => {
  140. ranges[ 0 ].detach.restore();
  141. ranges[ 1 ].detach.restore();
  142. } );
  143. it( 'should remove all stored ranges', () => {
  144. expect( selection.getRanges().length ).to.equal( 0 );
  145. expect( selection.anchor ).to.be.null;
  146. expect( selection.focus ).to.be.null;
  147. expect( selection.isCollapsed ).to.be.true;
  148. } );
  149. it( 'should fire exactly one update event', () => {
  150. expect( spy.calledOnce ).to.be.true;
  151. } );
  152. it( 'should detach removed ranges', () => {
  153. expect( ranges[ 0 ].detach.called ).to.be.true;
  154. expect( ranges[ 1 ].detach.called ).to.be.true;
  155. } );
  156. } );
  157. describe( 'setRanges', () => {
  158. let newRanges, spy, oldRanges;
  159. before( () => {
  160. newRanges = [
  161. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  162. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  163. ];
  164. } );
  165. beforeEach( () => {
  166. selection.addRange( liveRange );
  167. selection.addRange( range );
  168. spy = sinon.spy();
  169. selection.on( 'update', spy );
  170. oldRanges = selection.getRanges();
  171. sinon.spy( oldRanges[ 0 ], 'detach' );
  172. sinon.spy( oldRanges[ 1 ], 'detach' );
  173. } );
  174. afterEach( () => {
  175. oldRanges[ 0 ].detach.restore();
  176. oldRanges[ 1 ].detach.restore();
  177. } );
  178. it( 'should remove all ranges and add given ranges', () => {
  179. selection.setRanges( newRanges );
  180. let ranges = selection.getRanges();
  181. expect( ranges.length ).to.equal( 2 );
  182. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  183. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  184. } );
  185. it( 'should use last range from given array to get anchor and focus position', () => {
  186. selection.setRanges( newRanges );
  187. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  188. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  189. } );
  190. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  191. selection.setRanges( newRanges, true );
  192. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  193. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  194. } );
  195. it( 'should fire exactly one update event', () => {
  196. selection.setRanges( newRanges );
  197. expect( spy.calledOnce ).to.be.true;
  198. } );
  199. it( 'should detach removed LiveRanges', () => {
  200. selection.setRanges( newRanges );
  201. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  202. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  203. } );
  204. } );
  205. // Selection uses LiveRanges so here are only simple test to see if integration is
  206. // working well, without getting into complicated corner cases.
  207. describe( 'after applying an operation should get updated and not fire update event', () => {
  208. let spy;
  209. beforeEach( () => {
  210. root.insertChildren( 0, [ new Element( 'ul', [], 'abcdef' ), new Element( 'p', [], 'foobar' ), 'xyz' ] );
  211. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  212. spy = sinon.spy();
  213. selection.on( 'update', spy );
  214. } );
  215. describe( 'InsertOperation', () => {
  216. it( 'before selection', () => {
  217. doc.applyOperation(
  218. new InsertOperation(
  219. new Position( root, [ 0, 1 ] ),
  220. 'xyz',
  221. doc.version
  222. )
  223. );
  224. let range = selection.getRanges()[ 0 ];
  225. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  226. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  227. expect( spy.called ).to.be.false;
  228. } );
  229. it( 'inside selection', () => {
  230. doc.applyOperation(
  231. new InsertOperation(
  232. new Position( root, [ 1, 0 ] ),
  233. 'xyz',
  234. doc.version
  235. )
  236. );
  237. let range = selection.getRanges()[ 0 ];
  238. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  239. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  240. expect( spy.called ).to.be.false;
  241. } );
  242. } );
  243. describe( 'MoveOperation', () => {
  244. it( 'move range from before a selection', () => {
  245. doc.applyOperation(
  246. new MoveOperation(
  247. new Position( root, [ 0, 0 ] ),
  248. 2,
  249. new Position( root, [ 2 ] ),
  250. doc.version
  251. )
  252. );
  253. let range = selection.getRanges()[ 0 ];
  254. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  255. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  256. expect( spy.called ).to.be.false;
  257. } );
  258. it( 'moved into before a selection', () => {
  259. doc.applyOperation(
  260. new MoveOperation(
  261. new Position( root, [ 2 ] ),
  262. 2,
  263. new Position( root, [ 0, 0 ] ),
  264. doc.version
  265. )
  266. );
  267. let range = selection.getRanges()[ 0 ];
  268. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  269. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  270. expect( spy.called ).to.be.false;
  271. } );
  272. it( 'move range from inside of selection', () => {
  273. doc.applyOperation(
  274. new MoveOperation(
  275. new Position( root, [ 1, 0 ] ),
  276. 2,
  277. new Position( root, [ 2 ] ),
  278. doc.version
  279. )
  280. );
  281. let range = selection.getRanges()[ 0 ];
  282. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  283. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  284. expect( spy.called ).to.be.false;
  285. } );
  286. it( 'moved range intersects with selection', () => {
  287. doc.applyOperation(
  288. new MoveOperation(
  289. new Position( root, [ 1, 3 ] ),
  290. 2,
  291. new Position( root, [ 4 ] ),
  292. doc.version
  293. )
  294. );
  295. let range = selection.getRanges()[ 0 ];
  296. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  297. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  298. expect( spy.called ).to.be.false;
  299. } );
  300. it( 'split inside selection (do not break selection)', () => {
  301. doc.applyOperation(
  302. new InsertOperation(
  303. new Position( root, [ 2 ] ),
  304. new Element( 'p' ),
  305. doc.version
  306. )
  307. );
  308. doc.applyOperation(
  309. new MoveOperation(
  310. new Position( root, [ 1, 2 ] ),
  311. 4,
  312. new Position( root, [ 2, 0 ] ),
  313. doc.version
  314. )
  315. );
  316. let range = selection.getRanges()[ 0 ];
  317. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  318. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  319. expect( spy.called ).to.be.false;
  320. } );
  321. } );
  322. } );
  323. } );