liveselection.js 22 KB

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