selection.js 14 KB

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