selection.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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/engine/treemodel/document.js';
  8. import Element from '/ckeditor5/engine/treemodel/element.js';
  9. import Text from '/ckeditor5/engine/treemodel/text.js';
  10. import Range from '/ckeditor5/engine/treemodel/range.js';
  11. import Position from '/ckeditor5/engine/treemodel/position.js';
  12. import LiveRange from '/ckeditor5/engine/treemodel/liverange.js';
  13. import Selection from '/ckeditor5/engine/treemodel/selection.js';
  14. import InsertOperation from '/ckeditor5/engine/treemodel/operation/insertoperation.js';
  15. import MoveOperation from '/ckeditor5/engine/treemodel/operation/moveoperation.js';
  16. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  17. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  18. import utils from '/ckeditor5/utils/utils.js';
  19. testUtils.createSinonSandbox();
  20. describe( 'Selection', () => {
  21. let attrFooBar;
  22. before( () => {
  23. attrFooBar = { foo: 'bar' };
  24. } );
  25. let doc, root, selection, liveRange, range;
  26. beforeEach( () => {
  27. doc = new Document();
  28. root = doc.createRoot( 'root' );
  29. root.insertChildren( 0, [
  30. new Element( 'p' ),
  31. new Element( 'p' ),
  32. new Element( 'p', [], 'foobar' ),
  33. new Element( 'p' ),
  34. new Element( 'p' ),
  35. new Element( 'p' ),
  36. new Element( 'p', [], 'foobar' )
  37. ] );
  38. selection = doc.selection;
  39. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  40. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  41. } );
  42. afterEach( () => {
  43. doc.destroy();
  44. liveRange.detach();
  45. } );
  46. it( 'should be set to default range when just created', () => {
  47. const ranges = Array.from( selection.getRanges() );
  48. expect( ranges.length ).to.equal( 1 );
  49. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  50. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  51. expect( selection ).to.have.property( 'isBackward', false );
  52. expect( selection._attrs ).to.be.instanceof( Map );
  53. expect( selection._attrs.size ).to.equal( 0 );
  54. } );
  55. describe( 'isCollapsed', () => {
  56. it( 'should be true if all ranges are collapsed', () => {
  57. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  58. expect( selection.isCollapsed ).to.be.true;
  59. } );
  60. it( 'should be false when it has a range that is not collapsed', () => {
  61. selection.addRange( range );
  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. } );
  67. describe( 'isBackward', () => {
  68. it( 'is defined by the last added range', () => {
  69. selection.addRange( range, true );
  70. expect( selection ).to.have.property( 'isBackward', true );
  71. selection.addRange( liveRange );
  72. expect( selection ).to.have.property( 'isBackward', false );
  73. } );
  74. it( 'is false when last range is collapsed', () => {
  75. const pos = Position.createAt( root, 0 );
  76. selection.addRange( new Range( pos, pos ), true );
  77. expect( selection.isBackward ).to.be.false;
  78. } );
  79. } );
  80. describe( 'addRange', () => {
  81. it( 'should copy added ranges and store multiple ranges', () => {
  82. selection.addRange( liveRange );
  83. selection.addRange( range );
  84. const ranges = selection._ranges;
  85. expect( ranges.length ).to.equal( 2 );
  86. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  87. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  88. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  89. expect( ranges[ 1 ] ).not.to.be.equal( range );
  90. } );
  91. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  92. selection.addRange( liveRange );
  93. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  94. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  95. selection.addRange( range );
  96. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  97. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  98. } );
  99. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  100. selection.addRange( liveRange, true );
  101. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  102. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  103. selection.addRange( range, true );
  104. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  105. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  106. } );
  107. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  108. selection.addRange( liveRange );
  109. const ranges = Array.from( selection.getRanges() );
  110. selection.addRange( range );
  111. expect( ranges.length ).to.equal( 1 );
  112. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  113. } );
  114. it( 'should convert added Range to LiveRange', () => {
  115. selection.addRange( range );
  116. const ranges = selection._ranges;
  117. expect( ranges[ 0 ] ).to.be.instanceof( LiveRange );
  118. } );
  119. it( 'should fire change:range event when adding a range', () => {
  120. let spy = sinon.spy();
  121. selection.on( 'change:range', spy );
  122. selection.addRange( range );
  123. expect( spy.called ).to.be.true;
  124. } );
  125. it( 'should unbind all events when destroyed', () => {
  126. selection.addRange( liveRange );
  127. selection.addRange( range );
  128. const ranges = selection._ranges;
  129. sinon.spy( ranges[ 0 ], 'detach' );
  130. sinon.spy( ranges[ 1 ], 'detach' );
  131. selection.destroy();
  132. expect( ranges[ 0 ].detach.called ).to.be.true;
  133. expect( ranges[ 1 ].detach.called ).to.be.true;
  134. ranges[ 0 ].detach.restore();
  135. ranges[ 1 ].detach.restore();
  136. } );
  137. it( 'should throw an error if added range intersects with already stored range', () => {
  138. selection.addRange( liveRange );
  139. expect( () => {
  140. selection.addRange(
  141. new Range(
  142. new Position( root, [ 0, 4 ] ),
  143. new Position( root, [ 1, 2 ] )
  144. )
  145. );
  146. } ).to.throw( CKEditorError, /selection-range-intersects/ );
  147. } );
  148. } );
  149. describe( 'collapse', () => {
  150. it( 'detaches all existing ranges', () => {
  151. selection.addRange( range );
  152. selection.addRange( liveRange );
  153. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  154. selection.collapse( root );
  155. expect( spy.calledTwice ).to.be.true;
  156. } );
  157. it( 'fires change:range', () => {
  158. const spy = sinon.spy();
  159. selection.on( 'change:range', spy );
  160. selection.collapse( root );
  161. expect( spy.calledOnce ).to.be.true;
  162. } );
  163. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  164. selection.collapse( root );
  165. expect( selection ).to.have.property( 'isCollapsed', true );
  166. const focus = selection.focus;
  167. expect( focus ).to.have.property( 'parent', root );
  168. expect( focus ).to.have.property( 'offset', 0 );
  169. } );
  170. it( 'sets selection at given offset in given parent', () => {
  171. selection.collapse( root, 3 );
  172. expect( selection ).to.have.property( 'isCollapsed', true );
  173. const focus = selection.focus;
  174. expect( focus ).to.have.property( 'parent', root );
  175. expect( focus ).to.have.property( 'offset', 3 );
  176. } );
  177. it( 'sets selection at the end of the given parent', () => {
  178. selection.collapse( root, 'END' );
  179. expect( selection ).to.have.property( 'isCollapsed', true );
  180. const focus = selection.focus;
  181. expect( focus ).to.have.property( 'parent', root );
  182. expect( focus ).to.have.property( 'offset', root.getChildCount() );
  183. } );
  184. it( 'sets selection before the specified element', () => {
  185. selection.collapse( root.getChild( 1 ), 'BEFORE' );
  186. expect( selection ).to.have.property( 'isCollapsed', true );
  187. const focus = selection.focus;
  188. expect( focus ).to.have.property( 'parent', root );
  189. expect( focus ).to.have.property( 'offset', 1 );
  190. } );
  191. it( 'sets selection after the specified element', () => {
  192. selection.collapse( root.getChild( 1 ), 'AFTER' );
  193. expect( selection ).to.have.property( 'isCollapsed', true );
  194. const focus = selection.focus;
  195. expect( focus ).to.have.property( 'parent', root );
  196. expect( focus ).to.have.property( 'offset', 2 );
  197. } );
  198. it( 'sets selection at the specified position', () => {
  199. const pos = Position.createFromParentAndOffset( root, 3 );
  200. selection.collapse( pos );
  201. expect( selection ).to.have.property( 'isCollapsed', true );
  202. const focus = selection.focus;
  203. expect( focus ).to.have.property( 'parent', root );
  204. expect( focus ).to.have.property( 'offset', 3 );
  205. } );
  206. } );
  207. describe( 'setFocus', () => {
  208. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  209. selection.addRange( range );
  210. selection.addRange( liveRange );
  211. const spy = sinon.spy();
  212. selection.on( 'change:range', spy );
  213. selection.setFocus( selection.focus );
  214. expect( utils.count( selection.getRanges() ) ).to.equal( 2 );
  215. expect( spy.callCount ).to.equal( 0 );
  216. } );
  217. it( 'fires change:range', () => {
  218. selection.addRange( range );
  219. const spy = sinon.spy();
  220. selection.on( 'change:range', spy );
  221. selection.setFocus( Position.createAt( root, 'END' ) );
  222. expect( spy.calledOnce ).to.be.true;
  223. } );
  224. it( 'modifies default range', () => {
  225. const startPos = selection.getFirstPosition();
  226. const endPos = Position.createAt( root, 'END' );
  227. selection.setFocus( endPos );
  228. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  229. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  230. } );
  231. it( 'modifies existing collapsed selection', () => {
  232. const startPos = Position.createAt( root, 1 );
  233. const endPos = Position.createAt( root, 2 );
  234. selection.collapse( startPos );
  235. selection.setFocus( endPos );
  236. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  237. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  238. } );
  239. it( 'makes existing collapsed selection a backward selection', () => {
  240. const startPos = Position.createAt( root, 1 );
  241. const endPos = Position.createAt( root, 0 );
  242. selection.collapse( startPos );
  243. selection.setFocus( endPos );
  244. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  245. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  246. expect( selection.isBackward ).to.be.true;
  247. } );
  248. it( 'modifies existing non-collapsed selection', () => {
  249. const startPos = Position.createAt( root, 1 );
  250. const endPos = Position.createAt( root, 2 );
  251. const newEndPos = Position.createAt( root, 3 );
  252. selection.addRange( new Range( startPos, endPos ) );
  253. selection.setFocus( newEndPos );
  254. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  255. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  256. } );
  257. it( 'makes existing non-collapsed selection a backward selection', () => {
  258. const startPos = Position.createAt( root, 1 );
  259. const endPos = Position.createAt( root, 2 );
  260. const newEndPos = Position.createAt( root, 0 );
  261. selection.addRange( new Range( startPos, endPos ) );
  262. selection.setFocus( newEndPos );
  263. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  264. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  265. expect( selection.isBackward ).to.be.true;
  266. } );
  267. it( 'makes existing backward selection a forward selection', () => {
  268. const startPos = Position.createAt( root, 1 );
  269. const endPos = Position.createAt( root, 2 );
  270. const newEndPos = Position.createAt( root, 3 );
  271. selection.addRange( new Range( startPos, endPos ), true );
  272. selection.setFocus( newEndPos );
  273. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  274. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  275. expect( selection.isBackward ).to.be.false;
  276. } );
  277. it( 'modifies existing backward selection', () => {
  278. const startPos = Position.createAt( root, 1 );
  279. const endPos = Position.createAt( root, 2 );
  280. const newEndPos = Position.createAt( root, 0 );
  281. selection.addRange( new Range( startPos, endPos ), true );
  282. selection.setFocus( newEndPos );
  283. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  284. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  285. expect( selection.isBackward ).to.be.true;
  286. } );
  287. it( 'modifies only the last range', () => {
  288. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  289. const startPos1 = Position.createAt( root, 4 );
  290. const endPos1 = Position.createAt( root, 5 );
  291. const startPos2 = Position.createAt( root, 1 );
  292. const endPos2 = Position.createAt( root, 2 );
  293. const newEndPos = Position.createAt( root, 0 );
  294. selection.addRange( new Range( startPos1, endPos1 ) );
  295. selection.addRange( new Range( startPos2, endPos2 ) );
  296. const spy = sinon.spy();
  297. selection.on( 'change:range', spy );
  298. selection.setFocus( newEndPos );
  299. const ranges = Array.from( selection.getRanges() );
  300. expect( ranges ).to.have.lengthOf( 2 );
  301. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'SAME' );
  302. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'SAME' );
  303. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'SAME' );
  304. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  305. expect( selection.isBackward ).to.be.true;
  306. expect( spy.calledOnce ).to.be.true;
  307. } );
  308. it( 'collapses the selection when extending to the anchor', () => {
  309. const startPos = Position.createAt( root, 1 );
  310. const endPos = Position.createAt( root, 2 );
  311. selection.addRange( new Range( startPos, endPos ) );
  312. selection.setFocus( startPos );
  313. expect( selection.focus.compareWith( startPos ) ).to.equal( 'SAME' );
  314. expect( selection.isCollapsed ).to.be.true;
  315. } );
  316. it( 'uses Position.createAt', () => {
  317. const startPos = Position.createAt( root, 1 );
  318. const endPos = Position.createAt( root, 2 );
  319. const newEndPos = Position.createAt( root, 4 );
  320. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  321. selection.addRange( new Range( startPos, endPos ) );
  322. selection.setFocus( root, 'END' );
  323. expect( spy.calledOnce ).to.be.true;
  324. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  325. } );
  326. it( 'detaches the range it replaces', () => {
  327. const startPos = Position.createAt( root, 1 );
  328. const endPos = Position.createAt( root, 2 );
  329. const newEndPos = Position.createAt( root, 4 );
  330. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  331. selection.addRange( new Range( startPos, endPos ) );
  332. selection.setFocus( newEndPos );
  333. expect( spy.calledOnce ).to.be.true;
  334. } );
  335. } );
  336. describe( 'removeAllRanges', () => {
  337. let spy, ranges;
  338. beforeEach( () => {
  339. selection.addRange( liveRange );
  340. selection.addRange( range );
  341. spy = sinon.spy();
  342. selection.on( 'change:range', spy );
  343. ranges = selection._ranges;
  344. sinon.spy( ranges[ 0 ], 'detach' );
  345. sinon.spy( ranges[ 1 ], 'detach' );
  346. selection.removeAllRanges();
  347. } );
  348. afterEach( () => {
  349. ranges[ 0 ].detach.restore();
  350. ranges[ 1 ].detach.restore();
  351. } );
  352. it( 'should remove all stored ranges (and reset to default range)', () => {
  353. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  354. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  355. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  356. } );
  357. it( 'should fire exactly one update event', () => {
  358. expect( spy.calledOnce ).to.be.true;
  359. } );
  360. it( 'should detach ranges', () => {
  361. expect( ranges[ 0 ].detach.called ).to.be.true;
  362. expect( ranges[ 1 ].detach.called ).to.be.true;
  363. } );
  364. } );
  365. describe( 'setRanges', () => {
  366. let newRanges, spy, oldRanges;
  367. before( () => {
  368. newRanges = [
  369. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  370. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  371. ];
  372. } );
  373. beforeEach( () => {
  374. selection.addRange( liveRange );
  375. selection.addRange( range );
  376. spy = sinon.spy();
  377. selection.on( 'change:range', spy );
  378. oldRanges = selection._ranges;
  379. sinon.spy( oldRanges[ 0 ], 'detach' );
  380. sinon.spy( oldRanges[ 1 ], 'detach' );
  381. } );
  382. afterEach( () => {
  383. oldRanges[ 0 ].detach.restore();
  384. oldRanges[ 1 ].detach.restore();
  385. } );
  386. it( 'should remove all ranges and add given ranges', () => {
  387. selection.setRanges( newRanges );
  388. let ranges = selection._ranges;
  389. expect( ranges.length ).to.equal( 2 );
  390. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  391. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  392. } );
  393. it( 'should use last range from given array to get anchor and focus position', () => {
  394. selection.setRanges( newRanges );
  395. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  396. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  397. } );
  398. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  399. selection.setRanges( newRanges, true );
  400. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  401. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  402. } );
  403. it( 'should fire exactly one update event', () => {
  404. selection.setRanges( newRanges );
  405. expect( spy.calledOnce ).to.be.true;
  406. } );
  407. it( 'should detach removed LiveRanges', () => {
  408. selection.setRanges( newRanges );
  409. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  410. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  411. } );
  412. } );
  413. describe( 'getFirstRange', () => {
  414. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  415. // This will not be the first range despite being added as first
  416. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  417. // This should be the first range.
  418. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  419. // A random range that is not first.
  420. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  421. let range = selection.getFirstRange();
  422. expect( range.start.path ).to.deep.equal( [ 1 ] );
  423. expect( range.end.path ).to.deep.equal( [ 4 ] );
  424. } );
  425. } );
  426. describe( 'getFirstPosition', () => {
  427. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  428. // This will not be a range containing the first position despite being added as first
  429. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  430. // This should be the first range.
  431. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  432. // A random range that is not first.
  433. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  434. let position = selection.getFirstPosition();
  435. expect( position.path ).to.deep.equal( [ 1 ] );
  436. } );
  437. } );
  438. // Selection uses LiveRanges so here are only simple test to see if integration is
  439. // working well, without getting into complicated corner cases.
  440. describe( 'after applying an operation should get updated and not fire update event', () => {
  441. let spy;
  442. beforeEach( () => {
  443. root.insertChildren( 0, [ new Element( 'ul', [], 'abcdef' ), new Element( 'p', [], 'foobar' ), 'xyz' ] );
  444. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  445. spy = sinon.spy();
  446. selection.on( 'change:range', spy );
  447. } );
  448. describe( 'InsertOperation', () => {
  449. it( 'before selection', () => {
  450. doc.applyOperation(
  451. new InsertOperation(
  452. new Position( root, [ 0, 1 ] ),
  453. 'xyz',
  454. doc.version
  455. )
  456. );
  457. let range = selection._ranges[ 0 ];
  458. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  459. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  460. expect( spy.called ).to.be.false;
  461. } );
  462. it( 'inside selection', () => {
  463. doc.applyOperation(
  464. new InsertOperation(
  465. new Position( root, [ 1, 0 ] ),
  466. 'xyz',
  467. doc.version
  468. )
  469. );
  470. let range = selection._ranges[ 0 ];
  471. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  472. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  473. expect( spy.called ).to.be.false;
  474. } );
  475. } );
  476. describe( 'MoveOperation', () => {
  477. it( 'move range from before a selection', () => {
  478. doc.applyOperation(
  479. new MoveOperation(
  480. new Position( root, [ 0, 0 ] ),
  481. 2,
  482. new Position( root, [ 2 ] ),
  483. doc.version
  484. )
  485. );
  486. let range = selection._ranges[ 0 ];
  487. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  488. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  489. expect( spy.called ).to.be.false;
  490. } );
  491. it( 'moved into before a selection', () => {
  492. doc.applyOperation(
  493. new MoveOperation(
  494. new Position( root, [ 2 ] ),
  495. 2,
  496. new Position( root, [ 0, 0 ] ),
  497. doc.version
  498. )
  499. );
  500. let range = selection._ranges[ 0 ];
  501. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  502. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  503. expect( spy.called ).to.be.false;
  504. } );
  505. it( 'move range from inside of selection', () => {
  506. doc.applyOperation(
  507. new MoveOperation(
  508. new Position( root, [ 1, 0 ] ),
  509. 2,
  510. new Position( root, [ 2 ] ),
  511. doc.version
  512. )
  513. );
  514. let range = selection._ranges[ 0 ];
  515. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  516. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  517. expect( spy.called ).to.be.false;
  518. } );
  519. it( 'moved range intersects with selection', () => {
  520. doc.applyOperation(
  521. new MoveOperation(
  522. new Position( root, [ 1, 3 ] ),
  523. 2,
  524. new Position( root, [ 4 ] ),
  525. doc.version
  526. )
  527. );
  528. let range = selection._ranges[ 0 ];
  529. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  530. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  531. expect( spy.called ).to.be.false;
  532. } );
  533. it( 'split inside selection (do not break selection)', () => {
  534. doc.applyOperation(
  535. new InsertOperation(
  536. new Position( root, [ 2 ] ),
  537. new Element( 'p' ),
  538. doc.version
  539. )
  540. );
  541. doc.applyOperation(
  542. new MoveOperation(
  543. new Position( root, [ 1, 2 ] ),
  544. 4,
  545. new Position( root, [ 2, 0 ] ),
  546. doc.version
  547. )
  548. );
  549. let range = selection._ranges[ 0 ];
  550. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  551. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  552. expect( spy.called ).to.be.false;
  553. } );
  554. } );
  555. } );
  556. describe( 'attributes interface', () => {
  557. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  558. beforeEach( () => {
  559. root.insertChildren( 0, [
  560. new Element( 'p', [], 'foobar' ),
  561. new Element( 'p', [], [] )
  562. ] );
  563. fullP = root.getChild( 0 );
  564. emptyP = root.getChild( 1 );
  565. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  566. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  567. } );
  568. describe( 'setAttribute', () => {
  569. it( 'should set given attribute on the selection', () => {
  570. selection.setRanges( [ rangeInFullP ] );
  571. selection.setAttribute( 'foo', 'bar' );
  572. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  573. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  574. } );
  575. it( 'should store attribute if the selection is in empty node', () => {
  576. selection.setRanges( [ rangeInEmptyP ] );
  577. selection.setAttribute( 'foo', 'bar' );
  578. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  579. expect( emptyP.getAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  580. } );
  581. it( 'should fire change:attribute event', () => {
  582. let spy = sinon.spy();
  583. selection.on( 'change:attribute', spy );
  584. selection.setAttribute( 'foo', 'bar' );
  585. expect( spy.called ).to.be.true;
  586. } );
  587. } );
  588. describe( 'hasAttribute', () => {
  589. it( 'should return true if element contains attribute with given key', () => {
  590. selection.setRanges( [ rangeInFullP ] );
  591. selection.setAttribute( 'foo', 'bar' );
  592. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  593. } );
  594. it( 'should return false if element does not contain attribute with given key', () => {
  595. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  596. } );
  597. } );
  598. describe( 'getAttribute', () => {
  599. it( 'should return undefined if element does not contain given attribute', () => {
  600. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  601. } );
  602. } );
  603. describe( 'getAttributes', () => {
  604. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  605. selection.setRanges( [ rangeInFullP ] );
  606. selection.setAttribute( 'foo', 'bar' );
  607. selection.setAttribute( 'abc', 'xyz' );
  608. let attrs = Array.from( selection.getAttributes() );
  609. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  610. } );
  611. } );
  612. describe( 'setAttributesTo', () => {
  613. it( 'should remove all attributes set on element and set the given ones', () => {
  614. selection.setRanges( [ rangeInFullP ] );
  615. selection.setAttribute( 'abc', 'xyz' );
  616. selection.setAttributesTo( { foo: 'bar' } );
  617. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  618. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  619. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  620. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  621. } );
  622. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  623. selection.setRanges( [ rangeInEmptyP ] );
  624. selection.setAttribute( 'abc', 'xyz' );
  625. selection.setAttributesTo( { foo: 'bar' } );
  626. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  627. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  628. expect( emptyP.getAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  629. expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  630. } );
  631. it( 'should fire change:attribute event', () => {
  632. let spy = sinon.spy();
  633. selection.on( 'change:attribute', spy );
  634. selection.setAttributesTo( { foo: 'bar' } );
  635. expect( spy.called ).to.be.true;
  636. } );
  637. } );
  638. describe( 'removeAttribute', () => {
  639. it( 'should remove attribute set on the text fragment', () => {
  640. selection.setRanges( [ rangeInFullP ] );
  641. selection.setAttribute( 'foo', 'bar' );
  642. selection.removeAttribute( 'foo' );
  643. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  644. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  645. } );
  646. it( 'should remove stored attribute if the selection is in empty node', () => {
  647. selection.setRanges( [ rangeInEmptyP ] );
  648. selection.setAttribute( 'foo', 'bar' );
  649. selection.removeAttribute( 'foo' );
  650. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  651. expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  652. } );
  653. it( 'should fire change:attribute event', () => {
  654. let spy = sinon.spy();
  655. selection.on( 'change:attribute', spy );
  656. selection.removeAttribute( 'foo' );
  657. expect( spy.called ).to.be.true;
  658. } );
  659. } );
  660. describe( 'clearAttributes', () => {
  661. it( 'should remove all attributes from the element', () => {
  662. selection.setRanges( [ rangeInFullP ] );
  663. selection.setAttribute( 'foo', 'bar' );
  664. selection.setAttribute( 'abc', 'xyz' );
  665. selection.clearAttributes();
  666. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  667. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  668. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  669. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  670. } );
  671. it( 'should remove all stored attributes if the selection is in empty node', () => {
  672. selection.setRanges( [ rangeInEmptyP ] );
  673. selection.setAttribute( 'foo', 'bar' );
  674. selection.setAttribute( 'abc', 'xyz' );
  675. selection.clearAttributes();
  676. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  677. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  678. expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  679. expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  680. } );
  681. it( 'should fire change:attribute event', () => {
  682. let spy = sinon.spy();
  683. selection.on( 'change:attribute', spy );
  684. selection.clearAttributes();
  685. expect( spy.called ).to.be.true;
  686. } );
  687. } );
  688. } );
  689. describe( '_updateAttributes', () => {
  690. beforeEach( () => {
  691. root.insertChildren( 0, [
  692. new Element( 'p', { p: true } ),
  693. new Text( 'a', { a: true } ),
  694. new Element( 'p', { p: true } ),
  695. new Text( 'b', { b: true } ),
  696. new Text( 'c', { c: true } ),
  697. new Element( 'p', [], [
  698. new Text( 'd', { d: true } )
  699. ] ),
  700. new Element( 'p', { p: true } ),
  701. new Text( 'e', { e: true } )
  702. ] );
  703. } );
  704. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  705. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  706. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  707. // Step into elements when looking for first character:
  708. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  709. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  710. } );
  711. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  712. // Take styles from character before selection.
  713. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  714. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  715. // If there are none,
  716. // Take styles from character after selection.
  717. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  718. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  719. // If there are none,
  720. // Look from the selection position to the beginning of node looking for character to take attributes from.
  721. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  722. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  723. // If there are none,
  724. // Look from the selection position to the end of node looking for character to take attributes from.
  725. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  726. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  727. // If there are no characters to copy attributes from, use stored attributes.
  728. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  729. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  730. } );
  731. it( 'should fire change:attribute event', () => {
  732. let spy = sinon.spy();
  733. selection.on( 'change:attribute', spy );
  734. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  735. expect( spy.called ).to.be.true;
  736. } );
  737. } );
  738. describe( '_getStoredAttributes', () => {
  739. it( 'should return no values if there are no ranges in selection', () => {
  740. let values = Array.from( selection._getStoredAttributes() );
  741. expect( values ).to.deep.equal( [] );
  742. } );
  743. } );
  744. } );