selection.js 11 KB

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