documentselection.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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( 'setCollapsedAt()', () => {
  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.setCollapsedAt( 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( 'moveFocusTo()', () => {
  188. it( 'modifies default range', () => {
  189. const startPos = selection.getFirstPosition();
  190. const endPos = Position.createAt( root, 'end' );
  191. selection.moveFocusTo( 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.moveFocusTo( 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.setCollapsedAt( 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.setCollapsedAt( 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.setCollapsedAt( 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.setCollapsedAt( 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.setCollapsedAt( 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 removed even when there is no selection in it', () => {
  780. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  781. selection.setRanges( [ rangeInFullP ] );
  782. doc.batch().insert( rangeInEmptyP.start, 'x' );
  783. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  784. } );
  785. it( 'are removed only once in case of multi-op deltas', () => {
  786. const emptyP2 = new Element( 'p', null, 'x' );
  787. root.appendChildren( emptyP2 );
  788. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  789. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  790. const batch = doc.batch();
  791. const spy = sinon.spy( batch, 'removeAttribute' );
  792. // <emptyP>{}<emptyP2>
  793. batch.merge( Position.createAfter( emptyP ) );
  794. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  795. expect( spy.calledOnce ).to.be.true;
  796. } );
  797. it( 'uses document enqueue changes to clear attributes', () => {
  798. selection.setRanges( [ rangeInEmptyP ] );
  799. selection.setAttribute( 'foo', 'bar' );
  800. doc.enqueueChanges( () => {
  801. doc.batch().insert( rangeInEmptyP.start, 'x' );
  802. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  803. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  804. } );
  805. // When the dust settles, `emptyP` should not have the attribute.
  806. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  807. } );
  808. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  809. const emptyP2 = new Element( 'p', null );
  810. root.appendChildren( emptyP2 );
  811. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  812. emptyP2.setAttribute( abcStoreAttrKey, 'bar' );
  813. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  814. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  815. // <emptyP>{}<emptyP2>
  816. doc.batch().merge( Position.createAfter( emptyP ) );
  817. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  818. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  819. } );
  820. it( 'are not removed on transparent batches', () => {
  821. selection.setRanges( [ rangeInEmptyP ] );
  822. selection.setAttribute( 'foo', 'bar' );
  823. sinon.spy( doc, 'enqueueChanges' );
  824. doc.batch( 'transparent' ).insert( rangeInEmptyP.start, 'x' );
  825. expect( doc.enqueueChanges.called ).to.be.false;
  826. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  827. } );
  828. // Rename and some other deltas don't specify range in doc#change event.
  829. // So let's see if there's no crash or something.
  830. it( 'are not removed on rename', () => {
  831. selection.setRanges( [ rangeInEmptyP ] );
  832. selection.setAttribute( 'foo', 'bar' );
  833. sinon.spy( doc, 'enqueueChanges' );
  834. doc.batch().rename( emptyP, 'pnew' );
  835. expect( doc.enqueueChanges.called ).to.be.false;
  836. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  837. } );
  838. } );
  839. } );
  840. } );