liveselection.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Range from '../../src/model/range';
  9. import Position from '../../src/model/position';
  10. import LiveRange from '../../src/model/liverange';
  11. import LiveSelection from '../../src/model/liveselection';
  12. import InsertOperation from '../../src/model/operation/insertoperation';
  13. import MoveOperation from '../../src/model/operation/moveoperation';
  14. import RemoveOperation from '../../src/model/operation/removeoperation';
  15. import AttributeOperation from '../../src/model/operation/attributeoperation';
  16. import SplitDelta from '../../src/model/delta/splitdelta';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. import count from '@ckeditor/ckeditor5-utils/src/count';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. import { wrapInDelta } from '../../tests/model/_utils/utils';
  21. import { setData, getData } from '../../src/dev-utils/model';
  22. import log from '@ckeditor/ckeditor5-utils/src/log';
  23. testUtils.createSinonSandbox();
  24. describe( 'LiveSelection', () => {
  25. let doc, root, selection, liveRange, range;
  26. beforeEach( () => {
  27. doc = new Document();
  28. root = doc.createRoot();
  29. root.appendChildren( [
  30. new Element( 'p' ),
  31. new Element( 'p' ),
  32. new Element( 'p', [], new Text( 'foobar' ) ),
  33. new Element( 'p' ),
  34. new Element( 'p' ),
  35. new Element( 'p' ),
  36. new Element( 'p', [], new Text( 'foobar' ) )
  37. ] );
  38. selection = doc.selection;
  39. doc.schema.registerItem( 'p', '$block' );
  40. doc.schema.registerItem( 'widget' );
  41. doc.schema.objects.add( 'widget' );
  42. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  43. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  44. } );
  45. afterEach( () => {
  46. doc.destroy();
  47. liveRange.detach();
  48. } );
  49. describe( 'default range', () => {
  50. it( 'should go to the first editable element', () => {
  51. const ranges = Array.from( selection.getRanges() );
  52. expect( ranges.length ).to.equal( 1 );
  53. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  54. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  55. expect( selection ).to.have.property( 'isBackward', false );
  56. } );
  57. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  58. doc = new Document();
  59. root = doc.createRoot();
  60. root.insertChildren( 0, new Text( 'foobar' ) );
  61. selection = doc.selection;
  62. const ranges = Array.from( selection.getRanges() );
  63. expect( ranges.length ).to.equal( 1 );
  64. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  65. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  66. expect( selection ).to.have.property( 'isBackward', false );
  67. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  68. } );
  69. it( 'should skip element when you can not put selection', () => {
  70. doc = new Document();
  71. root = doc.createRoot();
  72. root.insertChildren( 0, [
  73. new Element( 'img' ),
  74. new Element( 'p', [], new Text( 'foobar' ) )
  75. ] );
  76. doc.schema.registerItem( 'img' );
  77. doc.schema.registerItem( 'p', '$block' );
  78. selection = doc.selection;
  79. const ranges = Array.from( selection.getRanges() );
  80. expect( ranges.length ).to.equal( 1 );
  81. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  82. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  83. expect( selection ).to.have.property( 'isBackward', false );
  84. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  85. } );
  86. } );
  87. describe( 'isCollapsed', () => {
  88. it( 'should be true for the default range (in text position)', () => {
  89. expect( selection.isCollapsed ).to.be.true;
  90. } );
  91. it( 'should be false for the default range (object selection) ', () => {
  92. root.insertChildren( 0, new Element( 'widget' ) );
  93. expect( selection.isCollapsed ).to.be.false;
  94. } );
  95. it( 'should back off to the default algorithm if selection has ranges', () => {
  96. selection.addRange( range );
  97. expect( selection.isCollapsed ).to.be.false;
  98. } );
  99. } );
  100. describe( 'anchor', () => {
  101. it( 'should equal the default range\'s start (in text position)', () => {
  102. const expectedPos = new Position( root, [ 0, 0 ] );
  103. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  104. } );
  105. it( 'should equal the default range\'s start (object selection)', () => {
  106. root.insertChildren( 0, new Element( 'widget' ) );
  107. const expectedPos = new Position( root, [ 0 ] );
  108. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  109. } );
  110. it( 'should back off to the default algorithm if selection has ranges', () => {
  111. selection.addRange( range );
  112. expect( selection.anchor.isEqual( range.start ) ).to.be.true;
  113. } );
  114. } );
  115. describe( 'focus', () => {
  116. it( 'should equal the default range\'s end (in text position)', () => {
  117. const expectedPos = new Position( root, [ 0, 0 ] );
  118. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  119. } );
  120. it( 'should equal the default range\'s end (object selection)', () => {
  121. root.insertChildren( 0, new Element( 'widget' ) );
  122. const expectedPos = new Position( root, [ 1 ] );
  123. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  124. expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
  125. } );
  126. it( 'should back off to the default algorithm if selection has ranges', () => {
  127. selection.addRange( range );
  128. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  129. } );
  130. } );
  131. describe( 'rangeCount', () => {
  132. it( 'should return proper range count', () => {
  133. expect( selection.rangeCount ).to.equal( 1 );
  134. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  135. expect( selection.rangeCount ).to.equal( 1 );
  136. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  137. expect( selection.rangeCount ).to.equal( 2 );
  138. } );
  139. } );
  140. describe( 'addRange()', () => {
  141. it( 'should convert added Range to LiveRange', () => {
  142. selection.addRange( range );
  143. expect( selection._ranges[ 0 ] ).to.be.instanceof( LiveRange );
  144. } );
  145. it( 'should throw an error when range is invalid', () => {
  146. expect( () => {
  147. selection.addRange( { invalid: 'range' } );
  148. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  149. } );
  150. it( 'should not add a range that is in graveyard', () => {
  151. const spy = testUtils.sinon.stub( log, 'warn' );
  152. selection.addRange( Range.createIn( doc.graveyard ) );
  153. expect( selection._ranges.length ).to.equal( 0 );
  154. expect( spy.calledOnce ).to.be.true;
  155. } );
  156. it( 'should refresh attributes', () => {
  157. const spy = testUtils.sinon.spy( selection, '_updateAttributes' );
  158. selection.addRange( range );
  159. expect( spy.called ).to.be.true;
  160. } );
  161. } );
  162. describe( 'collapse()', () => {
  163. it( 'detaches all existing ranges', () => {
  164. selection.addRange( range );
  165. selection.addRange( liveRange );
  166. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  167. selection.collapse( root );
  168. expect( spy.calledTwice ).to.be.true;
  169. } );
  170. } );
  171. describe( 'destroy()', () => {
  172. it( 'should unbind all events', () => {
  173. selection.addRange( liveRange );
  174. selection.addRange( range );
  175. const ranges = selection._ranges;
  176. sinon.spy( ranges[ 0 ], 'detach' );
  177. sinon.spy( ranges[ 1 ], 'detach' );
  178. selection.destroy();
  179. expect( ranges[ 0 ].detach.called ).to.be.true;
  180. expect( ranges[ 1 ].detach.called ).to.be.true;
  181. ranges[ 0 ].detach.restore();
  182. ranges[ 1 ].detach.restore();
  183. } );
  184. } );
  185. describe( 'setFocus()', () => {
  186. it( 'modifies default range', () => {
  187. const startPos = selection.getFirstPosition();
  188. const endPos = Position.createAt( root, 'end' );
  189. selection.setFocus( endPos );
  190. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  191. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  192. } );
  193. it( 'detaches the range it replaces', () => {
  194. const startPos = Position.createAt( root, 1 );
  195. const endPos = Position.createAt( root, 2 );
  196. const newEndPos = Position.createAt( root, 4 );
  197. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  198. selection.addRange( new Range( startPos, endPos ) );
  199. selection.setFocus( newEndPos );
  200. expect( spy.calledOnce ).to.be.true;
  201. } );
  202. } );
  203. describe( 'removeAllRanges()', () => {
  204. let spy, ranges;
  205. beforeEach( () => {
  206. selection.addRange( liveRange );
  207. selection.addRange( range );
  208. spy = sinon.spy();
  209. selection.on( 'change:range', spy );
  210. ranges = Array.from( selection._ranges );
  211. sinon.spy( ranges[ 0 ], 'detach' );
  212. sinon.spy( ranges[ 1 ], 'detach' );
  213. selection.removeAllRanges();
  214. } );
  215. afterEach( () => {
  216. ranges[ 0 ].detach.restore();
  217. ranges[ 1 ].detach.restore();
  218. } );
  219. it( 'should remove all stored ranges (and reset to default range)', () => {
  220. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  221. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  222. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  223. } );
  224. it( 'should detach ranges', () => {
  225. expect( ranges[ 0 ].detach.called ).to.be.true;
  226. expect( ranges[ 1 ].detach.called ).to.be.true;
  227. } );
  228. it( 'should refresh attributes', () => {
  229. const spy = sinon.spy( selection, '_updateAttributes' );
  230. selection.removeAllRanges();
  231. expect( spy.called ).to.be.true;
  232. } );
  233. } );
  234. describe( 'setRanges()', () => {
  235. it( 'should throw an error when range is invalid', () => {
  236. expect( () => {
  237. selection.setRanges( [ { invalid: 'range' } ] );
  238. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  239. } );
  240. it( 'should detach removed ranges', () => {
  241. selection.addRange( liveRange );
  242. selection.addRange( range );
  243. const oldRanges = Array.from( selection._ranges );
  244. sinon.spy( oldRanges[ 0 ], 'detach' );
  245. sinon.spy( oldRanges[ 1 ], 'detach' );
  246. selection.setRanges( [] );
  247. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  248. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  249. } );
  250. it( 'should refresh attributes', () => {
  251. const spy = sinon.spy( selection, '_updateAttributes' );
  252. selection.setRanges( [ range ] );
  253. expect( spy.called ).to.be.true;
  254. } );
  255. // See #630.
  256. it( 'should refresh attributes – integration test for #630', () => {
  257. doc.schema.allow( { name: '$text', inside: '$root' } );
  258. setData( doc, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  259. selection.setRanges( [ Range.createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  260. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  261. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  262. expect( getData( doc ) )
  263. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  264. } );
  265. } );
  266. describe( 'getFirstRange()', () => {
  267. it( 'should return default range if no ranges were added', () => {
  268. const firstRange = selection.getFirstRange();
  269. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  270. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  271. } );
  272. } );
  273. describe( 'getLastRange()', () => {
  274. it( 'should return default range if no ranges were added', () => {
  275. const lastRange = selection.getLastRange();
  276. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  277. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  278. } );
  279. } );
  280. describe( 'createFromSelection()', () => {
  281. it( 'should return a LiveSelection instance', () => {
  282. selection.addRange( range, true );
  283. expect( LiveSelection.createFromSelection( selection ) ).to.be.instanceof( LiveSelection );
  284. } );
  285. } );
  286. // LiveSelection uses LiveRanges so here are only simple test to see if integration is
  287. // working well, without getting into complicated corner cases.
  288. describe( 'after applying an operation should get updated and fire events', () => {
  289. let spyRange;
  290. beforeEach( () => {
  291. root.removeChildren( 0, root.childCount );
  292. root.insertChildren( 0, [
  293. new Element( 'p', [], new Text( 'abcdef' ) ),
  294. new Element( 'p', [], new Text( 'foobar' ) ),
  295. new Text( 'xyz' )
  296. ] );
  297. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  298. spyRange = sinon.spy();
  299. selection.on( 'change:range', spyRange );
  300. } );
  301. describe( 'InsertOperation', () => {
  302. it( 'before selection', () => {
  303. selection.on( 'change:range', ( evt, data ) => {
  304. expect( data.directChange ).to.be.false;
  305. } );
  306. doc.applyOperation( wrapInDelta(
  307. new InsertOperation(
  308. new Position( root, [ 0, 1 ] ),
  309. 'xyz',
  310. doc.version
  311. )
  312. ) );
  313. const range = selection.getFirstRange();
  314. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  315. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  316. expect( spyRange.calledOnce ).to.be.true;
  317. } );
  318. it( 'inside selection', () => {
  319. selection.on( 'change:range', ( evt, data ) => {
  320. expect( data.directChange ).to.be.false;
  321. } );
  322. doc.applyOperation( wrapInDelta(
  323. new InsertOperation(
  324. new Position( root, [ 1, 0 ] ),
  325. 'xyz',
  326. doc.version
  327. )
  328. ) );
  329. const range = selection.getFirstRange();
  330. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  331. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  332. expect( spyRange.calledOnce ).to.be.true;
  333. } );
  334. } );
  335. describe( 'MoveOperation', () => {
  336. it( 'move range from before a selection', () => {
  337. selection.on( 'change:range', ( evt, data ) => {
  338. expect( data.directChange ).to.be.false;
  339. } );
  340. doc.applyOperation( wrapInDelta(
  341. new MoveOperation(
  342. new Position( root, [ 0, 0 ] ),
  343. 2,
  344. new Position( root, [ 2 ] ),
  345. doc.version
  346. )
  347. ) );
  348. const range = selection.getFirstRange();
  349. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  350. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  351. expect( spyRange.calledOnce ).to.be.true;
  352. } );
  353. it( 'moved into before a selection', () => {
  354. selection.on( 'change:range', ( evt, data ) => {
  355. expect( data.directChange ).to.be.false;
  356. } );
  357. doc.applyOperation( wrapInDelta(
  358. new MoveOperation(
  359. new Position( root, [ 2 ] ),
  360. 2,
  361. new Position( root, [ 0, 0 ] ),
  362. doc.version
  363. )
  364. ) );
  365. const range = selection.getFirstRange();
  366. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  367. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  368. expect( spyRange.calledOnce ).to.be.true;
  369. } );
  370. it( 'move range from inside of selection', () => {
  371. selection.on( 'change:range', ( evt, data ) => {
  372. expect( data.directChange ).to.be.false;
  373. } );
  374. doc.applyOperation( wrapInDelta(
  375. new MoveOperation(
  376. new Position( root, [ 1, 0 ] ),
  377. 2,
  378. new Position( root, [ 2 ] ),
  379. doc.version
  380. )
  381. ) );
  382. const range = selection.getFirstRange();
  383. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  384. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  385. expect( spyRange.calledOnce ).to.be.true;
  386. } );
  387. it( 'moved range intersects with selection', () => {
  388. selection.on( 'change:range', ( evt, data ) => {
  389. expect( data.directChange ).to.be.false;
  390. } );
  391. doc.applyOperation( wrapInDelta(
  392. new MoveOperation(
  393. new Position( root, [ 1, 3 ] ),
  394. 2,
  395. new Position( root, [ 4 ] ),
  396. doc.version
  397. )
  398. ) );
  399. const range = selection.getFirstRange();
  400. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  401. expect( range.end.path ).to.deep.equal( [ 5 ] );
  402. expect( spyRange.calledOnce ).to.be.true;
  403. } );
  404. it( 'split inside selection (do not break selection)', () => {
  405. selection.on( 'change:range', ( evt, data ) => {
  406. expect( data.directChange ).to.be.false;
  407. } );
  408. const splitDelta = new SplitDelta();
  409. const insertOperation = new InsertOperation(
  410. new Position( root, [ 2 ] ),
  411. new Element( 'p' ),
  412. 0
  413. );
  414. const moveOperation = new MoveOperation(
  415. new Position( root, [ 1, 2 ] ),
  416. 4,
  417. new Position( root, [ 2, 0 ] ),
  418. 1
  419. );
  420. splitDelta.addOperation( insertOperation );
  421. splitDelta.addOperation( moveOperation );
  422. doc.applyOperation( insertOperation );
  423. doc.applyOperation( moveOperation );
  424. const range = selection.getFirstRange();
  425. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  426. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  427. expect( spyRange.calledOnce ).to.be.true;
  428. } );
  429. } );
  430. describe( 'AttributeOperation', () => {
  431. it( 'changed range includes selection anchor', () => {
  432. const spyAttribute = sinon.spy();
  433. selection.on( 'change:attribute', spyAttribute );
  434. selection.on( 'change:attribute', ( evt, data ) => {
  435. expect( data.directChange ).to.be.false;
  436. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  437. } );
  438. doc.applyOperation( wrapInDelta(
  439. new AttributeOperation(
  440. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  441. 'foo',
  442. null,
  443. 'bar',
  444. doc.version
  445. )
  446. ) );
  447. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  448. expect( spyAttribute.calledOnce ).to.be.true;
  449. } );
  450. it( 'should not overwrite previously set attributes', () => {
  451. selection.setAttribute( 'foo', 'xyz' );
  452. const spyAttribute = sinon.spy();
  453. selection.on( 'change:attribute', spyAttribute );
  454. doc.applyOperation( wrapInDelta(
  455. new AttributeOperation(
  456. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  457. 'foo',
  458. null,
  459. 'bar',
  460. doc.version
  461. )
  462. ) );
  463. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  464. expect( spyAttribute.called ).to.be.false;
  465. } );
  466. it( 'should not overwrite previously removed attributes', () => {
  467. selection.setAttribute( 'foo', 'xyz' );
  468. selection.removeAttribute( 'foo' );
  469. const spyAttribute = sinon.spy();
  470. selection.on( 'change:attribute', spyAttribute );
  471. doc.applyOperation( wrapInDelta(
  472. new AttributeOperation(
  473. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  474. 'foo',
  475. null,
  476. 'bar',
  477. doc.version
  478. )
  479. ) );
  480. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  481. expect( spyAttribute.called ).to.be.false;
  482. } );
  483. } );
  484. describe( 'RemoveOperation', () => {
  485. it( 'fix selection range if it ends up in graveyard #1', () => {
  486. selection.collapse( new Position( root, [ 1, 3 ] ) );
  487. doc.applyOperation( wrapInDelta(
  488. new RemoveOperation(
  489. new Position( root, [ 1, 2 ] ),
  490. 2,
  491. doc.version
  492. )
  493. ) );
  494. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  495. } );
  496. it( 'fix selection range if it ends up in graveyard #2', () => {
  497. selection.setRanges( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  498. doc.applyOperation( wrapInDelta(
  499. new RemoveOperation(
  500. new Position( root, [ 1, 2 ] ),
  501. 2,
  502. doc.version
  503. )
  504. ) );
  505. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  506. } );
  507. it( 'fix selection range if it ends up in graveyard #3', () => {
  508. selection.setRanges( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  509. doc.applyOperation( wrapInDelta(
  510. new RemoveOperation(
  511. new Position( root, [ 1 ] ),
  512. 2,
  513. doc.version
  514. )
  515. ) );
  516. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  517. } );
  518. } );
  519. } );
  520. describe( 'attributes interface', () => {
  521. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  522. beforeEach( () => {
  523. root.insertChildren( 0, [
  524. new Element( 'p', [], new Text( 'foobar' ) ),
  525. new Element( 'p', [], [] )
  526. ] );
  527. fullP = root.getChild( 0 );
  528. emptyP = root.getChild( 1 );
  529. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  530. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  531. } );
  532. describe( 'setAttribute', () => {
  533. it( 'should store attribute if the selection is in empty node', () => {
  534. selection.setRanges( [ rangeInEmptyP ] );
  535. selection.setAttribute( 'foo', 'bar' );
  536. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  537. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  538. } );
  539. } );
  540. describe( 'setAttributesTo', () => {
  541. it( 'should fire change:attribute event with correct parameters', done => {
  542. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  543. selection.on( 'change:attribute', ( evt, data ) => {
  544. expect( data.directChange ).to.be.true;
  545. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  546. done();
  547. } );
  548. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  549. } );
  550. it( 'should not fire change:attribute event if same attributes are set', () => {
  551. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  552. const spy = sinon.spy();
  553. selection.on( 'change:attribute', spy );
  554. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  555. expect( spy.called ).to.be.false;
  556. } );
  557. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  558. selection.setRanges( [ rangeInEmptyP ] );
  559. selection.setAttribute( 'abc', 'xyz' );
  560. selection.setAttributesTo( { foo: 'bar' } );
  561. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  562. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  563. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  564. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  565. } );
  566. } );
  567. describe( 'removeAttribute', () => {
  568. it( 'should remove attribute set on the text fragment', () => {
  569. selection.setRanges( [ rangeInFullP ] );
  570. selection.setAttribute( 'foo', 'bar' );
  571. selection.removeAttribute( 'foo' );
  572. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  573. expect( fullP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  574. } );
  575. it( 'should remove stored attribute if the selection is in empty node', () => {
  576. selection.setRanges( [ rangeInEmptyP ] );
  577. selection.setAttribute( 'foo', 'bar' );
  578. selection.removeAttribute( 'foo' );
  579. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  580. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  581. } );
  582. } );
  583. describe( 'clearAttributes', () => {
  584. it( 'should remove all stored attributes if the selection is in empty node', () => {
  585. selection.setRanges( [ rangeInEmptyP ] );
  586. selection.setAttribute( 'foo', 'bar' );
  587. selection.setAttribute( 'abc', 'xyz' );
  588. selection.clearAttributes();
  589. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  590. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  591. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  592. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  593. } );
  594. } );
  595. } );
  596. describe( 'update attributes on direct range change', () => {
  597. beforeEach( () => {
  598. root.insertChildren( 0, [
  599. new Element( 'p', { p: true } ),
  600. new Text( 'a', { a: true } ),
  601. new Element( 'p', { p: true } ),
  602. new Text( 'b', { b: true } ),
  603. new Text( 'c', { c: true } ),
  604. new Element( 'p', [], [
  605. new Text( 'd', { d: true } )
  606. ] ),
  607. new Element( 'p', { p: true } ),
  608. new Text( 'e', { e: true } )
  609. ] );
  610. } );
  611. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  612. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  613. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  614. // Step into elements when looking for first character:
  615. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  616. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  617. } );
  618. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  619. // Take styles from character before selection.
  620. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  621. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  622. // If there are none,
  623. // Take styles from character after selection.
  624. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  625. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  626. // If there are none,
  627. // Look from the selection position to the beginning of node looking for character to take attributes from.
  628. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  629. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  630. // If there are none,
  631. // Look from the selection position to the end of node looking for character to take attributes from.
  632. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  633. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  634. // If there are no characters to copy attributes from, use stored attributes.
  635. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  636. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  637. } );
  638. it( 'should overwrite any previously set attributes', () => {
  639. selection.collapse( new Position( root, [ 5, 0 ] ) );
  640. selection.setAttribute( 'x', true );
  641. selection.setAttribute( 'y', true );
  642. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  643. selection.collapse( new Position( root, [ 1 ] ) );
  644. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  645. } );
  646. it( 'should fire change:attribute event', () => {
  647. const spy = sinon.spy();
  648. selection.on( 'change:attribute', spy );
  649. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  650. expect( spy.calledOnce ).to.be.true;
  651. } );
  652. it( 'should not fire change:attribute event if attributes did not change', () => {
  653. selection.collapse( new Position( root, [ 5, 0 ] ) );
  654. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  655. const spy = sinon.spy();
  656. selection.on( 'change:attribute', spy );
  657. selection.collapse( new Position( root, [ 5, 1 ] ) );
  658. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  659. expect( spy.called ).to.be.false;
  660. } );
  661. } );
  662. describe( '_getStoredAttributes', () => {
  663. it( 'should return no values if there are no ranges in selection', () => {
  664. const values = Array.from( selection._getStoredAttributes() );
  665. expect( values ).to.deep.equal( [] );
  666. } );
  667. } );
  668. } );