selection.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 Selection from '/ckeditor5/engine/model/selection.js';
  14. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  15. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  16. import count from '/ckeditor5/utils/count.js';
  17. testUtils.createSinonSandbox();
  18. describe( 'Selection', () => {
  19. let doc, root, selection, liveRange, range, range1, range2, range3;
  20. beforeEach( () => {
  21. doc = new Document();
  22. root = doc.createRoot();
  23. root.appendChildren( [
  24. new Element( 'p' ),
  25. new Element( 'p' ),
  26. new Element( 'p', [], new Text( 'foobar' ) ),
  27. new Element( 'p' ),
  28. new Element( 'p' ),
  29. new Element( 'p' ),
  30. new Element( 'p', [], new Text( 'foobar' ) )
  31. ] );
  32. selection = new Selection();
  33. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  34. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  35. range1 = new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) );
  36. range2 = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  37. range3 = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  38. } );
  39. afterEach( () => {
  40. doc.destroy();
  41. liveRange.detach();
  42. } );
  43. describe( 'isCollapsed', () => {
  44. it( 'should return false for empty selection', () => {
  45. expect( selection.isCollapsed ).to.be.false;
  46. } );
  47. it( 'should return true when there is single collapsed ranges', () => {
  48. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  49. expect( selection.isCollapsed ).to.be.true;
  50. } );
  51. it( 'should return false when there are multiple ranges', () => {
  52. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  53. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  54. expect( selection.isCollapsed ).to.be.false;
  55. } );
  56. it( 'should return false when there is not collapsed range', () => {
  57. selection.addRange( range );
  58. expect( selection.isCollapsed ).to.be.false;
  59. } );
  60. } );
  61. describe( 'rangeCount', () => {
  62. it( 'should return proper range count', () => {
  63. expect( selection.rangeCount ).to.equal( 0 );
  64. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  65. expect( selection.rangeCount ).to.equal( 1 );
  66. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  67. expect( selection.rangeCount ).to.equal( 2 );
  68. } );
  69. } );
  70. describe( 'isBackward', () => {
  71. it( 'is defined by the last added range', () => {
  72. selection.addRange( range, true );
  73. expect( selection ).to.have.property( 'isBackward', true );
  74. selection.addRange( liveRange );
  75. expect( selection ).to.have.property( 'isBackward', false );
  76. } );
  77. it( 'is false when last range is collapsed', () => {
  78. const pos = Position.createAt( root, 0 );
  79. selection.addRange( new Range( pos, pos ), true );
  80. expect( selection.isBackward ).to.be.false;
  81. } );
  82. } );
  83. describe( 'addRange', () => {
  84. it( 'should throw an error when range is invalid', () => {
  85. expect( () => {
  86. selection.addRange( { invalid: 'Range' } );
  87. } ).to.throw( CKEditorError, 'selection-invalid-range: Invalid Range.' );
  88. } );
  89. it( 'should copy added ranges and store multiple ranges', () => {
  90. selection.addRange( liveRange );
  91. selection.addRange( range );
  92. const ranges = selection._ranges;
  93. expect( ranges.length ).to.equal( 2 );
  94. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  95. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  96. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  97. expect( ranges[ 1 ] ).not.to.be.equal( range );
  98. } );
  99. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  100. selection.addRange( liveRange );
  101. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  102. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  103. selection.addRange( range );
  104. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  105. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  106. } );
  107. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  108. selection.addRange( liveRange, true );
  109. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  110. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  111. selection.addRange( range, true );
  112. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  113. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  114. } );
  115. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  116. selection.addRange( liveRange );
  117. const ranges = Array.from( selection.getRanges() );
  118. selection.addRange( range );
  119. expect( ranges.length ).to.equal( 1 );
  120. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  121. } );
  122. it( 'should fire change:range event when adding a range', () => {
  123. let spy = sinon.spy();
  124. selection.on( 'change:range', spy );
  125. selection.addRange( range );
  126. expect( spy.called ).to.be.true;
  127. } );
  128. it( 'should throw an error if added range intersects with already stored range', () => {
  129. selection.addRange( liveRange );
  130. expect( () => {
  131. selection.addRange(
  132. new Range(
  133. new Position( root, [ 0, 4 ] ),
  134. new Position( root, [ 1, 2 ] )
  135. )
  136. );
  137. } ).to.throw( CKEditorError, /selection-range-intersects/ );
  138. } );
  139. } );
  140. describe( 'collapse', () => {
  141. it( 'fires change:range', () => {
  142. const spy = sinon.spy();
  143. selection.on( 'change:range', spy );
  144. selection.collapse( root );
  145. expect( spy.calledOnce ).to.be.true;
  146. } );
  147. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  148. selection.collapse( root );
  149. expect( selection ).to.have.property( 'isCollapsed', true );
  150. const focus = selection.focus;
  151. expect( focus ).to.have.property( 'parent', root );
  152. expect( focus ).to.have.property( 'offset', 0 );
  153. } );
  154. it( 'sets selection at given offset in given parent', () => {
  155. selection.collapse( root, 3 );
  156. expect( selection ).to.have.property( 'isCollapsed', true );
  157. const focus = selection.focus;
  158. expect( focus ).to.have.property( 'parent', root );
  159. expect( focus ).to.have.property( 'offset', 3 );
  160. } );
  161. it( 'sets selection at the end of the given parent', () => {
  162. selection.collapse( root, 'end' );
  163. expect( selection ).to.have.property( 'isCollapsed', true );
  164. const focus = selection.focus;
  165. expect( focus ).to.have.property( 'parent', root );
  166. expect( focus ).to.have.property( 'offset', root.getMaxOffset() );
  167. } );
  168. it( 'sets selection before the specified element', () => {
  169. selection.collapse( root.getChild( 1 ), 'before' );
  170. expect( selection ).to.have.property( 'isCollapsed', true );
  171. const focus = selection.focus;
  172. expect( focus ).to.have.property( 'parent', root );
  173. expect( focus ).to.have.property( 'offset', 1 );
  174. } );
  175. it( 'sets selection after the specified element', () => {
  176. selection.collapse( root.getChild( 1 ), 'after' );
  177. expect( selection ).to.have.property( 'isCollapsed', true );
  178. const focus = selection.focus;
  179. expect( focus ).to.have.property( 'parent', root );
  180. expect( focus ).to.have.property( 'offset', 2 );
  181. } );
  182. it( 'sets selection at the specified position', () => {
  183. const pos = Position.createFromParentAndOffset( root, 3 );
  184. selection.collapse( pos );
  185. expect( selection ).to.have.property( 'isCollapsed', true );
  186. const focus = selection.focus;
  187. expect( focus ).to.have.property( 'parent', root );
  188. expect( focus ).to.have.property( 'offset', 3 );
  189. } );
  190. } );
  191. describe( 'focus', () => {
  192. let r3;
  193. beforeEach( () => {
  194. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  195. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  196. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  197. selection.addRange( r1 );
  198. selection.addRange( r2 );
  199. } );
  200. it( 'should return correct focus when last added range is not backward one', () => {
  201. selection.addRange( r3 );
  202. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  203. } );
  204. it( 'should return correct focus when last added range is backward one', () => {
  205. selection.addRange( r3, true );
  206. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  207. } );
  208. it( 'should return null if no ranges in selection', () => {
  209. selection.removeAllRanges();
  210. expect( selection.focus ).to.be.null;
  211. } );
  212. } );
  213. describe( 'setFocus', () => {
  214. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  215. selection.addRange( range );
  216. selection.addRange( liveRange );
  217. const spy = sinon.spy();
  218. selection.on( 'change:range', spy );
  219. selection.setFocus( selection.focus );
  220. expect( count( selection.getRanges() ) ).to.equal( 2 );
  221. expect( spy.callCount ).to.equal( 0 );
  222. } );
  223. it( 'fires change:range', () => {
  224. selection.addRange( range );
  225. const spy = sinon.spy();
  226. selection.on( 'change:range', spy );
  227. selection.setFocus( Position.createAt( root, 'end' ) );
  228. expect( spy.calledOnce ).to.be.true;
  229. } );
  230. it( 'throws if there are no ranges in selection', () => {
  231. const endPos = Position.createAt( root, 'end' );
  232. expect( () => {
  233. selection.setFocus( endPos );
  234. } ).to.throw( CKEditorError, /selection-setFocus-no-ranges/ );
  235. } );
  236. it( 'modifies existing collapsed selection', () => {
  237. const startPos = Position.createAt( root, 1 );
  238. const endPos = Position.createAt( root, 2 );
  239. selection.collapse( startPos );
  240. selection.setFocus( endPos );
  241. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  242. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  243. } );
  244. it( 'makes existing collapsed selection a backward selection', () => {
  245. const startPos = Position.createAt( root, 1 );
  246. const endPos = Position.createAt( root, 0 );
  247. selection.collapse( startPos );
  248. selection.setFocus( endPos );
  249. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  250. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  251. expect( selection.isBackward ).to.be.true;
  252. } );
  253. it( 'modifies existing non-collapsed selection', () => {
  254. const startPos = Position.createAt( root, 1 );
  255. const endPos = Position.createAt( root, 2 );
  256. const newEndPos = Position.createAt( root, 3 );
  257. selection.addRange( new Range( startPos, endPos ) );
  258. selection.setFocus( newEndPos );
  259. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  260. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  261. } );
  262. it( 'makes existing non-collapsed selection a backward selection', () => {
  263. const startPos = Position.createAt( root, 1 );
  264. const endPos = Position.createAt( root, 2 );
  265. const newEndPos = Position.createAt( root, 0 );
  266. selection.addRange( new Range( startPos, endPos ) );
  267. selection.setFocus( newEndPos );
  268. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  269. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  270. expect( selection.isBackward ).to.be.true;
  271. } );
  272. it( 'makes existing backward selection a forward selection', () => {
  273. const startPos = Position.createAt( root, 1 );
  274. const endPos = Position.createAt( root, 2 );
  275. const newEndPos = Position.createAt( root, 3 );
  276. selection.addRange( new Range( startPos, endPos ), true );
  277. selection.setFocus( newEndPos );
  278. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  279. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  280. expect( selection.isBackward ).to.be.false;
  281. } );
  282. it( 'modifies existing backward selection', () => {
  283. const startPos = Position.createAt( root, 1 );
  284. const endPos = Position.createAt( root, 2 );
  285. const newEndPos = Position.createAt( root, 0 );
  286. selection.addRange( new Range( startPos, endPos ), true );
  287. selection.setFocus( newEndPos );
  288. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  289. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  290. expect( selection.isBackward ).to.be.true;
  291. } );
  292. it( 'modifies only the last range', () => {
  293. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  294. const startPos1 = Position.createAt( root, 4 );
  295. const endPos1 = Position.createAt( root, 5 );
  296. const startPos2 = Position.createAt( root, 1 );
  297. const endPos2 = Position.createAt( root, 2 );
  298. const newEndPos = Position.createAt( root, 0 );
  299. selection.addRange( new Range( startPos1, endPos1 ) );
  300. selection.addRange( new Range( startPos2, endPos2 ) );
  301. const spy = sinon.spy();
  302. selection.on( 'change:range', spy );
  303. selection.setFocus( newEndPos );
  304. const ranges = Array.from( selection.getRanges() );
  305. expect( ranges ).to.have.lengthOf( 2 );
  306. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  307. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  308. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  309. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  310. expect( selection.isBackward ).to.be.true;
  311. expect( spy.calledOnce ).to.be.true;
  312. } );
  313. it( 'collapses the selection when extending to the anchor', () => {
  314. const startPos = Position.createAt( root, 1 );
  315. const endPos = Position.createAt( root, 2 );
  316. selection.addRange( new Range( startPos, endPos ) );
  317. selection.setFocus( startPos );
  318. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  319. expect( selection.isCollapsed ).to.be.true;
  320. } );
  321. it( 'uses Position.createAt', () => {
  322. const startPos = Position.createAt( root, 1 );
  323. const endPos = Position.createAt( root, 2 );
  324. const newEndPos = Position.createAt( root, 4 );
  325. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  326. selection.addRange( new Range( startPos, endPos ) );
  327. selection.setFocus( root, 'end' );
  328. expect( spy.calledOnce ).to.be.true;
  329. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  330. } );
  331. } );
  332. describe( 'removeAllRanges', () => {
  333. let spy, ranges;
  334. beforeEach( () => {
  335. selection.addRange( liveRange );
  336. selection.addRange( range );
  337. spy = sinon.spy();
  338. selection.on( 'change:range', spy );
  339. ranges = selection._ranges;
  340. selection.removeAllRanges();
  341. } );
  342. it( 'should remove all stored ranges', () => {
  343. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  344. } );
  345. it( 'should fire exactly one update event', () => {
  346. expect( spy.calledOnce ).to.be.true;
  347. } );
  348. } );
  349. describe( 'setRanges', () => {
  350. let newRanges, spy, oldRanges;
  351. before( () => {
  352. newRanges = [
  353. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  354. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  355. ];
  356. } );
  357. beforeEach( () => {
  358. selection.addRange( liveRange );
  359. selection.addRange( range );
  360. spy = sinon.spy();
  361. selection.on( 'change:range', spy );
  362. oldRanges = selection._ranges;
  363. } );
  364. it( 'should throw an error when range is invalid', () => {
  365. expect( () => {
  366. selection.setRanges( [ { invalid: 'range' } ] );
  367. } ).to.throw( CKEditorError, 'selection-invalid-range: Invalid Range.' );
  368. } );
  369. it( 'should remove all ranges and add given ranges', () => {
  370. selection.setRanges( newRanges );
  371. let ranges = selection._ranges;
  372. expect( ranges.length ).to.equal( 2 );
  373. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  374. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  375. } );
  376. it( 'should use last range from given array to get anchor and focus position', () => {
  377. selection.setRanges( newRanges );
  378. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  379. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  380. } );
  381. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  382. selection.setRanges( newRanges, true );
  383. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  384. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  385. } );
  386. it( 'should fire exactly one update event', () => {
  387. selection.setRanges( newRanges );
  388. expect( spy.calledOnce ).to.be.true;
  389. } );
  390. } );
  391. describe( 'getFirstRange', () => {
  392. it( 'should return null if no ranges were added', () => {
  393. expect( selection.getFirstRange() ).to.be.null;
  394. } );
  395. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  396. // This will not be the first range despite being added as first
  397. selection.addRange( range2 );
  398. // This should be the first range.
  399. selection.addRange( range1 );
  400. // A random range that is not first.
  401. selection.addRange( range3 );
  402. let range = selection.getFirstRange();
  403. expect( range.start.path ).to.deep.equal( [ 1 ] );
  404. expect( range.end.path ).to.deep.equal( [ 4 ] );
  405. } );
  406. } );
  407. describe( 'getFirstPosition', () => {
  408. it( 'should return null if no ranges were added', () => {
  409. expect( selection.getFirstPosition() ).to.be.null;
  410. } );
  411. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  412. // This will not be the first range despite being added as first
  413. selection.addRange( range2 );
  414. // This should be the first range.
  415. selection.addRange( range1 );
  416. // A random range that is not first.
  417. selection.addRange( range3 );
  418. let position = selection.getFirstPosition();
  419. expect( position.path ).to.deep.equal( [ 1 ] );
  420. } );
  421. } );
  422. describe( 'createFromSelection', () => {
  423. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  424. selection.addRange( liveRange );
  425. selection.addRange( range, true );
  426. const snapshot = Selection.createFromSelection( selection );
  427. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  428. const selectionRanges = Array.from( selection.getRanges() );
  429. const snapshotRanges = Array.from( snapshot.getRanges() );
  430. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  431. for ( let i = 0; i < selectionRanges.length; i++ ) {
  432. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  433. }
  434. } );
  435. } );
  436. describe( 'attributes interface', () => {
  437. let rangeInFullP;
  438. beforeEach( () => {
  439. root.insertChildren( 0, [
  440. new Element( 'p', [], new Text( 'foobar' ) ),
  441. new Element( 'p', [], [] )
  442. ] );
  443. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  444. } );
  445. describe( 'setAttribute', () => {
  446. it( 'should set given attribute on the selection', () => {
  447. selection.setRanges( [ rangeInFullP ] );
  448. selection.setAttribute( 'foo', 'bar' );
  449. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  450. } );
  451. it( 'should fire change:attribute event', () => {
  452. let spy = sinon.spy();
  453. selection.on( 'change:attribute', spy );
  454. selection.setAttribute( 'foo', 'bar' );
  455. expect( spy.called ).to.be.true;
  456. } );
  457. } );
  458. describe( 'getAttribute', () => {
  459. it( 'should return undefined if element does not contain given attribute', () => {
  460. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  461. } );
  462. } );
  463. describe( 'getAttributes', () => {
  464. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  465. selection.setRanges( [ rangeInFullP ] );
  466. selection.setAttribute( 'foo', 'bar' );
  467. selection.setAttribute( 'abc', 'xyz' );
  468. let attrs = Array.from( selection.getAttributes() );
  469. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  470. } );
  471. } );
  472. describe( 'getAttributeKeys', () => {
  473. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  474. selection.setRanges( [ rangeInFullP ] );
  475. selection.setAttribute( 'foo', 'bar' );
  476. selection.setAttribute( 'abc', 'xyz' );
  477. let attrs = Array.from( selection.getAttributeKeys() );
  478. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  479. } );
  480. } );
  481. describe( 'hasAttribute', () => {
  482. it( 'should return true if element contains attribute with given key', () => {
  483. selection.setRanges( [ rangeInFullP ] );
  484. selection.setAttribute( 'foo', 'bar' );
  485. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  486. } );
  487. it( 'should return false if element does not contain attribute with given key', () => {
  488. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  489. } );
  490. } );
  491. describe( 'clearAttributes', () => {
  492. it( 'should remove all attributes from the element', () => {
  493. selection.setRanges( [ rangeInFullP ] );
  494. selection.setAttribute( 'foo', 'bar' );
  495. selection.setAttribute( 'abc', 'xyz' );
  496. selection.clearAttributes();
  497. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  498. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  499. } );
  500. it( 'should fire change:attribute event', () => {
  501. let spy = sinon.spy();
  502. selection.on( 'change:attribute', spy );
  503. selection.clearAttributes();
  504. expect( spy.called ).to.be.true;
  505. } );
  506. } );
  507. describe( 'removeAttribute', () => {
  508. it( 'should remove attribute', () => {
  509. selection.setRanges( [ rangeInFullP ] );
  510. selection.setAttribute( 'foo', 'bar' );
  511. selection.removeAttribute( 'foo' );
  512. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  513. } );
  514. it( 'should fire change:attribute event', () => {
  515. let spy = sinon.spy();
  516. selection.on( 'change:attribute', spy );
  517. selection.removeAttribute( 'foo' );
  518. expect( spy.called ).to.be.true;
  519. } );
  520. } );
  521. describe( 'setAttributesTo', () => {
  522. it( 'should remove all attributes set on element and set the given ones', () => {
  523. selection.setRanges( [ rangeInFullP ] );
  524. selection.setAttribute( 'abc', 'xyz' );
  525. selection.setAttributesTo( { foo: 'bar' } );
  526. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  527. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  528. } );
  529. } );
  530. } );
  531. } );