selection.js 10 KB

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