selection-post-fixer.js 27 KB

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