documentselection.js 37 KB

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