8
0

documentselection.js 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals console */
  6. import Model from '../../src/model/model';
  7. import Batch from '../../src/model/batch';
  8. import Element from '../../src/model/element';
  9. import Text from '../../src/model/text';
  10. import Range from '../../src/model/range';
  11. import Position from '../../src/model/position';
  12. import LiveRange from '../../src/model/liverange';
  13. import DocumentSelection from '../../src/model/documentselection';
  14. import InsertOperation from '../../src/model/operation/insertoperation';
  15. import MoveOperation from '../../src/model/operation/moveoperation';
  16. import AttributeOperation from '../../src/model/operation/attributeoperation';
  17. import SplitOperation from '../../src/model/operation/splitoperation';
  18. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  19. import count from '@ckeditor/ckeditor5-utils/src/count';
  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. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  25. const abcStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'abc' );
  26. beforeEach( () => {
  27. model = new Model();
  28. doc = model.document;
  29. root = doc.createRoot();
  30. root._appendChild( [
  31. new Element( 'p' ),
  32. new Element( 'p' ),
  33. new Element( 'p', [], new Text( 'foobar' ) ),
  34. new Element( 'p' ),
  35. new Element( 'p' ),
  36. new Element( 'p' ),
  37. new Element( 'p', [], new Text( 'foobar' ) )
  38. ] );
  39. selection = doc.selection;
  40. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  41. model.schema.register( 'widget', {
  42. isObject: true
  43. } );
  44. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  45. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  46. } );
  47. afterEach( () => {
  48. sinon.restore();
  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. describe( 'should fire change:marker event when', () => {
  364. // Set marker to range 0-4.
  365. beforeEach( () => {
  366. model.change( writer => {
  367. writer.addMarker( 'marker-1', {
  368. range: writer.createRange(
  369. writer.createPositionFromPath( root, [ 2, 0 ] ),
  370. writer.createPositionFromPath( root, [ 2, 4 ] )
  371. ),
  372. usingOperation: false
  373. } );
  374. } );
  375. } );
  376. it( 'selection ranges change (marker added to the selection)', () => {
  377. const spy = sinon.spy();
  378. model.change( writer => {
  379. // The selection has no markers before the change.
  380. model.document.selection.on( 'change:marker', ( evt, data ) => {
  381. expect( data.oldMarkers ).to.deep.equal( [] );
  382. spy();
  383. } );
  384. // Move selection to 1-2, that is inside 0-4 marker.
  385. writer.setSelection( writer.createRange(
  386. writer.createPositionFromPath( root, [ 2, 1 ] ),
  387. writer.createPositionFromPath( root, [ 2, 2 ] )
  388. ) );
  389. } );
  390. expect( spy.calledOnce ).to.be.true;
  391. } );
  392. it( 'selection ranges change (marker removed from the selection)', () => {
  393. const spy = sinon.spy();
  394. model.change( writer => {
  395. writer.setSelection( writer.createRange(
  396. writer.createPositionFromPath( root, [ 2, 1 ] ),
  397. writer.createPositionFromPath( root, [ 2, 2 ] )
  398. ) );
  399. // The selection is in a marker before the change.
  400. model.document.selection.on( 'change:marker', ( evt, data ) => {
  401. expect( data.oldMarkers.map( marker => marker.name ) ).to.deep.equal( [ 'marker-1' ] );
  402. spy();
  403. } );
  404. // Move the selection out of the marker.
  405. writer.setSelection( writer.createPositionFromPath( root, [ 2, 5 ] ) );
  406. } );
  407. expect( spy.calledOnce ).to.be.true;
  408. } );
  409. it( 'selection focus changes (marker removed from the selection)', () => {
  410. const spy = sinon.spy();
  411. model.change( writer => {
  412. writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
  413. // The selection is in a marker before the change.
  414. model.document.selection.on( 'change:marker', ( evt, data ) => {
  415. expect( data.oldMarkers.map( marker => marker.name ) ).to.deep.equal( [ 'marker-1' ] );
  416. spy();
  417. } );
  418. // Move the selection focus out of the marker.
  419. writer.setSelectionFocus( writer.createPositionFromPath( root, [ 2, 5 ] ) );
  420. } );
  421. expect( spy.calledOnce ).to.be.true;
  422. } );
  423. it( 'a new marker contains the selection', () => {
  424. const spy = sinon.spy();
  425. model.change( writer => {
  426. writer.setSelection( writer.createPositionFromPath( root, [ 2, 5 ] ) );
  427. // The selection is not in a marker before the change.
  428. model.document.selection.on( 'change:marker', ( evt, data ) => {
  429. expect( data.oldMarkers ).to.deep.equal( [] );
  430. spy();
  431. } );
  432. writer.updateMarker( 'marker-1', {
  433. range: writer.createRange(
  434. writer.createPositionFromPath( root, [ 2, 0 ] ),
  435. writer.createPositionFromPath( root, [ 2, 6 ] )
  436. )
  437. } );
  438. } );
  439. expect( spy.calledOnce ).to.be.true;
  440. } );
  441. it( 'a marker stops contains the selection', () => {
  442. const spy = sinon.spy();
  443. model.change( writer => {
  444. writer.setSelection( writer.createPositionFromPath( root, [ 2, 3 ] ) );
  445. // The selection is in a marker before the change.
  446. model.document.selection.on( 'change:marker', ( evt, data ) => {
  447. expect( data.oldMarkers.map( marker => marker.name ) ).to.deep.equal( [ 'marker-1' ] );
  448. spy();
  449. } );
  450. writer.updateMarker( 'marker-1', {
  451. range: writer.createRange(
  452. writer.createPositionFromPath( root, [ 2, 0 ] ),
  453. writer.createPositionFromPath( root, [ 2, 1 ] )
  454. )
  455. } );
  456. } );
  457. expect( spy.calledOnce ).to.be.true;
  458. } );
  459. } );
  460. describe( 'should not fire change:marker event when', () => {
  461. // Set marker to range 0-4.
  462. beforeEach( () => {
  463. model.change( writer => {
  464. writer.addMarker( 'marker-1', {
  465. range: writer.createRange(
  466. writer.createPositionFromPath( root, [ 2, 0 ] ),
  467. writer.createPositionFromPath( root, [ 2, 4 ] )
  468. ),
  469. usingOperation: false
  470. } );
  471. } );
  472. } );
  473. it( 'selection ranges change does not change selection markers (no markers)', () => {
  474. const spy = sinon.spy();
  475. model.document.selection.on( 'change:marker', spy );
  476. model.change( writer => {
  477. writer.setSelection( writer.createPositionFromPath( root, [ 2, 5 ] ) );
  478. } );
  479. expect( spy.called ).to.be.false;
  480. } );
  481. it( 'selection ranges change does not change selection markers (same markers)', () => {
  482. model.change( writer => {
  483. writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
  484. } );
  485. const spy = sinon.spy();
  486. model.document.selection.on( 'change:marker', spy );
  487. model.change( writer => {
  488. writer.setSelection( writer.createPositionFromPath( root, [ 2, 3 ] ) );
  489. } );
  490. expect( spy.called ).to.be.false;
  491. } );
  492. it( 'selection focus change does not change selection markers', () => {
  493. model.change( writer => {
  494. writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
  495. } );
  496. const spy = sinon.spy();
  497. model.document.selection.on( 'change:marker', spy );
  498. model.change( writer => {
  499. writer.setSelectionFocus( writer.createPositionFromPath( root, [ 2, 3 ] ) );
  500. } );
  501. expect( spy.called ).to.be.false;
  502. } );
  503. it( 'changed marker still contains the selection', () => {
  504. model.change( writer => {
  505. writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
  506. } );
  507. const spy = sinon.spy();
  508. model.document.selection.on( 'change:marker', spy );
  509. model.change( writer => {
  510. writer.updateMarker( 'marker-1', {
  511. range: writer.createRange(
  512. writer.createPositionFromPath( root, [ 2, 0 ] ),
  513. writer.createPositionFromPath( root, [ 2, 5 ] )
  514. )
  515. } );
  516. } );
  517. expect( spy.called ).to.be.false;
  518. } );
  519. it( 'removed marker did not contain the selection', () => {
  520. model.change( writer => {
  521. writer.setSelection( writer.createPositionFromPath( root, [ 2, 5 ] ) );
  522. } );
  523. const spy = sinon.spy();
  524. model.document.selection.on( 'change:marker', spy );
  525. model.change( writer => {
  526. writer.removeMarker( 'marker-1' );
  527. } );
  528. expect( spy.called ).to.be.false;
  529. } );
  530. } );
  531. } );
  532. describe( 'destroy()', () => {
  533. it( 'should unbind all events', () => {
  534. selection._setTo( [ range, liveRange ] );
  535. const ranges = Array.from( selection._selection._ranges );
  536. sinon.spy( ranges[ 0 ], 'detach' );
  537. sinon.spy( ranges[ 1 ], 'detach' );
  538. selection.destroy();
  539. expect( ranges[ 0 ].detach.called ).to.be.true;
  540. expect( ranges[ 1 ].detach.called ).to.be.true;
  541. } );
  542. } );
  543. describe( 'getFirstRange()', () => {
  544. it( 'should return default range if no ranges were added', () => {
  545. const firstRange = selection.getFirstRange();
  546. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  547. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  548. } );
  549. } );
  550. describe( 'getLastRange()', () => {
  551. it( 'should return default range if no ranges were added', () => {
  552. const lastRange = selection.getLastRange();
  553. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  554. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  555. } );
  556. } );
  557. describe( 'getSelectedElement()', () => {
  558. it( 'should return selected element', () => {
  559. selection._setTo( liveRange );
  560. const p = root.getChild( 0 );
  561. expect( selection.getSelectedElement() ).to.equal( p );
  562. } );
  563. } );
  564. describe( 'is()', () => {
  565. it( 'should return true for selection', () => {
  566. expect( selection.is( 'selection' ) ).to.be.true;
  567. expect( selection.is( 'model:selection' ) ).to.be.true;
  568. } );
  569. it( 'should return true for documentSelection', () => {
  570. expect( selection.is( 'documentSelection' ) ).to.be.true;
  571. expect( selection.is( 'model:documentSelection' ) ).to.be.true;
  572. } );
  573. it( 'should return false for other values', () => {
  574. expect( selection.is( 'node' ) ).to.be.false;
  575. expect( selection.is( 'model:node' ) ).to.be.false;
  576. expect( selection.is( '$text' ) ).to.be.false;
  577. expect( selection.is( '$textProxy' ) ).to.be.false;
  578. expect( selection.is( 'element' ) ).to.be.false;
  579. expect( selection.is( 'element', 'paragraph' ) ).to.be.false;
  580. expect( selection.is( 'rootElement' ) ).to.be.false;
  581. expect( selection.is( 'view:selection' ) ).to.be.false;
  582. expect( selection.is( 'view:documentSelection' ) ).to.be.false;
  583. } );
  584. } );
  585. describe( '_setTo() - set collapsed at', () => {
  586. it( 'detaches all existing ranges', () => {
  587. selection._setTo( [ range, liveRange ] );
  588. const spy = sinon.spy( LiveRange.prototype, 'detach' );
  589. selection._setTo( root, 0 );
  590. expect( spy.calledTwice ).to.be.true;
  591. } );
  592. } );
  593. describe( '_setFocus()', () => {
  594. it( 'modifies default range', () => {
  595. const startPos = selection.getFirstPosition();
  596. const endPos = Position._createAt( root, 'end' );
  597. selection._setFocus( endPos );
  598. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  599. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  600. } );
  601. it( 'detaches the range it replaces', () => {
  602. const startPos = Position._createAt( root, 1 );
  603. const endPos = Position._createAt( root, 2 );
  604. const newEndPos = Position._createAt( root, 4 );
  605. const spy = sinon.spy( LiveRange.prototype, 'detach' );
  606. selection._setTo( new Range( startPos, endPos ) );
  607. selection._setFocus( newEndPos );
  608. expect( spy.calledOnce ).to.be.true;
  609. } );
  610. it( 'refreshes attributes', () => {
  611. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  612. selection._setFocus( Position._createAt( root, 1 ) );
  613. expect( spy.called ).to.be.true;
  614. } );
  615. } );
  616. describe( '_setTo() - remove all ranges', () => {
  617. let spy, ranges;
  618. beforeEach( () => {
  619. selection._setTo( [ liveRange, range ] );
  620. spy = sinon.spy();
  621. selection.on( 'change:range', spy );
  622. ranges = Array.from( selection._selection._ranges );
  623. sinon.spy( ranges[ 0 ], 'detach' );
  624. sinon.spy( ranges[ 1 ], 'detach' );
  625. selection._setTo( null );
  626. } );
  627. it( 'should remove all stored ranges (and reset to default range)', () => {
  628. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  629. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  630. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  631. } );
  632. it( 'should detach ranges', () => {
  633. expect( ranges[ 0 ].detach.called ).to.be.true;
  634. expect( ranges[ 1 ].detach.called ).to.be.true;
  635. } );
  636. it( 'should refresh attributes', () => {
  637. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  638. selection._setTo( null );
  639. expect( spy.called ).to.be.true;
  640. } );
  641. } );
  642. describe( '_setTo()', () => {
  643. it( 'should throw an error when range is invalid', () => {
  644. expectToThrowCKEditorError( () => {
  645. selection._setTo( [ { invalid: 'range' } ] );
  646. }, /model-selection-set-ranges-not-range/, model );
  647. } );
  648. it( 'should do nothing when trying to set selection to the graveyard', () => {
  649. // Catches the 'Trying to add a Range that is in the graveyard root. Range rejected.' warning in the CK_DEBUG mode.
  650. sinon.stub( console, 'warn' );
  651. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  652. selection._setTo( range );
  653. expect( selection._ranges ).to.deep.equal( [] );
  654. } );
  655. it( 'should detach removed ranges', () => {
  656. selection._setTo( [ liveRange, range ] );
  657. const oldRanges = Array.from( selection._selection._ranges );
  658. sinon.spy( oldRanges[ 0 ], 'detach' );
  659. sinon.spy( oldRanges[ 1 ], 'detach' );
  660. selection._setTo( [] );
  661. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  662. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  663. } );
  664. it( 'should refresh attributes', () => {
  665. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  666. selection._setTo( [ range ] );
  667. expect( spy.called ).to.be.true;
  668. } );
  669. // See #630.
  670. it( 'should refresh attributes – integration test for #630', () => {
  671. model.schema.extend( '$text', { allowIn: '$root' } );
  672. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  673. selection._setTo( [ Range._createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  674. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  675. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  676. expect( getData( model ) )
  677. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  678. } );
  679. } );
  680. describe( '_isStoreAttributeKey', () => {
  681. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  682. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  683. } );
  684. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  685. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  686. } );
  687. } );
  688. describe( 'attributes', () => {
  689. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  690. beforeEach( () => {
  691. root._removeChildren( 0, root.childCount );
  692. root._appendChild( [
  693. new Element( 'p', [], new Text( 'foobar' ) ),
  694. new Element( 'p', [], [] )
  695. ] );
  696. fullP = root.getChild( 0 );
  697. emptyP = root.getChild( 1 );
  698. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  699. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  700. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  701. // a root which wasn't empty (so the ranges didn't actually make much sense).
  702. expect( root.childCount ).to.equal( 2 );
  703. } );
  704. describe( '_setAttribute()', () => {
  705. it( 'should set attribute', () => {
  706. selection._setTo( [ rangeInEmptyP ] );
  707. selection._setAttribute( 'foo', 'bar' );
  708. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  709. } );
  710. } );
  711. describe( '_removeAttribute()', () => {
  712. it( 'should remove attribute set on the text fragment', () => {
  713. selection._setTo( [ rangeInFullP ] );
  714. selection._setAttribute( 'foo', 'bar' );
  715. selection._removeAttribute( 'foo' );
  716. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  717. } );
  718. it( 'should prevent auto update of the attribute even if attribute is not preset yet', () => {
  719. selection._setTo( new Position( root, [ 0, 1 ] ) );
  720. // Remove "foo" attribute that is not present in selection yet.
  721. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  722. selection._removeAttribute( 'foo' );
  723. // Trigger selecton auto update on document change. It should not get attribute from surrounding text;
  724. model.change( writer => {
  725. writer.setAttribute( 'foo', 'bar', Range._createIn( fullP ) );
  726. } );
  727. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  728. } );
  729. } );
  730. describe( '_getStoredAttributes()', () => {
  731. it( 'should return no values if there are no ranges in selection', () => {
  732. const values = Array.from( selection._getStoredAttributes() );
  733. expect( values ).to.deep.equal( [] );
  734. } );
  735. } );
  736. describe( 'are updated on a direct range change', () => {
  737. beforeEach( () => {
  738. root._insertChild( 0, [
  739. new Element( 'p', { p: true } ),
  740. new Text( 'a', { a: true } ),
  741. new Element( 'p', { p: true } ),
  742. new Text( 'b', { b: true } ),
  743. new Text( 'c', { c: true } ),
  744. new Element( 'p', [], [
  745. new Text( 'd', { d: true } )
  746. ] ),
  747. new Element( 'p', { p: true } ),
  748. new Text( 'e', { e: true } )
  749. ] );
  750. } );
  751. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  752. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  753. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  754. // Step into elements when looking for first character:
  755. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  756. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  757. } );
  758. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  759. // Take styles from character before selection.
  760. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  761. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  762. // If there are none,
  763. // Take styles from character after selection.
  764. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  765. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  766. // If there are none,
  767. // Look from the selection position to the beginning of node looking for character to take attributes from.
  768. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  769. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  770. // If there are none,
  771. // Look from the selection position to the end of node looking for character to take attributes from.
  772. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  773. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  774. // If there are no characters to copy attributes from, use stored attributes.
  775. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  776. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  777. } );
  778. it( 'should overwrite any previously set attributes', () => {
  779. selection._setTo( new Position( root, [ 5, 0 ] ) );
  780. selection._setAttribute( 'x', true );
  781. selection._setAttribute( 'y', true );
  782. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  783. selection._setTo( new Position( root, [ 1 ] ) );
  784. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  785. } );
  786. it( 'should fire change:attribute event', () => {
  787. const spy = sinon.spy();
  788. selection.on( 'change:attribute', spy );
  789. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  790. expect( spy.calledOnce ).to.be.true;
  791. } );
  792. it( 'should not fire change:attribute event if attributes did not change', () => {
  793. selection._setTo( new Position( root, [ 5, 0 ] ) );
  794. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  795. const spy = sinon.spy();
  796. selection.on( 'change:attribute', spy );
  797. selection._setTo( new Position( root, [ 5, 1 ] ) );
  798. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  799. expect( spy.called ).to.be.false;
  800. } );
  801. } );
  802. // #986
  803. describe( 'are not inherited from the inside of object elements', () => {
  804. beforeEach( () => {
  805. model.schema.register( 'image', {
  806. isObject: true
  807. } );
  808. model.schema.extend( 'image', { allowIn: '$root' } );
  809. model.schema.extend( 'image', { allowIn: '$block' } );
  810. model.schema.register( 'caption' );
  811. model.schema.extend( 'caption', { allowIn: 'image' } );
  812. model.schema.extend( '$text', {
  813. allowIn: [ 'image', 'caption' ],
  814. allowAttributes: 'bold'
  815. } );
  816. } );
  817. it( 'ignores attributes inside an object if selection contains that object', () => {
  818. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  819. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  820. } );
  821. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  822. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  823. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  824. } );
  825. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  826. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  827. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  828. } );
  829. it( 'reads attributes from text even if the selection contains an object', () => {
  830. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  831. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  832. } );
  833. it( 'reads attributes when the entire selection inside an object', () => {
  834. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  835. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  836. } );
  837. it( 'stops reading attributes if selection starts with an object', () => {
  838. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  839. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  840. } );
  841. } );
  842. describe( 'parent element\'s attributes', () => {
  843. it( 'are set using a normal batch', () => {
  844. const batchTypes = [];
  845. model.on( 'applyOperation', ( event, args ) => {
  846. const operation = args[ 0 ];
  847. const batch = operation.batch;
  848. batchTypes.push( batch.type );
  849. } );
  850. selection._setTo( [ rangeInEmptyP ] );
  851. model.change( writer => {
  852. writer.setSelectionAttribute( 'foo', 'bar' );
  853. } );
  854. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  855. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  856. } );
  857. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  858. // Dedupe batches by using a map (multiple change events will be fired).
  859. const batchTypes = new Map();
  860. selection._setTo( rangeInEmptyP );
  861. selection._setAttribute( 'foo', 'bar' );
  862. selection._setAttribute( 'abc', 'bar' );
  863. model.on( 'applyOperation', ( event, args ) => {
  864. const operation = args[ 0 ];
  865. const batch = operation.batch;
  866. batchTypes.set( batch, batch.type );
  867. } );
  868. model.change( writer => {
  869. writer.insertText( 'x', rangeInEmptyP.start );
  870. } );
  871. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  872. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  873. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  874. } );
  875. it( 'are removed when any content is moved into', () => {
  876. selection._setTo( rangeInEmptyP );
  877. selection._setAttribute( 'foo', 'bar' );
  878. model.change( writer => {
  879. writer.move( writer.createRangeOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  880. } );
  881. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  882. } );
  883. it( 'are removed when containing element is merged with a non-empty element', () => {
  884. const emptyP2 = new Element( 'p', null, 'x' );
  885. root._appendChild( emptyP2 );
  886. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  887. emptyP2._setAttribute( fooStoreAttrKey, 'bar' );
  888. model.change( writer => {
  889. // <emptyP>{}<emptyP2>
  890. writer.merge( writer.createPositionAfter( emptyP ) );
  891. } );
  892. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  893. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  894. } );
  895. it( 'are removed even when there is no selection in it', () => {
  896. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  897. selection._setTo( [ rangeInFullP ] );
  898. model.change( writer => {
  899. writer.insertText( 'x', rangeInEmptyP.start );
  900. } );
  901. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  902. } );
  903. it( 'uses model change to clear attributes', () => {
  904. selection._setTo( [ rangeInEmptyP ] );
  905. model.change( writer => {
  906. writer.setSelectionAttribute( 'foo', 'bar' );
  907. writer.insertText( 'x', rangeInEmptyP.start );
  908. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  909. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  910. } );
  911. // When the dust settles, `emptyP` should not have the attribute.
  912. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  913. } );
  914. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  915. const emptyP2 = new Element( 'p', null );
  916. root._appendChild( emptyP2 );
  917. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  918. emptyP2._setAttribute( abcStoreAttrKey, 'bar' );
  919. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  920. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  921. model.change( writer => {
  922. // <emptyP>{}<emptyP2>
  923. writer.merge( writer.createPositionAfter( emptyP ) );
  924. } );
  925. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  926. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  927. } );
  928. // Rename and some other operations don't specify range in doc#change event.
  929. // So let's see if there's no crash or something.
  930. it( 'are not removed on rename', () => {
  931. model.change( writer => {
  932. writer.setSelection( rangeInEmptyP );
  933. writer.setSelectionAttribute( 'foo', 'bar' );
  934. } );
  935. sinon.spy( model, 'enqueueChange' );
  936. model.change( writer => {
  937. writer.rename( emptyP, 'pnew' );
  938. } );
  939. expect( model.enqueueChange.called ).to.be.false;
  940. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  941. } );
  942. } );
  943. // #7459
  944. describe( 'ignores inline elements while reading surrounding attributes', () => {
  945. beforeEach( () => {
  946. model.schema.register( 'softBreak', {
  947. allowWhere: '$text',
  948. isInline: true
  949. } );
  950. } );
  951. it( 'should not inherit attributes from a node before an inline element', () => {
  952. setData( model, '<p><$text bold="true">Foo Bar.</$text><softBreak></softBreak>[]</p>' );
  953. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  954. } );
  955. it( 'should not inherit attributes from a node after an inline element (override gravity)', () => {
  956. setData( model, '<p>[]<softBreak></softBreak><$text bold="true">Foo Bar.</$text></p>' );
  957. const overrideGravityUid = selection._overrideGravity();
  958. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  959. selection._restoreGravity( overrideGravityUid );
  960. } );
  961. } );
  962. } );
  963. describe( '_overrideGravity()', () => {
  964. beforeEach( () => {
  965. model.schema.extend( '$text', {
  966. allowIn: '$root'
  967. } );
  968. } );
  969. it( 'should return the UID', () => {
  970. const overrideUidA = selection._overrideGravity();
  971. const overrideUidB = selection._overrideGravity();
  972. expect( overrideUidA ).to.be.a( 'string' );
  973. expect( overrideUidB ).to.be.a( 'string' );
  974. expect( overrideUidA ).to.not.equal( overrideUidB );
  975. } );
  976. it( 'should mark gravity as overridden', () => {
  977. expect( selection.isGravityOverridden ).to.false;
  978. selection._overrideGravity();
  979. expect( selection.isGravityOverridden ).to.true;
  980. } );
  981. it( 'should not inherit attributes from node before the caret', () => {
  982. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  983. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  984. selection._overrideGravity();
  985. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  986. } );
  987. it( 'should inherit attributes from node after the caret', () => {
  988. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  989. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  990. selection._overrideGravity();
  991. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  992. } );
  993. it( 'should not retain attributes that are set explicitly', () => {
  994. setData( model, '<$text italic="true">foo[]</$text>' );
  995. selection._setAttribute( 'bold', true );
  996. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  997. selection._overrideGravity();
  998. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [] );
  999. } );
  1000. it( 'should retain overridden until selection will not change range by a direct change', () => {
  1001. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  1002. selection._overrideGravity();
  1003. // Changed range but not directly.
  1004. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  1005. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  1006. // Changed range directly.
  1007. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  1008. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  1009. } );
  1010. } );
  1011. describe( '_restoreGravity()', () => {
  1012. beforeEach( () => {
  1013. model.schema.extend( '$text', {
  1014. allowIn: '$root'
  1015. } );
  1016. } );
  1017. it( 'should throw when a wrong, already restored, or no UID provided', () => {
  1018. const overrideUid = selection._overrideGravity();
  1019. selection._restoreGravity( overrideUid );
  1020. // Wrong UID
  1021. expectToThrowCKEditorError( () => {
  1022. selection._restoreGravity( 'foo' );
  1023. }, /^document-selection-gravity-wrong-restore/, model );
  1024. // Already restored
  1025. expectToThrowCKEditorError( () => {
  1026. selection._restoreGravity( overrideUid );
  1027. }, /^document-selection-gravity-wrong-restore/, model );
  1028. // No UID
  1029. expectToThrowCKEditorError( () => {
  1030. selection._restoreGravity();
  1031. }, /^document-selection-gravity-wrong-restore/, model );
  1032. } );
  1033. it( 'should revert default gravity when is overridden', () => {
  1034. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  1035. const overrideUid = selection._overrideGravity();
  1036. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  1037. expect( selection.isGravityOverridden ).to.true;
  1038. selection._restoreGravity( overrideUid );
  1039. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  1040. expect( selection.isGravityOverridden ).to.false;
  1041. } );
  1042. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  1043. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  1044. const overrideUidA = selection._overrideGravity();
  1045. const overrideUidB = selection._overrideGravity();
  1046. expect( selection.isGravityOverridden ).to.true;
  1047. selection._restoreGravity( overrideUidA );
  1048. expect( selection.isGravityOverridden ).to.true;
  1049. selection._restoreGravity( overrideUidB );
  1050. expect( selection.isGravityOverridden ).to.false;
  1051. } );
  1052. } );
  1053. // https://github.com/ckeditor/ckeditor5-engine/issues/1673
  1054. describe( 'refreshing selection data', () => {
  1055. it( 'should be up to date before post fixers', () => {
  1056. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  1057. const p = doc.getRoot().getChild( 0 );
  1058. doc.registerPostFixer( () => {
  1059. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1060. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker' ] );
  1061. } );
  1062. model.change( writer => {
  1063. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  1064. writer.addMarker( 'marker', {
  1065. range: writer.createRange(
  1066. writer.createPositionFromPath( p, [ 1 ] ),
  1067. writer.createPositionFromPath( p, [ 5 ] )
  1068. ),
  1069. usingOperation: false
  1070. } );
  1071. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  1072. } );
  1073. } );
  1074. it( 'should be up to date between post fixers', () => {
  1075. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  1076. const p = doc.getRoot().getChild( 0 );
  1077. doc.registerPostFixer( writer => {
  1078. writer.setAttribute( 'foo', 'biz', p.getChild( 0 ) );
  1079. writer.removeMarker( 'marker-1' );
  1080. writer.addMarker( 'marker-2', {
  1081. range: writer.createRange(
  1082. writer.createPositionFromPath( p, [ 1 ] ),
  1083. writer.createPositionFromPath( p, [ 5 ] )
  1084. ),
  1085. usingOperation: false
  1086. } );
  1087. } );
  1088. doc.registerPostFixer( () => {
  1089. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'biz' );
  1090. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker-2' ] );
  1091. } );
  1092. model.change( writer => {
  1093. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  1094. writer.addMarker( 'marker-1', {
  1095. range: writer.createRange(
  1096. writer.createPositionFromPath( p, [ 1 ] ),
  1097. writer.createPositionFromPath( p, [ 5 ] )
  1098. ),
  1099. usingOperation: false
  1100. } );
  1101. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  1102. } );
  1103. } );
  1104. it( 'should be up to date after post fixers (on `change` event)', done => {
  1105. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  1106. const p = doc.getRoot().getChild( 0 );
  1107. doc.on( 'change', () => {
  1108. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'biz' );
  1109. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker-2' ] );
  1110. done();
  1111. } );
  1112. doc.registerPostFixer( writer => {
  1113. writer.setAttribute( 'foo', 'biz', p.getChild( 0 ) );
  1114. writer.removeMarker( 'marker-1' );
  1115. writer.addMarker( 'marker-2', {
  1116. range: writer.createRange(
  1117. writer.createPositionFromPath( p, [ 1 ] ),
  1118. writer.createPositionFromPath( p, [ 5 ] )
  1119. ),
  1120. usingOperation: false
  1121. } );
  1122. } );
  1123. model.change( writer => {
  1124. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  1125. writer.addMarker( 'marker-1', {
  1126. range: writer.createRange(
  1127. writer.createPositionFromPath( p, [ 1 ] ),
  1128. writer.createPositionFromPath( p, [ 5 ] )
  1129. ),
  1130. usingOperation: false
  1131. } );
  1132. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  1133. } );
  1134. } );
  1135. } );
  1136. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  1137. // working well, without getting into complicated corner cases.
  1138. describe( 'after applying an operation should get updated and fire events', () => {
  1139. let spyRange;
  1140. beforeEach( () => {
  1141. root._removeChildren( 0, root.childCount );
  1142. root._insertChild( 0, [
  1143. new Element( 'p', [], new Text( 'abcdef' ) ),
  1144. new Element( 'p', [], new Text( 'foobar' ) ),
  1145. new Text( 'xyz' )
  1146. ] );
  1147. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  1148. spyRange = sinon.spy();
  1149. selection.on( 'change:range', spyRange );
  1150. } );
  1151. describe( 'InsertOperation', () => {
  1152. it( 'before selection', () => {
  1153. selection.on( 'change:range', ( evt, data ) => {
  1154. expect( data.directChange ).to.be.false;
  1155. } );
  1156. model.applyOperation(
  1157. new InsertOperation(
  1158. new Position( root, [ 0, 1 ] ),
  1159. 'xyz',
  1160. doc.version
  1161. )
  1162. );
  1163. const range = selection.getFirstRange();
  1164. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  1165. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1166. expect( spyRange.calledOnce ).to.be.true;
  1167. } );
  1168. it( 'inside selection', () => {
  1169. selection.on( 'change:range', ( evt, data ) => {
  1170. expect( data.directChange ).to.be.false;
  1171. } );
  1172. model.applyOperation(
  1173. new InsertOperation(
  1174. new Position( root, [ 1, 0 ] ),
  1175. 'xyz',
  1176. doc.version
  1177. )
  1178. );
  1179. const range = selection.getFirstRange();
  1180. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1181. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  1182. expect( spyRange.calledOnce ).to.be.true;
  1183. } );
  1184. } );
  1185. describe( 'MoveOperation', () => {
  1186. it( 'move range from before a selection', () => {
  1187. selection.on( 'change:range', ( evt, data ) => {
  1188. expect( data.directChange ).to.be.false;
  1189. } );
  1190. model.applyOperation(
  1191. new MoveOperation(
  1192. new Position( root, [ 0, 0 ] ),
  1193. 2,
  1194. new Position( root, [ 2 ] ),
  1195. doc.version
  1196. )
  1197. );
  1198. const range = selection.getFirstRange();
  1199. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  1200. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1201. expect( spyRange.calledOnce ).to.be.true;
  1202. } );
  1203. it( 'moved into before a selection', () => {
  1204. selection.on( 'change:range', ( evt, data ) => {
  1205. expect( data.directChange ).to.be.false;
  1206. } );
  1207. model.applyOperation(
  1208. new MoveOperation(
  1209. new Position( root, [ 2 ] ),
  1210. 2,
  1211. new Position( root, [ 0, 0 ] ),
  1212. doc.version
  1213. )
  1214. );
  1215. const range = selection.getFirstRange();
  1216. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  1217. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1218. expect( spyRange.calledOnce ).to.be.true;
  1219. } );
  1220. it( 'move range from inside of selection', () => {
  1221. selection.on( 'change:range', ( evt, data ) => {
  1222. expect( data.directChange ).to.be.false;
  1223. } );
  1224. model.applyOperation(
  1225. new MoveOperation(
  1226. new Position( root, [ 1, 0 ] ),
  1227. 2,
  1228. new Position( root, [ 2 ] ),
  1229. doc.version
  1230. )
  1231. );
  1232. const range = selection.getFirstRange();
  1233. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1234. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  1235. expect( spyRange.calledOnce ).to.be.true;
  1236. } );
  1237. it( 'moved range intersects with selection', () => {
  1238. selection.on( 'change:range', ( evt, data ) => {
  1239. expect( data.directChange ).to.be.false;
  1240. } );
  1241. model.applyOperation(
  1242. new MoveOperation(
  1243. new Position( root, [ 1, 3 ] ),
  1244. 2,
  1245. new Position( root, [ 4 ] ),
  1246. doc.version
  1247. )
  1248. );
  1249. const range = selection.getFirstRange();
  1250. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1251. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  1252. expect( spyRange.calledOnce ).to.be.true;
  1253. } );
  1254. it( 'split inside selection (do not break selection)', () => {
  1255. selection.on( 'change:range', ( evt, data ) => {
  1256. expect( data.directChange ).to.be.false;
  1257. } );
  1258. const batch = new Batch();
  1259. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  1260. batch.addOperation( splitOperation );
  1261. model.applyOperation( splitOperation );
  1262. const range = selection.getFirstRange();
  1263. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1264. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  1265. expect( spyRange.calledOnce ).to.be.true;
  1266. } );
  1267. } );
  1268. describe( 'AttributeOperation', () => {
  1269. it( 'changed range includes selection anchor', () => {
  1270. const spyAttribute = sinon.spy();
  1271. selection.on( 'change:attribute', spyAttribute );
  1272. selection.on( 'change:attribute', ( evt, data ) => {
  1273. expect( data.directChange ).to.be.false;
  1274. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  1275. } );
  1276. model.applyOperation(
  1277. new AttributeOperation(
  1278. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1279. 'foo',
  1280. null,
  1281. 'bar',
  1282. doc.version
  1283. )
  1284. );
  1285. // Attributes are auto updated on document change.
  1286. model.change( () => {} );
  1287. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1288. expect( spyAttribute.calledOnce ).to.be.true;
  1289. } );
  1290. it( 'should not overwrite previously set attributes', () => {
  1291. selection._setAttribute( 'foo', 'xyz' );
  1292. const spyAttribute = sinon.spy();
  1293. selection.on( 'change:attribute', spyAttribute );
  1294. model.applyOperation(
  1295. new AttributeOperation(
  1296. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1297. 'foo',
  1298. null,
  1299. 'bar',
  1300. doc.version
  1301. )
  1302. );
  1303. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1304. expect( spyAttribute.called ).to.be.false;
  1305. } );
  1306. it( 'should not overwrite previously set attributes with same values', () => {
  1307. selection._setAttribute( 'foo', 'xyz' );
  1308. const spyAttribute = sinon.spy();
  1309. selection.on( 'change:attribute', spyAttribute );
  1310. model.applyOperation(
  1311. new AttributeOperation(
  1312. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1313. 'foo',
  1314. null,
  1315. 'xyz',
  1316. doc.version
  1317. )
  1318. );
  1319. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1320. expect( spyAttribute.called ).to.be.false;
  1321. } );
  1322. it( 'should not overwrite previously removed attributes', () => {
  1323. selection._setAttribute( 'foo', 'xyz' );
  1324. selection._removeAttribute( 'foo' );
  1325. const spyAttribute = sinon.spy();
  1326. selection.on( 'change:attribute', spyAttribute );
  1327. model.applyOperation(
  1328. new AttributeOperation(
  1329. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1330. 'foo',
  1331. null,
  1332. 'bar',
  1333. doc.version
  1334. )
  1335. );
  1336. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  1337. expect( spyAttribute.called ).to.be.false;
  1338. } );
  1339. } );
  1340. describe( 'MoveOperation to graveyard', () => {
  1341. it( 'fix selection range if it ends up in graveyard - collapsed selection', () => {
  1342. selection._setTo( new Position( root, [ 1, 3 ] ) );
  1343. model.applyOperation(
  1344. new MoveOperation(
  1345. new Position( root, [ 1, 2 ] ),
  1346. 2,
  1347. new Position( doc.graveyard, [ 0 ] ),
  1348. doc.version
  1349. )
  1350. );
  1351. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1352. } );
  1353. it( 'fix selection range if it ends up in graveyard - text from non-collapsed selection is moved', () => {
  1354. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  1355. model.applyOperation(
  1356. new MoveOperation(
  1357. new Position( root, [ 1, 2 ] ),
  1358. 2,
  1359. new Position( doc.graveyard, [ 0 ] ),
  1360. doc.version
  1361. )
  1362. );
  1363. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1364. } );
  1365. it( 'fix selection range if it ends up in graveyard - parent of non-collapsed selection is moved', () => {
  1366. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1367. model.applyOperation(
  1368. new MoveOperation(
  1369. new Position( root, [ 1 ] ),
  1370. 2,
  1371. new Position( doc.graveyard, [ 0 ] ),
  1372. doc.version
  1373. )
  1374. );
  1375. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1376. } );
  1377. it( 'fix selection range if it ends up in graveyard - whole content removed', () => {
  1378. model.applyOperation(
  1379. new MoveOperation(
  1380. new Position( root, [ 0 ] ),
  1381. 3,
  1382. new Position( doc.graveyard, [ 0 ] ),
  1383. doc.version
  1384. )
  1385. );
  1386. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1387. model.applyOperation(
  1388. new InsertOperation(
  1389. new Position( root, [ 0 ] ),
  1390. new Element( 'p' ),
  1391. doc.version
  1392. )
  1393. );
  1394. // Now it's clear that it's the default range.
  1395. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1396. } );
  1397. it( 'handles multi-range selection in a text node by merging it into one range (resulting in a collapsed range)', () => {
  1398. const ranges = [
  1399. new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ),
  1400. new Range( new Position( root, [ 1, 3 ] ), new Position( root, [ 1, 4 ] ) )
  1401. ];
  1402. selection._setTo( ranges );
  1403. model.applyOperation(
  1404. new MoveOperation(
  1405. new Position( root, [ 1, 0 ] ),
  1406. 5,
  1407. new Position( doc.graveyard, [ 0 ] ),
  1408. doc.version
  1409. )
  1410. );
  1411. expect( selection.rangeCount ).to.equal( 1 );
  1412. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 0 ] );
  1413. expect( selection.getLastPosition().path ).to.deep.equal( [ 1, 0 ] );
  1414. } );
  1415. it( 'handles multi-range selection on object nodes by merging it into one range (resulting in a non-collapsed range)', () => {
  1416. model.schema.register( 'outer', {
  1417. isObject: true
  1418. } );
  1419. model.schema.register( 'inner', {
  1420. isObject: true,
  1421. allowIn: 'outer'
  1422. } );
  1423. root._removeChildren( 0, root.childCount );
  1424. root._insertChild( 0, [
  1425. new Element( 'outer', [], [ new Element( 'inner' ), new Element( 'inner' ), new Element( 'inner' ) ] )
  1426. ] );
  1427. const ranges = [
  1428. new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 1 ] ) ),
  1429. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 2 ] ) )
  1430. ];
  1431. selection._setTo( ranges );
  1432. model.applyOperation(
  1433. new MoveOperation(
  1434. new Position( root, [ 0, 0 ] ),
  1435. 2,
  1436. new Position( doc.graveyard, [ 0 ] ),
  1437. doc.version
  1438. )
  1439. );
  1440. expect( selection.rangeCount ).to.equal( 1 );
  1441. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1442. expect( selection.getLastPosition().path ).to.deep.equal( [ 0, 1 ] );
  1443. } );
  1444. it( 'should not fix the selection if not all ranges were removed', () => {
  1445. const ranges = [
  1446. new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ),
  1447. new Range( new Position( root, [ 1, 3 ] ), new Position( root, [ 1, 4 ] ) )
  1448. ];
  1449. selection._setTo( ranges );
  1450. model.applyOperation(
  1451. new MoveOperation(
  1452. new Position( root, [ 1, 1 ] ),
  1453. 1,
  1454. new Position( doc.graveyard, [ 0 ] ),
  1455. doc.version
  1456. )
  1457. );
  1458. expect( selection.rangeCount ).to.equal( 1 );
  1459. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1460. expect( selection.getLastPosition().path ).to.deep.equal( [ 1, 3 ] );
  1461. } );
  1462. } );
  1463. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1464. root._removeChildren( 0, root.childCount );
  1465. root._insertChild( 0, [
  1466. new Element( 'p', [], new Text( 'abcdef' ) ),
  1467. new Element( 'p', [], new Text( 'foobar' ) ),
  1468. new Text( 'xyz #2' )
  1469. ] );
  1470. selection._setTo( [
  1471. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1472. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1473. ] );
  1474. spyRange = sinon.spy();
  1475. selection.on( 'change:range', spyRange );
  1476. model.applyOperation(
  1477. new InsertOperation(
  1478. new Position( root, [ 0 ] ),
  1479. 'xyz #1',
  1480. doc.version
  1481. )
  1482. );
  1483. expect( spyRange.calledOnce ).to.be.true;
  1484. } );
  1485. } );
  1486. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1487. root._removeChildren( 0, root.childCount );
  1488. root._appendChild( '\uD83D\uDCA9' );
  1489. expectToThrowCKEditorError( () => {
  1490. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1491. }, /document-selection-wrong-position/, model );
  1492. expectToThrowCKEditorError( () => {
  1493. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1494. }, /document-selection-wrong-position/, model );
  1495. } );
  1496. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1497. root._removeChildren( 0, root.childCount );
  1498. root._appendChild( 'foo̻̐ͩbar' );
  1499. expectToThrowCKEditorError( () => {
  1500. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1501. }, /document-selection-wrong-position/, model );
  1502. expectToThrowCKEditorError( () => {
  1503. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1504. }, /document-selection-wrong-position/, model );
  1505. expectToThrowCKEditorError( () => {
  1506. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1507. }, /document-selection-wrong-position/, model );
  1508. expectToThrowCKEditorError( () => {
  1509. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1510. }, /document-selection-wrong-position/, model );
  1511. expectToThrowCKEditorError( () => {
  1512. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1513. }, /document-selection-wrong-position/, model );
  1514. expectToThrowCKEditorError( () => {
  1515. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1516. }, /document-selection-wrong-position/, model );
  1517. } );
  1518. } );