selection.js 11 KB

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