liveselection.js 25 KB

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