selection.js 22 KB

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