selection.js 40 KB

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