documentselection.js 37 KB

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