selection.js 22 KB

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