selection.js 22 KB

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