documentselection.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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. it( 'should fire change:marker event when selection markers change', () => {
  364. model.change( writer => {
  365. const spy = sinon.spy();
  366. model.document.selection.on( 'change:marker', spy );
  367. writer.setSelection( writer.createRange(
  368. writer.createPositionFromPath( root, [ 2, 1 ] ),
  369. writer.createPositionFromPath( root, [ 2, 2 ] )
  370. ) );
  371. expect( spy.called ).to.be.false;
  372. writer.addMarker( 'marker-1', {
  373. range: writer.createRange(
  374. writer.createPositionFromPath( root, [ 2, 0 ] ),
  375. writer.createPositionFromPath( root, [ 2, 5 ] )
  376. ),
  377. usingOperation: false
  378. } );
  379. expect( spy.calledOnce ).to.be.true;
  380. writer.addMarker( 'marker-2', {
  381. range: writer.createRange(
  382. writer.createPositionFromPath( root, [ 2, 0 ] ),
  383. writer.createPositionFromPath( root, [ 2, 3 ] )
  384. ),
  385. usingOperation: false
  386. } );
  387. expect( spy.calledTwice ).to.be.true;
  388. spy.resetHistory();
  389. writer.setSelection( writer.createPositionFromPath( root, [ 2, 6 ] ) );
  390. expect( spy.calledOnce ).to.be.true;
  391. writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
  392. expect( spy.calledTwice ).to.be.true;
  393. } );
  394. } );
  395. } );
  396. describe( 'destroy()', () => {
  397. it( 'should unbind all events', () => {
  398. selection._setTo( [ range, liveRange ] );
  399. const ranges = Array.from( selection._selection._ranges );
  400. sinon.spy( ranges[ 0 ], 'detach' );
  401. sinon.spy( ranges[ 1 ], 'detach' );
  402. selection.destroy();
  403. expect( ranges[ 0 ].detach.called ).to.be.true;
  404. expect( ranges[ 1 ].detach.called ).to.be.true;
  405. } );
  406. } );
  407. describe( 'getFirstRange()', () => {
  408. it( 'should return default range if no ranges were added', () => {
  409. const firstRange = selection.getFirstRange();
  410. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  411. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  412. } );
  413. } );
  414. describe( 'getLastRange()', () => {
  415. it( 'should return default range if no ranges were added', () => {
  416. const lastRange = selection.getLastRange();
  417. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  418. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  419. } );
  420. } );
  421. describe( 'getSelectedElement()', () => {
  422. it( 'should return selected element', () => {
  423. selection._setTo( liveRange );
  424. const p = root.getChild( 0 );
  425. expect( selection.getSelectedElement() ).to.equal( p );
  426. } );
  427. } );
  428. describe( 'is()', () => {
  429. it( 'should return true for selection', () => {
  430. expect( selection.is( 'selection' ) ).to.be.true;
  431. expect( selection.is( 'model:selection' ) ).to.be.true;
  432. } );
  433. it( 'should return true for documentSelection', () => {
  434. expect( selection.is( 'documentSelection' ) ).to.be.true;
  435. expect( selection.is( 'model:documentSelection' ) ).to.be.true;
  436. } );
  437. it( 'should return false for other values', () => {
  438. expect( selection.is( 'node' ) ).to.be.false;
  439. expect( selection.is( 'model:node' ) ).to.be.false;
  440. expect( selection.is( 'text' ) ).to.be.false;
  441. expect( selection.is( 'textProxy' ) ).to.be.false;
  442. expect( selection.is( 'element' ) ).to.be.false;
  443. expect( selection.is( 'element', 'paragraph' ) ).to.be.false;
  444. expect( selection.is( 'rootElement' ) ).to.be.false;
  445. expect( selection.is( 'view:selection' ) ).to.be.false;
  446. expect( selection.is( 'view:documentSelection' ) ).to.be.false;
  447. } );
  448. } );
  449. describe( '_setTo() - set collapsed at', () => {
  450. it( 'detaches all existing ranges', () => {
  451. selection._setTo( [ range, liveRange ] );
  452. const spy = sinon.spy( LiveRange.prototype, 'detach' );
  453. selection._setTo( root, 0 );
  454. expect( spy.calledTwice ).to.be.true;
  455. } );
  456. } );
  457. describe( '_setFocus()', () => {
  458. it( 'modifies default range', () => {
  459. const startPos = selection.getFirstPosition();
  460. const endPos = Position._createAt( root, 'end' );
  461. selection._setFocus( endPos );
  462. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  463. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  464. } );
  465. it( 'detaches the range it replaces', () => {
  466. const startPos = Position._createAt( root, 1 );
  467. const endPos = Position._createAt( root, 2 );
  468. const newEndPos = Position._createAt( root, 4 );
  469. const spy = sinon.spy( LiveRange.prototype, 'detach' );
  470. selection._setTo( new Range( startPos, endPos ) );
  471. selection._setFocus( newEndPos );
  472. expect( spy.calledOnce ).to.be.true;
  473. } );
  474. it( 'refreshes attributes', () => {
  475. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  476. selection._setFocus( Position._createAt( root, 1 ) );
  477. expect( spy.called ).to.be.true;
  478. } );
  479. } );
  480. describe( '_setTo() - remove all ranges', () => {
  481. let spy, ranges;
  482. beforeEach( () => {
  483. selection._setTo( [ liveRange, range ] );
  484. spy = sinon.spy();
  485. selection.on( 'change:range', spy );
  486. ranges = Array.from( selection._selection._ranges );
  487. sinon.spy( ranges[ 0 ], 'detach' );
  488. sinon.spy( ranges[ 1 ], 'detach' );
  489. selection._setTo( null );
  490. } );
  491. it( 'should remove all stored ranges (and reset to default range)', () => {
  492. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  493. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  494. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  495. } );
  496. it( 'should detach ranges', () => {
  497. expect( ranges[ 0 ].detach.called ).to.be.true;
  498. expect( ranges[ 1 ].detach.called ).to.be.true;
  499. } );
  500. it( 'should refresh attributes', () => {
  501. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  502. selection._setTo( null );
  503. expect( spy.called ).to.be.true;
  504. } );
  505. } );
  506. describe( '_setTo()', () => {
  507. it( 'should throw an error when range is invalid', () => {
  508. expectToThrowCKEditorError( () => {
  509. selection._setTo( [ { invalid: 'range' } ] );
  510. }, /model-selection-set-ranges-not-range/, model );
  511. } );
  512. it( 'should do nothing when trying to set selection to the graveyard', () => {
  513. // Catches the 'Trying to add a Range that is in the graveyard root. Range rejected.' warning in the CK_DEBUG mode.
  514. sinon.stub( console, 'warn' );
  515. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  516. selection._setTo( range );
  517. expect( selection._ranges ).to.deep.equal( [] );
  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. expectToThrowCKEditorError( () => {
  867. selection._restoreGravity( 'foo' );
  868. }, /^document-selection-gravity-wrong-restore/, model );
  869. // Already restored
  870. expectToThrowCKEditorError( () => {
  871. selection._restoreGravity( overrideUid );
  872. }, /^document-selection-gravity-wrong-restore/, model );
  873. // No UID
  874. expectToThrowCKEditorError( () => {
  875. selection._restoreGravity();
  876. }, /^document-selection-gravity-wrong-restore/, model );
  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. // https://github.com/ckeditor/ckeditor5-engine/issues/1673
  899. describe( 'refreshing selection data', () => {
  900. it( 'should be up to date before post fixers', () => {
  901. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  902. const p = doc.getRoot().getChild( 0 );
  903. doc.registerPostFixer( () => {
  904. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  905. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker' ] );
  906. } );
  907. model.change( writer => {
  908. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  909. writer.addMarker( 'marker', {
  910. range: writer.createRange(
  911. writer.createPositionFromPath( p, [ 1 ] ),
  912. writer.createPositionFromPath( p, [ 5 ] )
  913. ),
  914. usingOperation: false
  915. } );
  916. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  917. } );
  918. } );
  919. it( 'should be up to date between post fixers', () => {
  920. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  921. const p = doc.getRoot().getChild( 0 );
  922. doc.registerPostFixer( writer => {
  923. writer.setAttribute( 'foo', 'biz', p.getChild( 0 ) );
  924. writer.removeMarker( 'marker-1' );
  925. writer.addMarker( 'marker-2', {
  926. range: writer.createRange(
  927. writer.createPositionFromPath( p, [ 1 ] ),
  928. writer.createPositionFromPath( p, [ 5 ] )
  929. ),
  930. usingOperation: false
  931. } );
  932. } );
  933. doc.registerPostFixer( () => {
  934. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'biz' );
  935. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker-2' ] );
  936. } );
  937. model.change( writer => {
  938. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  939. writer.addMarker( 'marker-1', {
  940. range: writer.createRange(
  941. writer.createPositionFromPath( p, [ 1 ] ),
  942. writer.createPositionFromPath( p, [ 5 ] )
  943. ),
  944. usingOperation: false
  945. } );
  946. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  947. } );
  948. } );
  949. it( 'should be up to date after post fixers (on `change` event)', done => {
  950. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  951. const p = doc.getRoot().getChild( 0 );
  952. doc.on( 'change', () => {
  953. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'biz' );
  954. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker-2' ] );
  955. done();
  956. } );
  957. doc.registerPostFixer( writer => {
  958. writer.setAttribute( 'foo', 'biz', p.getChild( 0 ) );
  959. writer.removeMarker( 'marker-1' );
  960. writer.addMarker( 'marker-2', {
  961. range: writer.createRange(
  962. writer.createPositionFromPath( p, [ 1 ] ),
  963. writer.createPositionFromPath( p, [ 5 ] )
  964. ),
  965. usingOperation: false
  966. } );
  967. } );
  968. model.change( writer => {
  969. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  970. writer.addMarker( 'marker-1', {
  971. range: writer.createRange(
  972. writer.createPositionFromPath( p, [ 1 ] ),
  973. writer.createPositionFromPath( p, [ 5 ] )
  974. ),
  975. usingOperation: false
  976. } );
  977. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  978. } );
  979. } );
  980. } );
  981. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  982. // working well, without getting into complicated corner cases.
  983. describe( 'after applying an operation should get updated and fire events', () => {
  984. let spyRange;
  985. beforeEach( () => {
  986. root._removeChildren( 0, root.childCount );
  987. root._insertChild( 0, [
  988. new Element( 'p', [], new Text( 'abcdef' ) ),
  989. new Element( 'p', [], new Text( 'foobar' ) ),
  990. new Text( 'xyz' )
  991. ] );
  992. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  993. spyRange = sinon.spy();
  994. selection.on( 'change:range', spyRange );
  995. } );
  996. describe( 'InsertOperation', () => {
  997. it( 'before selection', () => {
  998. selection.on( 'change:range', ( evt, data ) => {
  999. expect( data.directChange ).to.be.false;
  1000. } );
  1001. model.applyOperation(
  1002. new InsertOperation(
  1003. new Position( root, [ 0, 1 ] ),
  1004. 'xyz',
  1005. doc.version
  1006. )
  1007. );
  1008. const range = selection.getFirstRange();
  1009. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  1010. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1011. expect( spyRange.calledOnce ).to.be.true;
  1012. } );
  1013. it( 'inside selection', () => {
  1014. selection.on( 'change:range', ( evt, data ) => {
  1015. expect( data.directChange ).to.be.false;
  1016. } );
  1017. model.applyOperation(
  1018. new InsertOperation(
  1019. new Position( root, [ 1, 0 ] ),
  1020. 'xyz',
  1021. doc.version
  1022. )
  1023. );
  1024. const range = selection.getFirstRange();
  1025. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1026. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  1027. expect( spyRange.calledOnce ).to.be.true;
  1028. } );
  1029. } );
  1030. describe( 'MoveOperation', () => {
  1031. it( 'move range from before a selection', () => {
  1032. selection.on( 'change:range', ( evt, data ) => {
  1033. expect( data.directChange ).to.be.false;
  1034. } );
  1035. model.applyOperation(
  1036. new MoveOperation(
  1037. new Position( root, [ 0, 0 ] ),
  1038. 2,
  1039. new Position( root, [ 2 ] ),
  1040. doc.version
  1041. )
  1042. );
  1043. const range = selection.getFirstRange();
  1044. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  1045. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1046. expect( spyRange.calledOnce ).to.be.true;
  1047. } );
  1048. it( 'moved into before a selection', () => {
  1049. selection.on( 'change:range', ( evt, data ) => {
  1050. expect( data.directChange ).to.be.false;
  1051. } );
  1052. model.applyOperation(
  1053. new MoveOperation(
  1054. new Position( root, [ 2 ] ),
  1055. 2,
  1056. new Position( root, [ 0, 0 ] ),
  1057. doc.version
  1058. )
  1059. );
  1060. const range = selection.getFirstRange();
  1061. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  1062. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1063. expect( spyRange.calledOnce ).to.be.true;
  1064. } );
  1065. it( 'move range from inside of selection', () => {
  1066. selection.on( 'change:range', ( evt, data ) => {
  1067. expect( data.directChange ).to.be.false;
  1068. } );
  1069. model.applyOperation(
  1070. new MoveOperation(
  1071. new Position( root, [ 1, 0 ] ),
  1072. 2,
  1073. new Position( root, [ 2 ] ),
  1074. doc.version
  1075. )
  1076. );
  1077. const range = selection.getFirstRange();
  1078. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1079. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  1080. expect( spyRange.calledOnce ).to.be.true;
  1081. } );
  1082. it( 'moved range intersects with selection', () => {
  1083. selection.on( 'change:range', ( evt, data ) => {
  1084. expect( data.directChange ).to.be.false;
  1085. } );
  1086. model.applyOperation(
  1087. new MoveOperation(
  1088. new Position( root, [ 1, 3 ] ),
  1089. 2,
  1090. new Position( root, [ 4 ] ),
  1091. doc.version
  1092. )
  1093. );
  1094. const range = selection.getFirstRange();
  1095. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1096. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  1097. expect( spyRange.calledOnce ).to.be.true;
  1098. } );
  1099. it( 'split inside selection (do not break selection)', () => {
  1100. selection.on( 'change:range', ( evt, data ) => {
  1101. expect( data.directChange ).to.be.false;
  1102. } );
  1103. const batch = new Batch();
  1104. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  1105. batch.addOperation( splitOperation );
  1106. model.applyOperation( splitOperation );
  1107. const range = selection.getFirstRange();
  1108. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1109. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  1110. expect( spyRange.calledOnce ).to.be.true;
  1111. } );
  1112. } );
  1113. describe( 'AttributeOperation', () => {
  1114. it( 'changed range includes selection anchor', () => {
  1115. const spyAttribute = sinon.spy();
  1116. selection.on( 'change:attribute', spyAttribute );
  1117. selection.on( 'change:attribute', ( evt, data ) => {
  1118. expect( data.directChange ).to.be.false;
  1119. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  1120. } );
  1121. model.applyOperation(
  1122. new AttributeOperation(
  1123. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1124. 'foo',
  1125. null,
  1126. 'bar',
  1127. doc.version
  1128. )
  1129. );
  1130. // Attributes are auto updated on document change.
  1131. model.change( () => {} );
  1132. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1133. expect( spyAttribute.calledOnce ).to.be.true;
  1134. } );
  1135. it( 'should not overwrite previously set attributes', () => {
  1136. selection._setAttribute( 'foo', 'xyz' );
  1137. const spyAttribute = sinon.spy();
  1138. selection.on( 'change:attribute', spyAttribute );
  1139. model.applyOperation(
  1140. new AttributeOperation(
  1141. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1142. 'foo',
  1143. null,
  1144. 'bar',
  1145. doc.version
  1146. )
  1147. );
  1148. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1149. expect( spyAttribute.called ).to.be.false;
  1150. } );
  1151. it( 'should not overwrite previously set attributes with same values', () => {
  1152. selection._setAttribute( 'foo', 'xyz' );
  1153. const spyAttribute = sinon.spy();
  1154. selection.on( 'change:attribute', spyAttribute );
  1155. model.applyOperation(
  1156. new AttributeOperation(
  1157. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1158. 'foo',
  1159. null,
  1160. 'xyz',
  1161. doc.version
  1162. )
  1163. );
  1164. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1165. expect( spyAttribute.called ).to.be.false;
  1166. } );
  1167. it( 'should not overwrite previously removed attributes', () => {
  1168. selection._setAttribute( 'foo', 'xyz' );
  1169. selection._removeAttribute( 'foo' );
  1170. const spyAttribute = sinon.spy();
  1171. selection.on( 'change:attribute', spyAttribute );
  1172. model.applyOperation(
  1173. new AttributeOperation(
  1174. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1175. 'foo',
  1176. null,
  1177. 'bar',
  1178. doc.version
  1179. )
  1180. );
  1181. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  1182. expect( spyAttribute.called ).to.be.false;
  1183. } );
  1184. } );
  1185. describe( 'MoveOperation to graveyard', () => {
  1186. it( 'fix selection range if it ends up in graveyard #1', () => {
  1187. selection._setTo( new Position( root, [ 1, 3 ] ) );
  1188. model.applyOperation(
  1189. new MoveOperation(
  1190. new Position( root, [ 1, 2 ] ),
  1191. 2,
  1192. new Position( doc.graveyard, [ 0 ] ),
  1193. doc.version
  1194. )
  1195. );
  1196. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1197. } );
  1198. it( 'fix selection range if it ends up in graveyard #2', () => {
  1199. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  1200. model.applyOperation(
  1201. new MoveOperation(
  1202. new Position( root, [ 1, 2 ] ),
  1203. 2,
  1204. new Position( doc.graveyard, [ 0 ] ),
  1205. doc.version
  1206. )
  1207. );
  1208. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1209. } );
  1210. it( 'fix selection range if it ends up in graveyard #3', () => {
  1211. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1212. model.applyOperation(
  1213. new MoveOperation(
  1214. new Position( root, [ 1 ] ),
  1215. 2,
  1216. new Position( doc.graveyard, [ 0 ] ),
  1217. doc.version
  1218. )
  1219. );
  1220. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1221. } );
  1222. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  1223. model.applyOperation(
  1224. new MoveOperation(
  1225. new Position( root, [ 0 ] ),
  1226. 3,
  1227. new Position( doc.graveyard, [ 0 ] ),
  1228. doc.version
  1229. )
  1230. );
  1231. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1232. model.applyOperation(
  1233. new InsertOperation(
  1234. new Position( root, [ 0 ] ),
  1235. new Element( 'p' ),
  1236. doc.version
  1237. )
  1238. );
  1239. // Now it's clear that it's the default range.
  1240. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1241. } );
  1242. } );
  1243. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1244. root._removeChildren( 0, root.childCount );
  1245. root._insertChild( 0, [
  1246. new Element( 'p', [], new Text( 'abcdef' ) ),
  1247. new Element( 'p', [], new Text( 'foobar' ) ),
  1248. new Text( 'xyz #2' )
  1249. ] );
  1250. selection._setTo( [
  1251. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1252. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1253. ] );
  1254. spyRange = sinon.spy();
  1255. selection.on( 'change:range', spyRange );
  1256. model.applyOperation(
  1257. new InsertOperation(
  1258. new Position( root, [ 0 ] ),
  1259. 'xyz #1',
  1260. doc.version
  1261. )
  1262. );
  1263. expect( spyRange.calledOnce ).to.be.true;
  1264. } );
  1265. } );
  1266. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1267. root._removeChildren( 0, root.childCount );
  1268. root._appendChild( '\uD83D\uDCA9' );
  1269. expectToThrowCKEditorError( () => {
  1270. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1271. }, /document-selection-wrong-position/, model );
  1272. expectToThrowCKEditorError( () => {
  1273. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1274. }, /document-selection-wrong-position/, model );
  1275. } );
  1276. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1277. root._removeChildren( 0, root.childCount );
  1278. root._appendChild( 'foo̻̐ͩbar' );
  1279. expectToThrowCKEditorError( () => {
  1280. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1281. }, /document-selection-wrong-position/, model );
  1282. expectToThrowCKEditorError( () => {
  1283. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1284. }, /document-selection-wrong-position/, model );
  1285. expectToThrowCKEditorError( () => {
  1286. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1287. }, /document-selection-wrong-position/, model );
  1288. expectToThrowCKEditorError( () => {
  1289. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1290. }, /document-selection-wrong-position/, model );
  1291. expectToThrowCKEditorError( () => {
  1292. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1293. }, /document-selection-wrong-position/, model );
  1294. expectToThrowCKEditorError( () => {
  1295. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1296. }, /document-selection-wrong-position/, model );
  1297. } );
  1298. } );