selection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'treemodel/document',
  11. 'treemodel/attribute',
  12. 'treemodel/element',
  13. 'treemodel/range',
  14. 'treemodel/position',
  15. 'treemodel/liverange',
  16. 'treemodel/selection',
  17. 'treemodel/operation/insertoperation',
  18. 'treemodel/operation/moveoperation',
  19. 'ckeditorerror'
  20. );
  21. describe( 'Selection', () => {
  22. let Document, Attribute, Element, Range, Position, LiveRange, Selection, InsertOperation, MoveOperation, CKEditorError;
  23. let attrFooBar;
  24. before( () => {
  25. Document = modules[ 'treemodel/document' ];
  26. Attribute = modules[ 'treemodel/attribute' ];
  27. Element = modules[ 'treemodel/element' ];
  28. Range = modules[ 'treemodel/range' ];
  29. Position = modules[ 'treemodel/position' ];
  30. LiveRange = modules[ 'treemodel/liverange' ];
  31. Selection = modules[ 'treemodel/selection' ];
  32. InsertOperation = modules[ 'treemodel/operation/insertoperation' ];
  33. MoveOperation = modules[ 'treemodel/operation/moveoperation' ];
  34. CKEditorError = modules.ckeditorerror;
  35. attrFooBar = new Attribute( 'foo', 'bar' );
  36. } );
  37. let doc, root, selection, liveRange, range;
  38. beforeEach( () => {
  39. doc = new Document();
  40. root = doc.createRoot( 'root' );
  41. selection = new Selection();
  42. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  43. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  44. } );
  45. afterEach( () => {
  46. selection.detach();
  47. liveRange.detach();
  48. } );
  49. it( 'should not have any range, anchor or focus position when just created', () => {
  50. let ranges = selection.getRanges();
  51. expect( ranges.length ).to.equal( 0 );
  52. expect( selection.anchor ).to.be.null;
  53. expect( selection.focus ).to.be.null;
  54. } );
  55. it( 'should be collapsed if it has no ranges or all ranges are collapsed', () => {
  56. expect( selection.isCollapsed ).to.be.true;
  57. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  58. expect( selection.isCollapsed ).to.be.true;
  59. } );
  60. it( 'should not be collapsed when it has a range that is not collapsed', () => {
  61. selection.addRange( liveRange );
  62. expect( selection.isCollapsed ).to.be.false;
  63. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  64. expect( selection.isCollapsed ).to.be.false;
  65. } );
  66. it( 'should copy added ranges and store multiple ranges', () => {
  67. selection.addRange( liveRange );
  68. selection.addRange( range );
  69. let ranges = selection.getRanges();
  70. expect( ranges.length ).to.equal( 2 );
  71. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  72. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  73. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  74. expect( ranges[ 1 ] ).not.to.be.equal( range );
  75. } );
  76. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  77. selection.addRange( liveRange );
  78. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  79. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  80. selection.addRange( range );
  81. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  82. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  83. } );
  84. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  85. selection.addRange( liveRange, true );
  86. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  87. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  88. selection.addRange( range, true );
  89. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  90. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  91. } );
  92. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  93. selection.addRange( liveRange );
  94. let ranges = selection.getRanges();
  95. selection.addRange( range );
  96. expect( ranges.length ).to.equal( 1 );
  97. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  98. } );
  99. it( 'should convert added Range to LiveRange', () => {
  100. selection.addRange( range );
  101. let ranges = selection.getRanges();
  102. expect( ranges[ 0 ] ).to.be.instanceof( LiveRange );
  103. } );
  104. it( 'should fire update event when adding a range', () => {
  105. let spy = sinon.spy();
  106. selection.on( 'update', spy );
  107. selection.addRange( range );
  108. expect( spy.called ).to.be.true;
  109. } );
  110. it( 'should unbind all events when detached', () => {
  111. selection.addRange( liveRange );
  112. selection.addRange( range );
  113. let ranges = selection.getRanges();
  114. sinon.spy( ranges[ 0 ], 'detach' );
  115. sinon.spy( ranges[ 1 ], 'detach' );
  116. selection.detach();
  117. expect( ranges[ 0 ].detach.called ).to.be.true;
  118. expect( ranges[ 1 ].detach.called ).to.be.true;
  119. ranges[ 0 ].detach.restore();
  120. ranges[ 1 ].detach.restore();
  121. } );
  122. it( 'should throw an error if added range intersects with already stored range', () => {
  123. selection.addRange( liveRange );
  124. expect( () => {
  125. selection.addRange(
  126. new Range(
  127. new Position( root, [ 0, 4 ] ),
  128. new Position( root, [ 1, 2 ] )
  129. )
  130. );
  131. } ).to.throw( CKEditorError, /selection-range-intersects/ );
  132. } );
  133. describe( 'removeAllRanges', () => {
  134. let spy, ranges;
  135. beforeEach( () => {
  136. selection.addRange( liveRange );
  137. selection.addRange( range );
  138. spy = sinon.spy();
  139. selection.on( 'update', spy );
  140. ranges = selection.getRanges();
  141. sinon.spy( ranges[ 0 ], 'detach' );
  142. sinon.spy( ranges[ 1 ], 'detach' );
  143. selection.removeAllRanges();
  144. } );
  145. afterEach( () => {
  146. ranges[ 0 ].detach.restore();
  147. ranges[ 1 ].detach.restore();
  148. } );
  149. it( 'should remove all stored ranges', () => {
  150. expect( selection.getRanges().length ).to.equal( 0 );
  151. expect( selection.anchor ).to.be.null;
  152. expect( selection.focus ).to.be.null;
  153. expect( selection.isCollapsed ).to.be.true;
  154. } );
  155. it( 'should fire exactly one update event', () => {
  156. expect( spy.calledOnce ).to.be.true;
  157. } );
  158. it( 'should detach removed ranges', () => {
  159. expect( ranges[ 0 ].detach.called ).to.be.true;
  160. expect( ranges[ 1 ].detach.called ).to.be.true;
  161. } );
  162. } );
  163. describe( 'setRanges', () => {
  164. let newRanges, spy, oldRanges;
  165. before( () => {
  166. newRanges = [
  167. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  168. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  169. ];
  170. } );
  171. beforeEach( () => {
  172. selection.addRange( liveRange );
  173. selection.addRange( range );
  174. spy = sinon.spy();
  175. selection.on( 'update', spy );
  176. oldRanges = selection.getRanges();
  177. sinon.spy( oldRanges[ 0 ], 'detach' );
  178. sinon.spy( oldRanges[ 1 ], 'detach' );
  179. } );
  180. afterEach( () => {
  181. oldRanges[ 0 ].detach.restore();
  182. oldRanges[ 1 ].detach.restore();
  183. } );
  184. it( 'should remove all ranges and add given ranges', () => {
  185. selection.setRanges( newRanges );
  186. let ranges = selection.getRanges();
  187. expect( ranges.length ).to.equal( 2 );
  188. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  189. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  190. } );
  191. it( 'should use last range from given array to get anchor and focus position', () => {
  192. selection.setRanges( newRanges );
  193. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  194. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  195. } );
  196. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  197. selection.setRanges( newRanges, true );
  198. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  199. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  200. } );
  201. it( 'should fire exactly one update event', () => {
  202. selection.setRanges( newRanges );
  203. expect( spy.calledOnce ).to.be.true;
  204. } );
  205. it( 'should detach removed LiveRanges', () => {
  206. selection.setRanges( newRanges );
  207. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  208. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  209. } );
  210. } );
  211. // Selection uses LiveRanges so here are only simple test to see if integration is
  212. // working well, without getting into complicated corner cases.
  213. describe( 'after applying an operation should get updated and not fire update event', () => {
  214. let spy;
  215. beforeEach( () => {
  216. root.insertChildren( 0, [ new Element( 'ul', [], 'abcdef' ), new Element( 'p', [], 'foobar' ), 'xyz' ] );
  217. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  218. spy = sinon.spy();
  219. selection.on( 'update', spy );
  220. } );
  221. describe( 'InsertOperation', () => {
  222. it( 'before selection', () => {
  223. doc.applyOperation(
  224. new InsertOperation(
  225. new Position( root, [ 0, 1 ] ),
  226. 'xyz',
  227. doc.version
  228. )
  229. );
  230. let range = selection.getRanges()[ 0 ];
  231. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  232. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  233. expect( spy.called ).to.be.false;
  234. } );
  235. it( 'inside selection', () => {
  236. doc.applyOperation(
  237. new InsertOperation(
  238. new Position( root, [ 1, 0 ] ),
  239. 'xyz',
  240. doc.version
  241. )
  242. );
  243. let range = selection.getRanges()[ 0 ];
  244. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  245. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  246. expect( spy.called ).to.be.false;
  247. } );
  248. } );
  249. describe( 'MoveOperation', () => {
  250. it( 'move range from before a selection', () => {
  251. doc.applyOperation(
  252. new MoveOperation(
  253. new Position( root, [ 0, 0 ] ),
  254. 2,
  255. new Position( root, [ 2 ] ),
  256. doc.version
  257. )
  258. );
  259. let range = selection.getRanges()[ 0 ];
  260. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  261. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  262. expect( spy.called ).to.be.false;
  263. } );
  264. it( 'moved into before a selection', () => {
  265. doc.applyOperation(
  266. new MoveOperation(
  267. new Position( root, [ 2 ] ),
  268. 2,
  269. new Position( root, [ 0, 0 ] ),
  270. doc.version
  271. )
  272. );
  273. let range = selection.getRanges()[ 0 ];
  274. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  275. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  276. expect( spy.called ).to.be.false;
  277. } );
  278. it( 'move range from inside of selection', () => {
  279. doc.applyOperation(
  280. new MoveOperation(
  281. new Position( root, [ 1, 0 ] ),
  282. 2,
  283. new Position( root, [ 2 ] ),
  284. doc.version
  285. )
  286. );
  287. let range = selection.getRanges()[ 0 ];
  288. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  289. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  290. expect( spy.called ).to.be.false;
  291. } );
  292. it( 'moved range intersects with selection', () => {
  293. doc.applyOperation(
  294. new MoveOperation(
  295. new Position( root, [ 1, 3 ] ),
  296. 2,
  297. new Position( root, [ 4 ] ),
  298. doc.version
  299. )
  300. );
  301. let range = selection.getRanges()[ 0 ];
  302. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  303. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  304. expect( spy.called ).to.be.false;
  305. } );
  306. it( 'split inside selection (do not break selection)', () => {
  307. doc.applyOperation(
  308. new InsertOperation(
  309. new Position( root, [ 2 ] ),
  310. new Element( 'p' ),
  311. doc.version
  312. )
  313. );
  314. doc.applyOperation(
  315. new MoveOperation(
  316. new Position( root, [ 1, 2 ] ),
  317. 4,
  318. new Position( root, [ 2, 0 ] ),
  319. doc.version
  320. )
  321. );
  322. let range = selection.getRanges()[ 0 ];
  323. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  324. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  325. expect( spy.called ).to.be.false;
  326. } );
  327. } );
  328. } );
  329. // Testing integration with attributes list.
  330. // Tests copied from AttributeList tests.
  331. // Some cases were omitted.
  332. describe( 'setAttr', () => {
  333. it( 'should insert an attribute', () => {
  334. selection.setAttr( attrFooBar );
  335. expect( getIteratorCount( selection.getAttrs() ) ).to.equal( 1 );
  336. expect( selection.getAttr( attrFooBar.key ) ).to.equal( attrFooBar.value );
  337. } );
  338. } );
  339. describe( 'setAttrsTo', () => {
  340. it( 'should remove all attributes and set passed ones', () => {
  341. selection.setAttr( attrFooBar );
  342. let attrs = [ new Attribute( 'abc', true ), new Attribute( 'xyz', false ) ];
  343. selection.setAttrsTo( attrs );
  344. expect( getIteratorCount( selection.getAttrs() ) ).to.equal( 2 );
  345. expect( selection.getAttr( 'foo' ) ).to.be.null;
  346. expect( selection.getAttr( 'abc' ) ).to.be.true;
  347. expect( selection.getAttr( 'xyz' ) ).to.be.false;
  348. } );
  349. } );
  350. describe( 'getAttr', () => {
  351. beforeEach( () => {
  352. selection.setAttr( attrFooBar );
  353. } );
  354. it( 'should return attribute value if key of previously set attribute has been passed', () => {
  355. expect( selection.getAttr( 'foo' ) ).to.equal( attrFooBar.value );
  356. } );
  357. it( 'should return null if attribute with given key has not been found', () => {
  358. expect( selection.getAttr( 'bar' ) ).to.be.null;
  359. } );
  360. } );
  361. describe( 'removeAttr', () => {
  362. it( 'should remove an attribute', () => {
  363. let attrA = new Attribute( 'a', 'A' );
  364. let attrB = new Attribute( 'b', 'B' );
  365. let attrC = new Attribute( 'c', 'C' );
  366. selection.setAttr( attrA );
  367. selection.setAttr( attrB );
  368. selection.setAttr( attrC );
  369. selection.removeAttr( attrB.key );
  370. expect( getIteratorCount( selection.getAttrs() ) ).to.equal( 2 );
  371. expect( selection.getAttr( attrA.key ) ).to.equal( attrA.value );
  372. expect( selection.getAttr( attrC.key ) ).to.equal( attrC.value );
  373. expect( selection.getAttr( attrB.key ) ).to.be.null;
  374. } );
  375. } );
  376. describe( 'hasAttr', () => {
  377. it( 'should check attribute by key', () => {
  378. selection.setAttr( attrFooBar );
  379. expect( selection.hasAttr( 'foo' ) ).to.be.true;
  380. } );
  381. it( 'should return false if attribute was not found by key', () => {
  382. expect( selection.hasAttr( 'bar' ) ).to.be.false;
  383. } );
  384. it( 'should check attribute by object', () => {
  385. selection.setAttr( attrFooBar );
  386. expect( selection.hasAttr( attrFooBar ) ).to.be.true;
  387. } );
  388. it( 'should return false if attribute was not found by object', () => {
  389. expect( selection.hasAttr( attrFooBar ) ).to.be.false;
  390. } );
  391. } );
  392. describe( 'getAttrs', () => {
  393. it( 'should return all set attributes', () => {
  394. let attrA = new Attribute( 'a', 'A' );
  395. let attrB = new Attribute( 'b', 'B' );
  396. let attrC = new Attribute( 'c', 'C' );
  397. selection.setAttrsTo( [
  398. attrA,
  399. attrB,
  400. attrC
  401. ] );
  402. selection.removeAttr( attrB.key );
  403. expect( [ attrA, attrC ] ).to.deep.equal( Array.from( selection.getAttrs() ) );
  404. } );
  405. } );
  406. } );