8
0

documentselection.js 41 KB

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