8
0

selection.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  220. let range = selection.getFirstRange();
  221. expect( range.start.path ).to.deep.equal( [ 1 ] );
  222. expect( range.end.path ).to.deep.equal( [ 4 ] );
  223. } );
  224. } );
  225. describe( 'getFirstPosition', () => {
  226. it( 'should return null if there are no ranges in selection', () => {
  227. selection.removeAllRanges();
  228. expect( selection.getFirstPosition() ).to.be.null;
  229. } );
  230. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  231. // This will not be a range containing the first position despite being added as first
  232. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  233. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  234. let position = selection.getFirstPosition();
  235. expect( position.path ).to.deep.equal( [ 1 ] );
  236. } );
  237. } );
  238. // Selection uses LiveRanges so here are only simple test to see if integration is
  239. // working well, without getting into complicated corner cases.
  240. describe( 'after applying an operation should get updated and not fire update event', () => {
  241. let spy;
  242. beforeEach( () => {
  243. root.insertChildren( 0, [ new Element( 'ul', [], 'abcdef' ), new Element( 'p', [], 'foobar' ), 'xyz' ] );
  244. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  245. spy = sinon.spy();
  246. selection.on( 'update', spy );
  247. } );
  248. describe( 'InsertOperation', () => {
  249. it( 'before selection', () => {
  250. doc.applyOperation(
  251. new InsertOperation(
  252. new Position( root, [ 0, 1 ] ),
  253. 'xyz',
  254. doc.version
  255. )
  256. );
  257. let range = selection.getRanges()[ 0 ];
  258. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  259. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  260. expect( spy.called ).to.be.false;
  261. } );
  262. it( 'inside selection', () => {
  263. doc.applyOperation(
  264. new InsertOperation(
  265. new Position( root, [ 1, 0 ] ),
  266. 'xyz',
  267. doc.version
  268. )
  269. );
  270. let range = selection.getRanges()[ 0 ];
  271. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  272. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  273. expect( spy.called ).to.be.false;
  274. } );
  275. } );
  276. describe( 'MoveOperation', () => {
  277. it( 'move range from before a selection', () => {
  278. doc.applyOperation(
  279. new MoveOperation(
  280. new Position( root, [ 0, 0 ] ),
  281. 2,
  282. new Position( root, [ 2 ] ),
  283. doc.version
  284. )
  285. );
  286. let range = selection.getRanges()[ 0 ];
  287. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  288. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  289. expect( spy.called ).to.be.false;
  290. } );
  291. it( 'moved into before a selection', () => {
  292. doc.applyOperation(
  293. new MoveOperation(
  294. new Position( root, [ 2 ] ),
  295. 2,
  296. new Position( root, [ 0, 0 ] ),
  297. doc.version
  298. )
  299. );
  300. let range = selection.getRanges()[ 0 ];
  301. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  302. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  303. expect( spy.called ).to.be.false;
  304. } );
  305. it( 'move range from inside of selection', () => {
  306. doc.applyOperation(
  307. new MoveOperation(
  308. new Position( root, [ 1, 0 ] ),
  309. 2,
  310. new Position( root, [ 2 ] ),
  311. doc.version
  312. )
  313. );
  314. let range = selection.getRanges()[ 0 ];
  315. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  316. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  317. expect( spy.called ).to.be.false;
  318. } );
  319. it( 'moved range intersects with selection', () => {
  320. doc.applyOperation(
  321. new MoveOperation(
  322. new Position( root, [ 1, 3 ] ),
  323. 2,
  324. new Position( root, [ 4 ] ),
  325. doc.version
  326. )
  327. );
  328. let range = selection.getRanges()[ 0 ];
  329. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  330. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  331. expect( spy.called ).to.be.false;
  332. } );
  333. it( 'split inside selection (do not break selection)', () => {
  334. doc.applyOperation(
  335. new InsertOperation(
  336. new Position( root, [ 2 ] ),
  337. new Element( 'p' ),
  338. doc.version
  339. )
  340. );
  341. doc.applyOperation(
  342. new MoveOperation(
  343. new Position( root, [ 1, 2 ] ),
  344. 4,
  345. new Position( root, [ 2, 0 ] ),
  346. doc.version
  347. )
  348. );
  349. let range = selection.getRanges()[ 0 ];
  350. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  351. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  352. expect( spy.called ).to.be.false;
  353. } );
  354. } );
  355. } );
  356. describe( 'attributes interface', () => {
  357. describe( 'setAttribute', () => {
  358. it( 'should set given attribute on the selection', () => {
  359. selection.setAttribute( 'foo', 'bar' );
  360. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  361. } );
  362. } );
  363. describe( 'hasAttribute', () => {
  364. it( 'should return true if element contains attribute with given key', () => {
  365. selection.setAttribute( 'foo', 'bar' );
  366. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  367. } );
  368. it( 'should return false if element does not contain attribute with given key', () => {
  369. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  370. } );
  371. } );
  372. describe( 'getAttribute', () => {
  373. it( 'should return undefined if element does not contain given attribute', () => {
  374. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  375. } );
  376. } );
  377. describe( 'getAttributes', () => {
  378. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  379. selection.setAttribute( 'foo', 'bar' );
  380. selection.setAttribute( 'abc', 'xyz' );
  381. let attrs = Array.from( selection.getAttributes() );
  382. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  383. } );
  384. } );
  385. describe( 'setAttributesTo', () => {
  386. it( 'should remove all attributes set on element and set the given ones', () => {
  387. selection.setAttribute( 'abc', 'xyz' );
  388. selection.setAttributesTo( { foo: 'bar' } );
  389. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  390. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  391. } );
  392. } );
  393. describe( 'removeAttribute', () => {
  394. it( 'should remove attribute set on the text fragment and return true', () => {
  395. selection.setAttribute( 'foo', 'bar' );
  396. let result = selection.removeAttribute( 'foo' );
  397. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  398. expect( result ).to.be.true;
  399. } );
  400. it( 'should return false if text fragment does not have given attribute', () => {
  401. let result = selection.removeAttribute( 'abc' );
  402. expect( result ).to.be.false;
  403. } );
  404. } );
  405. describe( 'clearAttributes', () => {
  406. it( 'should remove all attributes from the element', () => {
  407. selection.setAttribute( 'foo', 'bar' );
  408. selection.setAttribute( 'abc', 'xyz' );
  409. selection.clearAttributes();
  410. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  411. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  412. } );
  413. } );
  414. } );
  415. } );