8
0

documentselection.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  27. const abcStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'abc' );
  28. beforeEach( () => {
  29. doc = new Document();
  30. root = doc.createRoot();
  31. root.appendChildren( [
  32. new Element( 'p' ),
  33. new Element( 'p' ),
  34. new Element( 'p', [], new Text( 'foobar' ) ),
  35. new Element( 'p' ),
  36. new Element( 'p' ),
  37. new Element( 'p' ),
  38. new Element( 'p', [], new Text( 'foobar' ) )
  39. ] );
  40. selection = doc.selection;
  41. doc.schema.registerItem( 'p', '$block' );
  42. doc.schema.registerItem( 'widget' );
  43. doc.schema.objects.add( 'widget' );
  44. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  45. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  46. } );
  47. afterEach( () => {
  48. doc.destroy();
  49. liveRange.detach();
  50. } );
  51. describe( 'default range', () => {
  52. it( 'should go to the first editable element', () => {
  53. const ranges = Array.from( selection.getRanges() );
  54. expect( ranges.length ).to.equal( 1 );
  55. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  56. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  57. expect( selection ).to.have.property( 'isBackward', false );
  58. } );
  59. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  60. doc = new Document();
  61. root = doc.createRoot();
  62. root.insertChildren( 0, new Text( 'foobar' ) );
  63. selection = doc.selection;
  64. const ranges = Array.from( selection.getRanges() );
  65. expect( ranges.length ).to.equal( 1 );
  66. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  67. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  68. expect( selection ).to.have.property( 'isBackward', false );
  69. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  70. } );
  71. it( 'should skip element when you can not put selection', () => {
  72. doc = new Document();
  73. root = doc.createRoot();
  74. root.insertChildren( 0, [
  75. new Element( 'img' ),
  76. new Element( 'p', [], new Text( 'foobar' ) )
  77. ] );
  78. doc.schema.registerItem( 'img' );
  79. doc.schema.registerItem( 'p', '$block' );
  80. selection = doc.selection;
  81. const ranges = Array.from( selection.getRanges() );
  82. expect( ranges.length ).to.equal( 1 );
  83. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  84. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  85. expect( selection ).to.have.property( 'isBackward', false );
  86. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  87. } );
  88. } );
  89. describe( 'isCollapsed', () => {
  90. it( 'should be true for the default range (in text position)', () => {
  91. expect( selection.isCollapsed ).to.be.true;
  92. } );
  93. it( 'should be false for the default range (object selection) ', () => {
  94. root.insertChildren( 0, new Element( 'widget' ) );
  95. expect( selection.isCollapsed ).to.be.false;
  96. } );
  97. it( 'should back off to the default algorithm if selection has ranges', () => {
  98. selection.addRange( range );
  99. expect( selection.isCollapsed ).to.be.false;
  100. } );
  101. } );
  102. describe( 'anchor', () => {
  103. it( 'should equal the default range\'s start (in text position)', () => {
  104. const expectedPos = new Position( root, [ 0, 0 ] );
  105. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  106. } );
  107. it( 'should equal the default range\'s start (object selection)', () => {
  108. root.insertChildren( 0, new Element( 'widget' ) );
  109. const expectedPos = new Position( root, [ 0 ] );
  110. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  111. } );
  112. it( 'should back off to the default algorithm if selection has ranges', () => {
  113. selection.addRange( range );
  114. expect( selection.anchor.isEqual( range.start ) ).to.be.true;
  115. } );
  116. } );
  117. describe( 'focus', () => {
  118. it( 'should equal the default range\'s end (in text position)', () => {
  119. const expectedPos = new Position( root, [ 0, 0 ] );
  120. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  121. } );
  122. it( 'should equal the default range\'s end (object selection)', () => {
  123. root.insertChildren( 0, new Element( 'widget' ) );
  124. const expectedPos = new Position( root, [ 1 ] );
  125. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  126. expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
  127. } );
  128. it( 'should back off to the default algorithm if selection has ranges', () => {
  129. selection.addRange( range );
  130. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  131. } );
  132. } );
  133. describe( 'rangeCount', () => {
  134. it( 'should return proper range count', () => {
  135. expect( selection.rangeCount ).to.equal( 1 );
  136. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  137. expect( selection.rangeCount ).to.equal( 1 );
  138. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  139. expect( selection.rangeCount ).to.equal( 2 );
  140. } );
  141. } );
  142. describe( 'addRange()', () => {
  143. it( 'should convert added Range to LiveRange', () => {
  144. selection.addRange( range );
  145. expect( selection._ranges[ 0 ] ).to.be.instanceof( LiveRange );
  146. } );
  147. it( 'should throw an error when range is invalid', () => {
  148. expect( () => {
  149. selection.addRange( { invalid: 'range' } );
  150. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  151. } );
  152. it( 'should not add a range that is in graveyard', () => {
  153. const spy = testUtils.sinon.stub( log, 'warn' );
  154. selection.addRange( Range.createIn( doc.graveyard ) );
  155. expect( selection._ranges.length ).to.equal( 0 );
  156. expect( spy.calledOnce ).to.be.true;
  157. } );
  158. it( 'should refresh attributes', () => {
  159. const spy = testUtils.sinon.spy( selection, '_updateAttributes' );
  160. selection.addRange( range );
  161. expect( spy.called ).to.be.true;
  162. } );
  163. } );
  164. describe( 'collapse()', () => {
  165. it( 'detaches all existing ranges', () => {
  166. selection.addRange( range );
  167. selection.addRange( liveRange );
  168. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  169. selection.collapse( root );
  170. expect( spy.calledTwice ).to.be.true;
  171. } );
  172. } );
  173. describe( 'destroy()', () => {
  174. it( 'should unbind all events', () => {
  175. selection.addRange( liveRange );
  176. selection.addRange( range );
  177. const ranges = selection._ranges;
  178. sinon.spy( ranges[ 0 ], 'detach' );
  179. sinon.spy( ranges[ 1 ], 'detach' );
  180. selection.destroy();
  181. expect( ranges[ 0 ].detach.called ).to.be.true;
  182. expect( ranges[ 1 ].detach.called ).to.be.true;
  183. ranges[ 0 ].detach.restore();
  184. ranges[ 1 ].detach.restore();
  185. } );
  186. } );
  187. describe( 'setFocus()', () => {
  188. it( 'modifies default range', () => {
  189. const startPos = selection.getFirstPosition();
  190. const endPos = Position.createAt( root, 'end' );
  191. selection.setFocus( endPos );
  192. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  193. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  194. } );
  195. it( 'detaches the range it replaces', () => {
  196. const startPos = Position.createAt( root, 1 );
  197. const endPos = Position.createAt( root, 2 );
  198. const newEndPos = Position.createAt( root, 4 );
  199. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  200. selection.addRange( new Range( startPos, endPos ) );
  201. selection.setFocus( newEndPos );
  202. expect( spy.calledOnce ).to.be.true;
  203. } );
  204. } );
  205. describe( 'removeAllRanges()', () => {
  206. let spy, ranges;
  207. beforeEach( () => {
  208. selection.addRange( liveRange );
  209. selection.addRange( range );
  210. spy = sinon.spy();
  211. selection.on( 'change:range', spy );
  212. ranges = Array.from( selection._ranges );
  213. sinon.spy( ranges[ 0 ], 'detach' );
  214. sinon.spy( ranges[ 1 ], 'detach' );
  215. selection.removeAllRanges();
  216. } );
  217. afterEach( () => {
  218. ranges[ 0 ].detach.restore();
  219. ranges[ 1 ].detach.restore();
  220. } );
  221. it( 'should remove all stored ranges (and reset to default range)', () => {
  222. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  223. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  224. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  225. } );
  226. it( 'should detach ranges', () => {
  227. expect( ranges[ 0 ].detach.called ).to.be.true;
  228. expect( ranges[ 1 ].detach.called ).to.be.true;
  229. } );
  230. it( 'should refresh attributes', () => {
  231. const spy = sinon.spy( selection, '_updateAttributes' );
  232. selection.removeAllRanges();
  233. expect( spy.called ).to.be.true;
  234. } );
  235. } );
  236. describe( 'setRanges()', () => {
  237. it( 'should throw an error when range is invalid', () => {
  238. expect( () => {
  239. selection.setRanges( [ { invalid: 'range' } ] );
  240. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  241. } );
  242. it( 'should detach removed ranges', () => {
  243. selection.addRange( liveRange );
  244. selection.addRange( range );
  245. const oldRanges = Array.from( selection._ranges );
  246. sinon.spy( oldRanges[ 0 ], 'detach' );
  247. sinon.spy( oldRanges[ 1 ], 'detach' );
  248. selection.setRanges( [] );
  249. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  250. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  251. } );
  252. it( 'should refresh attributes', () => {
  253. const spy = sinon.spy( selection, '_updateAttributes' );
  254. selection.setRanges( [ range ] );
  255. expect( spy.called ).to.be.true;
  256. } );
  257. // See #630.
  258. it( 'should refresh attributes – integration test for #630', () => {
  259. doc.schema.allow( { name: '$text', inside: '$root' } );
  260. setData( doc, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  261. selection.setRanges( [ Range.createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  262. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  263. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  264. expect( getData( doc ) )
  265. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  266. } );
  267. } );
  268. describe( 'getFirstRange()', () => {
  269. it( 'should return default range if no ranges were added', () => {
  270. const firstRange = selection.getFirstRange();
  271. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  272. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  273. } );
  274. } );
  275. describe( 'getLastRange()', () => {
  276. it( 'should return default range if no ranges were added', () => {
  277. const lastRange = selection.getLastRange();
  278. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  279. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  280. } );
  281. } );
  282. describe( 'createFromSelection()', () => {
  283. it( 'should throw', () => {
  284. selection.addRange( range, true );
  285. expect( () => {
  286. DocumentSelection.createFromSelection( selection );
  287. } ).to.throw( CKEditorError, /^documentselection-cannot-create:/ );
  288. } );
  289. } );
  290. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  291. // working well, without getting into complicated corner cases.
  292. describe( 'after applying an operation should get updated and fire events', () => {
  293. let spyRange;
  294. beforeEach( () => {
  295. root.removeChildren( 0, root.childCount );
  296. root.insertChildren( 0, [
  297. new Element( 'p', [], new Text( 'abcdef' ) ),
  298. new Element( 'p', [], new Text( 'foobar' ) ),
  299. new Text( 'xyz' )
  300. ] );
  301. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  302. spyRange = sinon.spy();
  303. selection.on( 'change:range', spyRange );
  304. } );
  305. describe( 'InsertOperation', () => {
  306. it( 'before selection', () => {
  307. selection.on( 'change:range', ( evt, data ) => {
  308. expect( data.directChange ).to.be.false;
  309. } );
  310. doc.applyOperation( wrapInDelta(
  311. new InsertOperation(
  312. new Position( root, [ 0, 1 ] ),
  313. 'xyz',
  314. doc.version
  315. )
  316. ) );
  317. const range = selection.getFirstRange();
  318. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  319. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  320. expect( spyRange.calledOnce ).to.be.true;
  321. } );
  322. it( 'inside selection', () => {
  323. selection.on( 'change:range', ( evt, data ) => {
  324. expect( data.directChange ).to.be.false;
  325. } );
  326. doc.applyOperation( wrapInDelta(
  327. new InsertOperation(
  328. new Position( root, [ 1, 0 ] ),
  329. 'xyz',
  330. doc.version
  331. )
  332. ) );
  333. const range = selection.getFirstRange();
  334. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  335. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  336. expect( spyRange.calledOnce ).to.be.true;
  337. } );
  338. } );
  339. describe( 'MoveOperation', () => {
  340. it( 'move range from before a selection', () => {
  341. selection.on( 'change:range', ( evt, data ) => {
  342. expect( data.directChange ).to.be.false;
  343. } );
  344. doc.applyOperation( wrapInDelta(
  345. new MoveOperation(
  346. new Position( root, [ 0, 0 ] ),
  347. 2,
  348. new Position( root, [ 2 ] ),
  349. doc.version
  350. )
  351. ) );
  352. const range = selection.getFirstRange();
  353. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  354. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  355. expect( spyRange.calledOnce ).to.be.true;
  356. } );
  357. it( 'moved into before a selection', () => {
  358. selection.on( 'change:range', ( evt, data ) => {
  359. expect( data.directChange ).to.be.false;
  360. } );
  361. doc.applyOperation( wrapInDelta(
  362. new MoveOperation(
  363. new Position( root, [ 2 ] ),
  364. 2,
  365. new Position( root, [ 0, 0 ] ),
  366. doc.version
  367. )
  368. ) );
  369. const range = selection.getFirstRange();
  370. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  371. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  372. expect( spyRange.calledOnce ).to.be.true;
  373. } );
  374. it( 'move range from inside of selection', () => {
  375. selection.on( 'change:range', ( evt, data ) => {
  376. expect( data.directChange ).to.be.false;
  377. } );
  378. doc.applyOperation( wrapInDelta(
  379. new MoveOperation(
  380. new Position( root, [ 1, 0 ] ),
  381. 2,
  382. new Position( root, [ 2 ] ),
  383. doc.version
  384. )
  385. ) );
  386. const range = selection.getFirstRange();
  387. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  388. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  389. expect( spyRange.calledOnce ).to.be.true;
  390. } );
  391. it( 'moved range intersects with selection', () => {
  392. selection.on( 'change:range', ( evt, data ) => {
  393. expect( data.directChange ).to.be.false;
  394. } );
  395. doc.applyOperation( wrapInDelta(
  396. new MoveOperation(
  397. new Position( root, [ 1, 3 ] ),
  398. 2,
  399. new Position( root, [ 4 ] ),
  400. doc.version
  401. )
  402. ) );
  403. const range = selection.getFirstRange();
  404. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  405. expect( range.end.path ).to.deep.equal( [ 5 ] );
  406. expect( spyRange.calledOnce ).to.be.true;
  407. } );
  408. it( 'split inside selection (do not break selection)', () => {
  409. selection.on( 'change:range', ( evt, data ) => {
  410. expect( data.directChange ).to.be.false;
  411. } );
  412. const batch = doc.batch();
  413. const splitDelta = new SplitDelta();
  414. const insertOperation = new InsertOperation(
  415. new Position( root, [ 2 ] ),
  416. new Element( 'p' ),
  417. 0
  418. );
  419. const moveOperation = new MoveOperation(
  420. new Position( root, [ 1, 2 ] ),
  421. 4,
  422. new Position( root, [ 2, 0 ] ),
  423. 1
  424. );
  425. batch.addDelta( splitDelta );
  426. splitDelta.addOperation( insertOperation );
  427. splitDelta.addOperation( moveOperation );
  428. doc.applyOperation( insertOperation );
  429. doc.applyOperation( moveOperation );
  430. const range = selection.getFirstRange();
  431. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  432. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  433. expect( spyRange.calledOnce ).to.be.true;
  434. } );
  435. } );
  436. describe( 'AttributeOperation', () => {
  437. it( 'changed range includes selection anchor', () => {
  438. const spyAttribute = sinon.spy();
  439. selection.on( 'change:attribute', spyAttribute );
  440. selection.on( 'change:attribute', ( evt, data ) => {
  441. expect( data.directChange ).to.be.false;
  442. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  443. } );
  444. doc.applyOperation( wrapInDelta(
  445. new AttributeOperation(
  446. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  447. 'foo',
  448. null,
  449. 'bar',
  450. doc.version
  451. )
  452. ) );
  453. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  454. expect( spyAttribute.calledOnce ).to.be.true;
  455. } );
  456. it( 'should not overwrite previously set attributes', () => {
  457. selection.setAttribute( 'foo', 'xyz' );
  458. const spyAttribute = sinon.spy();
  459. selection.on( 'change:attribute', spyAttribute );
  460. doc.applyOperation( wrapInDelta(
  461. new AttributeOperation(
  462. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  463. 'foo',
  464. null,
  465. 'bar',
  466. doc.version
  467. )
  468. ) );
  469. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  470. expect( spyAttribute.called ).to.be.false;
  471. } );
  472. it( 'should not overwrite previously removed attributes', () => {
  473. selection.setAttribute( 'foo', 'xyz' );
  474. selection.removeAttribute( 'foo' );
  475. const spyAttribute = sinon.spy();
  476. selection.on( 'change:attribute', spyAttribute );
  477. doc.applyOperation( wrapInDelta(
  478. new AttributeOperation(
  479. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  480. 'foo',
  481. null,
  482. 'bar',
  483. doc.version
  484. )
  485. ) );
  486. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  487. expect( spyAttribute.called ).to.be.false;
  488. } );
  489. } );
  490. describe( 'RemoveOperation', () => {
  491. it( 'fix selection range if it ends up in graveyard #1', () => {
  492. selection.collapse( new Position( root, [ 1, 3 ] ) );
  493. doc.applyOperation( wrapInDelta(
  494. new RemoveOperation(
  495. new Position( root, [ 1, 2 ] ),
  496. 2,
  497. new Position( doc.graveyard, [ 0 ] ),
  498. doc.version
  499. )
  500. ) );
  501. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  502. } );
  503. it( 'fix selection range if it ends up in graveyard #2', () => {
  504. selection.setRanges( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  505. doc.applyOperation( wrapInDelta(
  506. new RemoveOperation(
  507. new Position( root, [ 1, 2 ] ),
  508. 2,
  509. new Position( doc.graveyard, [ 0 ] ),
  510. doc.version
  511. )
  512. ) );
  513. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  514. } );
  515. it( 'fix selection range if it ends up in graveyard #3', () => {
  516. selection.setRanges( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  517. doc.applyOperation( wrapInDelta(
  518. new RemoveOperation(
  519. new Position( root, [ 1 ] ),
  520. 2,
  521. new Position( doc.graveyard, [ 0 ] ),
  522. doc.version
  523. )
  524. ) );
  525. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  526. } );
  527. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  528. doc.applyOperation( wrapInDelta(
  529. new RemoveOperation(
  530. new Position( root, [ 0 ] ),
  531. 3,
  532. new Position( doc.graveyard, [ 0 ] ),
  533. doc.version
  534. )
  535. ) );
  536. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  537. doc.applyOperation( wrapInDelta(
  538. new InsertOperation(
  539. new Position( root, [ 0 ] ),
  540. new Element( 'p' ),
  541. doc.version
  542. )
  543. ) );
  544. // Now it's clear that it's the default range.
  545. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  546. } );
  547. } );
  548. } );
  549. describe( 'attributes', () => {
  550. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  551. beforeEach( () => {
  552. root.removeChildren( 0, root.childCount );
  553. root.appendChildren( [
  554. new Element( 'p', [], new Text( 'foobar' ) ),
  555. new Element( 'p', [], [] )
  556. ] );
  557. fullP = root.getChild( 0 );
  558. emptyP = root.getChild( 1 );
  559. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  560. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  561. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  562. // a root which wasn't empty (so the ranges didn't actualy make much sense).
  563. expect( root.childCount ).to.equal( 2 );
  564. } );
  565. describe( 'setAttribute()', () => {
  566. it( 'should store attribute if the selection is in empty node', () => {
  567. selection.setRanges( [ rangeInEmptyP ] );
  568. selection.setAttribute( 'foo', 'bar' );
  569. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  570. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  571. } );
  572. } );
  573. describe( 'setAttributesTo()', () => {
  574. it( 'should fire change:attribute event with correct parameters', done => {
  575. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  576. selection.on( 'change:attribute', ( evt, data ) => {
  577. expect( data.directChange ).to.be.true;
  578. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  579. done();
  580. } );
  581. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  582. } );
  583. it( 'should not fire change:attribute event if same attributes are set', () => {
  584. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  585. const spy = sinon.spy();
  586. selection.on( 'change:attribute', spy );
  587. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  588. expect( spy.called ).to.be.false;
  589. } );
  590. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  591. selection.setRanges( [ rangeInEmptyP ] );
  592. selection.setAttribute( 'abc', 'xyz' );
  593. selection.setAttributesTo( { foo: 'bar' } );
  594. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  595. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  596. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  597. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  598. } );
  599. } );
  600. describe( 'removeAttribute()', () => {
  601. it( 'should remove attribute set on the text fragment', () => {
  602. selection.setRanges( [ rangeInFullP ] );
  603. selection.setAttribute( 'foo', 'bar' );
  604. selection.removeAttribute( 'foo' );
  605. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  606. expect( fullP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  607. } );
  608. it( 'should remove stored attribute if the selection is in empty node', () => {
  609. selection.setRanges( [ rangeInEmptyP ] );
  610. selection.setAttribute( 'foo', 'bar' );
  611. selection.removeAttribute( 'foo' );
  612. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  613. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  614. } );
  615. } );
  616. describe( 'clearAttributes()', () => {
  617. it( 'should remove all stored attributes if the selection is in empty node', () => {
  618. selection.setRanges( [ rangeInEmptyP ] );
  619. selection.setAttribute( 'foo', 'bar' );
  620. selection.setAttribute( 'abc', 'xyz' );
  621. selection.clearAttributes();
  622. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  623. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  624. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  625. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  626. } );
  627. } );
  628. describe( '_getStoredAttributes()', () => {
  629. it( 'should return no values if there are no ranges in selection', () => {
  630. const values = Array.from( selection._getStoredAttributes() );
  631. expect( values ).to.deep.equal( [] );
  632. } );
  633. } );
  634. describe( 'are updated on a direct range change', () => {
  635. beforeEach( () => {
  636. root.insertChildren( 0, [
  637. new Element( 'p', { p: true } ),
  638. new Text( 'a', { a: true } ),
  639. new Element( 'p', { p: true } ),
  640. new Text( 'b', { b: true } ),
  641. new Text( 'c', { c: true } ),
  642. new Element( 'p', [], [
  643. new Text( 'd', { d: true } )
  644. ] ),
  645. new Element( 'p', { p: true } ),
  646. new Text( 'e', { e: true } )
  647. ] );
  648. } );
  649. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  650. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  651. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  652. // Step into elements when looking for first character:
  653. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  654. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  655. } );
  656. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  657. // Take styles from character before selection.
  658. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  659. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  660. // If there are none,
  661. // Take styles from character after selection.
  662. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  663. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  664. // If there are none,
  665. // Look from the selection position to the beginning of node looking for character to take attributes from.
  666. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  667. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  668. // If there are none,
  669. // Look from the selection position to the end of node looking for character to take attributes from.
  670. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  671. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  672. // If there are no characters to copy attributes from, use stored attributes.
  673. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  674. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  675. } );
  676. it( 'should overwrite any previously set attributes', () => {
  677. selection.collapse( new Position( root, [ 5, 0 ] ) );
  678. selection.setAttribute( 'x', true );
  679. selection.setAttribute( 'y', true );
  680. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  681. selection.collapse( new Position( root, [ 1 ] ) );
  682. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  683. } );
  684. it( 'should fire change:attribute event', () => {
  685. const spy = sinon.spy();
  686. selection.on( 'change:attribute', spy );
  687. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  688. expect( spy.calledOnce ).to.be.true;
  689. } );
  690. it( 'should not fire change:attribute event if attributes did not change', () => {
  691. selection.collapse( new Position( root, [ 5, 0 ] ) );
  692. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  693. const spy = sinon.spy();
  694. selection.on( 'change:attribute', spy );
  695. selection.collapse( new Position( root, [ 5, 1 ] ) );
  696. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  697. expect( spy.called ).to.be.false;
  698. } );
  699. } );
  700. // #986
  701. describe( 'are not inherited from the inside of object elements', () => {
  702. beforeEach( () => {
  703. doc.schema.registerItem( 'image' );
  704. doc.schema.allow( { name: 'image', inside: '$root' } );
  705. doc.schema.allow( { name: 'image', inside: '$block' } );
  706. doc.schema.allow( { name: '$inline', inside: 'image' } );
  707. doc.schema.objects.add( 'image' );
  708. doc.schema.registerItem( 'caption' );
  709. doc.schema.allow( { name: '$inline', inside: 'caption' } );
  710. doc.schema.allow( { name: 'caption', inside: 'image' } );
  711. doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } );
  712. } );
  713. it( 'ignores attributes inside an object if selection contains that object', () => {
  714. setData( doc, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  715. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  716. } );
  717. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  718. setData( doc, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  719. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  720. } );
  721. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  722. setData( doc, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  723. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  724. } );
  725. it( 'reads attributes from text even if the selection contains an object', () => {
  726. setData( doc, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  727. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  728. } );
  729. it( 'reads attributes when the entire selection inside an object', () => {
  730. setData( doc, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  731. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  732. } );
  733. it( 'stops reading attributes if selection starts with an object', () => {
  734. setData( doc, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  735. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  736. } );
  737. } );
  738. describe( 'parent element\'s attributes', () => {
  739. it( 'are set using a normal batch', () => {
  740. const batchTypes = [];
  741. doc.on( 'change', ( event, type, changes, batch ) => {
  742. batchTypes.push( batch.type );
  743. } );
  744. selection.setRanges( [ rangeInEmptyP ] );
  745. selection.setAttribute( 'foo', 'bar' );
  746. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  747. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  748. } );
  749. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  750. // Dedupe batches by using a map (multiple change events will be fired).
  751. const batchTypes = new Map();
  752. selection.setRanges( [ rangeInEmptyP ] );
  753. selection.setAttribute( 'foo', 'bar' );
  754. selection.setAttribute( 'abc', 'bar' );
  755. doc.on( 'change', ( event, type, changes, batch ) => {
  756. batchTypes.set( batch, batch.type );
  757. } );
  758. doc.batch().insert( rangeInEmptyP.start, 'x' );
  759. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  760. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  761. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  762. } );
  763. it( 'are removed when any content is moved into', () => {
  764. selection.setRanges( [ rangeInEmptyP ] );
  765. selection.setAttribute( 'foo', 'bar' );
  766. doc.batch().move( fullP.getChild( 0 ), rangeInEmptyP.start );
  767. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  768. } );
  769. it( 'are removed when containing element is merged with a non-empty element', () => {
  770. const emptyP2 = new Element( 'p', null, 'x' );
  771. root.appendChildren( emptyP2 );
  772. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  773. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  774. // <emptyP>{}<emptyP2>
  775. doc.batch().merge( Position.createAfter( emptyP ) );
  776. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  777. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  778. } );
  779. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  780. const emptyP2 = new Element( 'p', null );
  781. root.appendChildren( emptyP2 );
  782. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  783. emptyP2.setAttribute( abcStoreAttrKey, 'bar' );
  784. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  785. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  786. // <emptyP>{}<emptyP2>
  787. doc.batch().merge( Position.createAfter( emptyP ) );
  788. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  789. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  790. } );
  791. it( 'are removed even when there is no selection in it', () => {
  792. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  793. selection.setRanges( [ rangeInFullP ] );
  794. doc.batch().insert( rangeInEmptyP.start, 'x' );
  795. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  796. } );
  797. it( 'are removed only once in case of multi-op deltas', () => {
  798. const emptyP2 = new Element( 'p', null, 'x' );
  799. root.appendChildren( emptyP2 );
  800. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  801. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  802. const batch = doc.batch();
  803. const spy = sinon.spy( batch, 'removeAttribute' );
  804. // <emptyP>{}<emptyP2>
  805. batch.merge( Position.createAfter( emptyP ) );
  806. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  807. expect( spy.calledOnce ).to.be.true;
  808. } );
  809. it( 'are not removed on transparent batches', () => {
  810. selection.setRanges( [ rangeInEmptyP ] );
  811. selection.setAttribute( 'foo', 'bar' );
  812. doc.batch( 'transparent' ).insert( rangeInEmptyP.start, 'x' );
  813. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  814. } );
  815. // Rename and some other deltas don't specify range in doc#change event.
  816. // So let's see if there's no crash or something.
  817. it( 'are not removed on rename', () => {
  818. selection.setRanges( [ rangeInEmptyP ] );
  819. selection.setAttribute( 'foo', 'bar' );
  820. doc.batch().rename( emptyP, 'pnew' );
  821. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  822. } );
  823. } );
  824. } );
  825. } );