8
0

documentselection.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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( 'setTo - set collapsed at', () => {
  159. it( 'detaches all existing ranges', () => {
  160. selection._setTo( [ range, liveRange ] );
  161. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  162. selection._setTo( root );
  163. expect( spy.calledTwice ).to.be.true;
  164. } );
  165. } );
  166. describe( 'destroy()', () => {
  167. it( 'should unbind all events', () => {
  168. selection._setTo( [ range, liveRange ] );
  169. const ranges = Array.from( selection._selection._ranges );
  170. sinon.spy( ranges[ 0 ], 'detach' );
  171. sinon.spy( ranges[ 1 ], 'detach' );
  172. selection.destroy();
  173. expect( ranges[ 0 ].detach.called ).to.be.true;
  174. expect( ranges[ 1 ].detach.called ).to.be.true;
  175. ranges[ 0 ].detach.restore();
  176. ranges[ 1 ].detach.restore();
  177. } );
  178. } );
  179. describe( 'setFocus()', () => {
  180. it( 'modifies default range', () => {
  181. const startPos = selection.getFirstPosition();
  182. const endPos = Position.createAt( root, 'end' );
  183. selection._setFocus( endPos );
  184. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  185. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  186. } );
  187. it( 'detaches the range it replaces', () => {
  188. const startPos = Position.createAt( root, 1 );
  189. const endPos = Position.createAt( root, 2 );
  190. const newEndPos = Position.createAt( root, 4 );
  191. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  192. selection._setTo( new Range( startPos, endPos ) );
  193. selection._setFocus( newEndPos );
  194. expect( spy.calledOnce ).to.be.true;
  195. } );
  196. it( 'refreshes attributes', () => {
  197. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  198. selection._setFocus( Position.createAt( root, 1 ) );
  199. expect( spy.called ).to.be.true;
  200. } );
  201. } );
  202. describe( 'setTo - removeAllRanges', () => {
  203. let spy, ranges;
  204. beforeEach( () => {
  205. selection._setTo( [ liveRange, range ] );
  206. spy = sinon.spy();
  207. selection.on( 'change:range', spy );
  208. ranges = Array.from( selection._selection._ranges );
  209. sinon.spy( ranges[ 0 ], 'detach' );
  210. sinon.spy( ranges[ 1 ], 'detach' );
  211. selection._setTo( null );
  212. } );
  213. afterEach( () => {
  214. ranges[ 0 ].detach.restore();
  215. ranges[ 1 ].detach.restore();
  216. } );
  217. it( 'should remove all stored ranges (and reset to default range)', () => {
  218. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  219. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  220. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  221. } );
  222. it( 'should detach ranges', () => {
  223. expect( ranges[ 0 ].detach.called ).to.be.true;
  224. expect( ranges[ 1 ].detach.called ).to.be.true;
  225. } );
  226. it( 'should refresh attributes', () => {
  227. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  228. selection._setTo( null );
  229. expect( spy.called ).to.be.true;
  230. } );
  231. } );
  232. // TODO - merge with other setTo's
  233. describe( 'setTo()', () => {
  234. it( 'should throw an error when range is invalid', () => {
  235. expect( () => {
  236. selection._setTo( [ { invalid: 'range' } ] );
  237. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  238. } );
  239. it( 'should detach removed ranges', () => {
  240. selection._setTo( [ liveRange, range ] );
  241. const oldRanges = Array.from( selection._selection._ranges );
  242. sinon.spy( oldRanges[ 0 ], 'detach' );
  243. sinon.spy( oldRanges[ 1 ], 'detach' );
  244. selection._setTo( [] );
  245. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  246. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  247. } );
  248. it( 'should refresh attributes', () => {
  249. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  250. selection._setTo( [ range ] );
  251. expect( spy.called ).to.be.true;
  252. } );
  253. // See #630.
  254. it( 'should refresh attributes – integration test for #630', () => {
  255. model.schema.extend( '$text', { allowIn: '$root' } );
  256. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  257. selection._setTo( [ Range.createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  258. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  259. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  260. expect( getData( model ) )
  261. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  262. } );
  263. } );
  264. describe( 'getFirstRange()', () => {
  265. it( 'should return default range if no ranges were added', () => {
  266. const firstRange = selection.getFirstRange();
  267. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  268. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  269. } );
  270. } );
  271. describe( 'getLastRange()', () => {
  272. it( 'should return default range if no ranges were added', () => {
  273. const lastRange = selection.getLastRange();
  274. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  275. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  276. } );
  277. } );
  278. describe( '_isStoreAttributeKey', () => {
  279. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  280. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  281. } );
  282. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  283. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  284. } );
  285. } );
  286. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  287. // working well, without getting into complicated corner cases.
  288. describe( 'after applying an operation should get updated and fire events', () => {
  289. let spyRange;
  290. beforeEach( () => {
  291. root.removeChildren( 0, root.childCount );
  292. root.insertChildren( 0, [
  293. new Element( 'p', [], new Text( 'abcdef' ) ),
  294. new Element( 'p', [], new Text( 'foobar' ) ),
  295. new Text( 'xyz' )
  296. ] );
  297. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  298. spyRange = sinon.spy();
  299. selection.on( 'change:range', spyRange );
  300. } );
  301. describe( 'InsertOperation', () => {
  302. it( 'before selection', () => {
  303. selection.on( 'change:range', ( evt, data ) => {
  304. expect( data.directChange ).to.be.false;
  305. } );
  306. model.applyOperation( wrapInDelta(
  307. new InsertOperation(
  308. new Position( root, [ 0, 1 ] ),
  309. 'xyz',
  310. doc.version
  311. )
  312. ) );
  313. const range = selection.getFirstRange();
  314. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  315. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  316. expect( spyRange.calledOnce ).to.be.true;
  317. } );
  318. it( 'inside selection', () => {
  319. selection.on( 'change:range', ( evt, data ) => {
  320. expect( data.directChange ).to.be.false;
  321. } );
  322. model.applyOperation( wrapInDelta(
  323. new InsertOperation(
  324. new Position( root, [ 1, 0 ] ),
  325. 'xyz',
  326. doc.version
  327. )
  328. ) );
  329. const range = selection.getFirstRange();
  330. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  331. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  332. expect( spyRange.calledOnce ).to.be.true;
  333. } );
  334. } );
  335. describe( 'MoveOperation', () => {
  336. it( 'move range from before a selection', () => {
  337. selection.on( 'change:range', ( evt, data ) => {
  338. expect( data.directChange ).to.be.false;
  339. } );
  340. model.applyOperation( wrapInDelta(
  341. new MoveOperation(
  342. new Position( root, [ 0, 0 ] ),
  343. 2,
  344. new Position( root, [ 2 ] ),
  345. doc.version
  346. )
  347. ) );
  348. const range = selection.getFirstRange();
  349. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  350. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  351. expect( spyRange.calledOnce ).to.be.true;
  352. } );
  353. it( 'moved into before a selection', () => {
  354. selection.on( 'change:range', ( evt, data ) => {
  355. expect( data.directChange ).to.be.false;
  356. } );
  357. model.applyOperation( wrapInDelta(
  358. new MoveOperation(
  359. new Position( root, [ 2 ] ),
  360. 2,
  361. new Position( root, [ 0, 0 ] ),
  362. doc.version
  363. )
  364. ) );
  365. const range = selection.getFirstRange();
  366. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  367. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  368. expect( spyRange.calledOnce ).to.be.true;
  369. } );
  370. it( 'move range from inside of selection', () => {
  371. selection.on( 'change:range', ( evt, data ) => {
  372. expect( data.directChange ).to.be.false;
  373. } );
  374. model.applyOperation( wrapInDelta(
  375. new MoveOperation(
  376. new Position( root, [ 1, 0 ] ),
  377. 2,
  378. new Position( root, [ 2 ] ),
  379. doc.version
  380. )
  381. ) );
  382. const range = selection.getFirstRange();
  383. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  384. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  385. expect( spyRange.calledOnce ).to.be.true;
  386. } );
  387. it( 'moved range intersects with selection', () => {
  388. selection.on( 'change:range', ( evt, data ) => {
  389. expect( data.directChange ).to.be.false;
  390. } );
  391. model.applyOperation( wrapInDelta(
  392. new MoveOperation(
  393. new Position( root, [ 1, 3 ] ),
  394. 2,
  395. new Position( root, [ 4 ] ),
  396. doc.version
  397. )
  398. ) );
  399. const range = selection.getFirstRange();
  400. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  401. expect( range.end.path ).to.deep.equal( [ 5 ] );
  402. expect( spyRange.calledOnce ).to.be.true;
  403. } );
  404. it( 'split inside selection (do not break selection)', () => {
  405. selection.on( 'change:range', ( evt, data ) => {
  406. expect( data.directChange ).to.be.false;
  407. } );
  408. const batch = new Batch();
  409. const splitDelta = new SplitDelta();
  410. const insertOperation = new InsertOperation(
  411. new Position( root, [ 2 ] ),
  412. new Element( 'p' ),
  413. 0
  414. );
  415. const moveOperation = new MoveOperation(
  416. new Position( root, [ 1, 2 ] ),
  417. 4,
  418. new Position( root, [ 2, 0 ] ),
  419. 1
  420. );
  421. batch.addDelta( splitDelta );
  422. splitDelta.addOperation( insertOperation );
  423. splitDelta.addOperation( moveOperation );
  424. model.applyOperation( insertOperation );
  425. model.applyOperation( moveOperation );
  426. const range = selection.getFirstRange();
  427. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  428. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  429. expect( spyRange.calledOnce ).to.be.true;
  430. } );
  431. } );
  432. describe( 'AttributeOperation', () => {
  433. it( 'changed range includes selection anchor', () => {
  434. const spyAttribute = sinon.spy();
  435. selection.on( 'change:attribute', spyAttribute );
  436. selection.on( 'change:attribute', ( evt, data ) => {
  437. expect( data.directChange ).to.be.false;
  438. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  439. } );
  440. model.applyOperation( wrapInDelta(
  441. new AttributeOperation(
  442. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  443. 'foo',
  444. null,
  445. 'bar',
  446. doc.version
  447. )
  448. ) );
  449. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  450. expect( spyAttribute.calledOnce ).to.be.true;
  451. } );
  452. it( 'should not overwrite previously set attributes', () => {
  453. selection._setAttribute( 'foo', 'xyz' );
  454. const spyAttribute = sinon.spy();
  455. selection.on( 'change:attribute', spyAttribute );
  456. model.applyOperation( wrapInDelta(
  457. new AttributeOperation(
  458. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  459. 'foo',
  460. null,
  461. 'bar',
  462. doc.version
  463. )
  464. ) );
  465. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  466. expect( spyAttribute.called ).to.be.false;
  467. } );
  468. it( 'should not overwrite previously removed attributes', () => {
  469. selection._setAttribute( 'foo', 'xyz' );
  470. selection._removeAttribute( 'foo' );
  471. const spyAttribute = sinon.spy();
  472. selection.on( 'change:attribute', spyAttribute );
  473. model.applyOperation( wrapInDelta(
  474. new AttributeOperation(
  475. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  476. 'foo',
  477. null,
  478. 'bar',
  479. doc.version
  480. )
  481. ) );
  482. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  483. expect( spyAttribute.called ).to.be.false;
  484. } );
  485. } );
  486. describe( 'RemoveOperation', () => {
  487. it( 'fix selection range if it ends up in graveyard #1', () => {
  488. selection._setTo( new Position( root, [ 1, 3 ] ) );
  489. model.applyOperation( wrapInDelta(
  490. new RemoveOperation(
  491. new Position( root, [ 1, 2 ] ),
  492. 2,
  493. new Position( doc.graveyard, [ 0 ] ),
  494. doc.version
  495. )
  496. ) );
  497. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  498. } );
  499. it( 'fix selection range if it ends up in graveyard #2', () => {
  500. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  501. model.applyOperation( wrapInDelta(
  502. new RemoveOperation(
  503. new Position( root, [ 1, 2 ] ),
  504. 2,
  505. new Position( doc.graveyard, [ 0 ] ),
  506. doc.version
  507. )
  508. ) );
  509. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  510. } );
  511. it( 'fix selection range if it ends up in graveyard #3', () => {
  512. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  513. model.applyOperation( wrapInDelta(
  514. new RemoveOperation(
  515. new Position( root, [ 1 ] ),
  516. 2,
  517. new Position( doc.graveyard, [ 0 ] ),
  518. doc.version
  519. )
  520. ) );
  521. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  522. } );
  523. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  524. model.applyOperation( wrapInDelta(
  525. new RemoveOperation(
  526. new Position( root, [ 0 ] ),
  527. 3,
  528. new Position( doc.graveyard, [ 0 ] ),
  529. doc.version
  530. )
  531. ) );
  532. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  533. model.applyOperation( wrapInDelta(
  534. new InsertOperation(
  535. new Position( root, [ 0 ] ),
  536. new Element( 'p' ),
  537. doc.version
  538. )
  539. ) );
  540. // Now it's clear that it's the default range.
  541. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  542. } );
  543. } );
  544. } );
  545. describe( 'attributes', () => {
  546. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  547. beforeEach( () => {
  548. root.removeChildren( 0, root.childCount );
  549. root.appendChildren( [
  550. new Element( 'p', [], new Text( 'foobar' ) ),
  551. new Element( 'p', [], [] )
  552. ] );
  553. fullP = root.getChild( 0 );
  554. emptyP = root.getChild( 1 );
  555. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  556. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  557. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  558. // a root which wasn't empty (so the ranges didn't actualy make much sense).
  559. expect( root.childCount ).to.equal( 2 );
  560. } );
  561. describe( '_setAttribute()', () => {
  562. it( 'should set attribute', () => {
  563. selection._setTo( [ rangeInEmptyP ] );
  564. selection._setAttribute( 'foo', 'bar' );
  565. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  566. } );
  567. } );
  568. describe( 'removeAttribute()', () => {
  569. it( 'should remove attribute set on the text fragment', () => {
  570. selection._setTo( [ rangeInFullP ] );
  571. selection._setAttribute( 'foo', 'bar' );
  572. selection._removeAttribute( 'foo' );
  573. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  574. } );
  575. } );
  576. describe( '_getStoredAttributes()', () => {
  577. it( 'should return no values if there are no ranges in selection', () => {
  578. const values = Array.from( selection._getStoredAttributes() );
  579. expect( values ).to.deep.equal( [] );
  580. } );
  581. } );
  582. describe( 'are updated on a direct range change', () => {
  583. beforeEach( () => {
  584. root.insertChildren( 0, [
  585. new Element( 'p', { p: true } ),
  586. new Text( 'a', { a: true } ),
  587. new Element( 'p', { p: true } ),
  588. new Text( 'b', { b: true } ),
  589. new Text( 'c', { c: true } ),
  590. new Element( 'p', [], [
  591. new Text( 'd', { d: true } )
  592. ] ),
  593. new Element( 'p', { p: true } ),
  594. new Text( 'e', { e: true } )
  595. ] );
  596. } );
  597. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  598. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  599. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  600. // Step into elements when looking for first character:
  601. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  602. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  603. } );
  604. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  605. // Take styles from character before selection.
  606. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  607. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  608. // If there are none,
  609. // Take styles from character after selection.
  610. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  611. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  612. // If there are none,
  613. // Look from the selection position to the beginning of node looking for character to take attributes from.
  614. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  615. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  616. // If there are none,
  617. // Look from the selection position to the end of node looking for character to take attributes from.
  618. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  619. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  620. // If there are no characters to copy attributes from, use stored attributes.
  621. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  622. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  623. } );
  624. it( 'should overwrite any previously set attributes', () => {
  625. selection._setTo( new Position( root, [ 5, 0 ] ) );
  626. selection._setAttribute( 'x', true );
  627. selection._setAttribute( 'y', true );
  628. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  629. selection._setTo( new Position( root, [ 1 ] ) );
  630. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  631. } );
  632. it( 'should fire change:attribute event', () => {
  633. const spy = sinon.spy();
  634. selection.on( 'change:attribute', spy );
  635. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  636. expect( spy.calledOnce ).to.be.true;
  637. } );
  638. it( 'should not fire change:attribute event if attributes did not change', () => {
  639. selection._setTo( new Position( root, [ 5, 0 ] ) );
  640. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  641. const spy = sinon.spy();
  642. selection.on( 'change:attribute', spy );
  643. selection._setTo( new Position( root, [ 5, 1 ] ) );
  644. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  645. expect( spy.called ).to.be.false;
  646. } );
  647. } );
  648. // #986
  649. describe( 'are not inherited from the inside of object elements', () => {
  650. beforeEach( () => {
  651. model.schema.register( 'image', {
  652. isObject: true
  653. } );
  654. model.schema.extend( 'image', { allowIn: '$root' } );
  655. model.schema.extend( 'image', { allowIn: '$block' } );
  656. model.schema.register( 'caption' );
  657. model.schema.extend( 'caption', { allowIn: 'image' } );
  658. model.schema.extend( '$text', {
  659. allowIn: [ 'image', 'caption' ],
  660. allowAttributes: 'bold'
  661. } );
  662. } );
  663. it( 'ignores attributes inside an object if selection contains that object', () => {
  664. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  665. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  666. } );
  667. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  668. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  669. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  670. } );
  671. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  672. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  673. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  674. } );
  675. it( 'reads attributes from text even if the selection contains an object', () => {
  676. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  677. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  678. } );
  679. it( 'reads attributes when the entire selection inside an object', () => {
  680. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  681. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  682. } );
  683. it( 'stops reading attributes if selection starts with an object', () => {
  684. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  685. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  686. } );
  687. } );
  688. describe( 'parent element\'s attributes', () => {
  689. it( 'are set using a normal batch', () => {
  690. const batchTypes = [];
  691. model.on( 'applyOperation', ( event, args ) => {
  692. const operation = args[ 0 ];
  693. const batch = operation.delta.batch;
  694. batchTypes.push( batch.type );
  695. } );
  696. selection._setTo( [ rangeInEmptyP ] );
  697. model.change( writer => {
  698. writer.setSelectionAttribute( 'foo', 'bar' );
  699. } );
  700. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  701. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  702. } );
  703. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  704. // Dedupe batches by using a map (multiple change events will be fired).
  705. const batchTypes = new Map();
  706. selection._setTo( rangeInEmptyP );
  707. selection._setAttribute( 'foo', 'bar' );
  708. selection._setAttribute( 'abc', 'bar' );
  709. model.on( 'applyOperation', ( event, args ) => {
  710. const operation = args[ 0 ];
  711. const batch = operation.delta.batch;
  712. batchTypes.set( batch, batch.type );
  713. } );
  714. model.change( writer => {
  715. writer.insertText( 'x', rangeInEmptyP.start );
  716. } );
  717. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  718. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  719. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  720. } );
  721. it( 'are removed when any content is moved into', () => {
  722. selection._setTo( rangeInEmptyP );
  723. selection._setAttribute( 'foo', 'bar' );
  724. model.change( writer => {
  725. writer.move( Range.createOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  726. } );
  727. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  728. } );
  729. it( 'are removed when containing element is merged with a non-empty element', () => {
  730. const emptyP2 = new Element( 'p', null, 'x' );
  731. root.appendChildren( emptyP2 );
  732. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  733. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  734. model.change( writer => {
  735. // <emptyP>{}<emptyP2>
  736. writer.merge( Position.createAfter( emptyP ) );
  737. } );
  738. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  739. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  740. } );
  741. it( 'are removed even when there is no selection in it', () => {
  742. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  743. selection._setTo( [ rangeInFullP ] );
  744. model.change( writer => {
  745. writer.insertText( 'x', rangeInEmptyP.start );
  746. } );
  747. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  748. } );
  749. it( 'are removed only once in case of multi-op deltas', () => {
  750. let batch;
  751. const emptyP2 = new Element( 'p', null, 'x' );
  752. root.appendChildren( emptyP2 );
  753. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  754. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  755. model.change( writer => {
  756. batch = writer.batch;
  757. // <emptyP>{}<emptyP2>
  758. writer.merge( Position.createAfter( emptyP ) );
  759. } );
  760. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  761. // Attribute delta is only one.
  762. expect( Array.from( batch.deltas, delta => delta.type ) ).to.deep.equal( [ 'merge', 'attribute' ] );
  763. } );
  764. it( 'uses model change to clear attributes', () => {
  765. selection._setTo( [ rangeInEmptyP ] );
  766. model.change( writer => {
  767. writer.setSelectionAttribute( 'foo', 'bar' );
  768. writer.insertText( 'x', rangeInEmptyP.start );
  769. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  770. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  771. } );
  772. // When the dust settles, `emptyP` should not have the attribute.
  773. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  774. } );
  775. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  776. const emptyP2 = new Element( 'p', null );
  777. root.appendChildren( emptyP2 );
  778. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  779. emptyP2.setAttribute( abcStoreAttrKey, 'bar' );
  780. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  781. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  782. model.change( writer => {
  783. // <emptyP>{}<emptyP2>
  784. writer.merge( Position.createAfter( emptyP ) );
  785. } );
  786. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  787. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  788. } );
  789. // Rename and some other deltas don't specify range in doc#change event.
  790. // So let's see if there's no crash or something.
  791. it( 'are not removed on rename', () => {
  792. model.change( writer => {
  793. writer.setSelection( rangeInEmptyP );
  794. writer.setSelectionAttribute( 'foo', 'bar' );
  795. } );
  796. sinon.spy( model, 'enqueueChange' );
  797. model.change( writer => {
  798. writer.rename( emptyP, 'pnew' );
  799. } );
  800. expect( model.enqueueChange.called ).to.be.false;
  801. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  802. } );
  803. } );
  804. } );
  805. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  806. root.removeChildren( 0, root.childCount );
  807. root.appendChildren( '\uD83D\uDCA9' );
  808. expect( () => {
  809. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  810. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  811. expect( () => {
  812. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 2 ) );
  813. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  814. } );
  815. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  816. root.removeChildren( 0, root.childCount );
  817. root.appendChildren( 'foo̻̐ͩbar' );
  818. expect( () => {
  819. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 3, root, 9 ) );
  820. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  821. expect( () => {
  822. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 4, root, 9 ) );
  823. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  824. expect( () => {
  825. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 5, root, 9 ) );
  826. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  827. expect( () => {
  828. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 3 ) );
  829. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  830. expect( () => {
  831. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 4 ) );
  832. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  833. expect( () => {
  834. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 5 ) );
  835. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  836. } );
  837. } );