documentselection.js 37 KB

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