8
0

liveselection.js 24 KB

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