documentselection.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Batch from '../../src/model/batch';
  7. import Element from '../../src/model/element';
  8. import Text from '../../src/model/text';
  9. import Range from '../../src/model/range';
  10. import Position from '../../src/model/position';
  11. import LiveRange from '../../src/model/liverange';
  12. import DocumentSelection from '../../src/model/documentselection';
  13. import InsertOperation from '../../src/model/operation/insertoperation';
  14. import MoveOperation from '../../src/model/operation/moveoperation';
  15. import AttributeOperation from '../../src/model/operation/attributeoperation';
  16. import SplitOperation from '../../src/model/operation/splitoperation';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  19. import count from '@ckeditor/ckeditor5-utils/src/count';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. import { setData, getData } from '../../src/dev-utils/model';
  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. } );
  402. it( 'should return true for documentSelection', () => {
  403. expect( selection.is( 'documentSelection' ) ).to.be.true;
  404. } );
  405. it( 'should return false for other values', () => {
  406. expect( selection.is( 'node' ) ).to.be.false;
  407. expect( selection.is( 'text' ) ).to.be.false;
  408. expect( selection.is( 'textProxy' ) ).to.be.false;
  409. expect( selection.is( 'element' ) ).to.be.false;
  410. expect( selection.is( 'rootElement' ) ).to.be.false;
  411. } );
  412. } );
  413. describe( '_refresh()', () => {
  414. it( 'should refresh selection markers', () => {
  415. model.change( writer => {
  416. writer.setSelection( writer.createRange(
  417. writer.createPositionFromPath( root, [ 2, 3 ] )
  418. ) );
  419. } );
  420. const range = model.createRange(
  421. model.createPositionFromPath( root, [ 2, 0 ] ),
  422. model.createPositionFromPath( root, [ 2, 6 ] )
  423. );
  424. model.markers._set( 'marker', range, false, false );
  425. expect( Array.from( selection.markers ) ).to.length( 0 );
  426. selection._refresh();
  427. expect( Array.from( selection.markers ) ).to.length( 1 );
  428. } );
  429. it( 'should refresh selection attributes', () => {
  430. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  431. model.change( writer => {
  432. writer.setSelection( writer.createRange(
  433. writer.createPositionFromPath( root, [ 2, 3 ] )
  434. ) );
  435. } );
  436. root.getChild( 2 ).getChild( 0 )._setAttribute( 'foo', 'bar' );
  437. expect( selection.hasAttribute( 'foo' ) ).to.equal( false );
  438. selection._refresh();
  439. expect( selection.hasAttribute( 'foo' ) ).to.equal( true );
  440. } );
  441. } );
  442. describe( '_setTo() - set collapsed at', () => {
  443. it( 'detaches all existing ranges', () => {
  444. selection._setTo( [ range, liveRange ] );
  445. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  446. selection._setTo( root, 0 );
  447. expect( spy.calledTwice ).to.be.true;
  448. } );
  449. } );
  450. describe( '_setFocus()', () => {
  451. it( 'modifies default range', () => {
  452. const startPos = selection.getFirstPosition();
  453. const endPos = Position._createAt( root, 'end' );
  454. selection._setFocus( endPos );
  455. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  456. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  457. } );
  458. it( 'detaches the range it replaces', () => {
  459. const startPos = Position._createAt( root, 1 );
  460. const endPos = Position._createAt( root, 2 );
  461. const newEndPos = Position._createAt( root, 4 );
  462. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  463. selection._setTo( new Range( startPos, endPos ) );
  464. selection._setFocus( newEndPos );
  465. expect( spy.calledOnce ).to.be.true;
  466. } );
  467. it( 'refreshes attributes', () => {
  468. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  469. selection._setFocus( Position._createAt( root, 1 ) );
  470. expect( spy.called ).to.be.true;
  471. } );
  472. } );
  473. describe( '_setTo() - remove all ranges', () => {
  474. let spy, ranges;
  475. beforeEach( () => {
  476. selection._setTo( [ liveRange, range ] );
  477. spy = sinon.spy();
  478. selection.on( 'change:range', spy );
  479. ranges = Array.from( selection._selection._ranges );
  480. sinon.spy( ranges[ 0 ], 'detach' );
  481. sinon.spy( ranges[ 1 ], 'detach' );
  482. selection._setTo( null );
  483. } );
  484. afterEach( () => {
  485. ranges[ 0 ].detach.restore();
  486. ranges[ 1 ].detach.restore();
  487. } );
  488. it( 'should remove all stored ranges (and reset to default range)', () => {
  489. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  490. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  491. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  492. } );
  493. it( 'should detach ranges', () => {
  494. expect( ranges[ 0 ].detach.called ).to.be.true;
  495. expect( ranges[ 1 ].detach.called ).to.be.true;
  496. } );
  497. it( 'should refresh attributes', () => {
  498. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  499. selection._setTo( null );
  500. expect( spy.called ).to.be.true;
  501. } );
  502. } );
  503. describe( '_setTo()', () => {
  504. it( 'should throw an error when range is invalid', () => {
  505. expect( () => {
  506. selection._setTo( [ { invalid: 'range' } ] );
  507. } ).to.throw( CKEditorError, /model-selection-set-ranges-not-range/ );
  508. } );
  509. it( 'should log a warning when trying to set selection to the graveyard', () => {
  510. // eslint-disable-next-line no-undef
  511. const warnStub = sinon.stub( console, 'warn' );
  512. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  513. selection._setTo( range );
  514. expect( warnStub.calledOnce ).to.equal( true );
  515. expect( warnStub.getCall( 0 ).args[ 0 ] ).to.match( /model-selection-range-in-graveyard/ );
  516. expect( selection._ranges ).to.deep.equal( [] );
  517. warnStub.restore();
  518. } );
  519. it( 'should detach removed ranges', () => {
  520. selection._setTo( [ liveRange, range ] );
  521. const oldRanges = Array.from( selection._selection._ranges );
  522. sinon.spy( oldRanges[ 0 ], 'detach' );
  523. sinon.spy( oldRanges[ 1 ], 'detach' );
  524. selection._setTo( [] );
  525. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  526. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  527. } );
  528. it( 'should refresh attributes', () => {
  529. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  530. selection._setTo( [ range ] );
  531. expect( spy.called ).to.be.true;
  532. } );
  533. // See #630.
  534. it( 'should refresh attributes – integration test for #630', () => {
  535. model.schema.extend( '$text', { allowIn: '$root' } );
  536. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  537. selection._setTo( [ Range._createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  538. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  539. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  540. expect( getData( model ) )
  541. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  542. } );
  543. } );
  544. describe( '_isStoreAttributeKey', () => {
  545. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  546. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  547. } );
  548. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  549. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  550. } );
  551. } );
  552. describe( 'attributes', () => {
  553. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  554. beforeEach( () => {
  555. root._removeChildren( 0, root.childCount );
  556. root._appendChild( [
  557. new Element( 'p', [], new Text( 'foobar' ) ),
  558. new Element( 'p', [], [] )
  559. ] );
  560. fullP = root.getChild( 0 );
  561. emptyP = root.getChild( 1 );
  562. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  563. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  564. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  565. // a root which wasn't empty (so the ranges didn't actually make much sense).
  566. expect( root.childCount ).to.equal( 2 );
  567. } );
  568. describe( '_setAttribute()', () => {
  569. it( 'should set attribute', () => {
  570. selection._setTo( [ rangeInEmptyP ] );
  571. selection._setAttribute( 'foo', 'bar' );
  572. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  573. } );
  574. } );
  575. describe( '_removeAttribute()', () => {
  576. it( 'should remove attribute set on the text fragment', () => {
  577. selection._setTo( [ rangeInFullP ] );
  578. selection._setAttribute( 'foo', 'bar' );
  579. selection._removeAttribute( 'foo' );
  580. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  581. } );
  582. it( 'should prevent auto update of the attribute even if attribute is not preset yet', () => {
  583. selection._setTo( new Position( root, [ 0, 1 ] ) );
  584. // Remove "foo" attribute that is not present in selection yet.
  585. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  586. selection._removeAttribute( 'foo' );
  587. // Trigger selecton auto update on document change. It should not get attribute from surrounding text;
  588. model.change( writer => {
  589. writer.setAttribute( 'foo', 'bar', Range._createIn( fullP ) );
  590. } );
  591. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  592. } );
  593. } );
  594. describe( '_getStoredAttributes()', () => {
  595. it( 'should return no values if there are no ranges in selection', () => {
  596. const values = Array.from( selection._getStoredAttributes() );
  597. expect( values ).to.deep.equal( [] );
  598. } );
  599. } );
  600. describe( 'are updated on a direct range change', () => {
  601. beforeEach( () => {
  602. root._insertChild( 0, [
  603. new Element( 'p', { p: true } ),
  604. new Text( 'a', { a: true } ),
  605. new Element( 'p', { p: true } ),
  606. new Text( 'b', { b: true } ),
  607. new Text( 'c', { c: true } ),
  608. new Element( 'p', [], [
  609. new Text( 'd', { d: true } )
  610. ] ),
  611. new Element( 'p', { p: true } ),
  612. new Text( 'e', { e: true } )
  613. ] );
  614. } );
  615. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  616. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  617. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  618. // Step into elements when looking for first character:
  619. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  620. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  621. } );
  622. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  623. // Take styles from character before selection.
  624. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  625. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  626. // If there are none,
  627. // Take styles from character after selection.
  628. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  629. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  630. // If there are none,
  631. // Look from the selection position to the beginning of node looking for character to take attributes from.
  632. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  633. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  634. // If there are none,
  635. // Look from the selection position to the end of node looking for character to take attributes from.
  636. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  637. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  638. // If there are no characters to copy attributes from, use stored attributes.
  639. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  640. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  641. } );
  642. it( 'should overwrite any previously set attributes', () => {
  643. selection._setTo( new Position( root, [ 5, 0 ] ) );
  644. selection._setAttribute( 'x', true );
  645. selection._setAttribute( 'y', true );
  646. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  647. selection._setTo( new Position( root, [ 1 ] ) );
  648. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  649. } );
  650. it( 'should fire change:attribute event', () => {
  651. const spy = sinon.spy();
  652. selection.on( 'change:attribute', spy );
  653. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  654. expect( spy.calledOnce ).to.be.true;
  655. } );
  656. it( 'should not fire change:attribute event if attributes did not change', () => {
  657. selection._setTo( new Position( root, [ 5, 0 ] ) );
  658. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  659. const spy = sinon.spy();
  660. selection.on( 'change:attribute', spy );
  661. selection._setTo( new Position( root, [ 5, 1 ] ) );
  662. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  663. expect( spy.called ).to.be.false;
  664. } );
  665. } );
  666. // #986
  667. describe( 'are not inherited from the inside of object elements', () => {
  668. beforeEach( () => {
  669. model.schema.register( 'image', {
  670. isObject: true
  671. } );
  672. model.schema.extend( 'image', { allowIn: '$root' } );
  673. model.schema.extend( 'image', { allowIn: '$block' } );
  674. model.schema.register( 'caption' );
  675. model.schema.extend( 'caption', { allowIn: 'image' } );
  676. model.schema.extend( '$text', {
  677. allowIn: [ 'image', 'caption' ],
  678. allowAttributes: 'bold'
  679. } );
  680. } );
  681. it( 'ignores attributes inside an object if selection contains that object', () => {
  682. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  683. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  684. } );
  685. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  686. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  687. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  688. } );
  689. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  690. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  691. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  692. } );
  693. it( 'reads attributes from text even if the selection contains an object', () => {
  694. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  695. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  696. } );
  697. it( 'reads attributes when the entire selection inside an object', () => {
  698. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  699. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  700. } );
  701. it( 'stops reading attributes if selection starts with an object', () => {
  702. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  703. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  704. } );
  705. } );
  706. describe( 'parent element\'s attributes', () => {
  707. it( 'are set using a normal batch', () => {
  708. const batchTypes = [];
  709. model.on( 'applyOperation', ( event, args ) => {
  710. const operation = args[ 0 ];
  711. const batch = operation.batch;
  712. batchTypes.push( batch.type );
  713. } );
  714. selection._setTo( [ rangeInEmptyP ] );
  715. model.change( writer => {
  716. writer.setSelectionAttribute( 'foo', 'bar' );
  717. } );
  718. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  719. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  720. } );
  721. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  722. // Dedupe batches by using a map (multiple change events will be fired).
  723. const batchTypes = new Map();
  724. selection._setTo( rangeInEmptyP );
  725. selection._setAttribute( 'foo', 'bar' );
  726. selection._setAttribute( 'abc', 'bar' );
  727. model.on( 'applyOperation', ( event, args ) => {
  728. const operation = args[ 0 ];
  729. const batch = operation.batch;
  730. batchTypes.set( batch, batch.type );
  731. } );
  732. model.change( writer => {
  733. writer.insertText( 'x', rangeInEmptyP.start );
  734. } );
  735. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  736. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  737. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  738. } );
  739. it( 'are removed when any content is moved into', () => {
  740. selection._setTo( rangeInEmptyP );
  741. selection._setAttribute( 'foo', 'bar' );
  742. model.change( writer => {
  743. writer.move( writer.createRangeOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  744. } );
  745. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  746. } );
  747. it( 'are removed when containing element is merged with a non-empty element', () => {
  748. const emptyP2 = new Element( 'p', null, 'x' );
  749. root._appendChild( emptyP2 );
  750. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  751. emptyP2._setAttribute( fooStoreAttrKey, 'bar' );
  752. model.change( writer => {
  753. // <emptyP>{}<emptyP2>
  754. writer.merge( writer.createPositionAfter( emptyP ) );
  755. } );
  756. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  757. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  758. } );
  759. it( 'are removed even when there is no selection in it', () => {
  760. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  761. selection._setTo( [ rangeInFullP ] );
  762. model.change( writer => {
  763. writer.insertText( 'x', rangeInEmptyP.start );
  764. } );
  765. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  766. } );
  767. it( 'uses model change to clear attributes', () => {
  768. selection._setTo( [ rangeInEmptyP ] );
  769. model.change( writer => {
  770. writer.setSelectionAttribute( 'foo', 'bar' );
  771. writer.insertText( 'x', rangeInEmptyP.start );
  772. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  773. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  774. } );
  775. // When the dust settles, `emptyP` should not have the attribute.
  776. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  777. } );
  778. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  779. const emptyP2 = new Element( 'p', null );
  780. root._appendChild( emptyP2 );
  781. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  782. emptyP2._setAttribute( abcStoreAttrKey, 'bar' );
  783. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  784. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  785. model.change( writer => {
  786. // <emptyP>{}<emptyP2>
  787. writer.merge( writer.createPositionAfter( emptyP ) );
  788. } );
  789. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  790. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  791. } );
  792. // Rename and some other operations don't specify range in doc#change event.
  793. // So let's see if there's no crash or something.
  794. it( 'are not removed on rename', () => {
  795. model.change( writer => {
  796. writer.setSelection( rangeInEmptyP );
  797. writer.setSelectionAttribute( 'foo', 'bar' );
  798. } );
  799. sinon.spy( model, 'enqueueChange' );
  800. model.change( writer => {
  801. writer.rename( emptyP, 'pnew' );
  802. } );
  803. expect( model.enqueueChange.called ).to.be.false;
  804. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  805. } );
  806. } );
  807. } );
  808. describe( '_overrideGravity()', () => {
  809. beforeEach( () => {
  810. model.schema.extend( '$text', {
  811. allowIn: '$root'
  812. } );
  813. } );
  814. it( 'should return the UID', () => {
  815. const overrideUidA = selection._overrideGravity();
  816. const overrideUidB = selection._overrideGravity();
  817. expect( overrideUidA ).to.be.a( 'string' );
  818. expect( overrideUidB ).to.be.a( 'string' );
  819. expect( overrideUidA ).to.not.equal( overrideUidB );
  820. } );
  821. it( 'should mark gravity as overridden', () => {
  822. expect( selection.isGravityOverridden ).to.false;
  823. selection._overrideGravity();
  824. expect( selection.isGravityOverridden ).to.true;
  825. } );
  826. it( 'should not inherit attributes from node before the caret', () => {
  827. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  828. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  829. selection._overrideGravity();
  830. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  831. } );
  832. it( 'should inherit attributes from node after the caret', () => {
  833. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  834. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  835. selection._overrideGravity();
  836. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  837. } );
  838. it( 'should not retain attributes that are set explicitly', () => {
  839. setData( model, '<$text italic="true">foo[]</$text>' );
  840. selection._setAttribute( 'bold', true );
  841. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  842. selection._overrideGravity();
  843. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [] );
  844. } );
  845. it( 'should retain overridden until selection will not change range by a direct change', () => {
  846. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  847. selection._overrideGravity();
  848. // Changed range but not directly.
  849. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  850. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  851. // Changed range directly.
  852. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  853. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  854. } );
  855. } );
  856. describe( '_restoreGravity()', () => {
  857. beforeEach( () => {
  858. model.schema.extend( '$text', {
  859. allowIn: '$root'
  860. } );
  861. } );
  862. it( 'should throw when a wrong, already restored, or no UID provided', () => {
  863. const overrideUid = selection._overrideGravity();
  864. selection._restoreGravity( overrideUid );
  865. // Wrong UID
  866. expect( () => {
  867. selection._restoreGravity( 'foo' );
  868. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  869. // Already restored
  870. expect( () => {
  871. selection._restoreGravity( overrideUid );
  872. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  873. // No UID
  874. expect( () => {
  875. selection._restoreGravity();
  876. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  877. } );
  878. it( 'should revert default gravity when is overridden', () => {
  879. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  880. const overrideUid = selection._overrideGravity();
  881. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  882. expect( selection.isGravityOverridden ).to.true;
  883. selection._restoreGravity( overrideUid );
  884. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  885. expect( selection.isGravityOverridden ).to.false;
  886. } );
  887. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  888. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  889. const overrideUidA = selection._overrideGravity();
  890. const overrideUidB = selection._overrideGravity();
  891. expect( selection.isGravityOverridden ).to.true;
  892. selection._restoreGravity( overrideUidA );
  893. expect( selection.isGravityOverridden ).to.true;
  894. selection._restoreGravity( overrideUidB );
  895. expect( selection.isGravityOverridden ).to.false;
  896. } );
  897. } );
  898. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  899. // working well, without getting into complicated corner cases.
  900. describe( 'after applying an operation should get updated and fire events', () => {
  901. let spyRange;
  902. beforeEach( () => {
  903. root._removeChildren( 0, root.childCount );
  904. root._insertChild( 0, [
  905. new Element( 'p', [], new Text( 'abcdef' ) ),
  906. new Element( 'p', [], new Text( 'foobar' ) ),
  907. new Text( 'xyz' )
  908. ] );
  909. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  910. spyRange = sinon.spy();
  911. selection.on( 'change:range', spyRange );
  912. } );
  913. describe( 'InsertOperation', () => {
  914. it( 'before selection', () => {
  915. selection.on( 'change:range', ( evt, data ) => {
  916. expect( data.directChange ).to.be.false;
  917. } );
  918. model.applyOperation(
  919. new InsertOperation(
  920. new Position( root, [ 0, 1 ] ),
  921. 'xyz',
  922. doc.version
  923. )
  924. );
  925. const range = selection.getFirstRange();
  926. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  927. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  928. expect( spyRange.calledOnce ).to.be.true;
  929. } );
  930. it( 'inside selection', () => {
  931. selection.on( 'change:range', ( evt, data ) => {
  932. expect( data.directChange ).to.be.false;
  933. } );
  934. model.applyOperation(
  935. new InsertOperation(
  936. new Position( root, [ 1, 0 ] ),
  937. 'xyz',
  938. doc.version
  939. )
  940. );
  941. const range = selection.getFirstRange();
  942. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  943. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  944. expect( spyRange.calledOnce ).to.be.true;
  945. } );
  946. } );
  947. describe( 'MoveOperation', () => {
  948. it( 'move range from before a selection', () => {
  949. selection.on( 'change:range', ( evt, data ) => {
  950. expect( data.directChange ).to.be.false;
  951. } );
  952. model.applyOperation(
  953. new MoveOperation(
  954. new Position( root, [ 0, 0 ] ),
  955. 2,
  956. new Position( root, [ 2 ] ),
  957. doc.version
  958. )
  959. );
  960. const range = selection.getFirstRange();
  961. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  962. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  963. expect( spyRange.calledOnce ).to.be.true;
  964. } );
  965. it( 'moved into before a selection', () => {
  966. selection.on( 'change:range', ( evt, data ) => {
  967. expect( data.directChange ).to.be.false;
  968. } );
  969. model.applyOperation(
  970. new MoveOperation(
  971. new Position( root, [ 2 ] ),
  972. 2,
  973. new Position( root, [ 0, 0 ] ),
  974. doc.version
  975. )
  976. );
  977. const range = selection.getFirstRange();
  978. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  979. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  980. expect( spyRange.calledOnce ).to.be.true;
  981. } );
  982. it( 'move range from inside of selection', () => {
  983. selection.on( 'change:range', ( evt, data ) => {
  984. expect( data.directChange ).to.be.false;
  985. } );
  986. model.applyOperation(
  987. new MoveOperation(
  988. new Position( root, [ 1, 0 ] ),
  989. 2,
  990. new Position( root, [ 2 ] ),
  991. doc.version
  992. )
  993. );
  994. const range = selection.getFirstRange();
  995. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  996. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  997. expect( spyRange.calledOnce ).to.be.true;
  998. } );
  999. it( 'moved range intersects with selection', () => {
  1000. selection.on( 'change:range', ( evt, data ) => {
  1001. expect( data.directChange ).to.be.false;
  1002. } );
  1003. model.applyOperation(
  1004. new MoveOperation(
  1005. new Position( root, [ 1, 3 ] ),
  1006. 2,
  1007. new Position( root, [ 4 ] ),
  1008. doc.version
  1009. )
  1010. );
  1011. const range = selection.getFirstRange();
  1012. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1013. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  1014. expect( spyRange.calledOnce ).to.be.true;
  1015. } );
  1016. it( 'split inside selection (do not break selection)', () => {
  1017. selection.on( 'change:range', ( evt, data ) => {
  1018. expect( data.directChange ).to.be.false;
  1019. } );
  1020. const batch = new Batch();
  1021. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  1022. batch.addOperation( splitOperation );
  1023. model.applyOperation( splitOperation );
  1024. const range = selection.getFirstRange();
  1025. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1026. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  1027. expect( spyRange.calledOnce ).to.be.true;
  1028. } );
  1029. } );
  1030. describe( 'AttributeOperation', () => {
  1031. it( 'changed range includes selection anchor', () => {
  1032. const spyAttribute = sinon.spy();
  1033. selection.on( 'change:attribute', spyAttribute );
  1034. selection.on( 'change:attribute', ( evt, data ) => {
  1035. expect( data.directChange ).to.be.false;
  1036. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  1037. } );
  1038. model.applyOperation(
  1039. new AttributeOperation(
  1040. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1041. 'foo',
  1042. null,
  1043. 'bar',
  1044. doc.version
  1045. )
  1046. );
  1047. // Attributes are auto updated on document change.
  1048. model.change( () => {} );
  1049. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1050. expect( spyAttribute.calledOnce ).to.be.true;
  1051. } );
  1052. it( 'should not overwrite previously set attributes', () => {
  1053. selection._setAttribute( 'foo', 'xyz' );
  1054. const spyAttribute = sinon.spy();
  1055. selection.on( 'change:attribute', spyAttribute );
  1056. model.applyOperation(
  1057. new AttributeOperation(
  1058. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1059. 'foo',
  1060. null,
  1061. 'bar',
  1062. doc.version
  1063. )
  1064. );
  1065. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1066. expect( spyAttribute.called ).to.be.false;
  1067. } );
  1068. it( 'should not overwrite previously set attributes with same values', () => {
  1069. selection._setAttribute( 'foo', 'xyz' );
  1070. const spyAttribute = sinon.spy();
  1071. selection.on( 'change:attribute', spyAttribute );
  1072. model.applyOperation(
  1073. new AttributeOperation(
  1074. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1075. 'foo',
  1076. null,
  1077. 'xyz',
  1078. doc.version
  1079. )
  1080. );
  1081. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1082. expect( spyAttribute.called ).to.be.false;
  1083. } );
  1084. it( 'should not overwrite previously removed attributes', () => {
  1085. selection._setAttribute( 'foo', 'xyz' );
  1086. selection._removeAttribute( 'foo' );
  1087. const spyAttribute = sinon.spy();
  1088. selection.on( 'change:attribute', spyAttribute );
  1089. model.applyOperation(
  1090. new AttributeOperation(
  1091. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1092. 'foo',
  1093. null,
  1094. 'bar',
  1095. doc.version
  1096. )
  1097. );
  1098. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  1099. expect( spyAttribute.called ).to.be.false;
  1100. } );
  1101. } );
  1102. describe( 'MoveOperation to graveyard', () => {
  1103. it( 'fix selection range if it ends up in graveyard #1', () => {
  1104. selection._setTo( new Position( root, [ 1, 3 ] ) );
  1105. model.applyOperation(
  1106. new MoveOperation(
  1107. new Position( root, [ 1, 2 ] ),
  1108. 2,
  1109. new Position( doc.graveyard, [ 0 ] ),
  1110. doc.version
  1111. )
  1112. );
  1113. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1114. } );
  1115. it( 'fix selection range if it ends up in graveyard #2', () => {
  1116. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  1117. model.applyOperation(
  1118. new MoveOperation(
  1119. new Position( root, [ 1, 2 ] ),
  1120. 2,
  1121. new Position( doc.graveyard, [ 0 ] ),
  1122. doc.version
  1123. )
  1124. );
  1125. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1126. } );
  1127. it( 'fix selection range if it ends up in graveyard #3', () => {
  1128. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1129. model.applyOperation(
  1130. new MoveOperation(
  1131. new Position( root, [ 1 ] ),
  1132. 2,
  1133. new Position( doc.graveyard, [ 0 ] ),
  1134. doc.version
  1135. )
  1136. );
  1137. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1138. } );
  1139. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  1140. model.applyOperation(
  1141. new MoveOperation(
  1142. new Position( root, [ 0 ] ),
  1143. 3,
  1144. new Position( doc.graveyard, [ 0 ] ),
  1145. doc.version
  1146. )
  1147. );
  1148. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1149. model.applyOperation(
  1150. new InsertOperation(
  1151. new Position( root, [ 0 ] ),
  1152. new Element( 'p' ),
  1153. doc.version
  1154. )
  1155. );
  1156. // Now it's clear that it's the default range.
  1157. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1158. } );
  1159. } );
  1160. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1161. root._removeChildren( 0, root.childCount );
  1162. root._insertChild( 0, [
  1163. new Element( 'p', [], new Text( 'abcdef' ) ),
  1164. new Element( 'p', [], new Text( 'foobar' ) ),
  1165. new Text( 'xyz #2' )
  1166. ] );
  1167. selection._setTo( [
  1168. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1169. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1170. ] );
  1171. spyRange = sinon.spy();
  1172. selection.on( 'change:range', spyRange );
  1173. model.applyOperation(
  1174. new InsertOperation(
  1175. new Position( root, [ 0 ] ),
  1176. 'xyz #1',
  1177. doc.version
  1178. )
  1179. );
  1180. expect( spyRange.calledOnce ).to.be.true;
  1181. } );
  1182. } );
  1183. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1184. root._removeChildren( 0, root.childCount );
  1185. root._appendChild( '\uD83D\uDCA9' );
  1186. expect( () => {
  1187. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1188. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1189. expect( () => {
  1190. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1191. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1192. } );
  1193. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1194. root._removeChildren( 0, root.childCount );
  1195. root._appendChild( 'foo̻̐ͩbar' );
  1196. expect( () => {
  1197. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1198. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1199. expect( () => {
  1200. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1201. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1202. expect( () => {
  1203. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1204. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1205. expect( () => {
  1206. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1207. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1208. expect( () => {
  1209. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1210. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1211. expect( () => {
  1212. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1213. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1214. } );
  1215. } );