selection-post-fixer.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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 { injectSelectionPostFixer } from '../../../src/model/utils/selection-post-fixer';
  7. import { getData as getModelData, setData as setModelData } from '../../../src/dev-utils/model';
  8. describe( 'Selection post-fixer', () => {
  9. describe( 'injectSelectionPostFixer()', () => {
  10. it( 'is a function', () => {
  11. expect( injectSelectionPostFixer ).to.be.a( 'function' );
  12. } );
  13. } );
  14. describe( 'injected behavior', () => {
  15. let model, modelRoot;
  16. beforeEach( () => {
  17. model = new Model();
  18. modelRoot = model.document.createRoot();
  19. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  20. model.schema.register( 'table', {
  21. allowWhere: '$block',
  22. allowAttributes: [ 'headingRows', 'headingColumns' ],
  23. isLimit: true,
  24. isObject: true
  25. } );
  26. model.schema.register( 'tableRow', {
  27. allowIn: 'table',
  28. isLimit: true
  29. } );
  30. model.schema.register( 'tableCell', {
  31. allowIn: 'tableRow',
  32. allowAttributes: [ 'colspan', 'rowspan' ],
  33. isLimit: true
  34. } );
  35. model.schema.register( 'image', {
  36. isObject: true,
  37. isBlock: true,
  38. allowWhere: '$block'
  39. } );
  40. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  41. model.schema.register( 'caption', {
  42. allowIn: 'image',
  43. allowContentOf: '$block',
  44. isLimit: true
  45. } );
  46. model.schema.register( 'inlineWidget', {
  47. isObject: true,
  48. allowIn: [ '$block', '$clipboardHolder' ]
  49. } );
  50. model.schema.register( 'figure', {
  51. allowIn: '$root',
  52. allowAttributes: [ 'name', 'title' ]
  53. } );
  54. } );
  55. it( 'should not crash if there is no correct position for model selection', () => {
  56. setModelData( model, '' );
  57. expect( getModelData( model ) ).to.equal( '[]' );
  58. } );
  59. it( 'should react to structure changes', () => {
  60. setModelData( model, '<paragraph>[]foo</paragraph><image></image>' );
  61. model.change( writer => {
  62. writer.remove( modelRoot.getChild( 0 ) );
  63. } );
  64. expect( getModelData( model ) ).to.equal( '[<image></image>]' );
  65. } );
  66. it( 'should react to selection changes', () => {
  67. setModelData( model, '<paragraph>[]foo</paragraph><image></image>' );
  68. // <paragraph>foo</paragraph>[]<image></image>
  69. model.change( writer => {
  70. writer.setSelection(
  71. writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 1 ) )
  72. );
  73. } );
  74. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><image></image>' );
  75. } );
  76. describe( 'non-collapsed selection - table scenarios', () => {
  77. beforeEach( () => {
  78. setModelData( model,
  79. '<paragraph>[]foo</paragraph>' +
  80. '<table>' +
  81. '<tableRow>' +
  82. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  83. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  84. '</tableRow>' +
  85. '</table>' +
  86. '<paragraph>bar</paragraph>'
  87. );
  88. } );
  89. it( 'should fix #1 - range start outside table, end on table cell', () => {
  90. // <paragraph>f[oo</paragraph><table><tableRow><tableCell></tableCell>]<tableCell>...
  91. model.change( writer => {
  92. writer.setSelection( writer.createRange(
  93. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  94. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  95. ) );
  96. } );
  97. expect( getModelData( model ) ).to.equal(
  98. '<paragraph>f[oo</paragraph>' +
  99. '<table>' +
  100. '<tableRow>' +
  101. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  102. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  103. '</tableRow>' +
  104. '</table>]' +
  105. '<paragraph>bar</paragraph>'
  106. );
  107. } );
  108. it( 'should fix #2 - range start on table cell, end outside table', () => {
  109. // ...<table><tableRow><tableCell></tableCell>[<tableCell></tableCell></tableRow></table><paragraph>b]ar</paragraph>
  110. model.change( writer => {
  111. writer.setSelection( writer.createRange(
  112. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 ),
  113. writer.createPositionAt( modelRoot.getChild( 2 ), 1 )
  114. ) );
  115. } );
  116. expect( getModelData( model ) ).to.equal(
  117. '<paragraph>foo</paragraph>' +
  118. '[<table>' +
  119. '<tableRow>' +
  120. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  121. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  122. '</tableRow>' +
  123. '</table>' +
  124. '<paragraph>b]ar</paragraph>'
  125. );
  126. } );
  127. it( 'should fix #3', () => {
  128. // <paragraph>f[oo</paragraph><table>]<tableRow>...
  129. model.change( writer => {
  130. writer.setSelection( writer.createRange(
  131. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  132. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  133. ) );
  134. } );
  135. expect( getModelData( model ) ).to.equal(
  136. '<paragraph>f[oo</paragraph>' +
  137. '<table>' +
  138. '<tableRow>' +
  139. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  140. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  141. '</tableRow>' +
  142. '</table>]' +
  143. '<paragraph>bar</paragraph>'
  144. );
  145. } );
  146. it( 'should fix #4', () => {
  147. // <paragraph>foo</paragraph><table><tableRow><tableCell>a[aa</tableCell><tableCell>b]bb</tableCell>
  148. model.change( writer => {
  149. writer.setSelection( writer.createRange(
  150. writer.createPositionAt( modelRoot.getNodeByPath( [ 1, 0, 0, 0 ] ), 1 ),
  151. writer.createPositionAt( modelRoot.getNodeByPath( [ 1, 0, 1, 0 ] ), 2 )
  152. ) );
  153. } );
  154. expect( getModelData( model ) ).to.equal(
  155. '<paragraph>foo</paragraph>' +
  156. '[<table>' +
  157. '<tableRow>' +
  158. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  159. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  160. '</tableRow>' +
  161. '</table>]' +
  162. '<paragraph>bar</paragraph>'
  163. );
  164. } );
  165. it( 'should fix #5', () => {
  166. setModelData( model,
  167. '<paragraph>foo</paragraph>' +
  168. '<table>' +
  169. '<tableRow>' +
  170. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  171. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  172. '</tableRow>' +
  173. '</table>' +
  174. '[]' +
  175. '<table>' +
  176. '<tableRow>' +
  177. '<tableCell><paragraph>xxx</paragraph></tableCell>' +
  178. '<tableCell><paragraph>yyy</paragraph></tableCell>' +
  179. '</tableRow>' +
  180. '</table>' +
  181. '<paragraph>baz</paragraph>'
  182. );
  183. expect( getModelData( model ) ).to.equal(
  184. '<paragraph>foo</paragraph>' +
  185. '[<table>' +
  186. '<tableRow>' +
  187. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  188. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  189. '</tableRow>' +
  190. '</table>]' +
  191. '<table>' +
  192. '<tableRow>' +
  193. '<tableCell><paragraph>xxx</paragraph></tableCell>' +
  194. '<tableCell><paragraph>yyy</paragraph></tableCell>' +
  195. '</tableRow>' +
  196. '</table>' +
  197. '<paragraph>baz</paragraph>'
  198. );
  199. } );
  200. // There's a chance that this and the following test will not be up to date with
  201. // how the table feature is really implemented once we'll introduce row/cells/columns selection
  202. // in which case all these elements will need to be marked as objects.
  203. it( 'should fix #6 (element selection of not an object)', () => {
  204. setModelData( model,
  205. '<paragraph>foo</paragraph>' +
  206. '<table>' +
  207. '[<tableRow>' +
  208. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  209. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  210. '</tableRow>]' +
  211. '</table>' +
  212. '<paragraph>baz</paragraph>'
  213. );
  214. expect( getModelData( model ) ).to.equal(
  215. '<paragraph>foo</paragraph>' +
  216. '[<table>' +
  217. '<tableRow>' +
  218. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  219. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  220. '</tableRow>' +
  221. '</table>]' +
  222. '<paragraph>baz</paragraph>'
  223. );
  224. } );
  225. it( 'should fix #7 (element selection of non-objects)', () => {
  226. setModelData( model,
  227. '<paragraph>foo</paragraph>' +
  228. '<table>' +
  229. '[<tableRow>' +
  230. '<tableCell><paragraph>1</paragraph></tableCell>' +
  231. '<tableCell><paragraph>2</paragraph></tableCell>' +
  232. '</tableRow>' +
  233. '<tableRow>' +
  234. '<tableCell><paragraph>3</paragraph></tableCell>' +
  235. '<tableCell><paragraph>4</paragraph></tableCell>]' +
  236. '</tableRow>' +
  237. '<tableRow>' +
  238. '<tableCell><paragraph>5</paragraph></tableCell>' +
  239. '<tableCell><paragraph>6</paragraph></tableCell>' +
  240. '</tableRow>' +
  241. '</table>' +
  242. '<paragraph>baz</paragraph>'
  243. );
  244. expect( getModelData( model ) ).to.equal(
  245. '<paragraph>foo</paragraph>' +
  246. '[<table>' +
  247. '<tableRow>' +
  248. '<tableCell><paragraph>1</paragraph></tableCell>' +
  249. '<tableCell><paragraph>2</paragraph></tableCell>' +
  250. '</tableRow>' +
  251. '<tableRow>' +
  252. '<tableCell><paragraph>3</paragraph></tableCell>' +
  253. '<tableCell><paragraph>4</paragraph></tableCell>' +
  254. '</tableRow>' +
  255. '<tableRow>' +
  256. '<tableCell><paragraph>5</paragraph></tableCell>' +
  257. '<tableCell><paragraph>6</paragraph></tableCell>' +
  258. '</tableRow>' +
  259. '</table>]' +
  260. '<paragraph>baz</paragraph>'
  261. );
  262. } );
  263. it( 'should fix #8 (cross-limit selection which starts in a non-limit elements)', () => {
  264. model.schema.extend( 'paragraph', { allowIn: 'tableCell' } );
  265. setModelData( model,
  266. '<paragraph>foo</paragraph>' +
  267. '<table>' +
  268. '<tableRow>' +
  269. '<tableCell><paragraph>f[oo</paragraph></tableCell>' +
  270. '<tableCell><paragraph>b]ar</paragraph></tableCell>' +
  271. '</tableRow>' +
  272. '</table>' +
  273. '<paragraph>baz</paragraph>'
  274. );
  275. expect( getModelData( model ) ).to.equal(
  276. '<paragraph>foo</paragraph>' +
  277. '[<table>' +
  278. '<tableRow>' +
  279. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  280. '<tableCell><paragraph>bar</paragraph></tableCell>' +
  281. '</tableRow>' +
  282. '</table>]' +
  283. '<paragraph>baz</paragraph>'
  284. );
  285. } );
  286. it( 'should not fix #1 - selection over paragraphs outside table', () => {
  287. setModelData( model,
  288. '<paragraph>foo</paragraph>' +
  289. '<table>' +
  290. '<tableRow>' +
  291. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  292. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  293. '</tableRow>' +
  294. '</table>' +
  295. '<paragraph>b[ar</paragraph>' +
  296. '<paragraph>ba]z</paragraph>'
  297. );
  298. expect( getModelData( model ) ).to.equal(
  299. '<paragraph>foo</paragraph>' +
  300. '<table>' +
  301. '<tableRow>' +
  302. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  303. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  304. '</tableRow>' +
  305. '</table>' +
  306. '<paragraph>b[ar</paragraph>' +
  307. '<paragraph>ba]z</paragraph>'
  308. );
  309. } );
  310. it( 'should not fix #2 - selection over image in table', () => {
  311. setModelData( model,
  312. '<paragraph>foo</paragraph>' +
  313. '<table>' +
  314. '<tableRow>' +
  315. '<tableCell><paragraph>foo</paragraph><image></image></tableCell>' +
  316. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  317. '</tableRow>' +
  318. '</table>'
  319. );
  320. model.change( writer => {
  321. const image = model.document.getRoot().getNodeByPath( [ 1, 0, 0, 1 ] );
  322. writer.setSelection( writer.createRangeOn( image ) );
  323. } );
  324. expect( getModelData( model ) ).to.equal(
  325. '<paragraph>foo</paragraph>' +
  326. '<table>' +
  327. '<tableRow>' +
  328. '<tableCell><paragraph>foo</paragraph>[<image></image>]</tableCell>' +
  329. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  330. '</tableRow>' +
  331. '</table>'
  332. );
  333. } );
  334. it( 'should not fix #3 - selection over paragraph & image in table', () => {
  335. setModelData( model,
  336. '<paragraph>foo</paragraph>' +
  337. '<table>' +
  338. '<tableRow>' +
  339. '<tableCell><paragraph>foo</paragraph><image></image></tableCell>' +
  340. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  341. '</tableRow>' +
  342. '</table>'
  343. );
  344. model.change( writer => {
  345. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  346. writer.setSelection( writer.createRangeIn( tableCell ) );
  347. } );
  348. expect( getModelData( model ) ).to.equal(
  349. '<paragraph>foo</paragraph>' +
  350. '<table>' +
  351. '<tableRow>' +
  352. '<tableCell><paragraph>[foo</paragraph><image></image>]</tableCell>' +
  353. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  354. '</tableRow>' +
  355. '</table>'
  356. );
  357. } );
  358. it( 'should not fix #4 - selection over image & paragraph in table', () => {
  359. setModelData( model,
  360. '<paragraph>foo</paragraph>' +
  361. '<table>' +
  362. '<tableRow>' +
  363. '<tableCell><image></image><paragraph>foo</paragraph></tableCell>' +
  364. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  365. '</tableRow>' +
  366. '</table>'
  367. );
  368. model.change( writer => {
  369. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  370. writer.setSelection( writer.createRangeIn( tableCell ) );
  371. } );
  372. expect( getModelData( model ) ).to.equal(
  373. '<paragraph>foo</paragraph>' +
  374. '<table>' +
  375. '<tableRow>' +
  376. '<tableCell>[<image></image><paragraph>foo]</paragraph></tableCell>' +
  377. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  378. '</tableRow>' +
  379. '</table>'
  380. );
  381. } );
  382. it( 'should not fix #5 - selection over blockQuote in table', () => {
  383. model.schema.register( 'blockQuote', {
  384. allowWhere: '$block',
  385. allowContentOf: '$root'
  386. } );
  387. setModelData( model,
  388. '<paragraph>foo</paragraph>' +
  389. '<table>' +
  390. '<tableRow>' +
  391. '<tableCell><blockQuote><paragraph>foo</paragraph></blockQuote></tableCell>' +
  392. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  393. '</tableRow>' +
  394. '</table>'
  395. );
  396. model.change( writer => {
  397. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  398. writer.setSelection( writer.createRangeIn( tableCell ) );
  399. } );
  400. expect( getModelData( model ) ).to.equal(
  401. '<paragraph>foo</paragraph>' +
  402. '<table>' +
  403. '<tableRow>' +
  404. '<tableCell><blockQuote><paragraph>[foo]</paragraph></blockQuote></tableCell>' +
  405. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  406. '</tableRow>' +
  407. '</table>'
  408. );
  409. } );
  410. it( 'should fix multiple ranges #1', () => {
  411. model.change( writer => {
  412. const ranges = [
  413. writer.createRange(
  414. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  415. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  416. ),
  417. writer.createRange(
  418. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  419. writer.createPositionFromPath( modelRoot, [ 1, 1 ] )
  420. )
  421. ];
  422. writer.setSelection( ranges );
  423. } );
  424. expect( getModelData( model ) ).to.equal(
  425. '<paragraph>f[oo</paragraph>' +
  426. '<table>' +
  427. '<tableRow>' +
  428. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  429. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  430. '</tableRow>' +
  431. '</table>]' +
  432. '<paragraph>bar</paragraph>'
  433. );
  434. } );
  435. it( 'should fix multiple ranges #2', () => {
  436. model.change( writer => {
  437. const ranges = [
  438. writer.createRange(
  439. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  440. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  441. ),
  442. writer.createRange(
  443. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  444. writer.createPositionFromPath( modelRoot, [ 2, 2 ] )
  445. )
  446. ];
  447. writer.setSelection( ranges );
  448. } );
  449. expect( getModelData( model ) ).to.equal(
  450. '<paragraph>f[oo</paragraph>' +
  451. '<table>' +
  452. '<tableRow>' +
  453. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  454. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  455. '</tableRow>' +
  456. '</table>' +
  457. '<paragraph>ba]r</paragraph>'
  458. );
  459. } );
  460. it( 'should fix multiple ranges #3', () => {
  461. setModelData( model,
  462. '<paragraph>foo</paragraph>' +
  463. '<table>' +
  464. '<tableRow>' +
  465. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  466. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  467. '</tableRow>' +
  468. '<tableRow>]' +
  469. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  470. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  471. '</tableRow>' +
  472. '<tableRow>]' +
  473. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  474. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  475. '</tableRow>' +
  476. '<tableRow>]' +
  477. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  478. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  479. '</tableRow>' +
  480. '</table>' +
  481. '<paragraph>b]az</paragraph>'
  482. );
  483. expect( getModelData( model ) ).to.equal(
  484. '<paragraph>foo</paragraph>' +
  485. '[<table>' +
  486. '<tableRow>' +
  487. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  488. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  489. '</tableRow>' +
  490. '<tableRow>' +
  491. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  492. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  493. '</tableRow>' +
  494. '<tableRow>' +
  495. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  496. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  497. '</tableRow>' +
  498. '<tableRow>' +
  499. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  500. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  501. '</tableRow>' +
  502. '</table>' +
  503. '<paragraph>b]az</paragraph>'
  504. );
  505. } );
  506. it( 'should fix multiple ranges #4', () => {
  507. model.change( writer => {
  508. const ranges = [
  509. writer.createRange(
  510. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  511. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  512. ),
  513. writer.createRange(
  514. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  515. writer.createPositionFromPath( modelRoot, [ 2, 1 ] )
  516. ),
  517. writer.createRange(
  518. writer.createPositionFromPath( modelRoot, [ 2, 2 ] ),
  519. writer.createPositionFromPath( modelRoot, [ 2, 3 ] )
  520. )
  521. ];
  522. writer.setSelection( ranges );
  523. } );
  524. expect( getModelData( model ) ).to.equal(
  525. '<paragraph>f[oo</paragraph>' +
  526. '<table>' +
  527. '<tableRow>' +
  528. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  529. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  530. '</tableRow>' +
  531. '</table>' +
  532. '<paragraph>bar]</paragraph>'
  533. );
  534. } );
  535. } );
  536. describe( 'non-collapsed selection - image scenarios', () => {
  537. beforeEach( () => {
  538. setModelData( model,
  539. '<paragraph>[]foo</paragraph>' +
  540. '<image>' +
  541. '<caption>xxx</caption>' +
  542. '</image>' +
  543. '<paragraph>bar</paragraph>'
  544. );
  545. } );
  546. it( 'should fix #1 (crossing object and limit boundaries)', () => {
  547. model.change( writer => {
  548. // <paragraph>f[oo</paragraph><image><caption>x]xx</caption>...
  549. writer.setSelection( writer.createRange(
  550. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  551. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  552. ) );
  553. } );
  554. expect( getModelData( model ) ).to.equal(
  555. '<paragraph>f[oo</paragraph>' +
  556. '<image>' +
  557. '<caption>xxx</caption>' +
  558. '</image>]' +
  559. '<paragraph>bar</paragraph>'
  560. );
  561. } );
  562. it( 'should fix #2 (crossing object boundary)', () => {
  563. model.change( writer => {
  564. // <paragraph>f[oo</paragraph><image>]<caption>xxx</caption>...
  565. writer.setSelection( writer.createRange(
  566. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  567. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  568. ) );
  569. } );
  570. expect( getModelData( model ) ).to.equal(
  571. '<paragraph>f[oo</paragraph>' +
  572. '<image>' +
  573. '<caption>xxx</caption>' +
  574. '</image>]' +
  575. '<paragraph>bar</paragraph>'
  576. );
  577. } );
  578. it( 'should fix #3 (crossing object boundary)', () => {
  579. model.change( writer => {
  580. // <paragraph>f[oo</paragraph><image><caption>xxx</caption>]</image>...
  581. writer.setSelection( writer.createRange(
  582. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  583. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  584. ) );
  585. } );
  586. expect( getModelData( model ) ).to.equal(
  587. '<paragraph>f[oo</paragraph>' +
  588. '<image>' +
  589. '<caption>xxx</caption>' +
  590. '</image>]' +
  591. '<paragraph>bar</paragraph>'
  592. );
  593. } );
  594. it( 'should fix #4 (element selection of not an object)', () => {
  595. model.change( writer => {
  596. // <paragraph>foo</paragraph><image>[<caption>xxx</caption>]</image>...
  597. writer.setSelection( writer.createRange(
  598. writer.createPositionAt( modelRoot.getChild( 1 ), 0 ),
  599. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  600. ) );
  601. } );
  602. expect( getModelData( model ) ).to.equal(
  603. '<paragraph>foo</paragraph>' +
  604. '[<image>' +
  605. '<caption>xxx</caption>' +
  606. '</image>]' +
  607. '<paragraph>bar</paragraph>'
  608. );
  609. } );
  610. it( 'should not fix #1 (element selection of an object)', () => {
  611. model.change( writer => {
  612. // <paragraph>foo</paragraph>[<image><caption>xxx</caption></image>]...
  613. writer.setSelection( writer.createRange(
  614. writer.createPositionAt( modelRoot, 1 ),
  615. writer.createPositionAt( modelRoot, 2 )
  616. ) );
  617. } );
  618. expect( getModelData( model ) ).to.equal(
  619. '<paragraph>foo</paragraph>' +
  620. '[<image>' +
  621. '<caption>xxx</caption>' +
  622. '</image>]' +
  623. '<paragraph>bar</paragraph>'
  624. );
  625. } );
  626. it( 'should not fix #2 (inside a limit)', () => {
  627. model.change( writer => {
  628. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  629. // <paragraph>foo</paragraph><image><caption>[xxx]</caption></image>...
  630. writer.setSelection( writer.createRange(
  631. writer.createPositionAt( caption, 0 ),
  632. writer.createPositionAt( caption, 3 )
  633. ) );
  634. } );
  635. expect( getModelData( model ) ).to.equal(
  636. '<paragraph>foo</paragraph>' +
  637. '<image>' +
  638. '<caption>[xxx]</caption>' +
  639. '</image>' +
  640. '<paragraph>bar</paragraph>'
  641. );
  642. } );
  643. it( 'should not fix #3 (inside a limit - partial text selection)', () => {
  644. model.change( writer => {
  645. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  646. // <paragraph>foo</paragraph><image><caption>[xx]x</caption></image>...
  647. writer.setSelection( writer.createRange(
  648. writer.createPositionAt( caption, 0 ),
  649. writer.createPositionAt( caption, 2 )
  650. ) );
  651. } );
  652. expect( getModelData( model ) ).to.equal(
  653. '<paragraph>foo</paragraph>' +
  654. '<image>' +
  655. '<caption>[xx]x</caption>' +
  656. '</image>' +
  657. '<paragraph>bar</paragraph>'
  658. );
  659. } );
  660. it( 'should not fix #4 (inside a limit - partial text selection)', () => {
  661. model.change( writer => {
  662. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  663. // <paragraph>foo</paragraph><image><caption>x[xx]</caption></image>...
  664. writer.setSelection( writer.createRange(
  665. writer.createPositionAt( caption, 1 ),
  666. writer.createPositionAt( caption, 3 )
  667. ) );
  668. } );
  669. expect( getModelData( model ) ).to.equal(
  670. '<paragraph>foo</paragraph>' +
  671. '<image>' +
  672. '<caption>x[xx]</caption>' +
  673. '</image>' +
  674. '<paragraph>bar</paragraph>'
  675. );
  676. } );
  677. it( 'should not fix #5 (selection in root on non limit element that doesn\'t allow text)', () => {
  678. setModelData( model,
  679. '[<figure></figure>]'
  680. );
  681. expect( getModelData( model ) ).to.equal(
  682. '[<figure></figure>]'
  683. );
  684. } );
  685. } );
  686. describe( 'non-collapsed selection - other scenarios', () => {
  687. it( 'should fix #1 (element selection of not an object)', () => {
  688. setModelData( model,
  689. '<paragraph>aaa</paragraph>' +
  690. '[<paragraph>bbb</paragraph>]' +
  691. '<paragraph>ccc</paragraph>'
  692. );
  693. expect( getModelData( model ) ).to.equal(
  694. '<paragraph>aaa</paragraph>' +
  695. '<paragraph>[bbb]</paragraph>' +
  696. '<paragraph>ccc</paragraph>'
  697. );
  698. } );
  699. it( 'should fix #2 (elements selection of not an object)', () => {
  700. setModelData( model,
  701. '<paragraph>aaa</paragraph>' +
  702. '[<paragraph>bbb</paragraph>' +
  703. '<paragraph>ccc</paragraph>]'
  704. );
  705. expect( getModelData( model ) ).to.equal(
  706. '<paragraph>aaa</paragraph>' +
  707. '<paragraph>[bbb</paragraph>' +
  708. '<paragraph>ccc]</paragraph>'
  709. );
  710. } );
  711. it( 'should fix #3 (partial selection of not an object)', () => {
  712. setModelData( model,
  713. '<paragraph>aaa</paragraph>' +
  714. '[<paragraph>bbb</paragraph>' +
  715. '<paragraph>ccc]</paragraph>'
  716. );
  717. expect( getModelData( model ) ).to.equal(
  718. '<paragraph>aaa</paragraph>' +
  719. '<paragraph>[bbb</paragraph>' +
  720. '<paragraph>ccc]</paragraph>'
  721. );
  722. } );
  723. it( 'should fix #4 (partial selection of not an object)', () => {
  724. setModelData( model,
  725. '<paragraph>aaa</paragraph>' +
  726. '<paragraph>b[bb</paragraph>]' +
  727. '<paragraph>ccc</paragraph>'
  728. );
  729. expect( getModelData( model ) ).to.equal(
  730. '<paragraph>aaa</paragraph>' +
  731. '<paragraph>b[bb]</paragraph>' +
  732. '<paragraph>ccc</paragraph>'
  733. );
  734. } );
  735. it( 'should fix #5 (partial selection of not an object)', () => {
  736. setModelData( model,
  737. '<paragraph>aaa</paragraph>' +
  738. '[<paragraph>bb]b</paragraph>' +
  739. '<paragraph>ccc</paragraph>'
  740. );
  741. expect( getModelData( model ) ).to.equal(
  742. '<paragraph>aaa</paragraph>' +
  743. '<paragraph>[bb]b</paragraph>' +
  744. '<paragraph>ccc</paragraph>'
  745. );
  746. } );
  747. it( 'should fix #6 (selection must not cross a limit element; starts in a root)', () => {
  748. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  749. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  750. model.schema.register( 'c', { allowIn: 'b' } );
  751. model.schema.extend( '$text', { allowIn: 'c' } );
  752. setModelData( model,
  753. '<a><b><c>[</c></b></a>]'
  754. );
  755. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  756. } );
  757. it( 'should fix #7 (selection must not cross a limit element; ends in a root)', () => {
  758. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  759. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  760. model.schema.register( 'c', { allowIn: 'b' } );
  761. model.schema.extend( '$text', { allowIn: 'c' } );
  762. setModelData( model,
  763. '[<a><b><c>]</c></b></a>'
  764. );
  765. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  766. } );
  767. it( 'should fix #8 (selection must not cross a limit element; starts in a non-limit)', () => {
  768. model.schema.register( 'div', { allowIn: '$root' } );
  769. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  770. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  771. model.schema.register( 'c', { allowIn: 'b' } );
  772. model.schema.extend( '$text', { allowIn: 'c' } );
  773. setModelData( model,
  774. '<div>[<a><b><c>]</c></b></a></div>'
  775. );
  776. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  777. } );
  778. it( 'should fix #9 (selection must not cross a limit element; ends in a non-limit)', () => {
  779. model.schema.register( 'div', { allowIn: '$root' } );
  780. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  781. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  782. model.schema.register( 'c', { allowIn: 'b' } );
  783. model.schema.extend( '$text', { allowIn: 'c' } );
  784. setModelData( model,
  785. '<div><a><b><c>[</c></b></a>]</div>'
  786. );
  787. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  788. } );
  789. it( 'should not fix #1 (selection on text node)', () => {
  790. setModelData( model, '<paragraph>foob[a]r</paragraph>', { lastRangeBackward: true } );
  791. expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
  792. } );
  793. it( 'should not fix #2', () => {
  794. setModelData( model,
  795. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  796. );
  797. expect( getModelData( model ) ).to.equal(
  798. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  799. );
  800. } );
  801. it( 'should not fix #3', () => {
  802. setModelData( model,
  803. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  804. );
  805. expect( getModelData( model ) ).to.equal(
  806. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  807. );
  808. } );
  809. it( 'should not fix #4 - object in object', () => {
  810. model.schema.register( 'div', {
  811. allowIn: [ '$root', 'div' ],
  812. isObject: true
  813. } );
  814. setModelData( model, '<div>[<div></div>]</div>' );
  815. model.change( writer => {
  816. const innerDiv = model.document.getRoot().getNodeByPath( [ 0, 0 ] );
  817. writer.setSelection( writer.createRangeOn( innerDiv ) );
  818. } );
  819. expect( getModelData( model ) ).to.equal( '<div>[<div></div>]</div>' );
  820. } );
  821. } );
  822. describe( 'collapsed selection', () => {
  823. beforeEach( () => {
  824. setModelData( model,
  825. '<paragraph>[]foo</paragraph>' +
  826. '<table>' +
  827. '<tableRow>' +
  828. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  829. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  830. '</tableRow>' +
  831. '</table>' +
  832. '<paragraph>bar</paragraph>'
  833. );
  834. } );
  835. it( 'should fix #1', () => {
  836. // <table>[]<tableRow>...
  837. model.change( writer => {
  838. writer.setSelection(
  839. writer.createRange( writer.createPositionAt( modelRoot.getChild( 1 ), 0 ) )
  840. );
  841. } );
  842. expect( getModelData( model ) ).to.equal(
  843. '<paragraph>foo[]</paragraph>' +
  844. '<table>' +
  845. '<tableRow>' +
  846. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  847. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  848. '</tableRow>' +
  849. '</table>' +
  850. '<paragraph>bar</paragraph>'
  851. );
  852. } );
  853. it( 'should fix #2', () => {
  854. // <table><tableRow>[]<tableCell>...
  855. model.change( writer => {
  856. const row = modelRoot.getChild( 1 ).getChild( 0 );
  857. writer.setSelection(
  858. writer.createRange( writer.createPositionAt( row, 0 ) )
  859. );
  860. } );
  861. expect( getModelData( model ) ).to.equal(
  862. '<paragraph>foo</paragraph>' +
  863. '<table>' +
  864. '<tableRow>' +
  865. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  866. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  867. '</tableRow>' +
  868. '</table>' +
  869. '<paragraph>bar</paragraph>'
  870. );
  871. } );
  872. it( 'should fix multiple ranges #1', () => {
  873. // []<paragraph>foo</paragraph>[]<table>...
  874. model.change( writer => {
  875. writer.setSelection(
  876. [
  877. writer.createRange( writer.createPositionAt( modelRoot, 0 ) ),
  878. writer.createRange( writer.createPositionAt( modelRoot, 1 ) )
  879. ]
  880. );
  881. } );
  882. expect( getModelData( model ) ).to.equal(
  883. '<paragraph>[foo]</paragraph>' +
  884. '<table>' +
  885. '<tableRow>' +
  886. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  887. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  888. '</tableRow>' +
  889. '</table>' +
  890. '<paragraph>bar</paragraph>'
  891. );
  892. } );
  893. } );
  894. } );
  895. } );