8
0

selection-post-fixer.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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 ModelPosition from '../../../src/model/position';
  7. import ModelRange from '../../../src/model/range';
  8. import { injectSelectionPostFixer } from '../../../src/model/utils/selection-post-fixer';
  9. import { getData as getModelData, setData as setModelData } from '../../../src/dev-utils/model';
  10. describe( 'Selection post-fixer', () => {
  11. describe( 'injectSelectionPostFixer()', () => {
  12. it( 'is a function', () => {
  13. expect( injectSelectionPostFixer ).to.be.a( 'function' );
  14. } );
  15. } );
  16. describe( 'injected behavior', () => {
  17. let model, modelRoot;
  18. beforeEach( () => {
  19. model = new Model();
  20. modelRoot = model.document.createRoot();
  21. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  22. model.schema.register( 'table', {
  23. allowWhere: '$block',
  24. allowAttributes: [ 'headingRows', 'headingColumns' ],
  25. isObject: true
  26. } );
  27. model.schema.register( 'tableRow', {
  28. allowIn: 'table',
  29. isLimit: true
  30. } );
  31. model.schema.register( 'tableCell', {
  32. allowIn: 'tableRow',
  33. allowAttributes: [ 'colspan', 'rowspan' ],
  34. isObject: true
  35. } );
  36. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  37. model.schema.register( 'image', {
  38. isObject: true,
  39. isBlock: true,
  40. allowWhere: '$block'
  41. } );
  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. ModelRange.createFromParentsAndOffsets( modelRoot, 1, 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( ModelRange.createFromParentsAndOffsets(
  94. modelRoot.getChild( 0 ), 1,
  95. 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( ModelRange.createFromParentsAndOffsets(
  113. modelRoot.getChild( 1 ).getChild( 0 ), 1,
  114. 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( ModelRange.createFromParentsAndOffsets(
  132. modelRoot.getChild( 0 ), 1,
  133. 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( ModelRange.createFromParentsAndOffsets(
  151. modelRoot.getNodeByPath( [ 1, 0, 0, 0 ] ), 1,
  152. 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. // There's a chance that this and the following test will not be up to date with
  167. // how the table feature is really implemented once we'll introduce row/cells/columns selection
  168. // in which case all these elements will need to be marked as objects.
  169. it( 'should fix #6 (element selection of not an object)', () => {
  170. setModelData( model,
  171. '<paragraph>foo</paragraph>' +
  172. '<table>' +
  173. '[<tableRow>' +
  174. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  175. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  176. '</tableRow>]' +
  177. '</table>' +
  178. '<paragraph>baz</paragraph>'
  179. );
  180. expect( getModelData( model ) ).to.equal(
  181. '<paragraph>foo</paragraph>' +
  182. '[<table>' +
  183. '<tableRow>' +
  184. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  185. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  186. '</tableRow>' +
  187. '</table>]' +
  188. '<paragraph>baz</paragraph>'
  189. );
  190. } );
  191. it( 'should fix #7 (element selection of non-objects)', () => {
  192. setModelData( model,
  193. '<paragraph>foo</paragraph>' +
  194. '<table>' +
  195. '[<tableRow>' +
  196. '<tableCell><paragraph>1</paragraph></tableCell>' +
  197. '<tableCell><paragraph>2</paragraph></tableCell>' +
  198. '</tableRow>' +
  199. '<tableRow>' +
  200. '<tableCell><paragraph>3</paragraph></tableCell>' +
  201. '<tableCell><paragraph>4</paragraph></tableCell>]' +
  202. '</tableRow>' +
  203. '<tableRow>' +
  204. '<tableCell><paragraph>5</paragraph></tableCell>' +
  205. '<tableCell><paragraph>6</paragraph></tableCell>' +
  206. '</tableRow>' +
  207. '</table>' +
  208. '<paragraph>baz</paragraph>'
  209. );
  210. expect( getModelData( model ) ).to.equal(
  211. '<paragraph>foo</paragraph>' +
  212. '[<table>' +
  213. '<tableRow>' +
  214. '<tableCell><paragraph>1</paragraph></tableCell><tableCell><paragraph>2</paragraph></tableCell>' +
  215. '</tableRow>' +
  216. '<tableRow>' +
  217. '<tableCell><paragraph>3</paragraph></tableCell><tableCell><paragraph>4</paragraph></tableCell>' +
  218. '</tableRow>' +
  219. '<tableRow>' +
  220. '<tableCell><paragraph>5</paragraph></tableCell><tableCell><paragraph>6</paragraph></tableCell>' +
  221. '</tableRow>' +
  222. '</table>]' +
  223. '<paragraph>baz</paragraph>'
  224. );
  225. } );
  226. it( 'should fix #8 (cross-limit selection which starts in a non-limit elements)', () => {
  227. model.schema.extend( 'paragraph', { allowIn: 'tableCell' } );
  228. setModelData( model,
  229. '<paragraph>foo</paragraph>' +
  230. '<table>' +
  231. '<tableRow>' +
  232. '<tableCell><paragraph>f[oo</paragraph></tableCell>' +
  233. '<tableCell><paragraph>b]ar</paragraph></tableCell>' +
  234. '</tableRow>' +
  235. '</table>' +
  236. '<paragraph>baz</paragraph>'
  237. );
  238. expect( getModelData( model ) ).to.equal(
  239. '<paragraph>foo</paragraph>' +
  240. '[<table>' +
  241. '<tableRow>' +
  242. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  243. '<tableCell><paragraph>bar</paragraph></tableCell>' +
  244. '</tableRow>' +
  245. '</table>]' +
  246. '<paragraph>baz</paragraph>'
  247. );
  248. } );
  249. it( 'should not fix #1 (selection after a table)', () => {
  250. setModelData( model,
  251. '<paragraph>foo</paragraph>' +
  252. '<table>' +
  253. '<tableRow>' +
  254. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  255. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  256. '</tableRow>' +
  257. '</table>' +
  258. '<paragraph>b[ar</paragraph>' +
  259. '<paragraph>ba]z</paragraph>'
  260. );
  261. expect( getModelData( model ) ).to.equal(
  262. '<paragraph>foo</paragraph>' +
  263. '<table>' +
  264. '<tableRow>' +
  265. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  266. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  267. '</tableRow>' +
  268. '</table>' +
  269. '<paragraph>b[ar</paragraph>' +
  270. '<paragraph>ba]z</paragraph>'
  271. );
  272. } );
  273. it( 'should fix multiple ranges #1', () => {
  274. model.change( writer => {
  275. const ranges = [
  276. new ModelRange( new ModelPosition( modelRoot, [ 0, 1 ] ), new ModelPosition( modelRoot, [ 1, 0 ] ) ),
  277. new ModelRange( new ModelPosition( modelRoot, [ 1, 0, 0, 0 ] ), new ModelPosition( modelRoot, [ 1, 1 ] ) )
  278. ];
  279. writer.setSelection( ranges );
  280. } );
  281. expect( getModelData( model ) ).to.equal(
  282. '<paragraph>f[oo</paragraph>' +
  283. '<table>' +
  284. '<tableRow>' +
  285. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  286. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  287. '</tableRow>' +
  288. '</table>]' +
  289. '<paragraph>bar</paragraph>'
  290. );
  291. } );
  292. it( 'should fix multiple ranges #2', () => {
  293. model.change( writer => {
  294. const ranges = [
  295. new ModelRange( new ModelPosition( modelRoot, [ 0, 1 ] ), new ModelPosition( modelRoot, [ 1, 0 ] ) ),
  296. new ModelRange( new ModelPosition( modelRoot, [ 1, 0, 0, 0 ] ), new ModelPosition( modelRoot, [ 2, 2 ] ) )
  297. ];
  298. writer.setSelection( ranges );
  299. } );
  300. expect( getModelData( model ) ).to.equal(
  301. '<paragraph>f[oo</paragraph>' +
  302. '<table>' +
  303. '<tableRow>' +
  304. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  305. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  306. '</tableRow>' +
  307. '</table>' +
  308. '<paragraph>ba]r</paragraph>'
  309. );
  310. } );
  311. it( 'should fix multiple ranges #3', () => {
  312. setModelData( model,
  313. '<paragraph>foo</paragraph>' +
  314. '<table>' +
  315. '<tableRow>' +
  316. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  317. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  318. '</tableRow>' +
  319. '<tableRow>]' +
  320. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  321. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  322. '</tableRow>' +
  323. '<tableRow>]' +
  324. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  325. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  326. '</tableRow>' +
  327. '<tableRow>]' +
  328. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  329. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  330. '</tableRow>' +
  331. '</table>' +
  332. '<paragraph>b]az</paragraph>'
  333. );
  334. expect( getModelData( model ) ).to.equal(
  335. '<paragraph>foo</paragraph>' +
  336. '[<table>' +
  337. '<tableRow>' +
  338. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  339. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  340. '</tableRow>' +
  341. '<tableRow>' +
  342. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  343. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  344. '</tableRow>' +
  345. '<tableRow>' +
  346. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  347. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  348. '</tableRow>' +
  349. '<tableRow>' +
  350. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  351. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  352. '</tableRow>' +
  353. '</table>' +
  354. '<paragraph>b]az</paragraph>'
  355. );
  356. } );
  357. it( 'should not fix multiple ranges #1 - not overlapping ranges', () => {
  358. model.change( writer => {
  359. const ranges = [
  360. new ModelRange( new ModelPosition( modelRoot, [ 0, 1 ] ), new ModelPosition( modelRoot, [ 1, 0 ] ) ),
  361. new ModelRange( new ModelPosition( modelRoot, [ 1, 0, 0, 0 ] ), new ModelPosition( modelRoot, [ 2, 1 ] ) ),
  362. new ModelRange( new ModelPosition( modelRoot, [ 2, 2 ] ), new ModelPosition( modelRoot, [ 2, 3 ] ) )
  363. ];
  364. writer.setSelection( ranges );
  365. } );
  366. expect( getModelData( model ) ).to.equal(
  367. '<paragraph>f[oo</paragraph>' +
  368. '<table>' +
  369. '<tableRow>' +
  370. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  371. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  372. '</tableRow>' +
  373. '</table>' +
  374. '<paragraph>b]a[r]</paragraph>'
  375. );
  376. } );
  377. it( 'should not fix multiple ranges #1 - table selection', () => {
  378. setModelData( model,
  379. '<table>' +
  380. '<tableRow>' +
  381. '[<tableCell><paragraph>a</paragraph></tableCell>]' +
  382. '[<tableCell><paragraph>b</paragraph></tableCell>]' +
  383. '</tableRow>' +
  384. '<tableRow>' +
  385. '[<tableCell><paragraph>c</paragraph></tableCell>]' +
  386. '<tableCell><paragraph>d</paragraph></tableCell>' +
  387. '</tableRow>' +
  388. '</table>'
  389. );
  390. // model.change( writer => {
  391. // const ranges = [
  392. // new ModelRange( new ModelPosition( modelRoot, [ 0, 1 ] ), new ModelPosition( modelRoot, [ 1, 0 ] ) ),
  393. // new ModelRange( new ModelPosition( modelRoot, [ 1, 0, 0, 0 ] ), new ModelPosition( modelRoot, [ 2, 1 ] ) ),
  394. // new ModelRange( new ModelPosition( modelRoot, [ 2, 2 ] ), new ModelPosition( modelRoot, [ 2, 3 ] ) )
  395. // ];
  396. //
  397. // writer.setSelection( ranges );
  398. // } );
  399. expect( getModelData( model ) ).to.equal(
  400. '<table>' +
  401. '<tableRow>' +
  402. '[<tableCell><paragraph>a</paragraph></tableCell>]' +
  403. '[<tableCell><paragraph>b</paragraph></tableCell>]' +
  404. '</tableRow>' +
  405. '<tableRow>' +
  406. '[<tableCell><paragraph>c</paragraph></tableCell>]' +
  407. '<tableCell><paragraph>d</paragraph></tableCell>' +
  408. '</tableRow>' +
  409. '</table>'
  410. );
  411. } );
  412. it( 'should allow selection on block - limit element', () => {
  413. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  414. setModelData( model,
  415. '<table>' +
  416. '<tableRow><tableCell>[<paragraph>aaa</paragraph>]</tableCell></tableRow>' +
  417. '</table>'
  418. );
  419. expect( getModelData( model ) ).to.equal(
  420. '<table>' +
  421. '<tableRow><tableCell><paragraph>[aaa]</paragraph></tableCell></tableRow>' +
  422. '</table>'
  423. );
  424. } );
  425. } );
  426. describe( 'non-collapsed selection - image scenarios', () => {
  427. beforeEach( () => {
  428. setModelData( model,
  429. '<paragraph>[]foo</paragraph>' +
  430. '<image>' +
  431. '<caption>xxx</caption>' +
  432. '</image>' +
  433. '<paragraph>bar</paragraph>'
  434. );
  435. } );
  436. it( 'should fix #1 (crossing object and limit boundaries)', () => {
  437. model.change( writer => {
  438. // <paragraph>f[oo</paragraph><image><caption>x]xx</caption>...
  439. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  440. modelRoot.getChild( 0 ), 1,
  441. modelRoot.getChild( 1 ).getChild( 0 ), 1
  442. ) );
  443. } );
  444. expect( getModelData( model ) ).to.equal(
  445. '<paragraph>f[oo</paragraph>' +
  446. '<image>' +
  447. '<caption>xxx</caption>' +
  448. '</image>]' +
  449. '<paragraph>bar</paragraph>'
  450. );
  451. } );
  452. it( 'should fix #2 (crossing object boundary)', () => {
  453. model.change( writer => {
  454. // <paragraph>f[oo</paragraph><image>]<caption>xxx</caption>...
  455. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  456. modelRoot.getChild( 0 ), 1,
  457. modelRoot.getChild( 1 ), 0
  458. ) );
  459. } );
  460. expect( getModelData( model ) ).to.equal(
  461. '<paragraph>f[oo</paragraph>' +
  462. '<image>' +
  463. '<caption>xxx</caption>' +
  464. '</image>]' +
  465. '<paragraph>bar</paragraph>'
  466. );
  467. } );
  468. it( 'should fix #3 (crossing object boundary)', () => {
  469. model.change( writer => {
  470. // <paragraph>f[oo</paragraph><image><caption>xxx</caption>]</image>...
  471. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  472. modelRoot.getChild( 0 ), 1,
  473. modelRoot.getChild( 1 ), 1
  474. ) );
  475. } );
  476. expect( getModelData( model ) ).to.equal(
  477. '<paragraph>f[oo</paragraph>' +
  478. '<image>' +
  479. '<caption>xxx</caption>' +
  480. '</image>]' +
  481. '<paragraph>bar</paragraph>'
  482. );
  483. } );
  484. it( 'should fix #4 (element selection of not an object)', () => {
  485. model.change( writer => {
  486. // <paragraph>foo</paragraph><image>[<caption>xxx</caption>]</image>...
  487. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  488. modelRoot.getChild( 1 ), 0,
  489. modelRoot.getChild( 1 ), 1
  490. ) );
  491. } );
  492. expect( getModelData( model ) ).to.equal(
  493. '<paragraph>foo</paragraph>' +
  494. '[<image>' +
  495. '<caption>xxx</caption>' +
  496. '</image>]' +
  497. '<paragraph>bar</paragraph>'
  498. );
  499. } );
  500. it( 'should not fix #1 (element selection of an object)', () => {
  501. model.change( writer => {
  502. // <paragraph>foo</paragraph>[<image><caption>xxx</caption></image>]...
  503. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  504. modelRoot, 1,
  505. modelRoot, 2
  506. ) );
  507. } );
  508. expect( getModelData( model ) ).to.equal(
  509. '<paragraph>foo</paragraph>' +
  510. '[<image>' +
  511. '<caption>xxx</caption>' +
  512. '</image>]' +
  513. '<paragraph>bar</paragraph>'
  514. );
  515. } );
  516. it( 'should not fix #2 (inside a limit)', () => {
  517. model.change( writer => {
  518. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  519. // <paragraph>foo</paragraph><image><caption>[xxx]</caption></image>...
  520. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  521. caption, 0,
  522. caption, 3
  523. ) );
  524. } );
  525. expect( getModelData( model ) ).to.equal(
  526. '<paragraph>foo</paragraph>' +
  527. '<image>' +
  528. '<caption>[xxx]</caption>' +
  529. '</image>' +
  530. '<paragraph>bar</paragraph>'
  531. );
  532. } );
  533. it( 'should not fix #3 (inside a limit - partial text selection)', () => {
  534. model.change( writer => {
  535. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  536. // <paragraph>foo</paragraph><image><caption>[xx]x</caption></image>...
  537. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  538. caption, 0,
  539. caption, 2
  540. ) );
  541. } );
  542. expect( getModelData( model ) ).to.equal(
  543. '<paragraph>foo</paragraph>' +
  544. '<image>' +
  545. '<caption>[xx]x</caption>' +
  546. '</image>' +
  547. '<paragraph>bar</paragraph>'
  548. );
  549. } );
  550. it( 'should not fix #4 (inside a limit - partial text selection)', () => {
  551. model.change( writer => {
  552. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  553. // <paragraph>foo</paragraph><image><caption>x[xx]</caption></image>...
  554. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  555. caption, 1,
  556. caption, 3
  557. ) );
  558. } );
  559. expect( getModelData( model ) ).to.equal(
  560. '<paragraph>foo</paragraph>' +
  561. '<image>' +
  562. '<caption>x[xx]</caption>' +
  563. '</image>' +
  564. '<paragraph>bar</paragraph>'
  565. );
  566. } );
  567. it( 'should not fix #5 (selection in root on non limit element that doesn\'t allow text)', () => {
  568. setModelData( model,
  569. '[<figure></figure>]'
  570. );
  571. expect( getModelData( model ) ).to.equal(
  572. '[<figure></figure>]'
  573. );
  574. } );
  575. } );
  576. describe( 'non-collapsed selection - other scenarios', () => {
  577. it( 'should fix #1 (element selection of not an object)', () => {
  578. setModelData( model,
  579. '<paragraph>aaa</paragraph>' +
  580. '[<paragraph>bbb</paragraph>]' +
  581. '<paragraph>ccc</paragraph>'
  582. );
  583. expect( getModelData( model ) ).to.equal(
  584. '<paragraph>aaa</paragraph>' +
  585. '<paragraph>[bbb]</paragraph>' +
  586. '<paragraph>ccc</paragraph>'
  587. );
  588. } );
  589. it( 'should fix #2 (elements selection of not an object)', () => {
  590. setModelData( model,
  591. '<paragraph>aaa</paragraph>' +
  592. '[<paragraph>bbb</paragraph>' +
  593. '<paragraph>ccc</paragraph>]'
  594. );
  595. expect( getModelData( model ) ).to.equal(
  596. '<paragraph>aaa</paragraph>' +
  597. '<paragraph>[bbb</paragraph>' +
  598. '<paragraph>ccc]</paragraph>'
  599. );
  600. } );
  601. it( 'should fix #3 (partial selection of not an object)', () => {
  602. setModelData( model,
  603. '<paragraph>aaa</paragraph>' +
  604. '[<paragraph>bbb</paragraph>' +
  605. '<paragraph>ccc]</paragraph>'
  606. );
  607. expect( getModelData( model ) ).to.equal(
  608. '<paragraph>aaa</paragraph>' +
  609. '<paragraph>[bbb</paragraph>' +
  610. '<paragraph>ccc]</paragraph>'
  611. );
  612. } );
  613. it( 'should fix #4 (partial selection of not an object)', () => {
  614. setModelData( model,
  615. '<paragraph>aaa</paragraph>' +
  616. '<paragraph>b[bb</paragraph>]' +
  617. '<paragraph>ccc</paragraph>'
  618. );
  619. expect( getModelData( model ) ).to.equal(
  620. '<paragraph>aaa</paragraph>' +
  621. '<paragraph>b[bb]</paragraph>' +
  622. '<paragraph>ccc</paragraph>'
  623. );
  624. } );
  625. it( 'should fix #5 (partial selection of not an object)', () => {
  626. setModelData( model,
  627. '<paragraph>aaa</paragraph>' +
  628. '[<paragraph>bb]b</paragraph>' +
  629. '<paragraph>ccc</paragraph>'
  630. );
  631. expect( getModelData( model ) ).to.equal(
  632. '<paragraph>aaa</paragraph>' +
  633. '<paragraph>[bb]b</paragraph>' +
  634. '<paragraph>ccc</paragraph>'
  635. );
  636. } );
  637. it( 'should fix #6 (selection must not cross a limit element; starts in a root)', () => {
  638. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  639. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  640. model.schema.register( 'c', { allowIn: 'b' } );
  641. model.schema.extend( '$text', { allowIn: 'c' } );
  642. setModelData( model,
  643. '<a><b><c>[</c></b></a>]'
  644. );
  645. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  646. } );
  647. it( 'should fix #7 (selection must not cross a limit element; ends in a root)', () => {
  648. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  649. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  650. model.schema.register( 'c', { allowIn: 'b' } );
  651. model.schema.extend( '$text', { allowIn: 'c' } );
  652. setModelData( model,
  653. '[<a><b><c>]</c></b></a>'
  654. );
  655. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  656. } );
  657. it( 'should fix #8 (selection must not cross a limit element; starts in a non-limit)', () => {
  658. model.schema.register( 'div', { allowIn: '$root' } );
  659. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  660. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  661. model.schema.register( 'c', { allowIn: 'b' } );
  662. model.schema.extend( '$text', { allowIn: 'c' } );
  663. setModelData( model,
  664. '<div>[<a><b><c>]</c></b></a></div>'
  665. );
  666. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  667. } );
  668. it( 'should fix #9 (selection must not cross a limit element; ends in a non-limit)', () => {
  669. model.schema.register( 'div', { allowIn: '$root' } );
  670. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  671. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  672. model.schema.register( 'c', { allowIn: 'b' } );
  673. model.schema.extend( '$text', { allowIn: 'c' } );
  674. setModelData( model,
  675. '<div><a><b><c>[</c></b></a>]</div>'
  676. );
  677. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  678. } );
  679. it( 'should not fix #1 (selection on text node)', () => {
  680. setModelData( model, '<paragraph>foob[a]r</paragraph>', { lastRangeBackward: true } );
  681. expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
  682. } );
  683. it( 'should not fix #2 (inline widget selected)', () => {
  684. setModelData( model,
  685. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  686. );
  687. expect( getModelData( model ) ).to.equal(
  688. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  689. );
  690. } );
  691. it( 'should not fix #3 (text around inline widget)', () => {
  692. setModelData( model,
  693. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  694. );
  695. expect( getModelData( model ) ).to.equal(
  696. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  697. );
  698. } );
  699. } );
  700. describe( 'collapsed selection', () => {
  701. it( 'should fix #1 - selection in limit element & before limit element', () => {
  702. setModelData( model,
  703. '<paragraph>foo</paragraph>' +
  704. '<table>' +
  705. '[]<tableRow>' +
  706. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  707. '</tableRow>' +
  708. '</table>' +
  709. '<paragraph>bar</paragraph>'
  710. );
  711. expect( getModelData( model ) ).to.equal(
  712. '<paragraph>foo[]</paragraph>' +
  713. '<table>' +
  714. '<tableRow>' +
  715. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  716. '</tableRow>' +
  717. '</table>' +
  718. '<paragraph>bar</paragraph>'
  719. );
  720. } );
  721. it( 'should fix #2 - selection in limit element & before limit+object element', () => {
  722. setModelData( model,
  723. '<paragraph>foo</paragraph>' +
  724. '<table>' +
  725. '<tableRow>' +
  726. '[]<tableCell><paragraph>aaa</paragraph></tableCell>' +
  727. '</tableRow>' +
  728. '</table>' +
  729. '<paragraph>bar</paragraph>'
  730. );
  731. expect( getModelData( model ) ).to.equal(
  732. '<paragraph>foo</paragraph>' +
  733. '<table>' +
  734. '<tableRow>' +
  735. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  736. '</tableRow>' +
  737. '</table>' +
  738. '<paragraph>bar</paragraph>'
  739. );
  740. } );
  741. it( 'should fix #3 - selection in limit&object element & before object element', () => {
  742. setModelData( model,
  743. '<paragraph>foo</paragraph>' +
  744. '<table>' +
  745. '<tableRow>' +
  746. '<tableCell>[]<paragraph>aaa</paragraph></tableCell>' +
  747. '</tableRow>' +
  748. '</table>' +
  749. '<paragraph>bar</paragraph>'
  750. );
  751. expect( getModelData( model ) ).to.equal(
  752. '<paragraph>foo</paragraph>' +
  753. '<table>' +
  754. '<tableRow>' +
  755. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  756. '</tableRow>' +
  757. '</table>' +
  758. '<paragraph>bar</paragraph>'
  759. );
  760. } );
  761. it( 'should not fix multiple ranges #1', () => {
  762. setModelData( model,
  763. '[]<paragraph>foo</paragraph>[]' +
  764. '<table>' +
  765. '<tableRow>' +
  766. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  767. '</tableRow>' +
  768. '</table>' +
  769. '<paragraph>bar</paragraph>'
  770. );
  771. expect( getModelData( model ) ).to.equal(
  772. '<paragraph>[]foo[]</paragraph>' +
  773. '<table>' +
  774. '<tableRow>' +
  775. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  776. '</tableRow>' +
  777. '</table>' +
  778. '<paragraph>bar</paragraph>'
  779. );
  780. } );
  781. } );
  782. } );
  783. } );