selection.js 47 KB

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