documentselection.js 40 KB

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