liveselection.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Document from 'ckeditor5/engine/model/document.js';
  7. import Element from 'ckeditor5/engine/model/element.js';
  8. import Text from 'ckeditor5/engine/model/text.js';
  9. import Range from 'ckeditor5/engine/model/range.js';
  10. import Position from 'ckeditor5/engine/model/position.js';
  11. import LiveRange from 'ckeditor5/engine/model/liverange.js';
  12. import LiveSelection from 'ckeditor5/engine/model/liveselection.js';
  13. import InsertOperation from 'ckeditor5/engine/model/operation/insertoperation.js';
  14. import MoveOperation from 'ckeditor5/engine/model/operation/moveoperation.js';
  15. import RemoveOperation from 'ckeditor5/engine/model/operation/removeoperation.js';
  16. import AttributeOperation from 'ckeditor5/engine/model/operation/attributeoperation.js';
  17. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  18. import count from 'ckeditor5/utils/count.js';
  19. import testUtils from 'tests/core/_utils/utils.js';
  20. import { wrapInDelta } from 'tests/engine/model/_utils/utils.js';
  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. selection.addRange( Range.createIn( doc.graveyard ) );
  113. expect( selection._ranges.length ).to.equal( 0 );
  114. } );
  115. it( 'should refresh attributes', () => {
  116. const spy = 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. } );
  215. describe( 'getFirstRange', () => {
  216. it( 'should return default range if no ranges were added', () => {
  217. const firstRange = selection.getFirstRange();
  218. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  219. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  220. } );
  221. } );
  222. describe( 'getLastRange', () => {
  223. it( 'should return default range if no ranges were added', () => {
  224. const lastRange = selection.getLastRange();
  225. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  226. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  227. } );
  228. } );
  229. describe( 'createFromSelection', () => {
  230. it( 'should return a LiveSelection instance', () => {
  231. selection.addRange( range, true );
  232. expect( LiveSelection.createFromSelection( selection ) ).to.be.instanceof( LiveSelection );
  233. } );
  234. } );
  235. // LiveSelection uses LiveRanges so here are only simple test to see if integration is
  236. // working well, without getting into complicated corner cases.
  237. describe( 'after applying an operation should get updated and fire events', () => {
  238. let spyRange;
  239. beforeEach( () => {
  240. root.removeChildren( 0, root.childCount );
  241. root.insertChildren( 0, [
  242. new Element( 'ul', [], new Text( 'abcdef' ) ),
  243. new Element( 'p', [], new Text( 'foobar' ) ),
  244. new Text( 'xyz' )
  245. ] );
  246. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  247. spyRange = sinon.spy();
  248. selection.on( 'change:range', spyRange );
  249. } );
  250. describe( 'InsertOperation', () => {
  251. it( 'before selection', () => {
  252. selection.on( 'change:range', ( evt, data ) => {
  253. expect( data.directChange ).to.be.false;
  254. } );
  255. doc.applyOperation( wrapInDelta(
  256. new InsertOperation(
  257. new Position( root, [ 0, 1 ] ),
  258. 'xyz',
  259. doc.version
  260. )
  261. ) );
  262. let range = selection.getFirstRange();
  263. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  264. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  265. expect( spyRange.calledOnce ).to.be.true;
  266. } );
  267. it( 'inside selection', () => {
  268. selection.on( 'change:range', ( evt, data ) => {
  269. expect( data.directChange ).to.be.false;
  270. } );
  271. doc.applyOperation( wrapInDelta(
  272. new InsertOperation(
  273. new Position( root, [ 1, 0 ] ),
  274. 'xyz',
  275. doc.version
  276. )
  277. ) );
  278. let range = selection.getFirstRange();
  279. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  280. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  281. expect( spyRange.calledOnce ).to.be.true;
  282. } );
  283. } );
  284. describe( 'MoveOperation', () => {
  285. it( 'move range from before a selection', () => {
  286. selection.on( 'change:range', ( evt, data ) => {
  287. expect( data.directChange ).to.be.false;
  288. } );
  289. doc.applyOperation( wrapInDelta(
  290. new MoveOperation(
  291. new Position( root, [ 0, 0 ] ),
  292. 2,
  293. new Position( root, [ 2 ] ),
  294. doc.version
  295. )
  296. ) );
  297. let range = selection.getFirstRange();
  298. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  299. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  300. expect( spyRange.calledOnce ).to.be.true;
  301. } );
  302. it( 'moved into before a selection', () => {
  303. selection.on( 'change:range', ( evt, data ) => {
  304. expect( data.directChange ).to.be.false;
  305. } );
  306. doc.applyOperation( wrapInDelta(
  307. new MoveOperation(
  308. new Position( root, [ 2 ] ),
  309. 2,
  310. new Position( root, [ 0, 0 ] ),
  311. doc.version
  312. )
  313. ) );
  314. let range = selection.getFirstRange();
  315. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  316. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  317. expect( spyRange.calledOnce ).to.be.true;
  318. } );
  319. it( 'move range from inside of selection', () => {
  320. selection.on( 'change:range', ( evt, data ) => {
  321. expect( data.directChange ).to.be.false;
  322. } );
  323. doc.applyOperation( wrapInDelta(
  324. new MoveOperation(
  325. new Position( root, [ 1, 0 ] ),
  326. 2,
  327. new Position( root, [ 2 ] ),
  328. doc.version
  329. )
  330. ) );
  331. let range = selection.getFirstRange();
  332. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  333. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  334. expect( spyRange.calledOnce ).to.be.true;
  335. } );
  336. it( 'moved range intersects with selection', () => {
  337. selection.on( 'change:range', ( evt, data ) => {
  338. expect( data.directChange ).to.be.false;
  339. } );
  340. doc.applyOperation( wrapInDelta(
  341. new MoveOperation(
  342. new Position( root, [ 1, 3 ] ),
  343. 2,
  344. new Position( root, [ 4 ] ),
  345. doc.version
  346. )
  347. ) );
  348. let range = selection.getFirstRange();
  349. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  350. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  351. expect( spyRange.calledOnce ).to.be.true;
  352. } );
  353. it( 'split inside selection (do not break selection)', () => {
  354. selection.on( 'change:range', ( evt, data ) => {
  355. expect( data.directChange ).to.be.false;
  356. } );
  357. doc.applyOperation( wrapInDelta(
  358. new InsertOperation(
  359. new Position( root, [ 2 ] ),
  360. new Element( 'p' ),
  361. doc.version
  362. )
  363. ) );
  364. doc.applyOperation( wrapInDelta(
  365. new MoveOperation(
  366. new Position( root, [ 1, 2 ] ),
  367. 4,
  368. new Position( root, [ 2, 0 ] ),
  369. doc.version
  370. )
  371. ) );
  372. let 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, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  457. doc.applyOperation( wrapInDelta(
  458. new RemoveOperation(
  459. new Position( root, [ 0 ] ),
  460. 3,
  461. doc.version
  462. )
  463. ) );
  464. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  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. let 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. let 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. let values = Array.from( selection._getStoredAttributes() );
  613. expect( values ).to.deep.equal( [] );
  614. } );
  615. } );
  616. } );