liveselection.js 25 KB

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