8
0

documentselection.js 52 KB

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