documentselection.js 36 KB

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