selection.js 16 KB

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