selection-post-fixer.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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 #3 - 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 fix multiple ranges #1', () => {
  383. model.change( writer => {
  384. const ranges = [
  385. writer.createRange(
  386. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  387. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  388. ),
  389. writer.createRange(
  390. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  391. writer.createPositionFromPath( modelRoot, [ 1, 1 ] )
  392. )
  393. ];
  394. writer.setSelection( ranges );
  395. } );
  396. expect( getModelData( model ) ).to.equal(
  397. '<paragraph>f[oo</paragraph>' +
  398. '<table>' +
  399. '<tableRow>' +
  400. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  401. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  402. '</tableRow>' +
  403. '</table>]' +
  404. '<paragraph>bar</paragraph>'
  405. );
  406. } );
  407. it( 'should fix multiple ranges #2', () => {
  408. model.change( writer => {
  409. const ranges = [
  410. writer.createRange(
  411. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  412. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  413. ),
  414. writer.createRange(
  415. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  416. writer.createPositionFromPath( modelRoot, [ 2, 2 ] )
  417. )
  418. ];
  419. writer.setSelection( ranges );
  420. } );
  421. expect( getModelData( model ) ).to.equal(
  422. '<paragraph>f[oo</paragraph>' +
  423. '<table>' +
  424. '<tableRow>' +
  425. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  426. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  427. '</tableRow>' +
  428. '</table>' +
  429. '<paragraph>ba]r</paragraph>'
  430. );
  431. } );
  432. it( 'should fix multiple ranges #3', () => {
  433. setModelData( model,
  434. '<paragraph>foo</paragraph>' +
  435. '<table>' +
  436. '<tableRow>' +
  437. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  438. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  439. '</tableRow>' +
  440. '<tableRow>]' +
  441. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  442. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  443. '</tableRow>' +
  444. '<tableRow>]' +
  445. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  446. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  447. '</tableRow>' +
  448. '<tableRow>]' +
  449. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  450. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  451. '</tableRow>' +
  452. '</table>' +
  453. '<paragraph>b]az</paragraph>'
  454. );
  455. expect( getModelData( model ) ).to.equal(
  456. '<paragraph>foo</paragraph>' +
  457. '[<table>' +
  458. '<tableRow>' +
  459. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  460. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  461. '</tableRow>' +
  462. '<tableRow>' +
  463. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  464. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  465. '</tableRow>' +
  466. '<tableRow>' +
  467. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  468. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  469. '</tableRow>' +
  470. '<tableRow>' +
  471. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  472. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  473. '</tableRow>' +
  474. '</table>' +
  475. '<paragraph>b]az</paragraph>'
  476. );
  477. } );
  478. it( 'should fix multiple ranges #4', () => {
  479. model.change( writer => {
  480. const ranges = [
  481. writer.createRange(
  482. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  483. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  484. ),
  485. writer.createRange(
  486. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  487. writer.createPositionFromPath( modelRoot, [ 2, 1 ] )
  488. ),
  489. writer.createRange(
  490. writer.createPositionFromPath( modelRoot, [ 2, 2 ] ),
  491. writer.createPositionFromPath( modelRoot, [ 2, 3 ] )
  492. )
  493. ];
  494. writer.setSelection( ranges );
  495. } );
  496. expect( getModelData( model ) ).to.equal(
  497. '<paragraph>f[oo</paragraph>' +
  498. '<table>' +
  499. '<tableRow>' +
  500. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  501. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  502. '</tableRow>' +
  503. '</table>' +
  504. '<paragraph>bar]</paragraph>'
  505. );
  506. } );
  507. } );
  508. describe( 'non-collapsed selection - image scenarios', () => {
  509. beforeEach( () => {
  510. setModelData( model,
  511. '<paragraph>[]foo</paragraph>' +
  512. '<image>' +
  513. '<caption>xxx</caption>' +
  514. '</image>' +
  515. '<paragraph>bar</paragraph>'
  516. );
  517. } );
  518. it( 'should fix #1 (crossing object and limit boundaries)', () => {
  519. model.change( writer => {
  520. // <paragraph>f[oo</paragraph><image><caption>x]xx</caption>...
  521. writer.setSelection( writer.createRange(
  522. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  523. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  524. ) );
  525. } );
  526. expect( getModelData( model ) ).to.equal(
  527. '<paragraph>f[oo</paragraph>' +
  528. '<image>' +
  529. '<caption>xxx</caption>' +
  530. '</image>]' +
  531. '<paragraph>bar</paragraph>'
  532. );
  533. } );
  534. it( 'should fix #2 (crossing object boundary)', () => {
  535. model.change( writer => {
  536. // <paragraph>f[oo</paragraph><image>]<caption>xxx</caption>...
  537. writer.setSelection( writer.createRange(
  538. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  539. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  540. ) );
  541. } );
  542. expect( getModelData( model ) ).to.equal(
  543. '<paragraph>f[oo</paragraph>' +
  544. '<image>' +
  545. '<caption>xxx</caption>' +
  546. '</image>]' +
  547. '<paragraph>bar</paragraph>'
  548. );
  549. } );
  550. it( 'should fix #3 (crossing object boundary)', () => {
  551. model.change( writer => {
  552. // <paragraph>f[oo</paragraph><image><caption>xxx</caption>]</image>...
  553. writer.setSelection( writer.createRange(
  554. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  555. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  556. ) );
  557. } );
  558. expect( getModelData( model ) ).to.equal(
  559. '<paragraph>f[oo</paragraph>' +
  560. '<image>' +
  561. '<caption>xxx</caption>' +
  562. '</image>]' +
  563. '<paragraph>bar</paragraph>'
  564. );
  565. } );
  566. it( 'should fix #4 (element selection of not an object)', () => {
  567. model.change( writer => {
  568. // <paragraph>foo</paragraph><image>[<caption>xxx</caption>]</image>...
  569. writer.setSelection( writer.createRange(
  570. writer.createPositionAt( modelRoot.getChild( 1 ), 0 ),
  571. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  572. ) );
  573. } );
  574. expect( getModelData( model ) ).to.equal(
  575. '<paragraph>foo</paragraph>' +
  576. '[<image>' +
  577. '<caption>xxx</caption>' +
  578. '</image>]' +
  579. '<paragraph>bar</paragraph>'
  580. );
  581. } );
  582. it( 'should not fix #1 (element selection of an object)', () => {
  583. model.change( writer => {
  584. // <paragraph>foo</paragraph>[<image><caption>xxx</caption></image>]...
  585. writer.setSelection( writer.createRange(
  586. writer.createPositionAt( modelRoot, 1 ),
  587. writer.createPositionAt( modelRoot, 2 )
  588. ) );
  589. } );
  590. expect( getModelData( model ) ).to.equal(
  591. '<paragraph>foo</paragraph>' +
  592. '[<image>' +
  593. '<caption>xxx</caption>' +
  594. '</image>]' +
  595. '<paragraph>bar</paragraph>'
  596. );
  597. } );
  598. it( 'should not fix #2 (inside a limit)', () => {
  599. model.change( writer => {
  600. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  601. // <paragraph>foo</paragraph><image><caption>[xxx]</caption></image>...
  602. writer.setSelection( writer.createRange(
  603. writer.createPositionAt( caption, 0 ),
  604. writer.createPositionAt( caption, 3 )
  605. ) );
  606. } );
  607. expect( getModelData( model ) ).to.equal(
  608. '<paragraph>foo</paragraph>' +
  609. '<image>' +
  610. '<caption>[xxx]</caption>' +
  611. '</image>' +
  612. '<paragraph>bar</paragraph>'
  613. );
  614. } );
  615. it( 'should not fix #3 (inside a limit - partial text selection)', () => {
  616. model.change( writer => {
  617. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  618. // <paragraph>foo</paragraph><image><caption>[xx]x</caption></image>...
  619. writer.setSelection( writer.createRange(
  620. writer.createPositionAt( caption, 0 ),
  621. writer.createPositionAt( caption, 2 )
  622. ) );
  623. } );
  624. expect( getModelData( model ) ).to.equal(
  625. '<paragraph>foo</paragraph>' +
  626. '<image>' +
  627. '<caption>[xx]x</caption>' +
  628. '</image>' +
  629. '<paragraph>bar</paragraph>'
  630. );
  631. } );
  632. it( 'should not fix #4 (inside a limit - partial text selection)', () => {
  633. model.change( writer => {
  634. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  635. // <paragraph>foo</paragraph><image><caption>x[xx]</caption></image>...
  636. writer.setSelection( writer.createRange(
  637. writer.createPositionAt( caption, 1 ),
  638. writer.createPositionAt( caption, 3 )
  639. ) );
  640. } );
  641. expect( getModelData( model ) ).to.equal(
  642. '<paragraph>foo</paragraph>' +
  643. '<image>' +
  644. '<caption>x[xx]</caption>' +
  645. '</image>' +
  646. '<paragraph>bar</paragraph>'
  647. );
  648. } );
  649. it( 'should not fix #5 (selection in root on non limit element that doesn\'t allow text)', () => {
  650. setModelData( model,
  651. '[<figure></figure>]'
  652. );
  653. expect( getModelData( model ) ).to.equal(
  654. '[<figure></figure>]'
  655. );
  656. } );
  657. } );
  658. describe( 'non-collapsed selection - other scenarios', () => {
  659. it( 'should fix #1 (element selection of not an object)', () => {
  660. setModelData( model,
  661. '<paragraph>aaa</paragraph>' +
  662. '[<paragraph>bbb</paragraph>]' +
  663. '<paragraph>ccc</paragraph>'
  664. );
  665. expect( getModelData( model ) ).to.equal(
  666. '<paragraph>aaa</paragraph>' +
  667. '<paragraph>[bbb]</paragraph>' +
  668. '<paragraph>ccc</paragraph>'
  669. );
  670. } );
  671. it( 'should fix #2 (elements selection of not an object)', () => {
  672. setModelData( model,
  673. '<paragraph>aaa</paragraph>' +
  674. '[<paragraph>bbb</paragraph>' +
  675. '<paragraph>ccc</paragraph>]'
  676. );
  677. expect( getModelData( model ) ).to.equal(
  678. '<paragraph>aaa</paragraph>' +
  679. '<paragraph>[bbb</paragraph>' +
  680. '<paragraph>ccc]</paragraph>'
  681. );
  682. } );
  683. it( 'should fix #3 (partial selection of not an object)', () => {
  684. setModelData( model,
  685. '<paragraph>aaa</paragraph>' +
  686. '[<paragraph>bbb</paragraph>' +
  687. '<paragraph>ccc]</paragraph>'
  688. );
  689. expect( getModelData( model ) ).to.equal(
  690. '<paragraph>aaa</paragraph>' +
  691. '<paragraph>[bbb</paragraph>' +
  692. '<paragraph>ccc]</paragraph>'
  693. );
  694. } );
  695. it( 'should fix #4 (partial selection of not an object)', () => {
  696. setModelData( model,
  697. '<paragraph>aaa</paragraph>' +
  698. '<paragraph>b[bb</paragraph>]' +
  699. '<paragraph>ccc</paragraph>'
  700. );
  701. expect( getModelData( model ) ).to.equal(
  702. '<paragraph>aaa</paragraph>' +
  703. '<paragraph>b[bb]</paragraph>' +
  704. '<paragraph>ccc</paragraph>'
  705. );
  706. } );
  707. it( 'should fix #5 (partial selection of not an object)', () => {
  708. setModelData( model,
  709. '<paragraph>aaa</paragraph>' +
  710. '[<paragraph>bb]b</paragraph>' +
  711. '<paragraph>ccc</paragraph>'
  712. );
  713. expect( getModelData( model ) ).to.equal(
  714. '<paragraph>aaa</paragraph>' +
  715. '<paragraph>[bb]b</paragraph>' +
  716. '<paragraph>ccc</paragraph>'
  717. );
  718. } );
  719. it( 'should fix #6 (selection must not cross a limit element; starts in a root)', () => {
  720. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  721. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  722. model.schema.register( 'c', { allowIn: 'b' } );
  723. model.schema.extend( '$text', { allowIn: 'c' } );
  724. setModelData( model,
  725. '<a><b><c>[</c></b></a>]'
  726. );
  727. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  728. } );
  729. it( 'should fix #7 (selection must not cross a limit element; ends in a root)', () => {
  730. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  731. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  732. model.schema.register( 'c', { allowIn: 'b' } );
  733. model.schema.extend( '$text', { allowIn: 'c' } );
  734. setModelData( model,
  735. '[<a><b><c>]</c></b></a>'
  736. );
  737. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  738. } );
  739. it( 'should fix #8 (selection must not cross a limit element; starts in a non-limit)', () => {
  740. model.schema.register( 'div', { allowIn: '$root' } );
  741. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  742. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  743. model.schema.register( 'c', { allowIn: 'b' } );
  744. model.schema.extend( '$text', { allowIn: 'c' } );
  745. setModelData( model,
  746. '<div>[<a><b><c>]</c></b></a></div>'
  747. );
  748. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  749. } );
  750. it( 'should fix #9 (selection must not cross a limit element; ends in a non-limit)', () => {
  751. model.schema.register( 'div', { allowIn: '$root' } );
  752. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  753. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  754. model.schema.register( 'c', { allowIn: 'b' } );
  755. model.schema.extend( '$text', { allowIn: 'c' } );
  756. setModelData( model,
  757. '<div><a><b><c>[</c></b></a>]</div>'
  758. );
  759. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  760. } );
  761. it( 'should not fix #1 (selection on text node)', () => {
  762. setModelData( model, '<paragraph>foob[a]r</paragraph>', { lastRangeBackward: true } );
  763. expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
  764. } );
  765. it( 'should not fix #2', () => {
  766. setModelData( model,
  767. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  768. );
  769. expect( getModelData( model ) ).to.equal(
  770. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  771. );
  772. } );
  773. it( 'should not fix #3', () => {
  774. setModelData( model,
  775. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  776. );
  777. expect( getModelData( model ) ).to.equal(
  778. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  779. );
  780. } );
  781. } );
  782. describe( 'collapsed selection', () => {
  783. beforeEach( () => {
  784. setModelData( model,
  785. '<paragraph>[]foo</paragraph>' +
  786. '<table>' +
  787. '<tableRow>' +
  788. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  789. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  790. '</tableRow>' +
  791. '</table>' +
  792. '<paragraph>bar</paragraph>'
  793. );
  794. } );
  795. it( 'should fix #1', () => {
  796. // <table>[]<tableRow>...
  797. model.change( writer => {
  798. writer.setSelection(
  799. writer.createRange( writer.createPositionAt( modelRoot.getChild( 1 ), 0 ) )
  800. );
  801. } );
  802. expect( getModelData( model ) ).to.equal(
  803. '<paragraph>foo[]</paragraph>' +
  804. '<table>' +
  805. '<tableRow>' +
  806. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  807. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  808. '</tableRow>' +
  809. '</table>' +
  810. '<paragraph>bar</paragraph>'
  811. );
  812. } );
  813. it( 'should fix #2', () => {
  814. // <table><tableRow>[]<tableCell>...
  815. model.change( writer => {
  816. const row = modelRoot.getChild( 1 ).getChild( 0 );
  817. writer.setSelection(
  818. writer.createRange( writer.createPositionAt( row, 0 ) )
  819. );
  820. } );
  821. expect( getModelData( model ) ).to.equal(
  822. '<paragraph>foo</paragraph>' +
  823. '<table>' +
  824. '<tableRow>' +
  825. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  826. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  827. '</tableRow>' +
  828. '</table>' +
  829. '<paragraph>bar</paragraph>'
  830. );
  831. } );
  832. it( 'should fix multiple ranges #1', () => {
  833. // []<paragraph>foo</paragraph>[]<table>...
  834. model.change( writer => {
  835. writer.setSelection(
  836. [
  837. writer.createRange( writer.createPositionAt( modelRoot, 0 ) ),
  838. writer.createRange( writer.createPositionAt( modelRoot, 1 ) )
  839. ]
  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. } );
  854. } );
  855. } );