8
0

liveselection.js 25 KB

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