8
0

documentselection.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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( 'setCollapsedAt()', () => {
  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.setCollapsedAt( 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( 'moveFocusTo()', () => {
  188. it( 'modifies default range', () => {
  189. const startPos = selection.getFirstPosition();
  190. const endPos = Position.createAt( root, 'end' );
  191. selection.moveFocusTo( 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.moveFocusTo( 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. describe( '_isStoreAttributeKey', () => {
  291. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  292. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  293. } );
  294. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  295. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  296. } );
  297. } );
  298. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  299. // working well, without getting into complicated corner cases.
  300. describe( 'after applying an operation should get updated and fire events', () => {
  301. let spyRange;
  302. beforeEach( () => {
  303. root.removeChildren( 0, root.childCount );
  304. root.insertChildren( 0, [
  305. new Element( 'p', [], new Text( 'abcdef' ) ),
  306. new Element( 'p', [], new Text( 'foobar' ) ),
  307. new Text( 'xyz' )
  308. ] );
  309. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  310. spyRange = sinon.spy();
  311. selection.on( 'change:range', spyRange );
  312. } );
  313. describe( 'InsertOperation', () => {
  314. it( 'before selection', () => {
  315. selection.on( 'change:range', ( evt, data ) => {
  316. expect( data.directChange ).to.be.false;
  317. } );
  318. doc.applyOperation( wrapInDelta(
  319. new InsertOperation(
  320. new Position( root, [ 0, 1 ] ),
  321. 'xyz',
  322. doc.version
  323. )
  324. ) );
  325. const range = selection.getFirstRange();
  326. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  327. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  328. expect( spyRange.calledOnce ).to.be.true;
  329. } );
  330. it( 'inside selection', () => {
  331. selection.on( 'change:range', ( evt, data ) => {
  332. expect( data.directChange ).to.be.false;
  333. } );
  334. doc.applyOperation( wrapInDelta(
  335. new InsertOperation(
  336. new Position( root, [ 1, 0 ] ),
  337. 'xyz',
  338. doc.version
  339. )
  340. ) );
  341. const range = selection.getFirstRange();
  342. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  343. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  344. expect( spyRange.calledOnce ).to.be.true;
  345. } );
  346. } );
  347. describe( 'MoveOperation', () => {
  348. it( 'move range from before a selection', () => {
  349. selection.on( 'change:range', ( evt, data ) => {
  350. expect( data.directChange ).to.be.false;
  351. } );
  352. doc.applyOperation( wrapInDelta(
  353. new MoveOperation(
  354. new Position( root, [ 0, 0 ] ),
  355. 2,
  356. new Position( root, [ 2 ] ),
  357. doc.version
  358. )
  359. ) );
  360. const range = selection.getFirstRange();
  361. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  362. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  363. expect( spyRange.calledOnce ).to.be.true;
  364. } );
  365. it( 'moved into before a selection', () => {
  366. selection.on( 'change:range', ( evt, data ) => {
  367. expect( data.directChange ).to.be.false;
  368. } );
  369. doc.applyOperation( wrapInDelta(
  370. new MoveOperation(
  371. new Position( root, [ 2 ] ),
  372. 2,
  373. new Position( root, [ 0, 0 ] ),
  374. doc.version
  375. )
  376. ) );
  377. const range = selection.getFirstRange();
  378. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  379. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  380. expect( spyRange.calledOnce ).to.be.true;
  381. } );
  382. it( 'move range from inside of selection', () => {
  383. selection.on( 'change:range', ( evt, data ) => {
  384. expect( data.directChange ).to.be.false;
  385. } );
  386. doc.applyOperation( wrapInDelta(
  387. new MoveOperation(
  388. new Position( root, [ 1, 0 ] ),
  389. 2,
  390. new Position( root, [ 2 ] ),
  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( [ 1, 2 ] );
  397. expect( spyRange.calledOnce ).to.be.true;
  398. } );
  399. it( 'moved range intersects with selection', () => {
  400. selection.on( 'change:range', ( evt, data ) => {
  401. expect( data.directChange ).to.be.false;
  402. } );
  403. doc.applyOperation( wrapInDelta(
  404. new MoveOperation(
  405. new Position( root, [ 1, 3 ] ),
  406. 2,
  407. new Position( root, [ 4 ] ),
  408. doc.version
  409. )
  410. ) );
  411. const range = selection.getFirstRange();
  412. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  413. expect( range.end.path ).to.deep.equal( [ 5 ] );
  414. expect( spyRange.calledOnce ).to.be.true;
  415. } );
  416. it( 'split inside selection (do not break selection)', () => {
  417. selection.on( 'change:range', ( evt, data ) => {
  418. expect( data.directChange ).to.be.false;
  419. } );
  420. const batch = doc.batch();
  421. const splitDelta = new SplitDelta();
  422. const insertOperation = new InsertOperation(
  423. new Position( root, [ 2 ] ),
  424. new Element( 'p' ),
  425. 0
  426. );
  427. const moveOperation = new MoveOperation(
  428. new Position( root, [ 1, 2 ] ),
  429. 4,
  430. new Position( root, [ 2, 0 ] ),
  431. 1
  432. );
  433. batch.addDelta( splitDelta );
  434. splitDelta.addOperation( insertOperation );
  435. splitDelta.addOperation( moveOperation );
  436. doc.applyOperation( insertOperation );
  437. doc.applyOperation( moveOperation );
  438. const range = selection.getFirstRange();
  439. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  440. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  441. expect( spyRange.calledOnce ).to.be.true;
  442. } );
  443. } );
  444. describe( 'AttributeOperation', () => {
  445. it( 'changed range includes selection anchor', () => {
  446. const spyAttribute = sinon.spy();
  447. selection.on( 'change:attribute', spyAttribute );
  448. selection.on( 'change:attribute', ( evt, data ) => {
  449. expect( data.directChange ).to.be.false;
  450. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  451. } );
  452. doc.applyOperation( wrapInDelta(
  453. new AttributeOperation(
  454. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  455. 'foo',
  456. null,
  457. 'bar',
  458. doc.version
  459. )
  460. ) );
  461. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  462. expect( spyAttribute.calledOnce ).to.be.true;
  463. } );
  464. it( 'should not overwrite previously set attributes', () => {
  465. selection.setAttribute( 'foo', 'xyz' );
  466. const spyAttribute = sinon.spy();
  467. selection.on( 'change:attribute', spyAttribute );
  468. doc.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.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  478. expect( spyAttribute.called ).to.be.false;
  479. } );
  480. it( 'should not overwrite previously removed attributes', () => {
  481. selection.setAttribute( 'foo', 'xyz' );
  482. selection.removeAttribute( 'foo' );
  483. const spyAttribute = sinon.spy();
  484. selection.on( 'change:attribute', spyAttribute );
  485. doc.applyOperation( wrapInDelta(
  486. new AttributeOperation(
  487. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  488. 'foo',
  489. null,
  490. 'bar',
  491. doc.version
  492. )
  493. ) );
  494. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  495. expect( spyAttribute.called ).to.be.false;
  496. } );
  497. } );
  498. describe( 'RemoveOperation', () => {
  499. it( 'fix selection range if it ends up in graveyard #1', () => {
  500. selection.setCollapsedAt( new Position( root, [ 1, 3 ] ) );
  501. doc.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 #2', () => {
  512. selection.setRanges( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  513. doc.applyOperation( wrapInDelta(
  514. new RemoveOperation(
  515. new Position( root, [ 1, 2 ] ),
  516. 2,
  517. new Position( doc.graveyard, [ 0 ] ),
  518. doc.version
  519. )
  520. ) );
  521. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  522. } );
  523. it( 'fix selection range if it ends up in graveyard #3', () => {
  524. selection.setRanges( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  525. doc.applyOperation( wrapInDelta(
  526. new RemoveOperation(
  527. new Position( root, [ 1 ] ),
  528. 2,
  529. new Position( doc.graveyard, [ 0 ] ),
  530. doc.version
  531. )
  532. ) );
  533. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  534. } );
  535. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  536. doc.applyOperation( wrapInDelta(
  537. new RemoveOperation(
  538. new Position( root, [ 0 ] ),
  539. 3,
  540. new Position( doc.graveyard, [ 0 ] ),
  541. doc.version
  542. )
  543. ) );
  544. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  545. doc.applyOperation( wrapInDelta(
  546. new InsertOperation(
  547. new Position( root, [ 0 ] ),
  548. new Element( 'p' ),
  549. doc.version
  550. )
  551. ) );
  552. // Now it's clear that it's the default range.
  553. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  554. } );
  555. } );
  556. } );
  557. describe( 'attributes', () => {
  558. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  559. beforeEach( () => {
  560. root.removeChildren( 0, root.childCount );
  561. root.appendChildren( [
  562. new Element( 'p', [], new Text( 'foobar' ) ),
  563. new Element( 'p', [], [] )
  564. ] );
  565. fullP = root.getChild( 0 );
  566. emptyP = root.getChild( 1 );
  567. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  568. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  569. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  570. // a root which wasn't empty (so the ranges didn't actualy make much sense).
  571. expect( root.childCount ).to.equal( 2 );
  572. } );
  573. describe( 'setAttribute()', () => {
  574. it( 'should store attribute if the selection is in empty node', () => {
  575. selection.setRanges( [ rangeInEmptyP ] );
  576. selection.setAttribute( 'foo', 'bar' );
  577. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  578. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  579. } );
  580. } );
  581. describe( 'setAttributesTo()', () => {
  582. it( 'should fire change:attribute event with correct parameters', done => {
  583. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  584. selection.on( 'change:attribute', ( evt, data ) => {
  585. expect( data.directChange ).to.be.true;
  586. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  587. done();
  588. } );
  589. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  590. } );
  591. it( 'should not fire change:attribute event if same attributes are set', () => {
  592. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  593. const spy = sinon.spy();
  594. selection.on( 'change:attribute', spy );
  595. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  596. expect( spy.called ).to.be.false;
  597. } );
  598. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  599. selection.setRanges( [ rangeInEmptyP ] );
  600. selection.setAttribute( 'abc', 'xyz' );
  601. selection.setAttributesTo( { foo: 'bar' } );
  602. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  603. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  604. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  605. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  606. } );
  607. } );
  608. describe( 'removeAttribute()', () => {
  609. it( 'should remove attribute set on the text fragment', () => {
  610. selection.setRanges( [ rangeInFullP ] );
  611. selection.setAttribute( 'foo', 'bar' );
  612. selection.removeAttribute( 'foo' );
  613. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  614. expect( fullP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  615. } );
  616. it( 'should remove stored attribute if the selection is in empty node', () => {
  617. selection.setRanges( [ rangeInEmptyP ] );
  618. selection.setAttribute( 'foo', 'bar' );
  619. selection.removeAttribute( 'foo' );
  620. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  621. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  622. } );
  623. } );
  624. describe( 'clearAttributes()', () => {
  625. it( 'should remove all stored attributes if the selection is in empty node', () => {
  626. selection.setRanges( [ rangeInEmptyP ] );
  627. selection.setAttribute( 'foo', 'bar' );
  628. selection.setAttribute( 'abc', 'xyz' );
  629. selection.clearAttributes();
  630. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  631. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  632. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  633. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  634. } );
  635. } );
  636. describe( '_getStoredAttributes()', () => {
  637. it( 'should return no values if there are no ranges in selection', () => {
  638. const values = Array.from( selection._getStoredAttributes() );
  639. expect( values ).to.deep.equal( [] );
  640. } );
  641. } );
  642. describe( 'are updated on a direct range change', () => {
  643. beforeEach( () => {
  644. root.insertChildren( 0, [
  645. new Element( 'p', { p: true } ),
  646. new Text( 'a', { a: true } ),
  647. new Element( 'p', { p: true } ),
  648. new Text( 'b', { b: true } ),
  649. new Text( 'c', { c: true } ),
  650. new Element( 'p', [], [
  651. new Text( 'd', { d: true } )
  652. ] ),
  653. new Element( 'p', { p: true } ),
  654. new Text( 'e', { e: true } )
  655. ] );
  656. } );
  657. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  658. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  659. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  660. // Step into elements when looking for first character:
  661. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  662. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  663. } );
  664. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  665. // Take styles from character before selection.
  666. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  667. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  668. // If there are none,
  669. // Take styles from character after selection.
  670. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  671. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  672. // If there are none,
  673. // Look from the selection position to the beginning of node looking for character to take attributes from.
  674. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  675. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  676. // If there are none,
  677. // Look from the selection position to the end of node looking for character to take attributes from.
  678. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  679. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  680. // If there are no characters to copy attributes from, use stored attributes.
  681. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  682. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  683. } );
  684. it( 'should overwrite any previously set attributes', () => {
  685. selection.setCollapsedAt( new Position( root, [ 5, 0 ] ) );
  686. selection.setAttribute( 'x', true );
  687. selection.setAttribute( 'y', true );
  688. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  689. selection.setCollapsedAt( new Position( root, [ 1 ] ) );
  690. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  691. } );
  692. it( 'should fire change:attribute event', () => {
  693. const spy = sinon.spy();
  694. selection.on( 'change:attribute', spy );
  695. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  696. expect( spy.calledOnce ).to.be.true;
  697. } );
  698. it( 'should not fire change:attribute event if attributes did not change', () => {
  699. selection.setCollapsedAt( new Position( root, [ 5, 0 ] ) );
  700. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  701. const spy = sinon.spy();
  702. selection.on( 'change:attribute', spy );
  703. selection.setCollapsedAt( new Position( root, [ 5, 1 ] ) );
  704. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  705. expect( spy.called ).to.be.false;
  706. } );
  707. } );
  708. // #986
  709. describe( 'are not inherited from the inside of object elements', () => {
  710. beforeEach( () => {
  711. doc.schema.registerItem( 'image' );
  712. doc.schema.allow( { name: 'image', inside: '$root' } );
  713. doc.schema.allow( { name: 'image', inside: '$block' } );
  714. doc.schema.allow( { name: '$inline', inside: 'image' } );
  715. doc.schema.objects.add( 'image' );
  716. doc.schema.registerItem( 'caption' );
  717. doc.schema.allow( { name: '$inline', inside: 'caption' } );
  718. doc.schema.allow( { name: 'caption', inside: 'image' } );
  719. doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } );
  720. } );
  721. it( 'ignores attributes inside an object if selection contains that object', () => {
  722. setData( doc, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  723. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  724. } );
  725. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  726. setData( doc, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  727. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  728. } );
  729. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  730. setData( doc, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  731. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  732. } );
  733. it( 'reads attributes from text even if the selection contains an object', () => {
  734. setData( doc, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  735. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  736. } );
  737. it( 'reads attributes when the entire selection inside an object', () => {
  738. setData( doc, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  739. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  740. } );
  741. it( 'stops reading attributes if selection starts with an object', () => {
  742. setData( doc, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  743. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  744. } );
  745. } );
  746. describe( 'parent element\'s attributes', () => {
  747. it( 'are set using a normal batch', () => {
  748. const batchTypes = [];
  749. doc.on( 'change', ( event, type, changes, batch ) => {
  750. batchTypes.push( batch.type );
  751. } );
  752. selection.setRanges( [ rangeInEmptyP ] );
  753. selection.setAttribute( 'foo', 'bar' );
  754. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  755. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  756. } );
  757. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  758. // Dedupe batches by using a map (multiple change events will be fired).
  759. const batchTypes = new Map();
  760. selection.setRanges( [ rangeInEmptyP ] );
  761. selection.setAttribute( 'foo', 'bar' );
  762. selection.setAttribute( 'abc', 'bar' );
  763. doc.on( 'change', ( event, type, changes, batch ) => {
  764. batchTypes.set( batch, batch.type );
  765. } );
  766. doc.batch().insert( rangeInEmptyP.start, 'x' );
  767. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  768. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  769. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  770. } );
  771. it( 'are removed when any content is moved into', () => {
  772. selection.setRanges( [ rangeInEmptyP ] );
  773. selection.setAttribute( 'foo', 'bar' );
  774. doc.batch().move( fullP.getChild( 0 ), rangeInEmptyP.start );
  775. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  776. } );
  777. it( 'are removed when containing element is merged with a non-empty element', () => {
  778. const emptyP2 = new Element( 'p', null, 'x' );
  779. root.appendChildren( emptyP2 );
  780. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  781. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  782. // <emptyP>{}<emptyP2>
  783. doc.batch().merge( Position.createAfter( emptyP ) );
  784. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  785. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  786. } );
  787. it( 'are removed even when there is no selection in it', () => {
  788. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  789. selection.setRanges( [ rangeInFullP ] );
  790. doc.batch().insert( rangeInEmptyP.start, 'x' );
  791. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  792. } );
  793. it( 'are removed only once in case of multi-op deltas', () => {
  794. const emptyP2 = new Element( 'p', null, 'x' );
  795. root.appendChildren( emptyP2 );
  796. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  797. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  798. const batch = doc.batch();
  799. const spy = sinon.spy( batch, 'removeAttribute' );
  800. // <emptyP>{}<emptyP2>
  801. batch.merge( Position.createAfter( emptyP ) );
  802. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  803. expect( spy.calledOnce ).to.be.true;
  804. } );
  805. it( 'uses document enqueue changes to clear attributes', () => {
  806. selection.setRanges( [ rangeInEmptyP ] );
  807. selection.setAttribute( 'foo', 'bar' );
  808. doc.enqueueChanges( () => {
  809. doc.batch().insert( rangeInEmptyP.start, 'x' );
  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. // <emptyP>{}<emptyP2>
  824. doc.batch().merge( Position.createAfter( emptyP ) );
  825. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  826. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  827. } );
  828. it( 'are not removed on transparent batches', () => {
  829. selection.setRanges( [ rangeInEmptyP ] );
  830. selection.setAttribute( 'foo', 'bar' );
  831. sinon.spy( doc, 'enqueueChanges' );
  832. doc.batch( 'transparent' ).insert( rangeInEmptyP.start, 'x' );
  833. expect( doc.enqueueChanges.called ).to.be.false;
  834. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  835. } );
  836. // Rename and some other deltas don't specify range in doc#change event.
  837. // So let's see if there's no crash or something.
  838. it( 'are not removed on rename', () => {
  839. selection.setRanges( [ rangeInEmptyP ] );
  840. selection.setAttribute( 'foo', 'bar' );
  841. sinon.spy( doc, 'enqueueChanges' );
  842. doc.batch().rename( emptyP, 'pnew' );
  843. expect( doc.enqueueChanges.called ).to.be.false;
  844. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  845. } );
  846. } );
  847. } );
  848. } );