liveselection.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. import log from 'ckeditor5/utils/log.js';
  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. doc.applyOperation( wrapInDelta(
  361. new InsertOperation(
  362. new Position( root, [ 2 ] ),
  363. new Element( 'p' ),
  364. doc.version
  365. )
  366. ) );
  367. doc.applyOperation( wrapInDelta(
  368. new MoveOperation(
  369. new Position( root, [ 1, 2 ] ),
  370. 4,
  371. new Position( root, [ 2, 0 ] ),
  372. doc.version
  373. )
  374. ) );
  375. let range = selection.getFirstRange();
  376. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  377. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  378. expect( spyRange.calledOnce ).to.be.true;
  379. } );
  380. } );
  381. describe( 'AttributeOperation', () => {
  382. it( 'changed range includes selection anchor', () => {
  383. const spyAttribute = sinon.spy();
  384. selection.on( 'change:attribute', spyAttribute );
  385. selection.on( 'change:attribute', ( evt, data ) => {
  386. expect( data.directChange ).to.be.false;
  387. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  388. } );
  389. doc.applyOperation( wrapInDelta(
  390. new AttributeOperation(
  391. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  392. 'foo',
  393. null,
  394. 'bar',
  395. doc.version
  396. )
  397. ) );
  398. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  399. expect( spyAttribute.calledOnce ).to.be.true;
  400. } );
  401. it( 'should not overwrite previously set attributes', () => {
  402. selection.setAttribute( 'foo', 'xyz' );
  403. const spyAttribute = sinon.spy();
  404. selection.on( 'change:attribute', spyAttribute );
  405. doc.applyOperation( wrapInDelta(
  406. new AttributeOperation(
  407. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  408. 'foo',
  409. null,
  410. 'bar',
  411. doc.version
  412. )
  413. ) );
  414. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  415. expect( spyAttribute.called ).to.be.false;
  416. } );
  417. it( 'should not overwrite previously removed attributes', () => {
  418. selection.setAttribute( 'foo', 'xyz' );
  419. selection.removeAttribute( 'foo' );
  420. const spyAttribute = sinon.spy();
  421. selection.on( 'change:attribute', spyAttribute );
  422. doc.applyOperation( wrapInDelta(
  423. new AttributeOperation(
  424. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  425. 'foo',
  426. null,
  427. 'bar',
  428. doc.version
  429. )
  430. ) );
  431. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  432. expect( spyAttribute.called ).to.be.false;
  433. } );
  434. } );
  435. describe( 'RemoveOperation', () => {
  436. it( 'fix selection range if it ends up in graveyard #1', () => {
  437. selection.collapse( new Position( root, [ 1, 3 ] ) );
  438. doc.applyOperation( wrapInDelta(
  439. new RemoveOperation(
  440. new Position( root, [ 1, 2 ] ),
  441. 2,
  442. doc.version
  443. )
  444. ) );
  445. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  446. } );
  447. it( 'fix selection range if it ends up in graveyard #2', () => {
  448. selection.setRanges( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  449. doc.applyOperation( wrapInDelta(
  450. new RemoveOperation(
  451. new Position( root, [ 1, 2 ] ),
  452. 2,
  453. doc.version
  454. )
  455. ) );
  456. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  457. } );
  458. it( 'fix selection range if it ends up in graveyard #3', () => {
  459. selection.setRanges( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  460. doc.applyOperation( wrapInDelta(
  461. new RemoveOperation(
  462. new Position( root, [ 1 ] ),
  463. 2,
  464. doc.version
  465. )
  466. ) );
  467. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  468. } );
  469. } );
  470. } );
  471. describe( 'attributes interface', () => {
  472. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  473. beforeEach( () => {
  474. root.insertChildren( 0, [
  475. new Element( 'p', [], new Text( 'foobar' ) ),
  476. new Element( 'p', [], [] )
  477. ] );
  478. fullP = root.getChild( 0 );
  479. emptyP = root.getChild( 1 );
  480. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  481. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  482. } );
  483. describe( 'setAttribute', () => {
  484. it( 'should store attribute if the selection is in empty node', () => {
  485. selection.setRanges( [ rangeInEmptyP ] );
  486. selection.setAttribute( 'foo', 'bar' );
  487. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  488. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  489. } );
  490. } );
  491. describe( 'setAttributesTo', () => {
  492. it( 'should fire change:attribute event with correct parameters', ( done ) => {
  493. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  494. selection.on( 'change:attribute', ( evt, data ) => {
  495. expect( data.directChange ).to.be.true;
  496. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  497. done();
  498. } );
  499. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  500. } );
  501. it( 'should not fire change:attribute event if same attributes are set', () => {
  502. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  503. const spy = sinon.spy();
  504. selection.on( 'change:attribute', spy );
  505. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  506. expect( spy.called ).to.be.false;
  507. } );
  508. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  509. selection.setRanges( [ rangeInEmptyP ] );
  510. selection.setAttribute( 'abc', 'xyz' );
  511. selection.setAttributesTo( { foo: 'bar' } );
  512. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  513. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  514. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  515. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  516. } );
  517. } );
  518. describe( 'removeAttribute', () => {
  519. it( 'should remove attribute set on the text fragment', () => {
  520. selection.setRanges( [ rangeInFullP ] );
  521. selection.setAttribute( 'foo', 'bar' );
  522. selection.removeAttribute( 'foo' );
  523. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  524. expect( fullP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  525. } );
  526. it( 'should remove stored attribute if the selection is in empty node', () => {
  527. selection.setRanges( [ rangeInEmptyP ] );
  528. selection.setAttribute( 'foo', 'bar' );
  529. selection.removeAttribute( 'foo' );
  530. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  531. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  532. } );
  533. } );
  534. describe( 'clearAttributes', () => {
  535. it( 'should remove all stored attributes if the selection is in empty node', () => {
  536. selection.setRanges( [ rangeInEmptyP ] );
  537. selection.setAttribute( 'foo', 'bar' );
  538. selection.setAttribute( 'abc', 'xyz' );
  539. selection.clearAttributes();
  540. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  541. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  542. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  543. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  544. } );
  545. } );
  546. } );
  547. describe( 'update attributes on direct range change', () => {
  548. beforeEach( () => {
  549. root.insertChildren( 0, [
  550. new Element( 'p', { p: true } ),
  551. new Text( 'a', { a: true } ),
  552. new Element( 'p', { p: true } ),
  553. new Text( 'b', { b: true } ),
  554. new Text( 'c', { c: true } ),
  555. new Element( 'p', [], [
  556. new Text( 'd', { d: true } )
  557. ] ),
  558. new Element( 'p', { p: true } ),
  559. new Text( 'e', { e: true } )
  560. ] );
  561. } );
  562. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  563. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  564. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  565. // Step into elements when looking for first character:
  566. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  567. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  568. } );
  569. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  570. // Take styles from character before selection.
  571. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  572. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  573. // If there are none,
  574. // Take styles from character after selection.
  575. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  576. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  577. // If there are none,
  578. // Look from the selection position to the beginning of node looking for character to take attributes from.
  579. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  580. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  581. // If there are none,
  582. // Look from the selection position to the end of node looking for character to take attributes from.
  583. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  584. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  585. // If there are no characters to copy attributes from, use stored attributes.
  586. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  587. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  588. } );
  589. it( 'should overwrite any previously set attributes', () => {
  590. selection.collapse( new Position( root, [ 5, 0 ] ) );
  591. selection.setAttribute( 'x', true );
  592. selection.setAttribute( 'y', true );
  593. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  594. selection.collapse( new Position( root, [ 1 ] ) );
  595. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  596. } );
  597. it( 'should fire change:attribute event', () => {
  598. let spy = sinon.spy();
  599. selection.on( 'change:attribute', spy );
  600. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  601. expect( spy.calledOnce ).to.be.true;
  602. } );
  603. it( 'should not fire change:attribute event if attributes did not change', () => {
  604. selection.collapse( new Position( root, [ 5, 0 ] ) );
  605. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  606. let spy = sinon.spy();
  607. selection.on( 'change:attribute', spy );
  608. selection.collapse( new Position( root, [ 5, 1 ] ) );
  609. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  610. expect( spy.called ).to.be.false;
  611. } );
  612. } );
  613. describe( '_getStoredAttributes', () => {
  614. it( 'should return no values if there are no ranges in selection', () => {
  615. let values = Array.from( selection._getStoredAttributes() );
  616. expect( values ).to.deep.equal( [] );
  617. } );
  618. } );
  619. } );