8
0

liveselection.js 24 KB

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