documentselection.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 AttributeOperation from '../../src/model/operation/attributeoperation';
  16. import SplitOperation from '../../src/model/operation/splitoperation';
  17. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  18. import count from '@ckeditor/ckeditor5-utils/src/count';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. import { setData, getData } from '../../src/dev-utils/model';
  21. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  22. describe( 'DocumentSelection', () => {
  23. let model, doc, root, selection, liveRange, range;
  24. testUtils.createSinonSandbox();
  25. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  26. const abcStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'abc' );
  27. beforeEach( () => {
  28. model = new Model();
  29. doc = model.document;
  30. root = doc.createRoot();
  31. root._appendChild( [
  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. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. model.schema.register( 'widget', {
  43. isObject: true
  44. } );
  45. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  46. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  47. } );
  48. afterEach( () => {
  49. model.destroy();
  50. liveRange.detach();
  51. } );
  52. describe( 'default range', () => {
  53. it( 'should go to the first editable element', () => {
  54. const ranges = Array.from( selection.getRanges() );
  55. expect( ranges.length ).to.equal( 1 );
  56. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  57. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  58. expect( selection ).to.have.property( 'isBackward', false );
  59. } );
  60. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  61. model = new Model();
  62. doc = model.document;
  63. root = doc.createRoot();
  64. root._insertChild( 0, new Text( 'foobar' ) );
  65. selection = doc.selection;
  66. const ranges = Array.from( selection.getRanges() );
  67. expect( ranges.length ).to.equal( 1 );
  68. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  69. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  70. expect( selection ).to.have.property( 'isBackward', false );
  71. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  72. } );
  73. it( 'should skip element when you can not put selection', () => {
  74. model = new Model();
  75. doc = model.document;
  76. root = doc.createRoot();
  77. root._insertChild( 0, [
  78. new Element( 'img' ),
  79. new Element( 'p', [], new Text( 'foobar' ) )
  80. ] );
  81. model.schema.register( 'img' );
  82. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  83. selection = doc.selection;
  84. const ranges = Array.from( selection.getRanges() );
  85. expect( ranges.length ).to.equal( 1 );
  86. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  87. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  88. expect( selection ).to.have.property( 'isBackward', false );
  89. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  90. } );
  91. } );
  92. describe( 'isCollapsed', () => {
  93. it( 'should be true for the default range (in text position)', () => {
  94. expect( selection.isCollapsed ).to.be.true;
  95. } );
  96. it( 'should be false for the default range (object selection) ', () => {
  97. root._insertChild( 0, new Element( 'widget' ) );
  98. expect( selection.isCollapsed ).to.be.false;
  99. } );
  100. it( 'should back off to the default algorithm if selection has ranges', () => {
  101. selection._setTo( range );
  102. expect( selection.isCollapsed ).to.be.false;
  103. } );
  104. } );
  105. describe( 'anchor', () => {
  106. it( 'should equal the default range\'s start (in text position)', () => {
  107. const expectedPos = new Position( root, [ 0, 0 ] );
  108. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  109. } );
  110. it( 'should equal the default range\'s start (object selection)', () => {
  111. root._insertChild( 0, new Element( 'widget' ) );
  112. const expectedPos = new Position( root, [ 0 ] );
  113. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  114. } );
  115. it( 'should back off to the default algorithm if selection has ranges', () => {
  116. selection._setTo( range );
  117. expect( selection.anchor.isEqual( range.start ) ).to.be.true;
  118. } );
  119. } );
  120. describe( 'focus', () => {
  121. it( 'should equal the default range\'s end (in text position)', () => {
  122. const expectedPos = new Position( root, [ 0, 0 ] );
  123. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  124. } );
  125. it( 'should equal the default range\'s end (object selection)', () => {
  126. root._insertChild( 0, new Element( 'widget' ) );
  127. const expectedPos = new Position( root, [ 1 ] );
  128. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  129. expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
  130. } );
  131. it( 'should back off to the default algorithm if selection has ranges', () => {
  132. selection._setTo( range );
  133. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  134. } );
  135. } );
  136. describe( 'rangeCount', () => {
  137. it( 'should return proper range count', () => {
  138. expect( selection.rangeCount ).to.equal( 1 );
  139. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  140. expect( selection.rangeCount ).to.equal( 1 );
  141. selection._setTo( [
  142. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ),
  143. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) )
  144. ] );
  145. expect( selection.rangeCount ).to.equal( 2 );
  146. } );
  147. } );
  148. describe( 'hasOwnRange', () => {
  149. it( 'should return true if selection has any ranges set', () => {
  150. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  151. expect( selection.hasOwnRange ).to.be.true;
  152. } );
  153. it( 'should return false if selection has a default range', () => {
  154. expect( selection.hasOwnRange ).to.be.false;
  155. } );
  156. } );
  157. describe( 'markers', () => {
  158. it( 'should implement #markers collection', () => {
  159. expect( selection.markers ).to.instanceof( Collection );
  160. expect( selection.markers ).to.length( 0 );
  161. } );
  162. it( 'should add markers to the collection when selection is inside the marker range', () => {
  163. model.change( writer => {
  164. writer.setSelection( writer.createRange(
  165. writer.createPositionFromPath( root, [ 2, 2 ] ),
  166. writer.createPositionFromPath( root, [ 2, 4 ] )
  167. ) );
  168. writer.addMarker( 'marker-1', {
  169. range: writer.createRange(
  170. writer.createPositionFromPath( root, [ 0, 0 ] ),
  171. writer.createPositionFromPath( root, [ 2, 2 ] )
  172. ),
  173. usingOperation: false
  174. } );
  175. writer.addMarker( 'marker-2', {
  176. range: writer.createRange(
  177. writer.createPositionFromPath( root, [ 2, 2 ] ),
  178. writer.createPositionFromPath( root, [ 2, 4 ] )
  179. ),
  180. usingOperation: false
  181. } );
  182. writer.addMarker( 'marker-3', {
  183. range: writer.createRange(
  184. writer.createPositionFromPath( root, [ 2, 1 ] ),
  185. writer.createPositionFromPath( root, [ 2, 5 ] )
  186. ),
  187. usingOperation: false
  188. } );
  189. writer.addMarker( 'marker-4', {
  190. range: writer.createRange(
  191. writer.createPositionFromPath( root, [ 2, 4 ] ),
  192. writer.createPositionFromPath( root, [ 3, 0 ] )
  193. ),
  194. usingOperation: false
  195. } );
  196. } );
  197. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-2', 'marker-3' ] );
  198. } );
  199. it( 'should update markers after selection change', () => {
  200. model.change( writer => {
  201. writer.setSelection( writer.createRange(
  202. writer.createPositionFromPath( root, [ 2, 1 ] ),
  203. writer.createPositionFromPath( root, [ 2, 2 ] )
  204. ) );
  205. writer.addMarker( 'marker-1', {
  206. range: writer.createRange(
  207. writer.createPositionFromPath( root, [ 2, 0 ] ),
  208. writer.createPositionFromPath( root, [ 2, 6 ] )
  209. ),
  210. usingOperation: false
  211. } );
  212. writer.addMarker( 'marker-2', {
  213. range: writer.createRange(
  214. writer.createPositionFromPath( root, [ 2, 0 ] ),
  215. writer.createPositionFromPath( root, [ 2, 3 ] )
  216. ),
  217. usingOperation: false
  218. } );
  219. writer.addMarker( 'marker-3', {
  220. range: writer.createRange(
  221. writer.createPositionFromPath( root, [ 2, 3 ] ),
  222. writer.createPositionFromPath( root, [ 2, 6 ] )
  223. ),
  224. usingOperation: false
  225. } );
  226. } );
  227. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-1', 'marker-2' ] );
  228. model.change( writer => {
  229. writer.setSelection( writer.createRange(
  230. writer.createPositionFromPath( root, [ 2, 4 ] ),
  231. writer.createPositionFromPath( root, [ 2, 5 ] )
  232. ) );
  233. } );
  234. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-1', 'marker-3' ] );
  235. } );
  236. it( 'should update markers after markers change', () => {
  237. model.change( writer => {
  238. writer.setSelection( writer.createRange(
  239. writer.createPositionFromPath( root, [ 2, 1 ] ),
  240. writer.createPositionFromPath( root, [ 2, 2 ] )
  241. ) );
  242. writer.addMarker( 'marker-1', {
  243. range: writer.createRange(
  244. writer.createPositionFromPath( root, [ 2, 0 ] ),
  245. writer.createPositionFromPath( root, [ 2, 6 ] )
  246. ),
  247. usingOperation: false
  248. } );
  249. writer.addMarker( 'marker-2', {
  250. range: writer.createRange(
  251. writer.createPositionFromPath( root, [ 2, 0 ] ),
  252. writer.createPositionFromPath( root, [ 2, 3 ] )
  253. ),
  254. usingOperation: false
  255. } );
  256. writer.addMarker( 'marker-3', {
  257. range: writer.createRange(
  258. writer.createPositionFromPath( root, [ 2, 3 ] ),
  259. writer.createPositionFromPath( root, [ 2, 6 ] )
  260. ),
  261. usingOperation: false
  262. } );
  263. } );
  264. expect( selection.markers.map( marker => marker.name ), 1 ).to.have.members( [ 'marker-1', 'marker-2' ] );
  265. model.change( writer => {
  266. writer.removeMarker( 'marker-1' );
  267. writer.updateMarker( 'marker-2', {
  268. range: writer.createRange(
  269. writer.createPositionFromPath( root, [ 2, 3 ] ),
  270. writer.createPositionFromPath( root, [ 2, 6 ] )
  271. ),
  272. usingOperation: false
  273. } );
  274. writer.updateMarker( 'marker-3', {
  275. range: writer.createRange(
  276. writer.createPositionFromPath( root, [ 2, 0 ] ),
  277. writer.createPositionFromPath( root, [ 2, 3 ] )
  278. ),
  279. usingOperation: false
  280. } );
  281. } );
  282. expect( selection.markers.map( marker => marker.name ), 2 ).to.have.members( [ 'marker-3' ] );
  283. } );
  284. it( 'should not add marker when collapsed selection is on the marker left bound', () => {
  285. model.change( writer => {
  286. writer.setSelection( writer.createRange(
  287. writer.createPositionFromPath( root, [ 2, 2 ] ),
  288. writer.createPositionFromPath( root, [ 2, 4 ] )
  289. ) );
  290. writer.addMarker( 'marker', {
  291. range: writer.createRange(
  292. writer.createPositionFromPath( root, [ 2, 2 ] )
  293. ),
  294. usingOperation: false
  295. } );
  296. } );
  297. expect( selection.markers ).to.length( 0 );
  298. } );
  299. it( 'should not add marker when collapsed selection is on the marker right bound', () => {
  300. model.change( writer => {
  301. writer.setSelection( writer.createRange(
  302. writer.createPositionFromPath( root, [ 2, 4 ] )
  303. ) );
  304. writer.addMarker( 'marker', {
  305. range: writer.createRange(
  306. writer.createPositionFromPath( root, [ 2, 2 ] ),
  307. writer.createPositionFromPath( root, [ 2, 4 ] )
  308. ),
  309. usingOperation: false
  310. } );
  311. } );
  312. expect( selection.markers ).to.length( 0 );
  313. } );
  314. it( 'should add marker when non-collapsed selection is inside a marker and touches the left bound', () => {
  315. model.change( writer => {
  316. writer.setSelection( writer.createRange(
  317. writer.createPositionFromPath( root, [ 2, 1 ] ),
  318. writer.createPositionFromPath( root, [ 2, 3 ] )
  319. ) );
  320. writer.addMarker( 'marker', {
  321. range: writer.createRange(
  322. writer.createPositionFromPath( root, [ 2, 1 ] ),
  323. writer.createPositionFromPath( root, [ 2, 5 ] )
  324. ),
  325. usingOperation: false
  326. } );
  327. } );
  328. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  329. } );
  330. it( 'should add marker when non-collapsed selection is inside a marker and touches the right bound', () => {
  331. model.change( writer => {
  332. writer.setSelection( writer.createRange(
  333. writer.createPositionFromPath( root, [ 2, 2 ] ),
  334. writer.createPositionFromPath( root, [ 2, 5 ] )
  335. ) );
  336. writer.addMarker( 'marker', {
  337. range: writer.createRange(
  338. writer.createPositionFromPath( root, [ 2, 1 ] ),
  339. writer.createPositionFromPath( root, [ 2, 5 ] )
  340. ),
  341. usingOperation: false
  342. } );
  343. } );
  344. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  345. } );
  346. it( 'should add marker of selected widget', () => {
  347. root._insertChild( 0, new Element( 'widget' ) );
  348. model.change( writer => {
  349. writer.setSelection( writer.createRange(
  350. writer.createPositionFromPath( root, [ 0 ] ),
  351. writer.createPositionFromPath( root, [ 1 ] )
  352. ) );
  353. writer.addMarker( 'marker', {
  354. range: writer.createRange(
  355. writer.createPositionFromPath( root, [ 0 ] ),
  356. writer.createPositionFromPath( root, [ 1 ] )
  357. ),
  358. usingOperation: false
  359. } );
  360. } );
  361. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  362. } );
  363. } );
  364. describe( 'destroy()', () => {
  365. it( 'should unbind all events', () => {
  366. selection._setTo( [ range, liveRange ] );
  367. const ranges = Array.from( selection._selection._ranges );
  368. sinon.spy( ranges[ 0 ], 'detach' );
  369. sinon.spy( ranges[ 1 ], 'detach' );
  370. selection.destroy();
  371. expect( ranges[ 0 ].detach.called ).to.be.true;
  372. expect( ranges[ 1 ].detach.called ).to.be.true;
  373. ranges[ 0 ].detach.restore();
  374. ranges[ 1 ].detach.restore();
  375. } );
  376. } );
  377. describe( 'getFirstRange()', () => {
  378. it( 'should return default range if no ranges were added', () => {
  379. const firstRange = selection.getFirstRange();
  380. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  381. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  382. } );
  383. } );
  384. describe( 'getLastRange()', () => {
  385. it( 'should return default range if no ranges were added', () => {
  386. const lastRange = selection.getLastRange();
  387. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  388. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  389. } );
  390. } );
  391. describe( 'getSelectedElement()', () => {
  392. it( 'should return selected element', () => {
  393. selection._setTo( liveRange );
  394. const p = root.getChild( 0 );
  395. expect( selection.getSelectedElement() ).to.equal( p );
  396. } );
  397. } );
  398. describe( 'is', () => {
  399. it( 'should return true for selection', () => {
  400. expect( selection.is( 'selection' ) ).to.be.true;
  401. expect( selection.is( 'model:selection' ) ).to.be.true;
  402. } );
  403. it( 'should return true for documentSelection', () => {
  404. expect( selection.is( 'documentSelection' ) ).to.be.true;
  405. expect( selection.is( 'model:documentSelection' ) ).to.be.true;
  406. } );
  407. it( 'should return false for other values', () => {
  408. expect( selection.is( 'node' ) ).to.be.false;
  409. expect( selection.is( 'model:node' ) ).to.be.false;
  410. expect( selection.is( 'text' ) ).to.be.false;
  411. expect( selection.is( 'textProxy' ) ).to.be.false;
  412. expect( selection.is( 'element' ) ).to.be.false;
  413. expect( selection.is( 'element', 'paragraph' ) ).to.be.false;
  414. expect( selection.is( 'rootElement' ) ).to.be.false;
  415. expect( selection.is( 'view:selection' ) ).to.be.false;
  416. expect( selection.is( 'view:documentSelection' ) ).to.be.false;
  417. expect( selection.is() ).to.be.false;
  418. } );
  419. } );
  420. describe( '_setTo() - set collapsed at', () => {
  421. it( 'detaches all existing ranges', () => {
  422. selection._setTo( [ range, liveRange ] );
  423. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  424. selection._setTo( root, 0 );
  425. expect( spy.calledTwice ).to.be.true;
  426. } );
  427. } );
  428. describe( '_setFocus()', () => {
  429. it( 'modifies default range', () => {
  430. const startPos = selection.getFirstPosition();
  431. const endPos = Position._createAt( root, 'end' );
  432. selection._setFocus( endPos );
  433. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  434. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  435. } );
  436. it( 'detaches the range it replaces', () => {
  437. const startPos = Position._createAt( root, 1 );
  438. const endPos = Position._createAt( root, 2 );
  439. const newEndPos = Position._createAt( root, 4 );
  440. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  441. selection._setTo( new Range( startPos, endPos ) );
  442. selection._setFocus( newEndPos );
  443. expect( spy.calledOnce ).to.be.true;
  444. } );
  445. it( 'refreshes attributes', () => {
  446. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  447. selection._setFocus( Position._createAt( root, 1 ) );
  448. expect( spy.called ).to.be.true;
  449. } );
  450. } );
  451. describe( '_setTo() - remove all ranges', () => {
  452. let spy, ranges;
  453. beforeEach( () => {
  454. selection._setTo( [ liveRange, range ] );
  455. spy = sinon.spy();
  456. selection.on( 'change:range', spy );
  457. ranges = Array.from( selection._selection._ranges );
  458. sinon.spy( ranges[ 0 ], 'detach' );
  459. sinon.spy( ranges[ 1 ], 'detach' );
  460. selection._setTo( null );
  461. } );
  462. afterEach( () => {
  463. ranges[ 0 ].detach.restore();
  464. ranges[ 1 ].detach.restore();
  465. } );
  466. it( 'should remove all stored ranges (and reset to default range)', () => {
  467. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  468. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  469. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  470. } );
  471. it( 'should detach ranges', () => {
  472. expect( ranges[ 0 ].detach.called ).to.be.true;
  473. expect( ranges[ 1 ].detach.called ).to.be.true;
  474. } );
  475. it( 'should refresh attributes', () => {
  476. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  477. selection._setTo( null );
  478. expect( spy.called ).to.be.true;
  479. } );
  480. } );
  481. describe( '_setTo()', () => {
  482. it( 'should throw an error when range is invalid', () => {
  483. expectToThrowCKEditorError( () => {
  484. selection._setTo( [ { invalid: 'range' } ] );
  485. }, /model-selection-set-ranges-not-range/, model );
  486. } );
  487. it( 'should log a warning when trying to set selection to the graveyard', () => {
  488. // eslint-disable-next-line no-undef
  489. const warnStub = sinon.stub( console, 'warn' );
  490. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  491. selection._setTo( range );
  492. expect( warnStub.calledOnce ).to.equal( true );
  493. expect( warnStub.getCall( 0 ).args[ 0 ] ).to.match( /model-selection-range-in-graveyard/ );
  494. expect( selection._ranges ).to.deep.equal( [] );
  495. warnStub.restore();
  496. } );
  497. it( 'should detach removed ranges', () => {
  498. selection._setTo( [ liveRange, range ] );
  499. const oldRanges = Array.from( selection._selection._ranges );
  500. sinon.spy( oldRanges[ 0 ], 'detach' );
  501. sinon.spy( oldRanges[ 1 ], 'detach' );
  502. selection._setTo( [] );
  503. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  504. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  505. } );
  506. it( 'should refresh attributes', () => {
  507. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  508. selection._setTo( [ range ] );
  509. expect( spy.called ).to.be.true;
  510. } );
  511. // See #630.
  512. it( 'should refresh attributes – integration test for #630', () => {
  513. model.schema.extend( '$text', { allowIn: '$root' } );
  514. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  515. selection._setTo( [ Range._createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  516. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  517. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  518. expect( getData( model ) )
  519. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  520. } );
  521. } );
  522. describe( '_isStoreAttributeKey', () => {
  523. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  524. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  525. } );
  526. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  527. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  528. } );
  529. } );
  530. describe( 'attributes', () => {
  531. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  532. beforeEach( () => {
  533. root._removeChildren( 0, root.childCount );
  534. root._appendChild( [
  535. new Element( 'p', [], new Text( 'foobar' ) ),
  536. new Element( 'p', [], [] )
  537. ] );
  538. fullP = root.getChild( 0 );
  539. emptyP = root.getChild( 1 );
  540. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  541. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  542. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  543. // a root which wasn't empty (so the ranges didn't actually make much sense).
  544. expect( root.childCount ).to.equal( 2 );
  545. } );
  546. describe( '_setAttribute()', () => {
  547. it( 'should set attribute', () => {
  548. selection._setTo( [ rangeInEmptyP ] );
  549. selection._setAttribute( 'foo', 'bar' );
  550. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  551. } );
  552. } );
  553. describe( '_removeAttribute()', () => {
  554. it( 'should remove attribute set on the text fragment', () => {
  555. selection._setTo( [ rangeInFullP ] );
  556. selection._setAttribute( 'foo', 'bar' );
  557. selection._removeAttribute( 'foo' );
  558. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  559. } );
  560. it( 'should prevent auto update of the attribute even if attribute is not preset yet', () => {
  561. selection._setTo( new Position( root, [ 0, 1 ] ) );
  562. // Remove "foo" attribute that is not present in selection yet.
  563. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  564. selection._removeAttribute( 'foo' );
  565. // Trigger selecton auto update on document change. It should not get attribute from surrounding text;
  566. model.change( writer => {
  567. writer.setAttribute( 'foo', 'bar', Range._createIn( fullP ) );
  568. } );
  569. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  570. } );
  571. } );
  572. describe( '_getStoredAttributes()', () => {
  573. it( 'should return no values if there are no ranges in selection', () => {
  574. const values = Array.from( selection._getStoredAttributes() );
  575. expect( values ).to.deep.equal( [] );
  576. } );
  577. } );
  578. describe( 'are updated on a direct range change', () => {
  579. beforeEach( () => {
  580. root._insertChild( 0, [
  581. new Element( 'p', { p: true } ),
  582. new Text( 'a', { a: true } ),
  583. new Element( 'p', { p: true } ),
  584. new Text( 'b', { b: true } ),
  585. new Text( 'c', { c: true } ),
  586. new Element( 'p', [], [
  587. new Text( 'd', { d: true } )
  588. ] ),
  589. new Element( 'p', { p: true } ),
  590. new Text( 'e', { e: true } )
  591. ] );
  592. } );
  593. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  594. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  595. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  596. // Step into elements when looking for first character:
  597. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  598. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  599. } );
  600. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  601. // Take styles from character before selection.
  602. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  603. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  604. // If there are none,
  605. // Take styles from character after selection.
  606. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  607. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  608. // If there are none,
  609. // Look from the selection position to the beginning of node looking for character to take attributes from.
  610. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  611. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  612. // If there are none,
  613. // Look from the selection position to the end of node looking for character to take attributes from.
  614. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  615. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  616. // If there are no characters to copy attributes from, use stored attributes.
  617. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  618. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  619. } );
  620. it( 'should overwrite any previously set attributes', () => {
  621. selection._setTo( new Position( root, [ 5, 0 ] ) );
  622. selection._setAttribute( 'x', true );
  623. selection._setAttribute( 'y', true );
  624. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  625. selection._setTo( new Position( root, [ 1 ] ) );
  626. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  627. } );
  628. it( 'should fire change:attribute event', () => {
  629. const spy = sinon.spy();
  630. selection.on( 'change:attribute', spy );
  631. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  632. expect( spy.calledOnce ).to.be.true;
  633. } );
  634. it( 'should not fire change:attribute event if attributes did not change', () => {
  635. selection._setTo( new Position( root, [ 5, 0 ] ) );
  636. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  637. const spy = sinon.spy();
  638. selection.on( 'change:attribute', spy );
  639. selection._setTo( new Position( root, [ 5, 1 ] ) );
  640. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  641. expect( spy.called ).to.be.false;
  642. } );
  643. } );
  644. // #986
  645. describe( 'are not inherited from the inside of object elements', () => {
  646. beforeEach( () => {
  647. model.schema.register( 'image', {
  648. isObject: true
  649. } );
  650. model.schema.extend( 'image', { allowIn: '$root' } );
  651. model.schema.extend( 'image', { allowIn: '$block' } );
  652. model.schema.register( 'caption' );
  653. model.schema.extend( 'caption', { allowIn: 'image' } );
  654. model.schema.extend( '$text', {
  655. allowIn: [ 'image', 'caption' ],
  656. allowAttributes: 'bold'
  657. } );
  658. } );
  659. it( 'ignores attributes inside an object if selection contains that object', () => {
  660. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  661. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  662. } );
  663. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  664. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  665. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  666. } );
  667. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  668. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  669. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  670. } );
  671. it( 'reads attributes from text even if the selection contains an object', () => {
  672. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  673. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  674. } );
  675. it( 'reads attributes when the entire selection inside an object', () => {
  676. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  677. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  678. } );
  679. it( 'stops reading attributes if selection starts with an object', () => {
  680. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  681. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  682. } );
  683. } );
  684. describe( 'parent element\'s attributes', () => {
  685. it( 'are set using a normal batch', () => {
  686. const batchTypes = [];
  687. model.on( 'applyOperation', ( event, args ) => {
  688. const operation = args[ 0 ];
  689. const batch = operation.batch;
  690. batchTypes.push( batch.type );
  691. } );
  692. selection._setTo( [ rangeInEmptyP ] );
  693. model.change( writer => {
  694. writer.setSelectionAttribute( 'foo', 'bar' );
  695. } );
  696. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  697. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  698. } );
  699. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  700. // Dedupe batches by using a map (multiple change events will be fired).
  701. const batchTypes = new Map();
  702. selection._setTo( rangeInEmptyP );
  703. selection._setAttribute( 'foo', 'bar' );
  704. selection._setAttribute( 'abc', 'bar' );
  705. model.on( 'applyOperation', ( event, args ) => {
  706. const operation = args[ 0 ];
  707. const batch = operation.batch;
  708. batchTypes.set( batch, batch.type );
  709. } );
  710. model.change( writer => {
  711. writer.insertText( 'x', rangeInEmptyP.start );
  712. } );
  713. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  714. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  715. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  716. } );
  717. it( 'are removed when any content is moved into', () => {
  718. selection._setTo( rangeInEmptyP );
  719. selection._setAttribute( 'foo', 'bar' );
  720. model.change( writer => {
  721. writer.move( writer.createRangeOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  722. } );
  723. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  724. } );
  725. it( 'are removed when containing element is merged with a non-empty element', () => {
  726. const emptyP2 = new Element( 'p', null, 'x' );
  727. root._appendChild( emptyP2 );
  728. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  729. emptyP2._setAttribute( fooStoreAttrKey, 'bar' );
  730. model.change( writer => {
  731. // <emptyP>{}<emptyP2>
  732. writer.merge( writer.createPositionAfter( emptyP ) );
  733. } );
  734. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  735. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  736. } );
  737. it( 'are removed even when there is no selection in it', () => {
  738. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  739. selection._setTo( [ rangeInFullP ] );
  740. model.change( writer => {
  741. writer.insertText( 'x', rangeInEmptyP.start );
  742. } );
  743. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  744. } );
  745. it( 'uses model change to clear attributes', () => {
  746. selection._setTo( [ rangeInEmptyP ] );
  747. model.change( writer => {
  748. writer.setSelectionAttribute( 'foo', 'bar' );
  749. writer.insertText( 'x', rangeInEmptyP.start );
  750. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  751. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  752. } );
  753. // When the dust settles, `emptyP` should not have the attribute.
  754. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  755. } );
  756. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  757. const emptyP2 = new Element( 'p', null );
  758. root._appendChild( emptyP2 );
  759. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  760. emptyP2._setAttribute( abcStoreAttrKey, 'bar' );
  761. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  762. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  763. model.change( writer => {
  764. // <emptyP>{}<emptyP2>
  765. writer.merge( writer.createPositionAfter( emptyP ) );
  766. } );
  767. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  768. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  769. } );
  770. // Rename and some other operations don't specify range in doc#change event.
  771. // So let's see if there's no crash or something.
  772. it( 'are not removed on rename', () => {
  773. model.change( writer => {
  774. writer.setSelection( rangeInEmptyP );
  775. writer.setSelectionAttribute( 'foo', 'bar' );
  776. } );
  777. sinon.spy( model, 'enqueueChange' );
  778. model.change( writer => {
  779. writer.rename( emptyP, 'pnew' );
  780. } );
  781. expect( model.enqueueChange.called ).to.be.false;
  782. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  783. } );
  784. } );
  785. } );
  786. describe( '_overrideGravity()', () => {
  787. beforeEach( () => {
  788. model.schema.extend( '$text', {
  789. allowIn: '$root'
  790. } );
  791. } );
  792. it( 'should return the UID', () => {
  793. const overrideUidA = selection._overrideGravity();
  794. const overrideUidB = selection._overrideGravity();
  795. expect( overrideUidA ).to.be.a( 'string' );
  796. expect( overrideUidB ).to.be.a( 'string' );
  797. expect( overrideUidA ).to.not.equal( overrideUidB );
  798. } );
  799. it( 'should mark gravity as overridden', () => {
  800. expect( selection.isGravityOverridden ).to.false;
  801. selection._overrideGravity();
  802. expect( selection.isGravityOverridden ).to.true;
  803. } );
  804. it( 'should not inherit attributes from node before the caret', () => {
  805. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  806. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  807. selection._overrideGravity();
  808. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  809. } );
  810. it( 'should inherit attributes from node after the caret', () => {
  811. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  812. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  813. selection._overrideGravity();
  814. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  815. } );
  816. it( 'should not retain attributes that are set explicitly', () => {
  817. setData( model, '<$text italic="true">foo[]</$text>' );
  818. selection._setAttribute( 'bold', true );
  819. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  820. selection._overrideGravity();
  821. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [] );
  822. } );
  823. it( 'should retain overridden until selection will not change range by a direct change', () => {
  824. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  825. selection._overrideGravity();
  826. // Changed range but not directly.
  827. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  828. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  829. // Changed range directly.
  830. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  831. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  832. } );
  833. } );
  834. describe( '_restoreGravity()', () => {
  835. beforeEach( () => {
  836. model.schema.extend( '$text', {
  837. allowIn: '$root'
  838. } );
  839. } );
  840. it( 'should throw when a wrong, already restored, or no UID provided', () => {
  841. const overrideUid = selection._overrideGravity();
  842. selection._restoreGravity( overrideUid );
  843. // Wrong UID
  844. expectToThrowCKEditorError( () => {
  845. selection._restoreGravity( 'foo' );
  846. }, /^document-selection-gravity-wrong-restore/, model );
  847. // Already restored
  848. expectToThrowCKEditorError( () => {
  849. selection._restoreGravity( overrideUid );
  850. }, /^document-selection-gravity-wrong-restore/, model );
  851. // No UID
  852. expectToThrowCKEditorError( () => {
  853. selection._restoreGravity();
  854. }, /^document-selection-gravity-wrong-restore/, model );
  855. } );
  856. it( 'should revert default gravity when is overridden', () => {
  857. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  858. const overrideUid = selection._overrideGravity();
  859. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  860. expect( selection.isGravityOverridden ).to.true;
  861. selection._restoreGravity( overrideUid );
  862. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  863. expect( selection.isGravityOverridden ).to.false;
  864. } );
  865. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  866. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  867. const overrideUidA = selection._overrideGravity();
  868. const overrideUidB = selection._overrideGravity();
  869. expect( selection.isGravityOverridden ).to.true;
  870. selection._restoreGravity( overrideUidA );
  871. expect( selection.isGravityOverridden ).to.true;
  872. selection._restoreGravity( overrideUidB );
  873. expect( selection.isGravityOverridden ).to.false;
  874. } );
  875. } );
  876. // https://github.com/ckeditor/ckeditor5-engine/issues/1673
  877. describe( 'refreshing selection data', () => {
  878. it( 'should be up to date before post fixers', () => {
  879. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  880. const p = doc.getRoot().getChild( 0 );
  881. doc.registerPostFixer( () => {
  882. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  883. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker' ] );
  884. } );
  885. model.change( writer => {
  886. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  887. writer.addMarker( 'marker', {
  888. range: writer.createRange(
  889. writer.createPositionFromPath( p, [ 1 ] ),
  890. writer.createPositionFromPath( p, [ 5 ] )
  891. ),
  892. usingOperation: false
  893. } );
  894. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  895. } );
  896. } );
  897. it( 'should be up to date between post fixers', () => {
  898. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  899. const p = doc.getRoot().getChild( 0 );
  900. doc.registerPostFixer( writer => {
  901. writer.setAttribute( 'foo', 'biz', p.getChild( 0 ) );
  902. writer.removeMarker( 'marker-1' );
  903. writer.addMarker( 'marker-2', {
  904. range: writer.createRange(
  905. writer.createPositionFromPath( p, [ 1 ] ),
  906. writer.createPositionFromPath( p, [ 5 ] )
  907. ),
  908. usingOperation: false
  909. } );
  910. } );
  911. doc.registerPostFixer( () => {
  912. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'biz' );
  913. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker-2' ] );
  914. } );
  915. model.change( writer => {
  916. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  917. writer.addMarker( 'marker-1', {
  918. range: writer.createRange(
  919. writer.createPositionFromPath( p, [ 1 ] ),
  920. writer.createPositionFromPath( p, [ 5 ] )
  921. ),
  922. usingOperation: false
  923. } );
  924. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  925. } );
  926. } );
  927. it( 'should be up to date after post fixers (on `change` event)', done => {
  928. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  929. const p = doc.getRoot().getChild( 0 );
  930. doc.on( 'change', () => {
  931. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'biz' );
  932. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker-2' ] );
  933. done();
  934. } );
  935. doc.registerPostFixer( writer => {
  936. writer.setAttribute( 'foo', 'biz', p.getChild( 0 ) );
  937. writer.removeMarker( 'marker-1' );
  938. writer.addMarker( 'marker-2', {
  939. range: writer.createRange(
  940. writer.createPositionFromPath( p, [ 1 ] ),
  941. writer.createPositionFromPath( p, [ 5 ] )
  942. ),
  943. usingOperation: false
  944. } );
  945. } );
  946. model.change( writer => {
  947. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  948. writer.addMarker( 'marker-1', {
  949. range: writer.createRange(
  950. writer.createPositionFromPath( p, [ 1 ] ),
  951. writer.createPositionFromPath( p, [ 5 ] )
  952. ),
  953. usingOperation: false
  954. } );
  955. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  956. } );
  957. } );
  958. } );
  959. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  960. // working well, without getting into complicated corner cases.
  961. describe( 'after applying an operation should get updated and fire events', () => {
  962. let spyRange;
  963. beforeEach( () => {
  964. root._removeChildren( 0, root.childCount );
  965. root._insertChild( 0, [
  966. new Element( 'p', [], new Text( 'abcdef' ) ),
  967. new Element( 'p', [], new Text( 'foobar' ) ),
  968. new Text( 'xyz' )
  969. ] );
  970. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  971. spyRange = sinon.spy();
  972. selection.on( 'change:range', spyRange );
  973. } );
  974. describe( 'InsertOperation', () => {
  975. it( 'before selection', () => {
  976. selection.on( 'change:range', ( evt, data ) => {
  977. expect( data.directChange ).to.be.false;
  978. } );
  979. model.applyOperation(
  980. new InsertOperation(
  981. new Position( root, [ 0, 1 ] ),
  982. 'xyz',
  983. doc.version
  984. )
  985. );
  986. const range = selection.getFirstRange();
  987. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  988. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  989. expect( spyRange.calledOnce ).to.be.true;
  990. } );
  991. it( 'inside selection', () => {
  992. selection.on( 'change:range', ( evt, data ) => {
  993. expect( data.directChange ).to.be.false;
  994. } );
  995. model.applyOperation(
  996. new InsertOperation(
  997. new Position( root, [ 1, 0 ] ),
  998. 'xyz',
  999. doc.version
  1000. )
  1001. );
  1002. const range = selection.getFirstRange();
  1003. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1004. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  1005. expect( spyRange.calledOnce ).to.be.true;
  1006. } );
  1007. } );
  1008. describe( 'MoveOperation', () => {
  1009. it( 'move range from before a selection', () => {
  1010. selection.on( 'change:range', ( evt, data ) => {
  1011. expect( data.directChange ).to.be.false;
  1012. } );
  1013. model.applyOperation(
  1014. new MoveOperation(
  1015. new Position( root, [ 0, 0 ] ),
  1016. 2,
  1017. new Position( root, [ 2 ] ),
  1018. doc.version
  1019. )
  1020. );
  1021. const range = selection.getFirstRange();
  1022. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  1023. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1024. expect( spyRange.calledOnce ).to.be.true;
  1025. } );
  1026. it( 'moved into before a selection', () => {
  1027. selection.on( 'change:range', ( evt, data ) => {
  1028. expect( data.directChange ).to.be.false;
  1029. } );
  1030. model.applyOperation(
  1031. new MoveOperation(
  1032. new Position( root, [ 2 ] ),
  1033. 2,
  1034. new Position( root, [ 0, 0 ] ),
  1035. doc.version
  1036. )
  1037. );
  1038. const range = selection.getFirstRange();
  1039. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  1040. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1041. expect( spyRange.calledOnce ).to.be.true;
  1042. } );
  1043. it( 'move range from inside of selection', () => {
  1044. selection.on( 'change:range', ( evt, data ) => {
  1045. expect( data.directChange ).to.be.false;
  1046. } );
  1047. model.applyOperation(
  1048. new MoveOperation(
  1049. new Position( root, [ 1, 0 ] ),
  1050. 2,
  1051. new Position( root, [ 2 ] ),
  1052. doc.version
  1053. )
  1054. );
  1055. const range = selection.getFirstRange();
  1056. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1057. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  1058. expect( spyRange.calledOnce ).to.be.true;
  1059. } );
  1060. it( 'moved range intersects with selection', () => {
  1061. selection.on( 'change:range', ( evt, data ) => {
  1062. expect( data.directChange ).to.be.false;
  1063. } );
  1064. model.applyOperation(
  1065. new MoveOperation(
  1066. new Position( root, [ 1, 3 ] ),
  1067. 2,
  1068. new Position( root, [ 4 ] ),
  1069. doc.version
  1070. )
  1071. );
  1072. const range = selection.getFirstRange();
  1073. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1074. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  1075. expect( spyRange.calledOnce ).to.be.true;
  1076. } );
  1077. it( 'split inside selection (do not break selection)', () => {
  1078. selection.on( 'change:range', ( evt, data ) => {
  1079. expect( data.directChange ).to.be.false;
  1080. } );
  1081. const batch = new Batch();
  1082. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  1083. batch.addOperation( splitOperation );
  1084. model.applyOperation( splitOperation );
  1085. const range = selection.getFirstRange();
  1086. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1087. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  1088. expect( spyRange.calledOnce ).to.be.true;
  1089. } );
  1090. } );
  1091. describe( 'AttributeOperation', () => {
  1092. it( 'changed range includes selection anchor', () => {
  1093. const spyAttribute = sinon.spy();
  1094. selection.on( 'change:attribute', spyAttribute );
  1095. selection.on( 'change:attribute', ( evt, data ) => {
  1096. expect( data.directChange ).to.be.false;
  1097. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  1098. } );
  1099. model.applyOperation(
  1100. new AttributeOperation(
  1101. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1102. 'foo',
  1103. null,
  1104. 'bar',
  1105. doc.version
  1106. )
  1107. );
  1108. // Attributes are auto updated on document change.
  1109. model.change( () => {} );
  1110. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1111. expect( spyAttribute.calledOnce ).to.be.true;
  1112. } );
  1113. it( 'should not overwrite previously set attributes', () => {
  1114. selection._setAttribute( 'foo', 'xyz' );
  1115. const spyAttribute = sinon.spy();
  1116. selection.on( 'change:attribute', spyAttribute );
  1117. model.applyOperation(
  1118. new AttributeOperation(
  1119. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1120. 'foo',
  1121. null,
  1122. 'bar',
  1123. doc.version
  1124. )
  1125. );
  1126. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1127. expect( spyAttribute.called ).to.be.false;
  1128. } );
  1129. it( 'should not overwrite previously set attributes with same values', () => {
  1130. selection._setAttribute( 'foo', 'xyz' );
  1131. const spyAttribute = sinon.spy();
  1132. selection.on( 'change:attribute', spyAttribute );
  1133. model.applyOperation(
  1134. new AttributeOperation(
  1135. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1136. 'foo',
  1137. null,
  1138. 'xyz',
  1139. doc.version
  1140. )
  1141. );
  1142. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1143. expect( spyAttribute.called ).to.be.false;
  1144. } );
  1145. it( 'should not overwrite previously removed attributes', () => {
  1146. selection._setAttribute( 'foo', 'xyz' );
  1147. selection._removeAttribute( 'foo' );
  1148. const spyAttribute = sinon.spy();
  1149. selection.on( 'change:attribute', spyAttribute );
  1150. model.applyOperation(
  1151. new AttributeOperation(
  1152. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1153. 'foo',
  1154. null,
  1155. 'bar',
  1156. doc.version
  1157. )
  1158. );
  1159. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  1160. expect( spyAttribute.called ).to.be.false;
  1161. } );
  1162. } );
  1163. describe( 'MoveOperation to graveyard', () => {
  1164. it( 'fix selection range if it ends up in graveyard #1', () => {
  1165. selection._setTo( new Position( root, [ 1, 3 ] ) );
  1166. model.applyOperation(
  1167. new MoveOperation(
  1168. new Position( root, [ 1, 2 ] ),
  1169. 2,
  1170. new Position( doc.graveyard, [ 0 ] ),
  1171. doc.version
  1172. )
  1173. );
  1174. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1175. } );
  1176. it( 'fix selection range if it ends up in graveyard #2', () => {
  1177. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  1178. model.applyOperation(
  1179. new MoveOperation(
  1180. new Position( root, [ 1, 2 ] ),
  1181. 2,
  1182. new Position( doc.graveyard, [ 0 ] ),
  1183. doc.version
  1184. )
  1185. );
  1186. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1187. } );
  1188. it( 'fix selection range if it ends up in graveyard #3', () => {
  1189. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1190. model.applyOperation(
  1191. new MoveOperation(
  1192. new Position( root, [ 1 ] ),
  1193. 2,
  1194. new Position( doc.graveyard, [ 0 ] ),
  1195. doc.version
  1196. )
  1197. );
  1198. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1199. } );
  1200. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  1201. model.applyOperation(
  1202. new MoveOperation(
  1203. new Position( root, [ 0 ] ),
  1204. 3,
  1205. new Position( doc.graveyard, [ 0 ] ),
  1206. doc.version
  1207. )
  1208. );
  1209. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1210. model.applyOperation(
  1211. new InsertOperation(
  1212. new Position( root, [ 0 ] ),
  1213. new Element( 'p' ),
  1214. doc.version
  1215. )
  1216. );
  1217. // Now it's clear that it's the default range.
  1218. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1219. } );
  1220. } );
  1221. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1222. root._removeChildren( 0, root.childCount );
  1223. root._insertChild( 0, [
  1224. new Element( 'p', [], new Text( 'abcdef' ) ),
  1225. new Element( 'p', [], new Text( 'foobar' ) ),
  1226. new Text( 'xyz #2' )
  1227. ] );
  1228. selection._setTo( [
  1229. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1230. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1231. ] );
  1232. spyRange = sinon.spy();
  1233. selection.on( 'change:range', spyRange );
  1234. model.applyOperation(
  1235. new InsertOperation(
  1236. new Position( root, [ 0 ] ),
  1237. 'xyz #1',
  1238. doc.version
  1239. )
  1240. );
  1241. expect( spyRange.calledOnce ).to.be.true;
  1242. } );
  1243. } );
  1244. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1245. root._removeChildren( 0, root.childCount );
  1246. root._appendChild( '\uD83D\uDCA9' );
  1247. expectToThrowCKEditorError( () => {
  1248. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1249. }, /document-selection-wrong-position/, model );
  1250. expectToThrowCKEditorError( () => {
  1251. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1252. }, /document-selection-wrong-position/, model );
  1253. } );
  1254. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1255. root._removeChildren( 0, root.childCount );
  1256. root._appendChild( 'foo̻̐ͩbar' );
  1257. expectToThrowCKEditorError( () => {
  1258. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1259. }, /document-selection-wrong-position/, model );
  1260. expectToThrowCKEditorError( () => {
  1261. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1262. }, /document-selection-wrong-position/, model );
  1263. expectToThrowCKEditorError( () => {
  1264. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1265. }, /document-selection-wrong-position/, model );
  1266. expectToThrowCKEditorError( () => {
  1267. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1268. }, /document-selection-wrong-position/, model );
  1269. expectToThrowCKEditorError( () => {
  1270. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1271. }, /document-selection-wrong-position/, model );
  1272. expectToThrowCKEditorError( () => {
  1273. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1274. }, /document-selection-wrong-position/, model );
  1275. } );
  1276. } );