documentselection.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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 DocumentSelection from '../../src/model/documentselection';
  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( 'DocumentSelection', () => {
  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 throw', () => {
  282. selection.addRange( range, true );
  283. expect( () => {
  284. DocumentSelection.createFromSelection( selection );
  285. } ).to.throw( CKEditorError, /^documentselection-cannot-create:/ );
  286. } );
  287. } );
  288. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  289. // working well, without getting into complicated corner cases.
  290. describe( 'after applying an operation should get updated and fire events', () => {
  291. let spyRange;
  292. beforeEach( () => {
  293. root.removeChildren( 0, root.childCount );
  294. root.insertChildren( 0, [
  295. new Element( 'p', [], new Text( 'abcdef' ) ),
  296. new Element( 'p', [], new Text( 'foobar' ) ),
  297. new Text( 'xyz' )
  298. ] );
  299. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  300. spyRange = sinon.spy();
  301. selection.on( 'change:range', spyRange );
  302. } );
  303. describe( 'InsertOperation', () => {
  304. it( 'before selection', () => {
  305. selection.on( 'change:range', ( evt, data ) => {
  306. expect( data.directChange ).to.be.false;
  307. } );
  308. doc.applyOperation( wrapInDelta(
  309. new InsertOperation(
  310. new Position( root, [ 0, 1 ] ),
  311. 'xyz',
  312. doc.version
  313. )
  314. ) );
  315. const range = selection.getFirstRange();
  316. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  317. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  318. expect( spyRange.calledOnce ).to.be.true;
  319. } );
  320. it( 'inside selection', () => {
  321. selection.on( 'change:range', ( evt, data ) => {
  322. expect( data.directChange ).to.be.false;
  323. } );
  324. doc.applyOperation( wrapInDelta(
  325. new InsertOperation(
  326. new Position( root, [ 1, 0 ] ),
  327. 'xyz',
  328. doc.version
  329. )
  330. ) );
  331. const range = selection.getFirstRange();
  332. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  333. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  334. expect( spyRange.calledOnce ).to.be.true;
  335. } );
  336. } );
  337. describe( 'MoveOperation', () => {
  338. it( 'move range from before a selection', () => {
  339. selection.on( 'change:range', ( evt, data ) => {
  340. expect( data.directChange ).to.be.false;
  341. } );
  342. doc.applyOperation( wrapInDelta(
  343. new MoveOperation(
  344. new Position( root, [ 0, 0 ] ),
  345. 2,
  346. new Position( root, [ 2 ] ),
  347. doc.version
  348. )
  349. ) );
  350. const range = selection.getFirstRange();
  351. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  352. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  353. expect( spyRange.calledOnce ).to.be.true;
  354. } );
  355. it( 'moved into before a selection', () => {
  356. selection.on( 'change:range', ( evt, data ) => {
  357. expect( data.directChange ).to.be.false;
  358. } );
  359. doc.applyOperation( wrapInDelta(
  360. new MoveOperation(
  361. new Position( root, [ 2 ] ),
  362. 2,
  363. new Position( root, [ 0, 0 ] ),
  364. doc.version
  365. )
  366. ) );
  367. const range = selection.getFirstRange();
  368. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  369. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  370. expect( spyRange.calledOnce ).to.be.true;
  371. } );
  372. it( 'move range from inside of selection', () => {
  373. selection.on( 'change:range', ( evt, data ) => {
  374. expect( data.directChange ).to.be.false;
  375. } );
  376. doc.applyOperation( wrapInDelta(
  377. new MoveOperation(
  378. new Position( root, [ 1, 0 ] ),
  379. 2,
  380. new Position( root, [ 2 ] ),
  381. doc.version
  382. )
  383. ) );
  384. const range = selection.getFirstRange();
  385. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  386. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  387. expect( spyRange.calledOnce ).to.be.true;
  388. } );
  389. it( 'moved range intersects with selection', () => {
  390. selection.on( 'change:range', ( evt, data ) => {
  391. expect( data.directChange ).to.be.false;
  392. } );
  393. doc.applyOperation( wrapInDelta(
  394. new MoveOperation(
  395. new Position( root, [ 1, 3 ] ),
  396. 2,
  397. new Position( root, [ 4 ] ),
  398. doc.version
  399. )
  400. ) );
  401. const range = selection.getFirstRange();
  402. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  403. expect( range.end.path ).to.deep.equal( [ 5 ] );
  404. expect( spyRange.calledOnce ).to.be.true;
  405. } );
  406. it( 'split inside selection (do not break selection)', () => {
  407. selection.on( 'change:range', ( evt, data ) => {
  408. expect( data.directChange ).to.be.false;
  409. } );
  410. const splitDelta = new SplitDelta();
  411. const insertOperation = new InsertOperation(
  412. new Position( root, [ 2 ] ),
  413. new Element( 'p' ),
  414. 0
  415. );
  416. const moveOperation = new MoveOperation(
  417. new Position( root, [ 1, 2 ] ),
  418. 4,
  419. new Position( root, [ 2, 0 ] ),
  420. 1
  421. );
  422. splitDelta.addOperation( insertOperation );
  423. splitDelta.addOperation( moveOperation );
  424. doc.applyOperation( insertOperation );
  425. doc.applyOperation( moveOperation );
  426. const range = selection.getFirstRange();
  427. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  428. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  429. expect( spyRange.calledOnce ).to.be.true;
  430. } );
  431. } );
  432. describe( 'AttributeOperation', () => {
  433. it( 'changed range includes selection anchor', () => {
  434. const spyAttribute = sinon.spy();
  435. selection.on( 'change:attribute', spyAttribute );
  436. selection.on( 'change:attribute', ( evt, data ) => {
  437. expect( data.directChange ).to.be.false;
  438. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  439. } );
  440. doc.applyOperation( wrapInDelta(
  441. new AttributeOperation(
  442. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  443. 'foo',
  444. null,
  445. 'bar',
  446. doc.version
  447. )
  448. ) );
  449. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  450. expect( spyAttribute.calledOnce ).to.be.true;
  451. } );
  452. it( 'should not overwrite previously set attributes', () => {
  453. selection.setAttribute( 'foo', 'xyz' );
  454. const spyAttribute = sinon.spy();
  455. selection.on( 'change:attribute', spyAttribute );
  456. doc.applyOperation( wrapInDelta(
  457. new AttributeOperation(
  458. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  459. 'foo',
  460. null,
  461. 'bar',
  462. doc.version
  463. )
  464. ) );
  465. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  466. expect( spyAttribute.called ).to.be.false;
  467. } );
  468. it( 'should not overwrite previously removed attributes', () => {
  469. selection.setAttribute( 'foo', 'xyz' );
  470. selection.removeAttribute( 'foo' );
  471. const spyAttribute = sinon.spy();
  472. selection.on( 'change:attribute', spyAttribute );
  473. doc.applyOperation( wrapInDelta(
  474. new AttributeOperation(
  475. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  476. 'foo',
  477. null,
  478. 'bar',
  479. doc.version
  480. )
  481. ) );
  482. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  483. expect( spyAttribute.called ).to.be.false;
  484. } );
  485. } );
  486. describe( 'RemoveOperation', () => {
  487. it( 'fix selection range if it ends up in graveyard #1', () => {
  488. selection.collapse( new Position( root, [ 1, 3 ] ) );
  489. doc.applyOperation( wrapInDelta(
  490. new RemoveOperation(
  491. new Position( root, [ 1, 2 ] ),
  492. 2,
  493. new Position( doc.graveyard, [ 0 ] ),
  494. doc.version
  495. )
  496. ) );
  497. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  498. } );
  499. it( 'fix selection range if it ends up in graveyard #2', () => {
  500. selection.setRanges( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  501. doc.applyOperation( wrapInDelta(
  502. new RemoveOperation(
  503. new Position( root, [ 1, 2 ] ),
  504. 2,
  505. new Position( doc.graveyard, [ 0 ] ),
  506. doc.version
  507. )
  508. ) );
  509. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  510. } );
  511. it( 'fix selection range if it ends up in graveyard #3', () => {
  512. selection.setRanges( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  513. doc.applyOperation( wrapInDelta(
  514. new RemoveOperation(
  515. new Position( root, [ 1 ] ),
  516. 2,
  517. new Position( doc.graveyard, [ 0 ] ),
  518. doc.version
  519. )
  520. ) );
  521. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  522. } );
  523. } );
  524. } );
  525. describe( 'attributes', () => {
  526. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  527. beforeEach( () => {
  528. root.insertChildren( 0, [
  529. new Element( 'p', [], new Text( 'foobar' ) ),
  530. new Element( 'p', [], [] )
  531. ] );
  532. fullP = root.getChild( 0 );
  533. emptyP = root.getChild( 1 );
  534. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  535. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  536. } );
  537. describe( 'setAttribute()', () => {
  538. it( 'should store attribute if the selection is in empty node', () => {
  539. selection.setRanges( [ rangeInEmptyP ] );
  540. selection.setAttribute( 'foo', 'bar' );
  541. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  542. expect( emptyP.getAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  543. } );
  544. } );
  545. describe( 'setAttributesTo()', () => {
  546. it( 'should fire change:attribute event with correct parameters', done => {
  547. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  548. selection.on( 'change:attribute', ( evt, data ) => {
  549. expect( data.directChange ).to.be.true;
  550. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  551. done();
  552. } );
  553. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  554. } );
  555. it( 'should not fire change:attribute event if same attributes are set', () => {
  556. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  557. const spy = sinon.spy();
  558. selection.on( 'change:attribute', spy );
  559. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  560. expect( spy.called ).to.be.false;
  561. } );
  562. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  563. selection.setRanges( [ rangeInEmptyP ] );
  564. selection.setAttribute( 'abc', 'xyz' );
  565. selection.setAttributesTo( { foo: 'bar' } );
  566. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  567. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  568. expect( emptyP.getAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  569. expect( emptyP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  570. } );
  571. } );
  572. describe( 'removeAttribute()', () => {
  573. it( 'should remove attribute set on the text fragment', () => {
  574. selection.setRanges( [ rangeInFullP ] );
  575. selection.setAttribute( 'foo', 'bar' );
  576. selection.removeAttribute( 'foo' );
  577. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  578. expect( fullP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  579. } );
  580. it( 'should remove stored attribute if the selection is in empty node', () => {
  581. selection.setRanges( [ rangeInEmptyP ] );
  582. selection.setAttribute( 'foo', 'bar' );
  583. selection.removeAttribute( 'foo' );
  584. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  585. expect( emptyP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  586. } );
  587. } );
  588. describe( 'clearAttributes()', () => {
  589. it( 'should remove all stored attributes if the selection is in empty node', () => {
  590. selection.setRanges( [ rangeInEmptyP ] );
  591. selection.setAttribute( 'foo', 'bar' );
  592. selection.setAttribute( 'abc', 'xyz' );
  593. selection.clearAttributes();
  594. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  595. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  596. expect( emptyP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  597. expect( emptyP.hasAttribute( DocumentSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  598. } );
  599. } );
  600. describe( '_getStoredAttributes()', () => {
  601. it( 'should return no values if there are no ranges in selection', () => {
  602. const values = Array.from( selection._getStoredAttributes() );
  603. expect( values ).to.deep.equal( [] );
  604. } );
  605. } );
  606. describe( 'are updated on a direct range change', () => {
  607. beforeEach( () => {
  608. root.insertChildren( 0, [
  609. new Element( 'p', { p: true } ),
  610. new Text( 'a', { a: true } ),
  611. new Element( 'p', { p: true } ),
  612. new Text( 'b', { b: true } ),
  613. new Text( 'c', { c: true } ),
  614. new Element( 'p', [], [
  615. new Text( 'd', { d: true } )
  616. ] ),
  617. new Element( 'p', { p: true } ),
  618. new Text( 'e', { e: true } )
  619. ] );
  620. } );
  621. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  622. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  623. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  624. // Step into elements when looking for first character:
  625. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  626. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  627. } );
  628. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  629. // Take styles from character before selection.
  630. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  631. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  632. // If there are none,
  633. // Take styles from character after selection.
  634. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  635. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  636. // If there are none,
  637. // Look from the selection position to the beginning of node looking for character to take attributes from.
  638. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  639. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  640. // If there are none,
  641. // Look from the selection position to the end of node looking for character to take attributes from.
  642. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  643. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  644. // If there are no characters to copy attributes from, use stored attributes.
  645. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  646. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  647. } );
  648. it( 'should overwrite any previously set attributes', () => {
  649. selection.collapse( new Position( root, [ 5, 0 ] ) );
  650. selection.setAttribute( 'x', true );
  651. selection.setAttribute( 'y', true );
  652. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  653. selection.collapse( new Position( root, [ 1 ] ) );
  654. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  655. } );
  656. it( 'should fire change:attribute event', () => {
  657. const spy = sinon.spy();
  658. selection.on( 'change:attribute', spy );
  659. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  660. expect( spy.calledOnce ).to.be.true;
  661. } );
  662. it( 'should not fire change:attribute event if attributes did not change', () => {
  663. selection.collapse( new Position( root, [ 5, 0 ] ) );
  664. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  665. const spy = sinon.spy();
  666. selection.on( 'change:attribute', spy );
  667. selection.collapse( new Position( root, [ 5, 1 ] ) );
  668. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  669. expect( spy.called ).to.be.false;
  670. } );
  671. } );
  672. // #986
  673. describe( 'are not inherited from the inside of object elements', () => {
  674. beforeEach( () => {
  675. doc.schema.registerItem( 'image' );
  676. doc.schema.allow( { name: 'image', inside: '$root' } );
  677. doc.schema.allow( { name: 'image', inside: '$block' } );
  678. doc.schema.allow( { name: '$inline', inside: 'image' } );
  679. doc.schema.objects.add( 'image' );
  680. doc.schema.registerItem( 'caption' );
  681. doc.schema.allow( { name: '$inline', inside: 'caption' } );
  682. doc.schema.allow( { name: 'caption', inside: 'image' } );
  683. doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } );
  684. } );
  685. it( 'ignores attributes inside an object if selection contains that object', () => {
  686. setData( doc, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  687. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  688. } );
  689. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  690. setData( doc, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  691. expect( selection.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. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  696. } );
  697. it( 'reads attributes from text even if the selection contains an object', () => {
  698. setData( doc, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  699. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  700. } );
  701. it( 'reads attributes when the entire selection inside an object', () => {
  702. setData( doc, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  703. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  704. } );
  705. it( 'stops reading attributes if selection starts with an object', () => {
  706. setData( doc, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  707. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  708. } );
  709. } );
  710. } );
  711. } );