8
0

selection-postfixer.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 { getData as getModelData, setData as setModelData } from '../../../src/dev-utils/model';
  9. describe( 'Selection post fixer', () => {
  10. describe( 'selectionPostFixer()', () => {
  11. let model, modelRoot;
  12. beforeEach( () => {
  13. model = new Model();
  14. modelRoot = model.document.createRoot();
  15. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  16. model.schema.register( 'table', {
  17. allowWhere: '$block',
  18. isObject: true,
  19. isLimit: true
  20. } );
  21. model.schema.register( 'tableRow', {
  22. allowIn: 'table',
  23. isLimit: true
  24. } );
  25. model.schema.register( 'tableCell', {
  26. allowIn: 'tableRow',
  27. allowContentOf: '$block',
  28. isLimit: true
  29. } );
  30. setModelData( model,
  31. '<paragraph>[]foo</paragraph>' +
  32. '<table>' +
  33. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  34. '</table>' +
  35. '<paragraph>bar</paragraph>'
  36. );
  37. } );
  38. it( 'should not crash if there is no correct position for model selection', () => {
  39. setModelData( model, '' );
  40. expect( getModelData( model ) ).to.equal( '[]' );
  41. } );
  42. describe( 'not collapsed selection', () => {
  43. it( 'should fix #1', () => {
  44. model.change( writer => {
  45. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  46. modelRoot.getChild( 0 ), 1,
  47. modelRoot.getChild( 1 ).getChild( 0 ), 1
  48. ) );
  49. } );
  50. expect( getModelData( model ) ).to.equal(
  51. '<paragraph>f[oo</paragraph>' +
  52. '<table>' +
  53. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  54. '</table>]' +
  55. '<paragraph>bar</paragraph>'
  56. );
  57. } );
  58. it( 'should fix #2', () => {
  59. model.change( writer => {
  60. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  61. modelRoot.getChild( 1 ).getChild( 0 ), 1,
  62. modelRoot.getChild( 2 ), 1
  63. ) );
  64. } );
  65. expect( getModelData( model ) ).to.equal(
  66. '<paragraph>foo</paragraph>' +
  67. '[<table>' +
  68. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  69. '</table>' +
  70. '<paragraph>b]ar</paragraph>'
  71. );
  72. } );
  73. it( 'should fix #3', () => {
  74. model.change( writer => {
  75. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  76. modelRoot.getChild( 0 ), 1,
  77. modelRoot.getChild( 1 ), 0
  78. ) );
  79. } );
  80. expect( getModelData( model ) ).to.equal(
  81. '<paragraph>f[oo</paragraph>' +
  82. '<table>' +
  83. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  84. '</table>]' +
  85. '<paragraph>bar</paragraph>'
  86. );
  87. } );
  88. it( 'should fix #4', () => {
  89. model.change( writer => {
  90. writer.setSelection( ModelRange.createFromParentsAndOffsets(
  91. modelRoot.getChild( 1 ).getChild( 0 ).getChild( 0 ), 1,
  92. modelRoot.getChild( 1 ).getChild( 0 ).getChild( 1 ), 2
  93. ) );
  94. } );
  95. expect( getModelData( model ) ).to.equal(
  96. '<paragraph>foo</paragraph>' +
  97. '[<table>' +
  98. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  99. '</table>]' +
  100. '<paragraph>bar</paragraph>'
  101. );
  102. } );
  103. it( 'should fix #5', () => {
  104. setModelData( model,
  105. '<paragraph>foo</paragraph>' +
  106. '<table>' +
  107. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  108. '</table>' +
  109. '[]<table>' +
  110. '<tableRow><tableCell>xxx</tableCell><tableCell>yyy</tableCell></tableRow>' +
  111. '</table>' +
  112. '<paragraph>baz</paragraph>'
  113. );
  114. expect( getModelData( model ) ).to.equal(
  115. '<paragraph>foo</paragraph>' +
  116. '[<table>' +
  117. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  118. '</table>]' +
  119. '<table>' +
  120. '<tableRow><tableCell>xxx</tableCell><tableCell>yyy</tableCell></tableRow>' +
  121. '</table>' +
  122. '<paragraph>baz</paragraph>'
  123. );
  124. } );
  125. it( 'should not fix #1', () => {
  126. setModelData( model,
  127. '<paragraph>foo</paragraph>' +
  128. '<table>' +
  129. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  130. '</table>' +
  131. '<paragraph>b[ar</paragraph>' +
  132. '<paragraph>ba]z</paragraph>'
  133. );
  134. expect( getModelData( model ) ).to.equal(
  135. '<paragraph>foo</paragraph>' +
  136. '<table>' +
  137. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  138. '</table>' +
  139. '<paragraph>b[ar</paragraph>' +
  140. '<paragraph>ba]z</paragraph>'
  141. );
  142. } );
  143. it( 'should fix multiple ranges #1', () => {
  144. model.change( writer => {
  145. const ranges = [
  146. new ModelRange( new ModelPosition( modelRoot, [ 0, 1 ] ), new ModelPosition( modelRoot, [ 1, 0 ] ) ),
  147. new ModelRange( new ModelPosition( modelRoot, [ 1, 0, 0, 0 ] ), new ModelPosition( modelRoot, [ 1, 1 ] ) )
  148. ];
  149. writer.setSelection( ranges );
  150. } );
  151. expect( getModelData( model ) ).to.equal(
  152. '<paragraph>f[oo</paragraph>' +
  153. '<table>' +
  154. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  155. '</table>]' +
  156. '<paragraph>bar</paragraph>'
  157. );
  158. } );
  159. it( 'should fix multiple ranges #2', () => {
  160. model.change( writer => {
  161. const ranges = [
  162. new ModelRange( new ModelPosition( modelRoot, [ 0, 1 ] ), new ModelPosition( modelRoot, [ 1, 0 ] ) ),
  163. new ModelRange( new ModelPosition( modelRoot, [ 1, 0, 0, 0 ] ), new ModelPosition( modelRoot, [ 2, 2 ] ) )
  164. ];
  165. writer.setSelection( ranges );
  166. } );
  167. expect( getModelData( model ) ).to.equal(
  168. '<paragraph>f[oo</paragraph>' +
  169. '<table>' +
  170. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  171. '</table>' +
  172. '<paragraph>ba]r</paragraph>'
  173. );
  174. } );
  175. it( 'should fix multiple ranges #3', () => {
  176. setModelData( model,
  177. '<paragraph>foo</paragraph>' +
  178. '<table>' +
  179. '<tableRow><tableCell>[aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  180. '<tableRow>]<tableCell>[aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  181. '<tableRow>]<tableCell>[aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  182. '<tableRow>]<tableCell>[aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  183. '</table>' +
  184. '<paragraph>b]az</paragraph>'
  185. );
  186. expect( getModelData( model ) ).to.equal(
  187. '<paragraph>foo</paragraph>' +
  188. '[<table>' +
  189. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  190. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  191. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  192. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  193. '</table>' +
  194. '<paragraph>b]az</paragraph>'
  195. );
  196. } );
  197. it( 'should fix multiple ranges #4', () => {
  198. model.change( writer => {
  199. const ranges = [
  200. new ModelRange( new ModelPosition( modelRoot, [ 0, 1 ] ), new ModelPosition( modelRoot, [ 1, 0 ] ) ),
  201. new ModelRange( new ModelPosition( modelRoot, [ 1, 0, 0, 0 ] ), new ModelPosition( modelRoot, [ 2, 1 ] ) ),
  202. new ModelRange( new ModelPosition( modelRoot, [ 2, 2 ] ), new ModelPosition( modelRoot, [ 2, 3 ] ) )
  203. ];
  204. writer.setSelection( ranges );
  205. } );
  206. expect( getModelData( model ) ).to.equal(
  207. '<paragraph>f[oo</paragraph>' +
  208. '<table>' +
  209. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  210. '</table>' +
  211. '<paragraph>b]a[r]</paragraph>'
  212. );
  213. } );
  214. } );
  215. describe( 'collapsed selection', () => {
  216. it( 'should fix #1', () => {
  217. model.change( writer => {
  218. writer.setSelection(
  219. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 0, modelRoot.getChild( 1 ), 0 )
  220. );
  221. } );
  222. expect( getModelData( model ) ).to.equal(
  223. '<paragraph>foo[]</paragraph>' +
  224. '<table>' +
  225. '<tableRow><tableCell>aaa</tableCell><tableCell>bbb</tableCell></tableRow>' +
  226. '</table>' +
  227. '<paragraph>bar</paragraph>'
  228. );
  229. } );
  230. } );
  231. } );
  232. } );