8
0

selection.js 44 KB

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