selection.js 25 KB

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