8
0

selection.js 22 KB

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