selection-post-fixer.js 35 KB

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