documentselection.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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 );
  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. } );
  333. describe( '_getStoredAttributes()', () => {
  334. it( 'should return no values if there are no ranges in selection', () => {
  335. const values = Array.from( selection._getStoredAttributes() );
  336. expect( values ).to.deep.equal( [] );
  337. } );
  338. } );
  339. describe( 'are updated on a direct range change', () => {
  340. beforeEach( () => {
  341. root.insertChildren( 0, [
  342. new Element( 'p', { p: true } ),
  343. new Text( 'a', { a: true } ),
  344. new Element( 'p', { p: true } ),
  345. new Text( 'b', { b: true } ),
  346. new Text( 'c', { c: true } ),
  347. new Element( 'p', [], [
  348. new Text( 'd', { d: true } )
  349. ] ),
  350. new Element( 'p', { p: true } ),
  351. new Text( 'e', { e: true } )
  352. ] );
  353. } );
  354. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  355. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  356. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  357. // Step into elements when looking for first character:
  358. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  359. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  360. } );
  361. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  362. // Take styles from character before selection.
  363. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  364. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  365. // If there are none,
  366. // Take styles from character after selection.
  367. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  368. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  369. // If there are none,
  370. // Look from the selection position to the beginning of node looking for character to take attributes from.
  371. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  372. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  373. // If there are none,
  374. // Look from the selection position to the end of node looking for character to take attributes from.
  375. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  376. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  377. // If there are no characters to copy attributes from, use stored attributes.
  378. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  379. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  380. } );
  381. it( 'should overwrite any previously set attributes', () => {
  382. selection._setTo( new Position( root, [ 5, 0 ] ) );
  383. selection._setAttribute( 'x', true );
  384. selection._setAttribute( 'y', true );
  385. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  386. selection._setTo( new Position( root, [ 1 ] ) );
  387. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  388. } );
  389. it( 'should fire change:attribute event', () => {
  390. const spy = sinon.spy();
  391. selection.on( 'change:attribute', spy );
  392. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  393. expect( spy.calledOnce ).to.be.true;
  394. } );
  395. it( 'should not fire change:attribute event if attributes did not change', () => {
  396. selection._setTo( new Position( root, [ 5, 0 ] ) );
  397. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  398. const spy = sinon.spy();
  399. selection.on( 'change:attribute', spy );
  400. selection._setTo( new Position( root, [ 5, 1 ] ) );
  401. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  402. expect( spy.called ).to.be.false;
  403. } );
  404. } );
  405. // #986
  406. describe( 'are not inherited from the inside of object elements', () => {
  407. beforeEach( () => {
  408. model.schema.register( 'image', {
  409. isObject: true
  410. } );
  411. model.schema.extend( 'image', { allowIn: '$root' } );
  412. model.schema.extend( 'image', { allowIn: '$block' } );
  413. model.schema.register( 'caption' );
  414. model.schema.extend( 'caption', { allowIn: 'image' } );
  415. model.schema.extend( '$text', {
  416. allowIn: [ 'image', 'caption' ],
  417. allowAttributes: 'bold'
  418. } );
  419. } );
  420. it( 'ignores attributes inside an object if selection contains that object', () => {
  421. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  422. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  423. } );
  424. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  425. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  426. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  427. } );
  428. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  429. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  430. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  431. } );
  432. it( 'reads attributes from text even if the selection contains an object', () => {
  433. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  434. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  435. } );
  436. it( 'reads attributes when the entire selection inside an object', () => {
  437. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  438. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  439. } );
  440. it( 'stops reading attributes if selection starts with an object', () => {
  441. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  442. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  443. } );
  444. } );
  445. describe( 'parent element\'s attributes', () => {
  446. it( 'are set using a normal batch', () => {
  447. const batchTypes = [];
  448. model.on( 'applyOperation', ( event, args ) => {
  449. const operation = args[ 0 ];
  450. const batch = operation.delta.batch;
  451. batchTypes.push( batch.type );
  452. } );
  453. selection._setTo( [ rangeInEmptyP ] );
  454. model.change( writer => {
  455. writer.setSelectionAttribute( 'foo', 'bar' );
  456. } );
  457. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  458. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  459. } );
  460. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  461. // Dedupe batches by using a map (multiple change events will be fired).
  462. const batchTypes = new Map();
  463. selection._setTo( rangeInEmptyP );
  464. selection._setAttribute( 'foo', 'bar' );
  465. selection._setAttribute( 'abc', 'bar' );
  466. model.on( 'applyOperation', ( event, args ) => {
  467. const operation = args[ 0 ];
  468. const batch = operation.delta.batch;
  469. batchTypes.set( batch, batch.type );
  470. } );
  471. model.change( writer => {
  472. writer.insertText( 'x', rangeInEmptyP.start );
  473. } );
  474. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  475. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  476. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  477. } );
  478. it( 'are removed when any content is moved into', () => {
  479. selection._setTo( rangeInEmptyP );
  480. selection._setAttribute( 'foo', 'bar' );
  481. model.change( writer => {
  482. writer.move( Range.createOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  483. } );
  484. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  485. } );
  486. it( 'are removed when containing element is merged with a non-empty element', () => {
  487. const emptyP2 = new Element( 'p', null, 'x' );
  488. root.appendChildren( emptyP2 );
  489. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  490. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  491. model.change( writer => {
  492. // <emptyP>{}<emptyP2>
  493. writer.merge( Position.createAfter( emptyP ) );
  494. } );
  495. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  496. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  497. } );
  498. it( 'are removed even when there is no selection in it', () => {
  499. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  500. selection._setTo( [ rangeInFullP ] );
  501. model.change( writer => {
  502. writer.insertText( 'x', rangeInEmptyP.start );
  503. } );
  504. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  505. } );
  506. it( 'are removed only once in case of multi-op deltas', () => {
  507. let batch;
  508. const emptyP2 = new Element( 'p', null, 'x' );
  509. root.appendChildren( emptyP2 );
  510. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  511. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  512. model.change( writer => {
  513. batch = writer.batch;
  514. // <emptyP>{}<emptyP2>
  515. writer.merge( Position.createAfter( emptyP ) );
  516. } );
  517. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  518. // Attribute delta is only one.
  519. expect( Array.from( batch.deltas, delta => delta.type ) ).to.deep.equal( [ 'merge', 'attribute' ] );
  520. } );
  521. it( 'uses model change to clear attributes', () => {
  522. selection._setTo( [ rangeInEmptyP ] );
  523. model.change( writer => {
  524. writer.setSelectionAttribute( 'foo', 'bar' );
  525. writer.insertText( 'x', rangeInEmptyP.start );
  526. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  527. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  528. } );
  529. // When the dust settles, `emptyP` should not have the attribute.
  530. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  531. } );
  532. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  533. const emptyP2 = new Element( 'p', null );
  534. root.appendChildren( emptyP2 );
  535. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  536. emptyP2.setAttribute( abcStoreAttrKey, 'bar' );
  537. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  538. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  539. model.change( writer => {
  540. // <emptyP>{}<emptyP2>
  541. writer.merge( Position.createAfter( emptyP ) );
  542. } );
  543. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  544. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  545. } );
  546. // Rename and some other deltas don't specify range in doc#change event.
  547. // So let's see if there's no crash or something.
  548. it( 'are not removed on rename', () => {
  549. model.change( writer => {
  550. writer.setSelection( rangeInEmptyP );
  551. writer.setSelectionAttribute( 'foo', 'bar' );
  552. } );
  553. sinon.spy( model, 'enqueueChange' );
  554. model.change( writer => {
  555. writer.rename( emptyP, 'pnew' );
  556. } );
  557. expect( model.enqueueChange.called ).to.be.false;
  558. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  559. } );
  560. } );
  561. } );
  562. describe( '_overrideGravity()', () => {
  563. beforeEach( () => {
  564. model.schema.extend( '$text', {
  565. allowIn: '$root'
  566. } );
  567. } );
  568. it( 'should not inherit attributes from node before the caret', () => {
  569. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  570. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  571. selection._overrideGravity();
  572. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  573. } );
  574. it( 'should inherit attributes from node after the caret', () => {
  575. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  576. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  577. selection._overrideGravity();
  578. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  579. } );
  580. it( 'should retain attributes that are set explicit', () => {
  581. setData( model, '<$text italic="true">foo[]</$text>' );
  582. selection._setAttribute( 'bold', true );
  583. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  584. selection._overrideGravity();
  585. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold' ] );
  586. } );
  587. it( 'should retain overridden until selection will not change range by a direct change', () => {
  588. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  589. selection._overrideGravity();
  590. // Changed range but not directly.
  591. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  592. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  593. // Changed range directly.
  594. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  595. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  596. } );
  597. } );
  598. describe( '_restoreGravity()', () => {
  599. beforeEach( () => {
  600. model.schema.extend( '$text', {
  601. allowIn: '$root'
  602. } );
  603. } );
  604. it( 'should not revert default gravity when is overridden', () => {
  605. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  606. selection._overrideGravity();
  607. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  608. selection._restoreGravity();
  609. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  610. } );
  611. it( 'should do nothing when gravity is not overridden', () => {
  612. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  613. expect( () => {
  614. selection._restoreGravity();
  615. } ).to.not.throw();
  616. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  617. } );
  618. } );
  619. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  620. // working well, without getting into complicated corner cases.
  621. describe( 'after applying an operation should get updated and fire events', () => {
  622. let spyRange;
  623. beforeEach( () => {
  624. root.removeChildren( 0, root.childCount );
  625. root.insertChildren( 0, [
  626. new Element( 'p', [], new Text( 'abcdef' ) ),
  627. new Element( 'p', [], new Text( 'foobar' ) ),
  628. new Text( 'xyz' )
  629. ] );
  630. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  631. spyRange = sinon.spy();
  632. selection.on( 'change:range', spyRange );
  633. } );
  634. describe( 'InsertOperation', () => {
  635. it( 'before selection', () => {
  636. selection.on( 'change:range', ( evt, data ) => {
  637. expect( data.directChange ).to.be.false;
  638. } );
  639. model.applyOperation( wrapInDelta(
  640. new InsertOperation(
  641. new Position( root, [ 0, 1 ] ),
  642. 'xyz',
  643. doc.version
  644. )
  645. ) );
  646. const range = selection.getFirstRange();
  647. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  648. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  649. expect( spyRange.calledOnce ).to.be.true;
  650. } );
  651. it( 'inside selection', () => {
  652. selection.on( 'change:range', ( evt, data ) => {
  653. expect( data.directChange ).to.be.false;
  654. } );
  655. model.applyOperation( wrapInDelta(
  656. new InsertOperation(
  657. new Position( root, [ 1, 0 ] ),
  658. 'xyz',
  659. doc.version
  660. )
  661. ) );
  662. const range = selection.getFirstRange();
  663. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  664. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  665. expect( spyRange.calledOnce ).to.be.true;
  666. } );
  667. } );
  668. describe( 'MoveOperation', () => {
  669. it( 'move range from before a selection', () => {
  670. selection.on( 'change:range', ( evt, data ) => {
  671. expect( data.directChange ).to.be.false;
  672. } );
  673. model.applyOperation( wrapInDelta(
  674. new MoveOperation(
  675. new Position( root, [ 0, 0 ] ),
  676. 2,
  677. new Position( root, [ 2 ] ),
  678. doc.version
  679. )
  680. ) );
  681. const range = selection.getFirstRange();
  682. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  683. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  684. expect( spyRange.calledOnce ).to.be.true;
  685. } );
  686. it( 'moved into before a selection', () => {
  687. selection.on( 'change:range', ( evt, data ) => {
  688. expect( data.directChange ).to.be.false;
  689. } );
  690. model.applyOperation( wrapInDelta(
  691. new MoveOperation(
  692. new Position( root, [ 2 ] ),
  693. 2,
  694. new Position( root, [ 0, 0 ] ),
  695. doc.version
  696. )
  697. ) );
  698. const range = selection.getFirstRange();
  699. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  700. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  701. expect( spyRange.calledOnce ).to.be.true;
  702. } );
  703. it( 'move range from inside of selection', () => {
  704. selection.on( 'change:range', ( evt, data ) => {
  705. expect( data.directChange ).to.be.false;
  706. } );
  707. model.applyOperation( wrapInDelta(
  708. new MoveOperation(
  709. new Position( root, [ 1, 0 ] ),
  710. 2,
  711. new Position( root, [ 2 ] ),
  712. doc.version
  713. )
  714. ) );
  715. const range = selection.getFirstRange();
  716. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  717. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  718. expect( spyRange.calledOnce ).to.be.true;
  719. } );
  720. it( 'moved range intersects with selection', () => {
  721. selection.on( 'change:range', ( evt, data ) => {
  722. expect( data.directChange ).to.be.false;
  723. } );
  724. model.applyOperation( wrapInDelta(
  725. new MoveOperation(
  726. new Position( root, [ 1, 3 ] ),
  727. 2,
  728. new Position( root, [ 4 ] ),
  729. doc.version
  730. )
  731. ) );
  732. const range = selection.getFirstRange();
  733. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  734. expect( range.end.path ).to.deep.equal( [ 5 ] );
  735. expect( spyRange.calledOnce ).to.be.true;
  736. } );
  737. it( 'split inside selection (do not break selection)', () => {
  738. selection.on( 'change:range', ( evt, data ) => {
  739. expect( data.directChange ).to.be.false;
  740. } );
  741. const batch = new Batch();
  742. const splitDelta = new SplitDelta();
  743. const insertOperation = new InsertOperation(
  744. new Position( root, [ 2 ] ),
  745. new Element( 'p' ),
  746. 0
  747. );
  748. const moveOperation = new MoveOperation(
  749. new Position( root, [ 1, 2 ] ),
  750. 4,
  751. new Position( root, [ 2, 0 ] ),
  752. 1
  753. );
  754. batch.addDelta( splitDelta );
  755. splitDelta.addOperation( insertOperation );
  756. splitDelta.addOperation( moveOperation );
  757. model.applyOperation( insertOperation );
  758. model.applyOperation( moveOperation );
  759. const range = selection.getFirstRange();
  760. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  761. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  762. expect( spyRange.calledOnce ).to.be.true;
  763. } );
  764. } );
  765. describe( 'AttributeOperation', () => {
  766. it( 'changed range includes selection anchor', () => {
  767. const spyAttribute = sinon.spy();
  768. selection.on( 'change:attribute', spyAttribute );
  769. selection.on( 'change:attribute', ( evt, data ) => {
  770. expect( data.directChange ).to.be.false;
  771. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  772. } );
  773. model.applyOperation( wrapInDelta(
  774. new AttributeOperation(
  775. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  776. 'foo',
  777. null,
  778. 'bar',
  779. doc.version
  780. )
  781. ) );
  782. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  783. expect( spyAttribute.calledOnce ).to.be.true;
  784. } );
  785. it( 'should not overwrite previously set attributes', () => {
  786. selection._setAttribute( 'foo', 'xyz' );
  787. const spyAttribute = sinon.spy();
  788. selection.on( 'change:attribute', spyAttribute );
  789. model.applyOperation( wrapInDelta(
  790. new AttributeOperation(
  791. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  792. 'foo',
  793. null,
  794. 'bar',
  795. doc.version
  796. )
  797. ) );
  798. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  799. expect( spyAttribute.called ).to.be.false;
  800. } );
  801. it( 'should not overwrite previously set attributes with same values', () => {
  802. selection._setAttribute( 'foo', 'xyz' );
  803. const spyAttribute = sinon.spy();
  804. selection.on( 'change:attribute', spyAttribute );
  805. model.applyOperation( wrapInDelta(
  806. new AttributeOperation(
  807. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  808. 'foo',
  809. null,
  810. 'xyz',
  811. doc.version
  812. )
  813. ) );
  814. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  815. expect( spyAttribute.called ).to.be.false;
  816. } );
  817. it( 'should not overwrite previously removed attributes', () => {
  818. selection._setAttribute( 'foo', 'xyz' );
  819. selection._removeAttribute( 'foo' );
  820. const spyAttribute = sinon.spy();
  821. selection.on( 'change:attribute', spyAttribute );
  822. model.applyOperation( wrapInDelta(
  823. new AttributeOperation(
  824. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  825. 'foo',
  826. null,
  827. 'bar',
  828. doc.version
  829. )
  830. ) );
  831. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  832. expect( spyAttribute.called ).to.be.false;
  833. } );
  834. } );
  835. describe( 'RemoveOperation', () => {
  836. it( 'fix selection range if it ends up in graveyard #1', () => {
  837. selection._setTo( new Position( root, [ 1, 3 ] ) );
  838. model.applyOperation( wrapInDelta(
  839. new RemoveOperation(
  840. new Position( root, [ 1, 2 ] ),
  841. 2,
  842. new Position( doc.graveyard, [ 0 ] ),
  843. doc.version
  844. )
  845. ) );
  846. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  847. } );
  848. it( 'fix selection range if it ends up in graveyard #2', () => {
  849. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  850. model.applyOperation( wrapInDelta(
  851. new RemoveOperation(
  852. new Position( root, [ 1, 2 ] ),
  853. 2,
  854. new Position( doc.graveyard, [ 0 ] ),
  855. doc.version
  856. )
  857. ) );
  858. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  859. } );
  860. it( 'fix selection range if it ends up in graveyard #3', () => {
  861. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  862. model.applyOperation( wrapInDelta(
  863. new RemoveOperation(
  864. new Position( root, [ 1 ] ),
  865. 2,
  866. new Position( doc.graveyard, [ 0 ] ),
  867. doc.version
  868. )
  869. ) );
  870. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  871. } );
  872. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  873. model.applyOperation( wrapInDelta(
  874. new RemoveOperation(
  875. new Position( root, [ 0 ] ),
  876. 3,
  877. new Position( doc.graveyard, [ 0 ] ),
  878. doc.version
  879. )
  880. ) );
  881. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  882. model.applyOperation( wrapInDelta(
  883. new InsertOperation(
  884. new Position( root, [ 0 ] ),
  885. new Element( 'p' ),
  886. doc.version
  887. )
  888. ) );
  889. // Now it's clear that it's the default range.
  890. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  891. } );
  892. } );
  893. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  894. root.removeChildren( 0, root.childCount );
  895. root.insertChildren( 0, [
  896. new Element( 'p', [], new Text( 'abcdef' ) ),
  897. new Element( 'p', [], new Text( 'foobar' ) ),
  898. new Text( 'xyz #2' )
  899. ] );
  900. selection._setTo( [
  901. Range.createIn( root.getNodeByPath( [ 0 ] ) ),
  902. Range.createIn( root.getNodeByPath( [ 1 ] ) )
  903. ] );
  904. spyRange = sinon.spy();
  905. selection.on( 'change:range', spyRange );
  906. model.applyOperation( wrapInDelta(
  907. new InsertOperation(
  908. new Position( root, [ 0 ] ),
  909. 'xyz #1',
  910. doc.version
  911. )
  912. ) );
  913. expect( spyRange.calledOnce ).to.be.true;
  914. } );
  915. } );
  916. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  917. root.removeChildren( 0, root.childCount );
  918. root.appendChildren( '\uD83D\uDCA9' );
  919. expect( () => {
  920. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  921. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  922. expect( () => {
  923. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 2 ) );
  924. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  925. } );
  926. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  927. root.removeChildren( 0, root.childCount );
  928. root.appendChildren( 'foo̻̐ͩbar' );
  929. expect( () => {
  930. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 3, root, 9 ) );
  931. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  932. expect( () => {
  933. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 4, root, 9 ) );
  934. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  935. expect( () => {
  936. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 5, root, 9 ) );
  937. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  938. expect( () => {
  939. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 3 ) );
  940. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  941. expect( () => {
  942. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 4 ) );
  943. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  944. expect( () => {
  945. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 5 ) );
  946. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  947. } );
  948. } );