selection.js 46 KB

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