liveselection.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Range from '../../src/model/range';
  9. import Position from '../../src/model/position';
  10. import LiveRange from '../../src/model/liverange';
  11. import LiveSelection from '../../src/model/liveselection';
  12. import InsertOperation from '../../src/model/operation/insertoperation';
  13. import MoveOperation from '../../src/model/operation/moveoperation';
  14. import RemoveOperation from '../../src/model/operation/removeoperation';
  15. import AttributeOperation from '../../src/model/operation/attributeoperation';
  16. import SplitDelta from '../../src/model/delta/splitdelta';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. import count from '@ckeditor/ckeditor5-utils/src/count';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. import { wrapInDelta } from '../../tests/model/_utils/utils';
  21. import { setData, getData } from '../../src/dev-utils/model';
  22. import log from '@ckeditor/ckeditor5-utils/src/log';
  23. testUtils.createSinonSandbox();
  24. describe( 'LiveSelection', () => {
  25. let doc, root, selection, liveRange, range;
  26. beforeEach( () => {
  27. doc = new Document();
  28. root = doc.createRoot();
  29. root.appendChildren( [
  30. new Element( 'p' ),
  31. new Element( 'p' ),
  32. new Element( 'p', [], new Text( 'foobar' ) ),
  33. new Element( 'p' ),
  34. new Element( 'p' ),
  35. new Element( 'p' ),
  36. new Element( 'p', [], new Text( 'foobar' ) )
  37. ] );
  38. selection = doc.selection;
  39. doc.schema.registerItem( 'p', '$block' );
  40. doc.schema.registerItem( 'widget' );
  41. doc.schema.objects.add( 'widget' );
  42. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  43. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  44. } );
  45. afterEach( () => {
  46. doc.destroy();
  47. liveRange.detach();
  48. } );
  49. describe( 'default range', () => {
  50. it( 'should go to the first editable element', () => {
  51. const ranges = Array.from( selection.getRanges() );
  52. expect( ranges.length ).to.equal( 1 );
  53. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  54. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  55. expect( selection ).to.have.property( 'isBackward', false );
  56. } );
  57. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  58. doc = new Document();
  59. root = doc.createRoot();
  60. root.insertChildren( 0, new Text( 'foobar' ) );
  61. selection = doc.selection;
  62. const ranges = Array.from( selection.getRanges() );
  63. expect( ranges.length ).to.equal( 1 );
  64. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  65. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  66. expect( selection ).to.have.property( 'isBackward', false );
  67. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  68. } );
  69. it( 'should skip element when you can not put selection', () => {
  70. doc = new Document();
  71. root = doc.createRoot();
  72. root.insertChildren( 0, [
  73. new Element( 'img' ),
  74. new Element( 'p', [], new Text( 'foobar' ) )
  75. ] );
  76. doc.schema.registerItem( 'img' );
  77. doc.schema.registerItem( 'p', '$block' );
  78. selection = doc.selection;
  79. const ranges = Array.from( selection.getRanges() );
  80. expect( ranges.length ).to.equal( 1 );
  81. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  82. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  83. expect( selection ).to.have.property( 'isBackward', false );
  84. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  85. } );
  86. } );
  87. describe( 'isCollapsed', () => {
  88. it( 'should be true for the default range (in text position)', () => {
  89. expect( selection.isCollapsed ).to.be.true;
  90. } );
  91. it( 'should be false for the default range (object selection) ', () => {
  92. root.insertChildren( 0, new Element( 'widget' ) );
  93. expect( selection.isCollapsed ).to.be.false;
  94. } );
  95. it( 'should back off to the default algorithm if selection has ranges', () => {
  96. selection.addRange( range );
  97. expect( selection.isCollapsed ).to.be.false;
  98. } );
  99. } );
  100. describe( 'anchor', () => {
  101. it( 'should equal the default range\'s start (in text position)', () => {
  102. const expectedPos = new Position( root, [ 0, 0 ] );
  103. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  104. } );
  105. it( 'should equal the default range\'s start (object selection)', () => {
  106. root.insertChildren( 0, new Element( 'widget' ) );
  107. const expectedPos = new Position( root, [ 0 ] );
  108. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  109. } );
  110. it( 'should back off to the default algorithm if selection has ranges', () => {
  111. selection.addRange( range );
  112. expect( selection.anchor.isEqual( range.start ) ).to.be.true;
  113. } );
  114. } );
  115. describe( 'focus', () => {
  116. it( 'should equal the default range\'s end (in text position)', () => {
  117. const expectedPos = new Position( root, [ 0, 0 ] );
  118. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  119. } );
  120. it( 'should equal the default range\'s end (object selection)', () => {
  121. root.insertChildren( 0, new Element( 'widget' ) );
  122. const expectedPos = new Position( root, [ 1 ] );
  123. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  124. expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
  125. } );
  126. it( 'should back off to the default algorithm if selection has ranges', () => {
  127. selection.addRange( range );
  128. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  129. } );
  130. } );
  131. describe( 'rangeCount', () => {
  132. it( 'should return proper range count', () => {
  133. expect( selection.rangeCount ).to.equal( 1 );
  134. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  135. expect( selection.rangeCount ).to.equal( 1 );
  136. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  137. expect( selection.rangeCount ).to.equal( 2 );
  138. } );
  139. } );
  140. describe( 'addRange()', () => {
  141. it( 'should convert added Range to LiveRange', () => {
  142. selection.addRange( range );
  143. expect( selection._ranges[ 0 ] ).to.be.instanceof( LiveRange );
  144. } );
  145. it( 'should throw an error when range is invalid', () => {
  146. expect( () => {
  147. selection.addRange( { invalid: 'range' } );
  148. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  149. } );
  150. it( 'should not add a range that is in graveyard', () => {
  151. const spy = testUtils.sinon.stub( log, 'warn' );
  152. selection.addRange( Range.createIn( doc.graveyard ) );
  153. expect( selection._ranges.length ).to.equal( 0 );
  154. expect( spy.calledOnce ).to.be.true;
  155. } );
  156. it( 'should refresh attributes', () => {
  157. const spy = testUtils.sinon.spy( selection, '_updateAttributes' );
  158. selection.addRange( range );
  159. expect( spy.called ).to.be.true;
  160. } );
  161. } );
  162. describe( 'collapse()', () => {
  163. it( 'detaches all existing ranges', () => {
  164. selection.addRange( range );
  165. selection.addRange( liveRange );
  166. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  167. selection.collapse( root );
  168. expect( spy.calledTwice ).to.be.true;
  169. } );
  170. } );
  171. describe( 'destroy()', () => {
  172. it( 'should unbind all events', () => {
  173. selection.addRange( liveRange );
  174. selection.addRange( range );
  175. const ranges = selection._ranges;
  176. sinon.spy( ranges[ 0 ], 'detach' );
  177. sinon.spy( ranges[ 1 ], 'detach' );
  178. selection.destroy();
  179. expect( ranges[ 0 ].detach.called ).to.be.true;
  180. expect( ranges[ 1 ].detach.called ).to.be.true;
  181. ranges[ 0 ].detach.restore();
  182. ranges[ 1 ].detach.restore();
  183. } );
  184. } );
  185. describe( 'setFocus()', () => {
  186. it( 'modifies default range', () => {
  187. const startPos = selection.getFirstPosition();
  188. const endPos = Position.createAt( root, 'end' );
  189. selection.setFocus( endPos );
  190. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  191. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  192. } );
  193. it( 'detaches the range it replaces', () => {
  194. const startPos = Position.createAt( root, 1 );
  195. const endPos = Position.createAt( root, 2 );
  196. const newEndPos = Position.createAt( root, 4 );
  197. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  198. selection.addRange( new Range( startPos, endPos ) );
  199. selection.setFocus( newEndPos );
  200. expect( spy.calledOnce ).to.be.true;
  201. } );
  202. } );
  203. describe( 'removeAllRanges()', () => {
  204. let spy, ranges;
  205. beforeEach( () => {
  206. selection.addRange( liveRange );
  207. selection.addRange( range );
  208. spy = sinon.spy();
  209. selection.on( 'change:range', spy );
  210. ranges = Array.from( selection._ranges );
  211. sinon.spy( ranges[ 0 ], 'detach' );
  212. sinon.spy( ranges[ 1 ], 'detach' );
  213. selection.removeAllRanges();
  214. } );
  215. afterEach( () => {
  216. ranges[ 0 ].detach.restore();
  217. ranges[ 1 ].detach.restore();
  218. } );
  219. it( 'should remove all stored ranges (and reset to default range)', () => {
  220. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  221. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  222. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  223. } );
  224. it( 'should detach ranges', () => {
  225. expect( ranges[ 0 ].detach.called ).to.be.true;
  226. expect( ranges[ 1 ].detach.called ).to.be.true;
  227. } );
  228. it( 'should refresh attributes', () => {
  229. const spy = sinon.spy( selection, '_updateAttributes' );
  230. selection.removeAllRanges();
  231. expect( spy.called ).to.be.true;
  232. } );
  233. } );
  234. describe( 'setRanges()', () => {
  235. it( 'should throw an error when range is invalid', () => {
  236. expect( () => {
  237. selection.setRanges( [ { invalid: 'range' } ] );
  238. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  239. } );
  240. it( 'should detach removed ranges', () => {
  241. selection.addRange( liveRange );
  242. selection.addRange( range );
  243. const oldRanges = Array.from( selection._ranges );
  244. sinon.spy( oldRanges[ 0 ], 'detach' );
  245. sinon.spy( oldRanges[ 1 ], 'detach' );
  246. selection.setRanges( [] );
  247. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  248. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  249. } );
  250. it( 'should refresh attributes', () => {
  251. const spy = sinon.spy( selection, '_updateAttributes' );
  252. selection.setRanges( [ range ] );
  253. expect( spy.called ).to.be.true;
  254. } );
  255. // See #630.
  256. it( 'should refresh attributes – integration test for #630', () => {
  257. doc.schema.allow( { name: '$text', inside: '$root' } );
  258. setData( doc, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  259. selection.setRanges( [ Range.createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  260. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  261. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  262. expect( getData( doc ) )
  263. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  264. } );
  265. } );
  266. describe( 'getFirstRange()', () => {
  267. it( 'should return default range if no ranges were added', () => {
  268. const firstRange = selection.getFirstRange();
  269. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  270. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  271. } );
  272. } );
  273. describe( 'getLastRange()', () => {
  274. it( 'should return default range if no ranges were added', () => {
  275. const lastRange = selection.getLastRange();
  276. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  277. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  278. } );
  279. } );
  280. describe( 'createFromSelection()', () => {
  281. it( 'should return a LiveSelection instance', () => {
  282. selection.addRange( range, true );
  283. expect( LiveSelection.createFromSelection( selection ) ).to.be.instanceof( LiveSelection );
  284. } );
  285. } );
  286. // LiveSelection uses LiveRanges so here are only simple test to see if integration is
  287. // working well, without getting into complicated corner cases.
  288. describe( 'after applying an operation should get updated and fire events', () => {
  289. let spyRange;
  290. beforeEach( () => {
  291. root.removeChildren( 0, root.childCount );
  292. root.insertChildren( 0, [
  293. new Element( 'p', [], new Text( 'abcdef' ) ),
  294. new Element( 'p', [], new Text( 'foobar' ) ),
  295. new Text( 'xyz' )
  296. ] );
  297. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  298. spyRange = sinon.spy();
  299. selection.on( 'change:range', spyRange );
  300. } );
  301. describe( 'InsertOperation', () => {
  302. it( 'before selection', () => {
  303. selection.on( 'change:range', ( evt, data ) => {
  304. expect( data.directChange ).to.be.false;
  305. } );
  306. doc.applyOperation( wrapInDelta(
  307. new InsertOperation(
  308. new Position( root, [ 0, 1 ] ),
  309. 'xyz',
  310. doc.version
  311. )
  312. ) );
  313. const range = selection.getFirstRange();
  314. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  315. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  316. expect( spyRange.calledOnce ).to.be.true;
  317. } );
  318. it( 'inside selection', () => {
  319. selection.on( 'change:range', ( evt, data ) => {
  320. expect( data.directChange ).to.be.false;
  321. } );
  322. doc.applyOperation( wrapInDelta(
  323. new InsertOperation(
  324. new Position( root, [ 1, 0 ] ),
  325. 'xyz',
  326. doc.version
  327. )
  328. ) );
  329. const range = selection.getFirstRange();
  330. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  331. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  332. expect( spyRange.calledOnce ).to.be.true;
  333. } );
  334. } );
  335. describe( 'MoveOperation', () => {
  336. it( 'move range from before a selection', () => {
  337. selection.on( 'change:range', ( evt, data ) => {
  338. expect( data.directChange ).to.be.false;
  339. } );
  340. doc.applyOperation( wrapInDelta(
  341. new MoveOperation(
  342. new Position( root, [ 0, 0 ] ),
  343. 2,
  344. new Position( root, [ 2 ] ),
  345. doc.version
  346. )
  347. ) );
  348. const range = selection.getFirstRange();
  349. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  350. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  351. expect( spyRange.calledOnce ).to.be.true;
  352. } );
  353. it( 'moved into before a selection', () => {
  354. selection.on( 'change:range', ( evt, data ) => {
  355. expect( data.directChange ).to.be.false;
  356. } );
  357. doc.applyOperation( wrapInDelta(
  358. new MoveOperation(
  359. new Position( root, [ 2 ] ),
  360. 2,
  361. new Position( root, [ 0, 0 ] ),
  362. doc.version
  363. )
  364. ) );
  365. const range = selection.getFirstRange();
  366. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  367. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  368. expect( spyRange.calledOnce ).to.be.true;
  369. } );
  370. it( 'move range from inside of selection', () => {
  371. selection.on( 'change:range', ( evt, data ) => {
  372. expect( data.directChange ).to.be.false;
  373. } );
  374. doc.applyOperation( wrapInDelta(
  375. new MoveOperation(
  376. new Position( root, [ 1, 0 ] ),
  377. 2,
  378. new Position( root, [ 2 ] ),
  379. doc.version
  380. )
  381. ) );
  382. const range = selection.getFirstRange();
  383. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  384. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  385. expect( spyRange.calledOnce ).to.be.true;
  386. } );
  387. it( 'moved range intersects with selection', () => {
  388. selection.on( 'change:range', ( evt, data ) => {
  389. expect( data.directChange ).to.be.false;
  390. } );
  391. doc.applyOperation( wrapInDelta(
  392. new MoveOperation(
  393. new Position( root, [ 1, 3 ] ),
  394. 2,
  395. new Position( root, [ 4 ] ),
  396. doc.version
  397. )
  398. ) );
  399. const range = selection.getFirstRange();
  400. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  401. expect( range.end.path ).to.deep.equal( [ 5 ] );
  402. expect( spyRange.calledOnce ).to.be.true;
  403. } );
  404. it( 'split inside selection (do not break selection)', () => {
  405. selection.on( 'change:range', ( evt, data ) => {
  406. expect( data.directChange ).to.be.false;
  407. } );
  408. const splitDelta = new SplitDelta();
  409. const insertOperation = new InsertOperation(
  410. new Position( root, [ 2 ] ),
  411. new Element( 'p' ),
  412. 0
  413. );
  414. const moveOperation = new MoveOperation(
  415. new Position( root, [ 1, 2 ] ),
  416. 4,
  417. new Position( root, [ 2, 0 ] ),
  418. 1
  419. );
  420. splitDelta.addOperation( insertOperation );
  421. splitDelta.addOperation( moveOperation );
  422. doc.applyOperation( insertOperation );
  423. doc.applyOperation( moveOperation );
  424. const range = selection.getFirstRange();
  425. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  426. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  427. expect( spyRange.calledOnce ).to.be.true;
  428. } );
  429. } );
  430. describe( 'AttributeOperation', () => {
  431. it( 'changed range includes selection anchor', () => {
  432. const spyAttribute = sinon.spy();
  433. selection.on( 'change:attribute', spyAttribute );
  434. selection.on( 'change:attribute', ( evt, data ) => {
  435. expect( data.directChange ).to.be.false;
  436. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  437. } );
  438. doc.applyOperation( wrapInDelta(
  439. new AttributeOperation(
  440. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  441. 'foo',
  442. null,
  443. 'bar',
  444. doc.version
  445. )
  446. ) );
  447. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  448. expect( spyAttribute.calledOnce ).to.be.true;
  449. } );
  450. it( 'should not overwrite previously set attributes', () => {
  451. selection.setAttribute( 'foo', 'xyz' );
  452. const spyAttribute = sinon.spy();
  453. selection.on( 'change:attribute', spyAttribute );
  454. doc.applyOperation( wrapInDelta(
  455. new AttributeOperation(
  456. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  457. 'foo',
  458. null,
  459. 'bar',
  460. doc.version
  461. )
  462. ) );
  463. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  464. expect( spyAttribute.called ).to.be.false;
  465. } );
  466. it( 'should not overwrite previously removed attributes', () => {
  467. selection.setAttribute( 'foo', 'xyz' );
  468. selection.removeAttribute( 'foo' );
  469. const spyAttribute = sinon.spy();
  470. selection.on( 'change:attribute', spyAttribute );
  471. doc.applyOperation( wrapInDelta(
  472. new AttributeOperation(
  473. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  474. 'foo',
  475. null,
  476. 'bar',
  477. doc.version
  478. )
  479. ) );
  480. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  481. expect( spyAttribute.called ).to.be.false;
  482. } );
  483. } );
  484. describe( 'RemoveOperation', () => {
  485. it( 'fix selection range if it ends up in graveyard #1', () => {
  486. selection.collapse( new Position( root, [ 1, 3 ] ) );
  487. doc.applyOperation( wrapInDelta(
  488. new RemoveOperation(
  489. new Position( root, [ 1, 2 ] ),
  490. 2,
  491. new Position( doc.graveyard, [ 0 ] ),
  492. doc.version
  493. )
  494. ) );
  495. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  496. } );
  497. it( 'fix selection range if it ends up in graveyard #2', () => {
  498. selection.setRanges( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  499. doc.applyOperation( wrapInDelta(
  500. new RemoveOperation(
  501. new Position( root, [ 1, 2 ] ),
  502. 2,
  503. new Position( doc.graveyard, [ 0 ] ),
  504. doc.version
  505. )
  506. ) );
  507. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  508. } );
  509. it( 'fix selection range if it ends up in graveyard #3', () => {
  510. selection.setRanges( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  511. doc.applyOperation( wrapInDelta(
  512. new RemoveOperation(
  513. new Position( root, [ 1 ] ),
  514. 2,
  515. new Position( doc.graveyard, [ 0 ] ),
  516. doc.version
  517. )
  518. ) );
  519. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  520. } );
  521. } );
  522. } );
  523. describe( 'attributes', () => {
  524. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  525. beforeEach( () => {
  526. root.insertChildren( 0, [
  527. new Element( 'p', [], new Text( 'foobar' ) ),
  528. new Element( 'p', [], [] )
  529. ] );
  530. fullP = root.getChild( 0 );
  531. emptyP = root.getChild( 1 );
  532. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  533. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  534. } );
  535. describe( 'setAttribute()', () => {
  536. it( 'should store attribute if the selection is in empty node', () => {
  537. selection.setRanges( [ rangeInEmptyP ] );
  538. selection.setAttribute( 'foo', 'bar' );
  539. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  540. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  541. } );
  542. } );
  543. describe( 'setAttributesTo()', () => {
  544. it( 'should fire change:attribute event with correct parameters', done => {
  545. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  546. selection.on( 'change:attribute', ( evt, data ) => {
  547. expect( data.directChange ).to.be.true;
  548. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  549. done();
  550. } );
  551. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  552. } );
  553. it( 'should not fire change:attribute event if same attributes are set', () => {
  554. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  555. const spy = sinon.spy();
  556. selection.on( 'change:attribute', spy );
  557. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  558. expect( spy.called ).to.be.false;
  559. } );
  560. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  561. selection.setRanges( [ rangeInEmptyP ] );
  562. selection.setAttribute( 'abc', 'xyz' );
  563. selection.setAttributesTo( { foo: 'bar' } );
  564. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  565. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  566. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  567. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  568. } );
  569. } );
  570. describe( 'removeAttribute()', () => {
  571. it( 'should remove attribute set on the text fragment', () => {
  572. selection.setRanges( [ rangeInFullP ] );
  573. selection.setAttribute( 'foo', 'bar' );
  574. selection.removeAttribute( 'foo' );
  575. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  576. expect( fullP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  577. } );
  578. it( 'should remove stored attribute if the selection is in empty node', () => {
  579. selection.setRanges( [ rangeInEmptyP ] );
  580. selection.setAttribute( 'foo', 'bar' );
  581. selection.removeAttribute( 'foo' );
  582. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  583. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  584. } );
  585. } );
  586. describe( 'clearAttributes()', () => {
  587. it( 'should remove all stored attributes if the selection is in empty node', () => {
  588. selection.setRanges( [ rangeInEmptyP ] );
  589. selection.setAttribute( 'foo', 'bar' );
  590. selection.setAttribute( 'abc', 'xyz' );
  591. selection.clearAttributes();
  592. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  593. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  594. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  595. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  596. } );
  597. } );
  598. describe( '_getStoredAttributes()', () => {
  599. it( 'should return no values if there are no ranges in selection', () => {
  600. const values = Array.from( selection._getStoredAttributes() );
  601. expect( values ).to.deep.equal( [] );
  602. } );
  603. } );
  604. describe( 'are updated on a direct range change', () => {
  605. beforeEach( () => {
  606. root.insertChildren( 0, [
  607. new Element( 'p', { p: true } ),
  608. new Text( 'a', { a: true } ),
  609. new Element( 'p', { p: true } ),
  610. new Text( 'b', { b: true } ),
  611. new Text( 'c', { c: true } ),
  612. new Element( 'p', [], [
  613. new Text( 'd', { d: true } )
  614. ] ),
  615. new Element( 'p', { p: true } ),
  616. new Text( 'e', { e: true } )
  617. ] );
  618. } );
  619. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  620. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  621. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  622. // Step into elements when looking for first character:
  623. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  624. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  625. } );
  626. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  627. // Take styles from character before selection.
  628. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  629. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  630. // If there are none,
  631. // Take styles from character after selection.
  632. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  633. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  634. // If there are none,
  635. // Look from the selection position to the beginning of node looking for character to take attributes from.
  636. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  637. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  638. // If there are none,
  639. // Look from the selection position to the end of node looking for character to take attributes from.
  640. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  641. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  642. // If there are no characters to copy attributes from, use stored attributes.
  643. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  644. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  645. } );
  646. it( 'should overwrite any previously set attributes', () => {
  647. selection.collapse( new Position( root, [ 5, 0 ] ) );
  648. selection.setAttribute( 'x', true );
  649. selection.setAttribute( 'y', true );
  650. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  651. selection.collapse( new Position( root, [ 1 ] ) );
  652. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  653. } );
  654. it( 'should fire change:attribute event', () => {
  655. const spy = sinon.spy();
  656. selection.on( 'change:attribute', spy );
  657. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  658. expect( spy.calledOnce ).to.be.true;
  659. } );
  660. it( 'should not fire change:attribute event if attributes did not change', () => {
  661. selection.collapse( new Position( root, [ 5, 0 ] ) );
  662. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  663. const spy = sinon.spy();
  664. selection.on( 'change:attribute', spy );
  665. selection.collapse( new Position( root, [ 5, 1 ] ) );
  666. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  667. expect( spy.called ).to.be.false;
  668. } );
  669. } );
  670. // #986
  671. describe( 'are not inherited from the inside of object elements', () => {
  672. beforeEach( () => {
  673. doc.schema.registerItem( 'image' );
  674. doc.schema.allow( { name: 'image', inside: '$root' } );
  675. doc.schema.allow( { name: 'image', inside: '$block' } );
  676. doc.schema.allow( { name: '$inline', inside: 'image' } );
  677. doc.schema.objects.add( 'image' );
  678. doc.schema.registerItem( 'caption' );
  679. doc.schema.allow( { name: '$inline', inside: 'caption' } );
  680. doc.schema.allow( { name: 'caption', inside: 'image' } );
  681. doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } );
  682. } );
  683. it( 'ignores attributes inside an object if selection contains that object', () => {
  684. setData( doc, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  685. const liveSelection = LiveSelection.createFromSelection( selection );
  686. expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
  687. } );
  688. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  689. setData( doc, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  690. const liveSelection = LiveSelection.createFromSelection( selection );
  691. expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
  692. } );
  693. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  694. setData( doc, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  695. const liveSelection = LiveSelection.createFromSelection( selection );
  696. expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
  697. } );
  698. it( 'reads attributes from text even if the selection contains an object', () => {
  699. setData( doc, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  700. const liveSelection = LiveSelection.createFromSelection( selection );
  701. expect( liveSelection.getAttribute( 'bold' ) ).to.equal( true );
  702. } );
  703. it( 'reads attributes when the entire selection inside an object', () => {
  704. setData( doc, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  705. const liveSelection = LiveSelection.createFromSelection( selection );
  706. expect( liveSelection.getAttribute( 'bold' ) ).to.equal( true );
  707. } );
  708. it( 'stops reading attributes if selection starts with an object', () => {
  709. setData( doc, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  710. const liveSelection = LiveSelection.createFromSelection( selection );
  711. expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
  712. } );
  713. } );
  714. } );
  715. } );