documentselection.js 38 KB

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