selection.js 14 KB

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