documentselection.js 58 KB

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