documentselection.js 34 KB

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