liveselection.js 22 KB

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