8
0

liveselection.js 22 KB

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