8
0

selection.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import 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 Selection from '../../src/model/selection';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. import { parse, setData } from '../../src/dev-utils/model';
  16. import Schema from '../../src/model/schema';
  17. testUtils.createSinonSandbox();
  18. describe( 'Selection', () => {
  19. let model, doc, root, selection, liveRange, range, range1, range2, range3;
  20. beforeEach( () => {
  21. model = new Model();
  22. doc = model.document;
  23. root = doc.createRoot();
  24. root.appendChildren( [
  25. new Element( 'p' ),
  26. new Element( 'p' ),
  27. new Element( 'p', [], new Text( 'foobar' ) ),
  28. new Element( 'p' ),
  29. new Element( 'p' ),
  30. new Element( 'p' ),
  31. new Element( 'p', [], new Text( 'foobar' ) )
  32. ] );
  33. selection = new Selection();
  34. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  35. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  36. range1 = new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) );
  37. range2 = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  38. range3 = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  39. } );
  40. afterEach( () => {
  41. model.destroy();
  42. liveRange.detach();
  43. } );
  44. describe( 'constructor()', () => {
  45. it( 'should be able to create an empty selection', () => {
  46. const selection = new Selection();
  47. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [] );
  48. } );
  49. it( 'should be able to create a selection from the given ranges', () => {
  50. const ranges = [ range1, range2, range3 ];
  51. const selection = new Selection( ranges );
  52. expect( Array.from( selection.getRanges() ) ).to.deep.equal( ranges );
  53. } );
  54. it( 'should be able to create a selection from the given ranges and isLastBackward flag', () => {
  55. const ranges = [ range1, range2, range3 ];
  56. const selection = new Selection( ranges, true );
  57. expect( selection.isBackward ).to.be.true;
  58. } );
  59. } );
  60. describe( 'isCollapsed', () => {
  61. it( 'should return false for empty selection', () => {
  62. expect( selection.isCollapsed ).to.be.false;
  63. } );
  64. it( 'should return true when there is single collapsed ranges', () => {
  65. selection.setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  66. expect( selection.isCollapsed ).to.be.true;
  67. } );
  68. it( 'should return false when there are multiple ranges', () => {
  69. selection.setTo( [
  70. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ),
  71. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) )
  72. ] );
  73. expect( selection.isCollapsed ).to.be.false;
  74. } );
  75. it( 'should return false when there is not collapsed range', () => {
  76. selection.setTo( range );
  77. expect( selection.isCollapsed ).to.be.false;
  78. } );
  79. } );
  80. describe( 'rangeCount', () => {
  81. it( 'should return proper range count', () => {
  82. expect( selection.rangeCount ).to.equal( 0 );
  83. selection.setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  84. expect( selection.rangeCount ).to.equal( 1 );
  85. selection.setTo( [
  86. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ),
  87. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) )
  88. ] );
  89. expect( selection.rangeCount ).to.equal( 2 );
  90. } );
  91. } );
  92. describe( 'isBackward', () => {
  93. it( 'is defined by the last added range', () => {
  94. selection.setTo( [ range ], true );
  95. expect( selection ).to.have.property( 'isBackward', true );
  96. selection.setTo( liveRange );
  97. expect( selection ).to.have.property( 'isBackward', false );
  98. } );
  99. it( 'is false when last range is collapsed', () => {
  100. const pos = Position.createAt( root, 0 );
  101. selection.setTo( [ new Range( pos, pos ) ], true );
  102. expect( selection.isBackward ).to.be.false;
  103. } );
  104. } );
  105. describe( 'focus', () => {
  106. let r1, r2, r3;
  107. beforeEach( () => {
  108. r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  109. r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  110. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  111. selection.setTo( [ r1, r2 ] );
  112. } );
  113. it( 'should return correct focus when last added range is not backward one', () => {
  114. selection.setTo( [ r1, r2, r3 ] );
  115. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  116. } );
  117. it( 'should return correct focus when last added range is backward one', () => {
  118. selection.setTo( [ r1, r2, r3 ], true );
  119. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  120. } );
  121. it( 'should return null if no ranges in selection', () => {
  122. selection.setTo( null );
  123. expect( selection.focus ).to.be.null;
  124. } );
  125. } );
  126. describe.skip( 'addRange()', () => {
  127. it( 'should copy added ranges and store multiple ranges', () => {
  128. selection.addRange( liveRange );
  129. selection.addRange( range );
  130. const ranges = selection._ranges;
  131. expect( ranges.length ).to.equal( 2 );
  132. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  133. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  134. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  135. expect( ranges[ 1 ] ).not.to.be.equal( range );
  136. } );
  137. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  138. selection.addRange( liveRange );
  139. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  140. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  141. selection.addRange( range );
  142. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  143. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  144. } );
  145. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  146. selection.addRange( liveRange, true );
  147. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  148. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  149. selection.addRange( range, true );
  150. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  151. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  152. } );
  153. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  154. selection.addRange( liveRange );
  155. const ranges = Array.from( selection.getRanges() );
  156. selection.addRange( range );
  157. expect( ranges.length ).to.equal( 1 );
  158. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  159. } );
  160. it( 'should fire change:range event when adding a range', () => {
  161. const spy = sinon.spy();
  162. selection.on( 'change:range', spy );
  163. selection.addRange( range );
  164. expect( spy.called ).to.be.true;
  165. } );
  166. it( 'should throw an error when range is invalid', () => {
  167. expect( () => {
  168. selection.addRange( { invalid: 'range' } );
  169. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  170. } );
  171. it( 'should throw an error if added range intersects with already stored range', () => {
  172. selection.addRange( liveRange );
  173. expect( () => {
  174. selection.addRange(
  175. new Range(
  176. new Position( root, [ 0, 4 ] ),
  177. new Position( root, [ 1, 2 ] )
  178. )
  179. );
  180. } ).to.throw( CKEditorError, /model-selection-range-intersects/ );
  181. } );
  182. } );
  183. describe( 'setTo()', () => {
  184. it( 'should set selection inside an element', () => {
  185. const element = new Element( 'p', null, [ new Text( 'foo' ), new Text( 'bar' ) ] );
  186. selection.setTo( Range.createIn( element ) );
  187. const ranges = Array.from( selection.getRanges() );
  188. expect( ranges.length ).to.equal( 1 );
  189. expect( ranges[ 0 ].start.parent ).to.equal( element );
  190. expect( ranges[ 0 ].start.offset ).to.deep.equal( 0 );
  191. expect( ranges[ 0 ].end.parent ).to.equal( element );
  192. expect( ranges[ 0 ].end.offset ).to.deep.equal( 6 );
  193. } );
  194. } );
  195. describe( 'setTo()', () => {
  196. it( 'should set selection on an item', () => {
  197. const textNode1 = new Text( 'foo' );
  198. const textNode2 = new Text( 'bar' );
  199. const textNode3 = new Text( 'baz' );
  200. const element = new Element( 'p', null, [ textNode1, textNode2, textNode3 ] );
  201. selection.setTo( Range.createOn( textNode2 ) );
  202. const ranges = Array.from( selection.getRanges() );
  203. expect( ranges.length ).to.equal( 1 );
  204. expect( ranges[ 0 ].start.parent ).to.equal( element );
  205. expect( ranges[ 0 ].start.offset ).to.deep.equal( 3 );
  206. expect( ranges[ 0 ].end.parent ).to.equal( element );
  207. expect( ranges[ 0 ].end.offset ).to.deep.equal( 6 );
  208. } );
  209. } );
  210. describe( 'setTo - set to position or item', () => {
  211. it( 'fires change:range', () => {
  212. const spy = sinon.spy();
  213. selection.on( 'change:range', spy );
  214. selection.setTo( root );
  215. expect( spy.calledOnce ).to.be.true;
  216. } );
  217. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  218. selection.setTo( root );
  219. expect( selection ).to.have.property( 'isCollapsed', true );
  220. const focus = selection.focus;
  221. expect( focus ).to.have.property( 'parent', root );
  222. expect( focus ).to.have.property( 'offset', 0 );
  223. } );
  224. it( 'sets selection at given offset in given parent', () => {
  225. selection.setTo( root, 3 );
  226. expect( selection ).to.have.property( 'isCollapsed', true );
  227. const focus = selection.focus;
  228. expect( focus ).to.have.property( 'parent', root );
  229. expect( focus ).to.have.property( 'offset', 3 );
  230. } );
  231. it( 'sets selection at the end of the given parent', () => {
  232. selection.setTo( root, 'end' );
  233. expect( selection ).to.have.property( 'isCollapsed', true );
  234. const focus = selection.focus;
  235. expect( focus ).to.have.property( 'parent', root );
  236. expect( focus ).to.have.property( 'offset', root.maxOffset );
  237. } );
  238. it( 'sets selection before the specified element', () => {
  239. selection.setTo( root.getChild( 1 ), 'before' );
  240. expect( selection ).to.have.property( 'isCollapsed', true );
  241. const focus = selection.focus;
  242. expect( focus ).to.have.property( 'parent', root );
  243. expect( focus ).to.have.property( 'offset', 1 );
  244. } );
  245. it( 'sets selection after the specified element', () => {
  246. selection.setTo( root.getChild( 1 ), 'after' );
  247. expect( selection ).to.have.property( 'isCollapsed', true );
  248. const focus = selection.focus;
  249. expect( focus ).to.have.property( 'parent', root );
  250. expect( focus ).to.have.property( 'offset', 2 );
  251. } );
  252. it( 'sets selection at the specified position', () => {
  253. const pos = Position.createFromParentAndOffset( root, 3 );
  254. selection.setTo( pos );
  255. expect( selection ).to.have.property( 'isCollapsed', true );
  256. const focus = selection.focus;
  257. expect( focus ).to.have.property( 'parent', root );
  258. expect( focus ).to.have.property( 'offset', 3 );
  259. } );
  260. } );
  261. describe( 'moveFocusTo()', () => {
  262. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  263. selection.setTo( [ range, liveRange ] );
  264. const spy = sinon.spy();
  265. selection.on( 'change:range', spy );
  266. selection.moveFocusTo( selection.focus );
  267. expect( count( selection.getRanges() ) ).to.equal( 2 );
  268. expect( spy.callCount ).to.equal( 0 );
  269. } );
  270. it( 'fires change:range', () => {
  271. selection.setTo( range );
  272. const spy = sinon.spy();
  273. selection.on( 'change:range', spy );
  274. selection.moveFocusTo( Position.createAt( root, 'end' ) );
  275. expect( spy.calledOnce ).to.be.true;
  276. } );
  277. it( 'throws if there are no ranges in selection', () => {
  278. const endPos = Position.createAt( root, 'end' );
  279. expect( () => {
  280. selection.moveFocusTo( endPos );
  281. } ).to.throw( CKEditorError, /model-selection-moveFocusTo-no-ranges/ );
  282. } );
  283. it( 'modifies existing collapsed selection', () => {
  284. const startPos = Position.createAt( root, 1 );
  285. const endPos = Position.createAt( root, 2 );
  286. selection.setCollapsedAt( startPos );
  287. selection.moveFocusTo( endPos );
  288. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  289. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  290. } );
  291. it( 'makes existing collapsed selection a backward selection', () => {
  292. const startPos = Position.createAt( root, 1 );
  293. const endPos = Position.createAt( root, 0 );
  294. selection.setCollapsedAt( startPos );
  295. selection.moveFocusTo( endPos );
  296. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  297. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  298. expect( selection.isBackward ).to.be.true;
  299. } );
  300. it( 'modifies existing non-collapsed selection', () => {
  301. const startPos = Position.createAt( root, 1 );
  302. const endPos = Position.createAt( root, 2 );
  303. const newEndPos = Position.createAt( root, 3 );
  304. selection.setTo( new Range( startPos, endPos ) );
  305. selection.moveFocusTo( newEndPos );
  306. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  307. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  308. } );
  309. it( 'makes existing non-collapsed selection a backward selection', () => {
  310. const startPos = Position.createAt( root, 1 );
  311. const endPos = Position.createAt( root, 2 );
  312. const newEndPos = Position.createAt( root, 0 );
  313. selection.setTo( new Range( startPos, endPos ) );
  314. selection.moveFocusTo( newEndPos );
  315. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  316. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  317. expect( selection.isBackward ).to.be.true;
  318. } );
  319. it( 'makes existing backward selection a forward selection', () => {
  320. const startPos = Position.createAt( root, 1 );
  321. const endPos = Position.createAt( root, 2 );
  322. const newEndPos = Position.createAt( root, 3 );
  323. selection.setTo( new Range( startPos, endPos ), true );
  324. selection.moveFocusTo( newEndPos );
  325. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  326. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  327. expect( selection.isBackward ).to.be.false;
  328. } );
  329. it( 'modifies existing backward selection', () => {
  330. const startPos = Position.createAt( root, 1 );
  331. const endPos = Position.createAt( root, 2 );
  332. const newEndPos = Position.createAt( root, 0 );
  333. selection.setTo( new Range( startPos, endPos ), true );
  334. selection.moveFocusTo( newEndPos );
  335. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  336. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  337. expect( selection.isBackward ).to.be.true;
  338. } );
  339. it( 'modifies only the last range', () => {
  340. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  341. const startPos1 = Position.createAt( root, 4 );
  342. const endPos1 = Position.createAt( root, 5 );
  343. const startPos2 = Position.createAt( root, 1 );
  344. const endPos2 = Position.createAt( root, 2 );
  345. const newEndPos = Position.createAt( root, 0 );
  346. selection.setTo( [
  347. new Range( startPos1, endPos1 ),
  348. new Range( startPos2, endPos2 )
  349. ] );
  350. const spy = sinon.spy();
  351. selection.on( 'change:range', spy );
  352. selection.moveFocusTo( newEndPos );
  353. const ranges = Array.from( selection.getRanges() );
  354. expect( ranges ).to.have.lengthOf( 2 );
  355. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  356. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  357. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  358. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  359. expect( selection.isBackward ).to.be.true;
  360. expect( spy.calledOnce ).to.be.true;
  361. } );
  362. it( 'collapses the selection when extending to the anchor', () => {
  363. const startPos = Position.createAt( root, 1 );
  364. const endPos = Position.createAt( root, 2 );
  365. selection.setTo( new Range( startPos, endPos ) );
  366. selection.moveFocusTo( startPos );
  367. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  368. expect( selection.isCollapsed ).to.be.true;
  369. } );
  370. it( 'uses Position.createAt', () => {
  371. const startPos = Position.createAt( root, 1 );
  372. const endPos = Position.createAt( root, 2 );
  373. const newEndPos = Position.createAt( root, 4 );
  374. const spy = testUtils.sinon.stub( Position, 'createAt' ).returns( newEndPos );
  375. selection.setTo( new Range( startPos, endPos ) );
  376. selection.moveFocusTo( root, 'end' );
  377. expect( spy.calledOnce ).to.be.true;
  378. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  379. } );
  380. } );
  381. describe( 'setTo - selection set to null', () => {
  382. let spy;
  383. it( 'should remove all stored ranges', () => {
  384. selection.setTo( [ liveRange, range ] );
  385. selection.setTo( null );
  386. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  387. } );
  388. it( 'should fire exactly one change:range event', () => {
  389. selection.setTo( [ liveRange, range ] );
  390. spy = sinon.spy();
  391. selection.on( 'change:range', spy );
  392. selection.setTo( null );
  393. expect( spy.calledOnce ).to.be.true;
  394. } );
  395. it( 'should not fire change:range event if there were no ranges', () => {
  396. spy = sinon.spy();
  397. selection.on( 'change:range', spy );
  398. selection.setTo( null );
  399. expect( spy.called ).to.be.false;
  400. } );
  401. } );
  402. describe( '_setRanges()', () => {
  403. let newRanges, spy;
  404. beforeEach( () => {
  405. newRanges = [
  406. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  407. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  408. ];
  409. selection.setTo( [ liveRange, range ] );
  410. spy = sinon.spy();
  411. selection.on( 'change:range', spy );
  412. } );
  413. it( 'should throw an error when range is invalid', () => {
  414. expect( () => {
  415. selection._setRanges( [ { invalid: 'range' } ] );
  416. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  417. } );
  418. it( 'should remove all ranges and add given ranges', () => {
  419. selection._setRanges( newRanges );
  420. const ranges = Array.from( selection.getRanges() );
  421. expect( ranges ).to.deep.equal( newRanges );
  422. } );
  423. it( 'should use last range from given array to get anchor and focus position', () => {
  424. selection._setRanges( newRanges );
  425. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  426. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  427. } );
  428. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  429. selection._setRanges( newRanges, true );
  430. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  431. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  432. } );
  433. it( 'should fire exactly one change:range event', () => {
  434. selection._setRanges( newRanges );
  435. expect( spy.calledOnce ).to.be.true;
  436. } );
  437. it( 'should fire change:range event with correct parameters', () => {
  438. selection.on( 'change:range', ( evt, data ) => {
  439. expect( data.directChange ).to.be.true;
  440. } );
  441. selection._setRanges( newRanges );
  442. } );
  443. it( 'should not fire change:range event if given ranges are the same', () => {
  444. selection._setRanges( [ liveRange, range ] );
  445. expect( spy.calledOnce ).to.be.false;
  446. } );
  447. } );
  448. describe( 'setTo()', () => {
  449. it( 'should set selection to be same as given selection, using _setRanges method', () => {
  450. const spy = sinon.spy( selection, '_setRanges' );
  451. const otherSelection = new Selection( [ range1, range2 ], true );
  452. selection.setTo( otherSelection );
  453. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  454. expect( selection.isBackward ).to.be.true;
  455. expect( selection._setRanges.calledOnce ).to.be.true;
  456. spy.restore();
  457. } );
  458. it( 'should set selection on the given Range using _setRanges method', () => {
  459. const spy = sinon.spy( selection, '_setRanges' );
  460. selection.setTo( range1 );
  461. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1 ] );
  462. expect( selection.isBackward ).to.be.false;
  463. expect( selection._setRanges.calledOnce ).to.be.true;
  464. spy.restore();
  465. } );
  466. it( 'should set selection on the given iterable of Ranges using _setRanges method', () => {
  467. const spy = sinon.spy( selection, '_setRanges' );
  468. selection.setTo( new Set( [ range1, range2 ] ) );
  469. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  470. expect( selection.isBackward ).to.be.false;
  471. expect( selection._setRanges.calledOnce ).to.be.true;
  472. spy.restore();
  473. } );
  474. it( 'should set collapsed selection on the given Position using _setRanges method', () => {
  475. const spy = sinon.spy( selection, '_setRanges' );
  476. const position = new Position( root, [ 4 ] );
  477. selection.setTo( position );
  478. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  479. expect( Array.from( selection.getRanges() )[ 0 ].start ).to.deep.equal( position );
  480. expect( selection.isBackward ).to.be.false;
  481. expect( selection.isCollapsed ).to.be.true;
  482. expect( selection._setRanges.calledOnce ).to.be.true;
  483. spy.restore();
  484. } );
  485. } );
  486. describe( 'getFirstRange()', () => {
  487. it( 'should return null if no ranges were added', () => {
  488. expect( selection.getFirstRange() ).to.be.null;
  489. } );
  490. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  491. selection.setTo( [
  492. // This will not be the first range despite being added as first
  493. range2,
  494. // This should be the first range.
  495. range1,
  496. // A random range that is not first.
  497. range3
  498. ] );
  499. const range = selection.getFirstRange();
  500. expect( range.start.path ).to.deep.equal( [ 1 ] );
  501. expect( range.end.path ).to.deep.equal( [ 4 ] );
  502. } );
  503. } );
  504. describe( 'getFirstPosition()', () => {
  505. it( 'should return null if no ranges were added', () => {
  506. expect( selection.getFirstPosition() ).to.be.null;
  507. } );
  508. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  509. selection.setTo( [
  510. // This will not be the first range despite being added as first
  511. range2,
  512. // This should be the first range.
  513. range1,
  514. // A random range that is not first.
  515. range3
  516. ] );
  517. const position = selection.getFirstPosition();
  518. expect( position.path ).to.deep.equal( [ 1 ] );
  519. } );
  520. } );
  521. describe( 'getLastRange()', () => {
  522. it( 'should return null if no ranges were added', () => {
  523. expect( selection.getLastRange() ).to.be.null;
  524. } );
  525. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  526. selection.setTo( [ range3, range1, range2 ] );
  527. const range = selection.getLastRange();
  528. expect( range.start.path ).to.deep.equal( [ 6 ] );
  529. expect( range.end.path ).to.deep.equal( [ 7 ] );
  530. } );
  531. } );
  532. describe( 'getLastPosition()', () => {
  533. it( 'should return null if no ranges were added', () => {
  534. expect( selection.getLastPosition() ).to.be.null;
  535. } );
  536. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  537. selection.setTo( [ range3, range1, range2 ] );
  538. const position = selection.getLastPosition();
  539. expect( position.path ).to.deep.equal( [ 7 ] );
  540. } );
  541. } );
  542. describe( 'isEqual()', () => {
  543. it( 'should return true if selections equal', () => {
  544. selection.setTo( [ range1, range2 ] );
  545. const otherSelection = new Selection( [ range1, range2 ] );
  546. expect( selection.isEqual( otherSelection ) ).to.be.true;
  547. } );
  548. it( 'should return true if backward selections equal', () => {
  549. selection.setTo( [ range1 ], true );
  550. const otherSelection = new Selection( [ range1 ], true );
  551. expect( selection.isEqual( otherSelection ) ).to.be.true;
  552. } );
  553. it( 'should return true if both selections have no ranges', () => {
  554. const otherSelection = new Selection();
  555. expect( selection.isEqual( otherSelection ) ).to.be.true;
  556. } );
  557. it( 'should return false if ranges count does not equal', () => {
  558. selection.setTo( [ range1, range2 ] );
  559. const otherSelection = new Selection( [ range2 ] );
  560. expect( selection.isEqual( otherSelection ) ).to.be.false;
  561. } );
  562. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  563. selection.setTo( [ range1, range3 ] );
  564. const otherSelection = new Selection( [ range2, range3 ] );
  565. expect( selection.isEqual( otherSelection ) ).to.be.false;
  566. } );
  567. it( 'should return false if directions do not equal', () => {
  568. selection.setTo( range1 );
  569. const otherSelection = new Selection( [ range1 ], true );
  570. expect( selection.isEqual( otherSelection ) ).to.be.false;
  571. } );
  572. } );
  573. describe.skip( 'collapseToStart()', () => {
  574. it( 'should collapse to start position and fire change event', () => {
  575. selection._setRanges( [ range2, range1, range3 ] );
  576. const spy = sinon.spy();
  577. selection.on( 'change:range', spy );
  578. selection.collapseToStart();
  579. expect( selection.rangeCount ).to.equal( 1 );
  580. expect( selection.isCollapsed ).to.be.true;
  581. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  582. expect( spy.calledOnce ).to.be.true;
  583. } );
  584. it( 'should do nothing if selection was already collapsed', () => {
  585. selection.setCollapsedAt( range1.start );
  586. const spy = sinon.spy( selection, 'fire' );
  587. selection.collapseToStart();
  588. expect( spy.notCalled ).to.be.true;
  589. spy.restore();
  590. } );
  591. it( 'should do nothing if no ranges present', () => {
  592. const spy = sinon.spy( selection, 'fire' );
  593. selection.collapseToStart();
  594. spy.restore();
  595. expect( spy.notCalled ).to.be.true;
  596. } );
  597. } );
  598. describe.skip( 'collapseToEnd()', () => {
  599. it( 'should collapse to start position and fire change:range event', () => {
  600. selection._setRanges( [ range2, range3, range1 ] );
  601. const spy = sinon.spy();
  602. selection.on( 'change:range', spy );
  603. selection.collapseToEnd();
  604. expect( selection.rangeCount ).to.equal( 1 );
  605. expect( selection.isCollapsed ).to.be.true;
  606. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  607. expect( spy.calledOnce ).to.be.true;
  608. } );
  609. it( 'should do nothing if selection was already collapsed', () => {
  610. selection.setCollapsedAt( range1.start );
  611. const spy = sinon.spy( selection, 'fire' );
  612. selection.collapseToEnd();
  613. expect( spy.notCalled ).to.be.true;
  614. spy.restore();
  615. } );
  616. it( 'should do nothing if selection has no ranges', () => {
  617. const spy = sinon.spy( selection, 'fire' );
  618. selection.collapseToEnd();
  619. expect( spy.notCalled ).to.be.true;
  620. spy.restore();
  621. } );
  622. } );
  623. describe( 'createFromSelection()', () => {
  624. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  625. selection.setTo( [ liveRange, range ], true );
  626. const snapshot = Selection.createFromSelection( selection );
  627. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  628. const selectionRanges = Array.from( selection.getRanges() );
  629. const snapshotRanges = Array.from( snapshot.getRanges() );
  630. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  631. for ( let i = 0; i < selectionRanges.length; i++ ) {
  632. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  633. }
  634. } );
  635. } );
  636. describe( 'getSelectedElement()', () => {
  637. let schema;
  638. beforeEach( () => {
  639. schema = new Schema();
  640. schema.register( '$root' );
  641. schema.register( 'p', { allowIn: '$root' } );
  642. schema.register( '$text', { allowIn: 'p' } );
  643. } );
  644. it( 'should return selected element', () => {
  645. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  646. const p = model.getChild( 1 );
  647. expect( selection.getSelectedElement() ).to.equal( p );
  648. } );
  649. it( 'should return null if there is more than one range', () => {
  650. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  651. expect( selection.getSelectedElement() ).to.be.null;
  652. } );
  653. it( 'should return null if there is no selection', () => {
  654. expect( selection.getSelectedElement() ).to.be.null;
  655. } );
  656. it( 'should return null if selection is not over single element #1', () => {
  657. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  658. expect( selection.getSelectedElement() ).to.be.null;
  659. } );
  660. it( 'should return null if selection is not over single element #2', () => {
  661. const { selection } = parse( '<p>{bar}</p>', schema );
  662. expect( selection.getSelectedElement() ).to.be.null;
  663. } );
  664. } );
  665. describe( 'getSelectedBlocks()', () => {
  666. beforeEach( () => {
  667. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  668. model.schema.register( 'h', { inheritAllFrom: '$block' } );
  669. model.schema.register( 'blockquote' );
  670. model.schema.extend( 'blockquote', { allowIn: '$root' } );
  671. model.schema.extend( '$block', { allowIn: 'blockquote' } );
  672. model.schema.register( 'image', {
  673. allowIn: [ '$root', '$block' ]
  674. } );
  675. model.schema.extend( '$text', { allowIn: 'image' } );
  676. // Special block which can contain another blocks.
  677. model.schema.register( 'nestedBlock', { inheritAllFrom: '$block' } );
  678. model.schema.extend( 'nestedBlock', { allowIn: '$block' } );
  679. } );
  680. it( 'returns an iterator', () => {
  681. setData( model, '<p>a</p><p>[]b</p><p>c</p>' );
  682. expect( doc.selection.getSelectedBlocks().next ).to.be.a( 'function' );
  683. } );
  684. it( 'returns block for a collapsed selection', () => {
  685. setData( model, '<p>a</p><p>[]b</p><p>c</p>' );
  686. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  687. } );
  688. it( 'returns block for a collapsed selection (empty block)', () => {
  689. setData( model, '<p>a</p><p>[]</p><p>c</p>' );
  690. const blocks = Array.from( doc.selection.getSelectedBlocks() );
  691. expect( blocks ).to.have.length( 1 );
  692. expect( blocks[ 0 ].childCount ).to.equal( 0 );
  693. } );
  694. it( 'returns block for a non collapsed selection', () => {
  695. setData( model, '<p>a</p><p>[b]</p><p>c</p>' );
  696. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  697. } );
  698. it( 'returns two blocks for a non collapsed selection', () => {
  699. setData( model, '<p>a</p><h>[b</h><p>c]</p><p>d</p>' );
  700. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  701. } );
  702. it( 'returns two blocks for a non collapsed selection (starts at block end)', () => {
  703. setData( model, '<p>a</p><h>b[</h><p>c]</p><p>d</p>' );
  704. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  705. } );
  706. it( 'returns proper block for a multi-range selection', () => {
  707. setData( model, '<p>a</p><h>[b</h><p>c]</p><p>d</p><p>[e]</p>' );
  708. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c', 'e' ] );
  709. } );
  710. it( 'does not return a block twice if two ranges are anchored in it', () => {
  711. setData( model, '<p>[a]b[c]</p>' );
  712. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'abc' ] );
  713. } );
  714. it( 'returns only blocks', () => {
  715. setData( model, '<p>[a</p><image>b</image><p>c]</p>' );
  716. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'c' ] );
  717. } );
  718. it( 'gets deeper into the tree', () => {
  719. setData( model, '<p>[a</p><blockquote><p>b</p><p>c</p></blockquote><p>d]</p>' );
  720. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  721. } );
  722. it( 'gets deeper into the tree (end deeper)', () => {
  723. setData( model, '<p>[a</p><blockquote><p>b]</p><p>c</p></blockquote><p>d</p>' );
  724. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  725. } );
  726. it( 'gets deeper into the tree (start deeper)', () => {
  727. setData( model, '<p>a</p><blockquote><p>b</p><p>[c</p></blockquote><p>d]</p>' );
  728. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'c', 'd' ] );
  729. } );
  730. it( 'returns an empty array if none of the selected elements is a block', () => {
  731. setData( model, '<p>a</p><image>[a</image><image>b]</image><p>b</p>' );
  732. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  733. } );
  734. it( 'returns an empty array if the selected element is not a block', () => {
  735. setData( model, '<p>a</p><image>[]a</image><p>b</p>' );
  736. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  737. } );
  738. // Super edge case – should not happen (blocks should never be nested),
  739. // but since the code handles it already it's worth testing.
  740. it( 'returns only the lowest block if blocks are nested', () => {
  741. setData( model, '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' );
  742. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  743. } );
  744. // Like above but trickier.
  745. it( 'returns only the lowest block if blocks are nested', () => {
  746. setData(
  747. model,
  748. '<nestedBlock>a<nestedBlock>[b</nestedBlock></nestedBlock>' +
  749. '<nestedBlock>c<nestedBlock>d]</nestedBlock></nestedBlock>'
  750. );
  751. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'd' ] );
  752. } );
  753. it( 'returns nothing if directly in a root', () => {
  754. doc.createRoot( 'p', 'inlineOnlyRoot' );
  755. setData( model, 'a[b]c', { rootName: 'inlineOnlyRoot' } );
  756. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  757. } );
  758. describe( '#984', () => {
  759. it( 'does not return the last block if none of its content is selected', () => {
  760. setData( model, '<p>[a</p><p>b</p><p>]c</p>' );
  761. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  762. } );
  763. it( 'returns only the first block for a non collapsed selection if only end of selection is touching a block', () => {
  764. setData( model, '<p>a</p><h>b[</h><p>]c</p><p>d</p>' );
  765. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  766. } );
  767. it( 'does not return the last block if none of its content is selected (nested case)', () => {
  768. setData( model, '<p>[a</p><nestedBlock><nestedBlock>]b</nestedBlock></nestedBlock>' );
  769. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  770. } );
  771. // Like a super edge case, we can live with this behavior as I don't even know what we could expect here
  772. // since only the innermost block is considerd a block to return (so the <nB>b...</nB> needs to be ignored).
  773. it( 'does not return the last block if none of its content is selected (nested case, wrapper with a content)', () => {
  774. setData( model, '<p>[a</p><nestedBlock>b<nestedBlock>]c</nestedBlock></nestedBlock>' );
  775. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  776. } );
  777. it( 'returns the last block if at least one of its child nodes is selected', () => {
  778. setData( model, '<p>[a</p><p>b</p><p><image></image>]c</p>' );
  779. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  780. } );
  781. // I needed these last 2 cases to justify the use of isTouching() instead of simple `offset == 0` check.
  782. it( 'returns the last block if at least one of its child nodes is selected (end in an inline element)', () => {
  783. setData( model, '<p>[a</p><p>b</p><p><image>x]</image>c</p>' );
  784. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  785. } );
  786. it(
  787. 'does not return the last block if at least one of its child nodes is selected ' +
  788. '(end in an inline element, no content selected)',
  789. () => {
  790. setData( model, '<p>[a</p><p>b</p><p><image>]x</image>c</p>' );
  791. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  792. }
  793. );
  794. } );
  795. // Map all elements to data of its first child text node.
  796. function toText( elements ) {
  797. return Array.from( elements ).map( el => {
  798. return Array.from( el.getChildren() ).find( child => child.data ).data;
  799. } );
  800. }
  801. } );
  802. describe( 'attributes interface', () => {
  803. let rangeInFullP;
  804. beforeEach( () => {
  805. root.insertChildren( 0, [
  806. new Element( 'p', [], new Text( 'foobar' ) ),
  807. new Element( 'p', [], [] )
  808. ] );
  809. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  810. } );
  811. describe( 'setAttribute()', () => {
  812. it( 'should set given attribute on the selection', () => {
  813. selection.setTo( [ rangeInFullP ] );
  814. selection.setAttribute( 'foo', 'bar' );
  815. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  816. } );
  817. it( 'should fire change:attribute event with correct parameters', () => {
  818. selection.on( 'change:attribute', ( evt, data ) => {
  819. expect( data.directChange ).to.be.true;
  820. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  821. } );
  822. selection.setAttribute( 'foo', 'bar' );
  823. } );
  824. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  825. selection.setAttribute( 'foo', 'bar' );
  826. const spy = sinon.spy();
  827. selection.on( 'change:attribute', spy );
  828. selection.setAttribute( 'foo', 'bar' );
  829. expect( spy.called ).to.be.false;
  830. } );
  831. } );
  832. describe( 'getAttribute()', () => {
  833. it( 'should return undefined if element does not contain given attribute', () => {
  834. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  835. } );
  836. } );
  837. describe( 'getAttributes()', () => {
  838. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  839. selection.setTo( [ rangeInFullP ] );
  840. selection.setAttribute( 'foo', 'bar' );
  841. selection.setAttribute( 'abc', 'xyz' );
  842. const attrs = Array.from( selection.getAttributes() );
  843. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  844. } );
  845. } );
  846. describe( 'getAttributeKeys()', () => {
  847. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  848. selection.setTo( [ rangeInFullP ] );
  849. selection.setAttribute( 'foo', 'bar' );
  850. selection.setAttribute( 'abc', 'xyz' );
  851. const attrs = Array.from( selection.getAttributeKeys() );
  852. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  853. } );
  854. } );
  855. describe( 'hasAttribute()', () => {
  856. it( 'should return true if element contains attribute with given key', () => {
  857. selection.setTo( [ rangeInFullP ] );
  858. selection.setAttribute( 'foo', 'bar' );
  859. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  860. } );
  861. it( 'should return false if element does not contain attribute with given key', () => {
  862. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  863. } );
  864. } );
  865. describe( 'clearAttributes()', () => {
  866. it( 'should remove all attributes from the element', () => {
  867. selection.setTo( [ rangeInFullP ] );
  868. selection.setAttribute( 'foo', 'bar' );
  869. selection.setAttribute( 'abc', 'xyz' );
  870. selection.clearAttributes();
  871. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  872. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  873. } );
  874. it( 'should fire change:attribute event with correct parameters', () => {
  875. selection.setAttribute( 'foo', 'bar' );
  876. selection.on( 'change:attribute', ( evt, data ) => {
  877. expect( data.directChange ).to.be.true;
  878. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  879. } );
  880. selection.clearAttributes();
  881. } );
  882. it( 'should not fire change:attribute event if there were no attributes', () => {
  883. const spy = sinon.spy();
  884. selection.on( 'change:attribute', spy );
  885. selection.clearAttributes();
  886. expect( spy.called ).to.be.false;
  887. } );
  888. } );
  889. describe( 'removeAttribute()', () => {
  890. it( 'should remove attribute', () => {
  891. selection.setTo( [ rangeInFullP ] );
  892. selection.setAttribute( 'foo', 'bar' );
  893. selection.removeAttribute( 'foo' );
  894. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  895. } );
  896. it( 'should fire change:attribute event with correct parameters', () => {
  897. selection.setAttribute( 'foo', 'bar' );
  898. selection.on( 'change:attribute', ( evt, data ) => {
  899. expect( data.directChange ).to.be.true;
  900. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  901. } );
  902. selection.removeAttribute( 'foo' );
  903. } );
  904. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  905. const spy = sinon.spy();
  906. selection.on( 'change:attribute', spy );
  907. selection.removeAttribute( 'foo' );
  908. expect( spy.called ).to.be.false;
  909. } );
  910. } );
  911. describe( 'setAttributesTo()', () => {
  912. it( 'should remove all attributes set on element and set the given ones', () => {
  913. selection.setAttribute( 'abc', 'xyz' );
  914. selection.setAttributesTo( { foo: 'bar' } );
  915. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  916. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  917. } );
  918. it( 'should fire only one change:attribute event', () => {
  919. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  920. const spy = sinon.spy();
  921. selection.on( 'change:attribute', spy );
  922. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  923. expect( spy.calledOnce ).to.be.true;
  924. } );
  925. it( 'should fire change:attribute event with correct parameters', () => {
  926. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  927. selection.on( 'change:attribute', ( evt, data ) => {
  928. expect( data.directChange ).to.be.true;
  929. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  930. } );
  931. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  932. } );
  933. it( 'should not fire change:attribute event if attributes had not changed', () => {
  934. selection.setTo( [ rangeInFullP ] );
  935. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  936. const spy = sinon.spy();
  937. selection.on( 'change:attribute', spy );
  938. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  939. expect( spy.called ).to.be.false;
  940. } );
  941. } );
  942. } );
  943. describe( 'containsEntireContent()', () => {
  944. beforeEach( () => {
  945. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  946. model.schema.extend( 'p', { allowIn: '$root' } );
  947. } );
  948. it( 'returns true if the entire content in $root is selected', () => {
  949. setData( model, '<p>[Foo</p><p>Bom</p><p>Bar]</p>' );
  950. expect( doc.selection.containsEntireContent() ).to.equal( true );
  951. } );
  952. it( 'returns false when only a fragment of the content in $root is selected', () => {
  953. setData( model, '<p>Fo[o</p><p>Bom</p><p>Bar]</p>' );
  954. expect( doc.selection.containsEntireContent() ).to.equal( false );
  955. } );
  956. it( 'returns true if the entire content in specified element is selected', () => {
  957. setData( model, '<p>Foo</p><p>[Bom]</p><p>Bar</p>' );
  958. const root = doc.getRoot();
  959. const secondParagraph = root.getNodeByPath( [ 1 ] );
  960. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( true );
  961. } );
  962. it( 'returns false if the entire content in specified element is not selected', () => {
  963. setData( model, '<p>Foo</p><p>[Bom</p><p>B]ar</p>' );
  964. const root = doc.getRoot();
  965. const secondParagraph = root.getNodeByPath( [ 1 ] );
  966. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( false );
  967. } );
  968. it( 'returns false when the entire content except an empty element is selected', () => {
  969. model.schema.register( 'img', {
  970. allowIn: 'p'
  971. } );
  972. setData( model, '<p><img></img>[Foo]</p>' );
  973. expect( doc.selection.containsEntireContent() ).to.equal( false );
  974. } );
  975. it( 'returns true if the content is empty', () => {
  976. setData( model, '[]' );
  977. expect( doc.selection.containsEntireContent() ).to.equal( true );
  978. } );
  979. it( 'returns false if empty selection is at the end of non-empty content', () => {
  980. setData( model, '<p>Foo bar bom.</p>[]' );
  981. expect( doc.selection.containsEntireContent() ).to.equal( false );
  982. } );
  983. } );
  984. } );