8
0

documentselection.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Batch from '../../src/model/batch';
  7. import Element from '../../src/model/element';
  8. import Text from '../../src/model/text';
  9. import Range from '../../src/model/range';
  10. import Position from '../../src/model/position';
  11. import LiveRange from '../../src/model/liverange';
  12. import DocumentSelection from '../../src/model/documentselection';
  13. import InsertOperation from '../../src/model/operation/insertoperation';
  14. import MoveOperation from '../../src/model/operation/moveoperation';
  15. import AttributeOperation from '../../src/model/operation/attributeoperation';
  16. import SplitOperation from '../../src/model/operation/splitoperation';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  19. import count from '@ckeditor/ckeditor5-utils/src/count';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. import { setData, getData } from '../../src/dev-utils/model';
  22. describe( 'DocumentSelection', () => {
  23. let model, doc, root, selection, liveRange, range;
  24. testUtils.createSinonSandbox();
  25. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  26. const abcStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'abc' );
  27. beforeEach( () => {
  28. model = new Model();
  29. doc = model.document;
  30. root = doc.createRoot();
  31. root._appendChild( [
  32. new Element( 'p' ),
  33. new Element( 'p' ),
  34. new Element( 'p', [], new Text( 'foobar' ) ),
  35. new Element( 'p' ),
  36. new Element( 'p' ),
  37. new Element( 'p' ),
  38. new Element( 'p', [], new Text( 'foobar' ) )
  39. ] );
  40. selection = doc.selection;
  41. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. model.schema.register( 'widget', {
  43. isObject: true
  44. } );
  45. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  46. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  47. } );
  48. afterEach( () => {
  49. model.destroy();
  50. liveRange.detach();
  51. } );
  52. describe( 'default range', () => {
  53. it( 'should go to the first editable element', () => {
  54. const ranges = Array.from( selection.getRanges() );
  55. expect( ranges.length ).to.equal( 1 );
  56. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  57. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  58. expect( selection ).to.have.property( 'isBackward', false );
  59. } );
  60. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  61. model = new Model();
  62. doc = model.document;
  63. root = doc.createRoot();
  64. root._insertChild( 0, new Text( 'foobar' ) );
  65. selection = doc.selection;
  66. const ranges = Array.from( selection.getRanges() );
  67. expect( ranges.length ).to.equal( 1 );
  68. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  69. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  70. expect( selection ).to.have.property( 'isBackward', false );
  71. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  72. } );
  73. it( 'should skip element when you can not put selection', () => {
  74. model = new Model();
  75. doc = model.document;
  76. root = doc.createRoot();
  77. root._insertChild( 0, [
  78. new Element( 'img' ),
  79. new Element( 'p', [], new Text( 'foobar' ) )
  80. ] );
  81. model.schema.register( 'img' );
  82. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  83. selection = doc.selection;
  84. const ranges = Array.from( selection.getRanges() );
  85. expect( ranges.length ).to.equal( 1 );
  86. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  87. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  88. expect( selection ).to.have.property( 'isBackward', false );
  89. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  90. } );
  91. } );
  92. describe( 'isCollapsed', () => {
  93. it( 'should be true for the default range (in text position)', () => {
  94. expect( selection.isCollapsed ).to.be.true;
  95. } );
  96. it( 'should be false for the default range (object selection) ', () => {
  97. root._insertChild( 0, new Element( 'widget' ) );
  98. expect( selection.isCollapsed ).to.be.false;
  99. } );
  100. it( 'should back off to the default algorithm if selection has ranges', () => {
  101. selection._setTo( range );
  102. expect( selection.isCollapsed ).to.be.false;
  103. } );
  104. } );
  105. describe( 'anchor', () => {
  106. it( 'should equal the default range\'s start (in text position)', () => {
  107. const expectedPos = new Position( root, [ 0, 0 ] );
  108. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  109. } );
  110. it( 'should equal the default range\'s start (object selection)', () => {
  111. root._insertChild( 0, new Element( 'widget' ) );
  112. const expectedPos = new Position( root, [ 0 ] );
  113. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  114. } );
  115. it( 'should back off to the default algorithm if selection has ranges', () => {
  116. selection._setTo( range );
  117. expect( selection.anchor.isEqual( range.start ) ).to.be.true;
  118. } );
  119. } );
  120. describe( 'focus', () => {
  121. it( 'should equal the default range\'s end (in text position)', () => {
  122. const expectedPos = new Position( root, [ 0, 0 ] );
  123. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  124. } );
  125. it( 'should equal the default range\'s end (object selection)', () => {
  126. root._insertChild( 0, new Element( 'widget' ) );
  127. const expectedPos = new Position( root, [ 1 ] );
  128. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  129. expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
  130. } );
  131. it( 'should back off to the default algorithm if selection has ranges', () => {
  132. selection._setTo( range );
  133. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  134. } );
  135. } );
  136. describe( 'rangeCount', () => {
  137. it( 'should return proper range count', () => {
  138. expect( selection.rangeCount ).to.equal( 1 );
  139. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  140. expect( selection.rangeCount ).to.equal( 1 );
  141. selection._setTo( [
  142. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ),
  143. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) )
  144. ] );
  145. expect( selection.rangeCount ).to.equal( 2 );
  146. } );
  147. } );
  148. describe( 'hasOwnRange', () => {
  149. it( 'should return true if selection has any ranges set', () => {
  150. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  151. expect( selection.hasOwnRange ).to.be.true;
  152. } );
  153. it( 'should return false if selection has a default range', () => {
  154. expect( selection.hasOwnRange ).to.be.false;
  155. } );
  156. } );
  157. describe( 'markers', () => {
  158. it( 'should implement #markers collection', () => {
  159. expect( selection.markers ).to.instanceof( Collection );
  160. expect( selection.markers ).to.length( 0 );
  161. } );
  162. it( 'should add markers to the collection when selection is inside the marker range', () => {
  163. model.change( writer => {
  164. writer.setSelection( writer.createRange(
  165. writer.createPositionFromPath( root, [ 2, 2 ] ),
  166. writer.createPositionFromPath( root, [ 2, 4 ] )
  167. ) );
  168. writer.addMarker( 'marker-1', {
  169. range: writer.createRange(
  170. writer.createPositionFromPath( root, [ 0, 0 ] ),
  171. writer.createPositionFromPath( root, [ 2, 2 ] )
  172. ),
  173. usingOperation: false
  174. } );
  175. writer.addMarker( 'marker-2', {
  176. range: writer.createRange(
  177. writer.createPositionFromPath( root, [ 2, 2 ] ),
  178. writer.createPositionFromPath( root, [ 2, 4 ] )
  179. ),
  180. usingOperation: false
  181. } );
  182. writer.addMarker( 'marker-3', {
  183. range: writer.createRange(
  184. writer.createPositionFromPath( root, [ 2, 1 ] ),
  185. writer.createPositionFromPath( root, [ 2, 5 ] )
  186. ),
  187. usingOperation: false
  188. } );
  189. writer.addMarker( 'marker-4', {
  190. range: writer.createRange(
  191. writer.createPositionFromPath( root, [ 2, 4 ] ),
  192. writer.createPositionFromPath( root, [ 3, 0 ] )
  193. ),
  194. usingOperation: false
  195. } );
  196. } );
  197. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-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. } );
  285. describe( 'destroy()', () => {
  286. it( 'should unbind all events', () => {
  287. selection._setTo( [ range, liveRange ] );
  288. const ranges = Array.from( selection._selection._ranges );
  289. sinon.spy( ranges[ 0 ], 'detach' );
  290. sinon.spy( ranges[ 1 ], 'detach' );
  291. selection.destroy();
  292. expect( ranges[ 0 ].detach.called ).to.be.true;
  293. expect( ranges[ 1 ].detach.called ).to.be.true;
  294. ranges[ 0 ].detach.restore();
  295. ranges[ 1 ].detach.restore();
  296. } );
  297. } );
  298. describe( 'getFirstRange()', () => {
  299. it( 'should return default range if no ranges were added', () => {
  300. const firstRange = selection.getFirstRange();
  301. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  302. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  303. } );
  304. } );
  305. describe( 'getLastRange()', () => {
  306. it( 'should return default range if no ranges were added', () => {
  307. const lastRange = selection.getLastRange();
  308. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  309. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  310. } );
  311. } );
  312. describe( 'getSelectedElement()', () => {
  313. it( 'should return selected element', () => {
  314. selection._setTo( liveRange );
  315. const p = root.getChild( 0 );
  316. expect( selection.getSelectedElement() ).to.equal( p );
  317. } );
  318. } );
  319. describe( '_setTo() - set collapsed at', () => {
  320. it( 'detaches all existing ranges', () => {
  321. selection._setTo( [ range, liveRange ] );
  322. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  323. selection._setTo( root, 0 );
  324. expect( spy.calledTwice ).to.be.true;
  325. } );
  326. } );
  327. describe( '_setFocus()', () => {
  328. it( 'modifies default range', () => {
  329. const startPos = selection.getFirstPosition();
  330. const endPos = Position._createAt( root, 'end' );
  331. selection._setFocus( endPos );
  332. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  333. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  334. } );
  335. it( 'detaches the range it replaces', () => {
  336. const startPos = Position._createAt( root, 1 );
  337. const endPos = Position._createAt( root, 2 );
  338. const newEndPos = Position._createAt( root, 4 );
  339. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  340. selection._setTo( new Range( startPos, endPos ) );
  341. selection._setFocus( newEndPos );
  342. expect( spy.calledOnce ).to.be.true;
  343. } );
  344. it( 'refreshes attributes', () => {
  345. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  346. selection._setFocus( Position._createAt( root, 1 ) );
  347. expect( spy.called ).to.be.true;
  348. } );
  349. } );
  350. describe( '_setTo() - remove all ranges', () => {
  351. let spy, ranges;
  352. beforeEach( () => {
  353. selection._setTo( [ liveRange, range ] );
  354. spy = sinon.spy();
  355. selection.on( 'change:range', spy );
  356. ranges = Array.from( selection._selection._ranges );
  357. sinon.spy( ranges[ 0 ], 'detach' );
  358. sinon.spy( ranges[ 1 ], 'detach' );
  359. selection._setTo( null );
  360. } );
  361. afterEach( () => {
  362. ranges[ 0 ].detach.restore();
  363. ranges[ 1 ].detach.restore();
  364. } );
  365. it( 'should remove all stored ranges (and reset to default range)', () => {
  366. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  367. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  368. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  369. } );
  370. it( 'should detach ranges', () => {
  371. expect( ranges[ 0 ].detach.called ).to.be.true;
  372. expect( ranges[ 1 ].detach.called ).to.be.true;
  373. } );
  374. it( 'should refresh attributes', () => {
  375. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  376. selection._setTo( null );
  377. expect( spy.called ).to.be.true;
  378. } );
  379. } );
  380. describe( '_setTo()', () => {
  381. it( 'should throw an error when range is invalid', () => {
  382. expect( () => {
  383. selection._setTo( [ { invalid: 'range' } ] );
  384. } ).to.throw( CKEditorError, /model-selection-set-ranges-not-range/ );
  385. } );
  386. it( 'should log a warning when trying to set selection to the graveyard', () => {
  387. // eslint-disable-next-line no-undef
  388. const warnStub = sinon.stub( console, 'warn' );
  389. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  390. selection._setTo( range );
  391. expect( warnStub.calledOnce ).to.equal( true );
  392. expect( warnStub.getCall( 0 ).args[ 0 ] ).to.match( /model-selection-range-in-graveyard/ );
  393. expect( selection._ranges ).to.deep.equal( [] );
  394. warnStub.restore();
  395. } );
  396. it( 'should detach removed ranges', () => {
  397. selection._setTo( [ liveRange, range ] );
  398. const oldRanges = Array.from( selection._selection._ranges );
  399. sinon.spy( oldRanges[ 0 ], 'detach' );
  400. sinon.spy( oldRanges[ 1 ], 'detach' );
  401. selection._setTo( [] );
  402. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  403. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  404. } );
  405. it( 'should refresh attributes', () => {
  406. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  407. selection._setTo( [ range ] );
  408. expect( spy.called ).to.be.true;
  409. } );
  410. // See #630.
  411. it( 'should refresh attributes – integration test for #630', () => {
  412. model.schema.extend( '$text', { allowIn: '$root' } );
  413. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  414. selection._setTo( [ Range._createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  415. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  416. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  417. expect( getData( model ) )
  418. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  419. } );
  420. } );
  421. describe( '_isStoreAttributeKey', () => {
  422. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  423. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  424. } );
  425. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  426. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  427. } );
  428. } );
  429. describe( 'attributes', () => {
  430. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  431. beforeEach( () => {
  432. root._removeChildren( 0, root.childCount );
  433. root._appendChild( [
  434. new Element( 'p', [], new Text( 'foobar' ) ),
  435. new Element( 'p', [], [] )
  436. ] );
  437. fullP = root.getChild( 0 );
  438. emptyP = root.getChild( 1 );
  439. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  440. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  441. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  442. // a root which wasn't empty (so the ranges didn't actually make much sense).
  443. expect( root.childCount ).to.equal( 2 );
  444. } );
  445. describe( '_setAttribute()', () => {
  446. it( 'should set attribute', () => {
  447. selection._setTo( [ rangeInEmptyP ] );
  448. selection._setAttribute( 'foo', 'bar' );
  449. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  450. } );
  451. } );
  452. describe( '_removeAttribute()', () => {
  453. it( 'should remove attribute set on the text fragment', () => {
  454. selection._setTo( [ rangeInFullP ] );
  455. selection._setAttribute( 'foo', 'bar' );
  456. selection._removeAttribute( 'foo' );
  457. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  458. } );
  459. it( 'should prevent auto update of the attribute even if attribute is not preset yet', () => {
  460. selection._setTo( new Position( root, [ 0, 1 ] ) );
  461. // Remove "foo" attribute that is not present in selection yet.
  462. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  463. selection._removeAttribute( 'foo' );
  464. // Trigger selecton auto update on document change. It should not get attribute from surrounding text;
  465. model.change( writer => {
  466. writer.setAttribute( 'foo', 'bar', Range._createIn( fullP ) );
  467. } );
  468. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  469. } );
  470. } );
  471. describe( '_getStoredAttributes()', () => {
  472. it( 'should return no values if there are no ranges in selection', () => {
  473. const values = Array.from( selection._getStoredAttributes() );
  474. expect( values ).to.deep.equal( [] );
  475. } );
  476. } );
  477. describe( 'are updated on a direct range change', () => {
  478. beforeEach( () => {
  479. root._insertChild( 0, [
  480. new Element( 'p', { p: true } ),
  481. new Text( 'a', { a: true } ),
  482. new Element( 'p', { p: true } ),
  483. new Text( 'b', { b: true } ),
  484. new Text( 'c', { c: true } ),
  485. new Element( 'p', [], [
  486. new Text( 'd', { d: true } )
  487. ] ),
  488. new Element( 'p', { p: true } ),
  489. new Text( 'e', { e: true } )
  490. ] );
  491. } );
  492. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  493. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  494. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  495. // Step into elements when looking for first character:
  496. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  497. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  498. } );
  499. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  500. // Take styles from character before selection.
  501. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  502. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  503. // If there are none,
  504. // Take styles from character after selection.
  505. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  506. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  507. // If there are none,
  508. // Look from the selection position to the beginning of node looking for character to take attributes from.
  509. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  510. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  511. // If there are none,
  512. // Look from the selection position to the end of node looking for character to take attributes from.
  513. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  514. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  515. // If there are no characters to copy attributes from, use stored attributes.
  516. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  517. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  518. } );
  519. it( 'should overwrite any previously set attributes', () => {
  520. selection._setTo( new Position( root, [ 5, 0 ] ) );
  521. selection._setAttribute( 'x', true );
  522. selection._setAttribute( 'y', true );
  523. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  524. selection._setTo( new Position( root, [ 1 ] ) );
  525. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  526. } );
  527. it( 'should fire change:attribute event', () => {
  528. const spy = sinon.spy();
  529. selection.on( 'change:attribute', spy );
  530. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  531. expect( spy.calledOnce ).to.be.true;
  532. } );
  533. it( 'should not fire change:attribute event if attributes did not change', () => {
  534. selection._setTo( new Position( root, [ 5, 0 ] ) );
  535. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  536. const spy = sinon.spy();
  537. selection.on( 'change:attribute', spy );
  538. selection._setTo( new Position( root, [ 5, 1 ] ) );
  539. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  540. expect( spy.called ).to.be.false;
  541. } );
  542. } );
  543. // #986
  544. describe( 'are not inherited from the inside of object elements', () => {
  545. beforeEach( () => {
  546. model.schema.register( 'image', {
  547. isObject: true
  548. } );
  549. model.schema.extend( 'image', { allowIn: '$root' } );
  550. model.schema.extend( 'image', { allowIn: '$block' } );
  551. model.schema.register( 'caption' );
  552. model.schema.extend( 'caption', { allowIn: 'image' } );
  553. model.schema.extend( '$text', {
  554. allowIn: [ 'image', 'caption' ],
  555. allowAttributes: 'bold'
  556. } );
  557. } );
  558. it( 'ignores attributes inside an object if selection contains that object', () => {
  559. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  560. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  561. } );
  562. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  563. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  564. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  565. } );
  566. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  567. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  568. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  569. } );
  570. it( 'reads attributes from text even if the selection contains an object', () => {
  571. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  572. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  573. } );
  574. it( 'reads attributes when the entire selection inside an object', () => {
  575. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  576. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  577. } );
  578. it( 'stops reading attributes if selection starts with an object', () => {
  579. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  580. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  581. } );
  582. } );
  583. describe( 'parent element\'s attributes', () => {
  584. it( 'are set using a normal batch', () => {
  585. const batchTypes = [];
  586. model.on( 'applyOperation', ( event, args ) => {
  587. const operation = args[ 0 ];
  588. const batch = operation.batch;
  589. batchTypes.push( batch.type );
  590. } );
  591. selection._setTo( [ rangeInEmptyP ] );
  592. model.change( writer => {
  593. writer.setSelectionAttribute( 'foo', 'bar' );
  594. } );
  595. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  596. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  597. } );
  598. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  599. // Dedupe batches by using a map (multiple change events will be fired).
  600. const batchTypes = new Map();
  601. selection._setTo( rangeInEmptyP );
  602. selection._setAttribute( 'foo', 'bar' );
  603. selection._setAttribute( 'abc', 'bar' );
  604. model.on( 'applyOperation', ( event, args ) => {
  605. const operation = args[ 0 ];
  606. const batch = operation.batch;
  607. batchTypes.set( batch, batch.type );
  608. } );
  609. model.change( writer => {
  610. writer.insertText( 'x', rangeInEmptyP.start );
  611. } );
  612. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  613. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  614. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  615. } );
  616. it( 'are removed when any content is moved into', () => {
  617. selection._setTo( rangeInEmptyP );
  618. selection._setAttribute( 'foo', 'bar' );
  619. model.change( writer => {
  620. writer.move( writer.createRangeOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  621. } );
  622. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  623. } );
  624. it( 'are removed when containing element is merged with a non-empty element', () => {
  625. const emptyP2 = new Element( 'p', null, 'x' );
  626. root._appendChild( emptyP2 );
  627. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  628. emptyP2._setAttribute( fooStoreAttrKey, 'bar' );
  629. model.change( writer => {
  630. // <emptyP>{}<emptyP2>
  631. writer.merge( writer.createPositionAfter( emptyP ) );
  632. } );
  633. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  634. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  635. } );
  636. it( 'are removed even when there is no selection in it', () => {
  637. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  638. selection._setTo( [ rangeInFullP ] );
  639. model.change( writer => {
  640. writer.insertText( 'x', rangeInEmptyP.start );
  641. } );
  642. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  643. } );
  644. it( 'uses model change to clear attributes', () => {
  645. selection._setTo( [ rangeInEmptyP ] );
  646. model.change( writer => {
  647. writer.setSelectionAttribute( 'foo', 'bar' );
  648. writer.insertText( 'x', rangeInEmptyP.start );
  649. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  650. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  651. } );
  652. // When the dust settles, `emptyP` should not have the attribute.
  653. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  654. } );
  655. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  656. const emptyP2 = new Element( 'p', null );
  657. root._appendChild( emptyP2 );
  658. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  659. emptyP2._setAttribute( abcStoreAttrKey, 'bar' );
  660. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  661. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  662. model.change( writer => {
  663. // <emptyP>{}<emptyP2>
  664. writer.merge( writer.createPositionAfter( emptyP ) );
  665. } );
  666. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  667. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  668. } );
  669. // Rename and some other operations don't specify range in doc#change event.
  670. // So let's see if there's no crash or something.
  671. it( 'are not removed on rename', () => {
  672. model.change( writer => {
  673. writer.setSelection( rangeInEmptyP );
  674. writer.setSelectionAttribute( 'foo', 'bar' );
  675. } );
  676. sinon.spy( model, 'enqueueChange' );
  677. model.change( writer => {
  678. writer.rename( emptyP, 'pnew' );
  679. } );
  680. expect( model.enqueueChange.called ).to.be.false;
  681. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  682. } );
  683. } );
  684. } );
  685. describe( '_overrideGravity()', () => {
  686. beforeEach( () => {
  687. model.schema.extend( '$text', {
  688. allowIn: '$root'
  689. } );
  690. } );
  691. it( 'should return the UID', () => {
  692. const overrideUidA = selection._overrideGravity();
  693. const overrideUidB = selection._overrideGravity();
  694. expect( overrideUidA ).to.be.a( 'string' );
  695. expect( overrideUidB ).to.be.a( 'string' );
  696. expect( overrideUidA ).to.not.equal( overrideUidB );
  697. } );
  698. it( 'should mark gravity as overridden', () => {
  699. expect( selection.isGravityOverridden ).to.false;
  700. selection._overrideGravity();
  701. expect( selection.isGravityOverridden ).to.true;
  702. } );
  703. it( 'should not inherit attributes from node before the caret', () => {
  704. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  705. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  706. selection._overrideGravity();
  707. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  708. } );
  709. it( 'should inherit attributes from node after the caret', () => {
  710. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  711. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  712. selection._overrideGravity();
  713. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  714. } );
  715. it( 'should not retain attributes that are set explicitly', () => {
  716. setData( model, '<$text italic="true">foo[]</$text>' );
  717. selection._setAttribute( 'bold', true );
  718. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  719. selection._overrideGravity();
  720. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [] );
  721. } );
  722. it( 'should retain overridden until selection will not change range by a direct change', () => {
  723. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  724. selection._overrideGravity();
  725. // Changed range but not directly.
  726. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  727. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  728. // Changed range directly.
  729. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  730. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  731. } );
  732. } );
  733. describe( '_restoreGravity()', () => {
  734. beforeEach( () => {
  735. model.schema.extend( '$text', {
  736. allowIn: '$root'
  737. } );
  738. } );
  739. it( 'should throw when a wrong, already restored, or no UID provided', () => {
  740. const overrideUid = selection._overrideGravity();
  741. selection._restoreGravity( overrideUid );
  742. // Wrong UID
  743. expect( () => {
  744. selection._restoreGravity( 'foo' );
  745. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  746. // Already restored
  747. expect( () => {
  748. selection._restoreGravity( overrideUid );
  749. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  750. // No UID
  751. expect( () => {
  752. selection._restoreGravity();
  753. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  754. } );
  755. it( 'should revert default gravity when is overridden', () => {
  756. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  757. const overrideUid = selection._overrideGravity();
  758. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  759. expect( selection.isGravityOverridden ).to.true;
  760. selection._restoreGravity( overrideUid );
  761. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  762. expect( selection.isGravityOverridden ).to.false;
  763. } );
  764. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  765. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  766. const overrideUidA = selection._overrideGravity();
  767. const overrideUidB = selection._overrideGravity();
  768. expect( selection.isGravityOverridden ).to.true;
  769. selection._restoreGravity( overrideUidA );
  770. expect( selection.isGravityOverridden ).to.true;
  771. selection._restoreGravity( overrideUidB );
  772. expect( selection.isGravityOverridden ).to.false;
  773. } );
  774. } );
  775. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  776. // working well, without getting into complicated corner cases.
  777. describe( 'after applying an operation should get updated and fire events', () => {
  778. let spyRange;
  779. beforeEach( () => {
  780. root._removeChildren( 0, root.childCount );
  781. root._insertChild( 0, [
  782. new Element( 'p', [], new Text( 'abcdef' ) ),
  783. new Element( 'p', [], new Text( 'foobar' ) ),
  784. new Text( 'xyz' )
  785. ] );
  786. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  787. spyRange = sinon.spy();
  788. selection.on( 'change:range', spyRange );
  789. } );
  790. describe( 'InsertOperation', () => {
  791. it( 'before selection', () => {
  792. selection.on( 'change:range', ( evt, data ) => {
  793. expect( data.directChange ).to.be.false;
  794. } );
  795. model.applyOperation(
  796. new InsertOperation(
  797. new Position( root, [ 0, 1 ] ),
  798. 'xyz',
  799. doc.version
  800. )
  801. );
  802. const range = selection.getFirstRange();
  803. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  804. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  805. expect( spyRange.calledOnce ).to.be.true;
  806. } );
  807. it( 'inside selection', () => {
  808. selection.on( 'change:range', ( evt, data ) => {
  809. expect( data.directChange ).to.be.false;
  810. } );
  811. model.applyOperation(
  812. new InsertOperation(
  813. new Position( root, [ 1, 0 ] ),
  814. 'xyz',
  815. doc.version
  816. )
  817. );
  818. const range = selection.getFirstRange();
  819. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  820. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  821. expect( spyRange.calledOnce ).to.be.true;
  822. } );
  823. } );
  824. describe( 'MoveOperation', () => {
  825. it( 'move range from before a selection', () => {
  826. selection.on( 'change:range', ( evt, data ) => {
  827. expect( data.directChange ).to.be.false;
  828. } );
  829. model.applyOperation(
  830. new MoveOperation(
  831. new Position( root, [ 0, 0 ] ),
  832. 2,
  833. new Position( root, [ 2 ] ),
  834. doc.version
  835. )
  836. );
  837. const range = selection.getFirstRange();
  838. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  839. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  840. expect( spyRange.calledOnce ).to.be.true;
  841. } );
  842. it( 'moved into before a selection', () => {
  843. selection.on( 'change:range', ( evt, data ) => {
  844. expect( data.directChange ).to.be.false;
  845. } );
  846. model.applyOperation(
  847. new MoveOperation(
  848. new Position( root, [ 2 ] ),
  849. 2,
  850. new Position( root, [ 0, 0 ] ),
  851. doc.version
  852. )
  853. );
  854. const range = selection.getFirstRange();
  855. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  856. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  857. expect( spyRange.calledOnce ).to.be.true;
  858. } );
  859. it( 'move range from inside of selection', () => {
  860. selection.on( 'change:range', ( evt, data ) => {
  861. expect( data.directChange ).to.be.false;
  862. } );
  863. model.applyOperation(
  864. new MoveOperation(
  865. new Position( root, [ 1, 0 ] ),
  866. 2,
  867. new Position( root, [ 2 ] ),
  868. doc.version
  869. )
  870. );
  871. const range = selection.getFirstRange();
  872. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  873. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  874. expect( spyRange.calledOnce ).to.be.true;
  875. } );
  876. it( 'moved range intersects with selection', () => {
  877. selection.on( 'change:range', ( evt, data ) => {
  878. expect( data.directChange ).to.be.false;
  879. } );
  880. model.applyOperation(
  881. new MoveOperation(
  882. new Position( root, [ 1, 3 ] ),
  883. 2,
  884. new Position( root, [ 4 ] ),
  885. doc.version
  886. )
  887. );
  888. const range = selection.getFirstRange();
  889. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  890. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  891. expect( spyRange.calledOnce ).to.be.true;
  892. } );
  893. it( 'split inside selection (do not break selection)', () => {
  894. selection.on( 'change:range', ( evt, data ) => {
  895. expect( data.directChange ).to.be.false;
  896. } );
  897. const batch = new Batch();
  898. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  899. batch.addOperation( splitOperation );
  900. model.applyOperation( splitOperation );
  901. const range = selection.getFirstRange();
  902. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  903. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  904. expect( spyRange.calledOnce ).to.be.true;
  905. } );
  906. } );
  907. describe( 'AttributeOperation', () => {
  908. it( 'changed range includes selection anchor', () => {
  909. const spyAttribute = sinon.spy();
  910. selection.on( 'change:attribute', spyAttribute );
  911. selection.on( 'change:attribute', ( evt, data ) => {
  912. expect( data.directChange ).to.be.false;
  913. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  914. } );
  915. model.applyOperation(
  916. new AttributeOperation(
  917. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  918. 'foo',
  919. null,
  920. 'bar',
  921. doc.version
  922. )
  923. );
  924. // Attributes are auto updated on document change.
  925. model.change( () => {} );
  926. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  927. expect( spyAttribute.calledOnce ).to.be.true;
  928. } );
  929. it( 'should not overwrite previously set attributes', () => {
  930. selection._setAttribute( 'foo', 'xyz' );
  931. const spyAttribute = sinon.spy();
  932. selection.on( 'change:attribute', spyAttribute );
  933. model.applyOperation(
  934. new AttributeOperation(
  935. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  936. 'foo',
  937. null,
  938. 'bar',
  939. doc.version
  940. )
  941. );
  942. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  943. expect( spyAttribute.called ).to.be.false;
  944. } );
  945. it( 'should not overwrite previously set attributes with same values', () => {
  946. selection._setAttribute( 'foo', 'xyz' );
  947. const spyAttribute = sinon.spy();
  948. selection.on( 'change:attribute', spyAttribute );
  949. model.applyOperation(
  950. new AttributeOperation(
  951. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  952. 'foo',
  953. null,
  954. 'xyz',
  955. doc.version
  956. )
  957. );
  958. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  959. expect( spyAttribute.called ).to.be.false;
  960. } );
  961. it( 'should not overwrite previously removed attributes', () => {
  962. selection._setAttribute( 'foo', 'xyz' );
  963. selection._removeAttribute( 'foo' );
  964. const spyAttribute = sinon.spy();
  965. selection.on( 'change:attribute', spyAttribute );
  966. model.applyOperation(
  967. new AttributeOperation(
  968. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  969. 'foo',
  970. null,
  971. 'bar',
  972. doc.version
  973. )
  974. );
  975. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  976. expect( spyAttribute.called ).to.be.false;
  977. } );
  978. } );
  979. describe( 'MoveOperation to graveyard', () => {
  980. it( 'fix selection range if it ends up in graveyard #1', () => {
  981. selection._setTo( new Position( root, [ 1, 3 ] ) );
  982. model.applyOperation(
  983. new MoveOperation(
  984. new Position( root, [ 1, 2 ] ),
  985. 2,
  986. new Position( doc.graveyard, [ 0 ] ),
  987. doc.version
  988. )
  989. );
  990. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  991. } );
  992. it( 'fix selection range if it ends up in graveyard #2', () => {
  993. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  994. model.applyOperation(
  995. new MoveOperation(
  996. new Position( root, [ 1, 2 ] ),
  997. 2,
  998. new Position( doc.graveyard, [ 0 ] ),
  999. doc.version
  1000. )
  1001. );
  1002. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1003. } );
  1004. it( 'fix selection range if it ends up in graveyard #3', () => {
  1005. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1006. model.applyOperation(
  1007. new MoveOperation(
  1008. new Position( root, [ 1 ] ),
  1009. 2,
  1010. new Position( doc.graveyard, [ 0 ] ),
  1011. doc.version
  1012. )
  1013. );
  1014. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1015. } );
  1016. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  1017. model.applyOperation(
  1018. new MoveOperation(
  1019. new Position( root, [ 0 ] ),
  1020. 3,
  1021. new Position( doc.graveyard, [ 0 ] ),
  1022. doc.version
  1023. )
  1024. );
  1025. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1026. model.applyOperation(
  1027. new InsertOperation(
  1028. new Position( root, [ 0 ] ),
  1029. new Element( 'p' ),
  1030. doc.version
  1031. )
  1032. );
  1033. // Now it's clear that it's the default range.
  1034. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1035. } );
  1036. } );
  1037. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1038. root._removeChildren( 0, root.childCount );
  1039. root._insertChild( 0, [
  1040. new Element( 'p', [], new Text( 'abcdef' ) ),
  1041. new Element( 'p', [], new Text( 'foobar' ) ),
  1042. new Text( 'xyz #2' )
  1043. ] );
  1044. selection._setTo( [
  1045. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1046. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1047. ] );
  1048. spyRange = sinon.spy();
  1049. selection.on( 'change:range', spyRange );
  1050. model.applyOperation(
  1051. new InsertOperation(
  1052. new Position( root, [ 0 ] ),
  1053. 'xyz #1',
  1054. doc.version
  1055. )
  1056. );
  1057. expect( spyRange.calledOnce ).to.be.true;
  1058. } );
  1059. } );
  1060. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1061. root._removeChildren( 0, root.childCount );
  1062. root._appendChild( '\uD83D\uDCA9' );
  1063. expect( () => {
  1064. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1065. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1066. expect( () => {
  1067. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1068. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1069. } );
  1070. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1071. root._removeChildren( 0, root.childCount );
  1072. root._appendChild( 'foo̻̐ͩbar' );
  1073. expect( () => {
  1074. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1075. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1076. expect( () => {
  1077. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1078. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1079. expect( () => {
  1080. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1081. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1082. expect( () => {
  1083. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1084. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1085. expect( () => {
  1086. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1087. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1088. expect( () => {
  1089. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1090. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1091. } );
  1092. } );