injectandroidbackspacemutationshandling.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  9. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  10. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  11. import Input from '../../src/input';
  12. import Delete from '../../src/delete';
  13. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  14. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  15. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  16. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  18. /* global document */
  19. describe( 'injectAndroidBackspaceMutationsHandling', () => {
  20. let editor, model, modelRoot, view, viewDocument, viewRoot, mutationsSpy, dateNowStub;
  21. testUtils.createSinonSandbox();
  22. before( () => {
  23. mutationsSpy = sinon.spy();
  24. } );
  25. beforeEach( () => {
  26. const domElement = document.createElement( 'div' );
  27. document.body.appendChild( domElement );
  28. return ClassicTestEditor.create( domElement, { plugins: [ Input, Delete, Paragraph, Heading, Italic, Undo ] } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. model = editor.model;
  32. modelRoot = model.document.getRoot();
  33. view = editor.editing.view;
  34. viewDocument = view.document;
  35. viewRoot = viewDocument.getRoot();
  36. editor.setData( '<h2>Heading 1</h2><p>Paragraph</p><h3>Heading 2</h3>' );
  37. } );
  38. } );
  39. afterEach( () => {
  40. if ( dateNowStub ) {
  41. dateNowStub.restore();
  42. dateNowStub = null;
  43. }
  44. mutationsSpy.resetHistory();
  45. return editor.destroy();
  46. } );
  47. it( 'should handle block merging', () => {
  48. // 1. Set selection to '<h2>Heading 1</h2><p>{}Paragraph</p><h3>Heading 2</h3>'.
  49. model.change( writer => {
  50. writer.setSelection(
  51. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 0, modelRoot.getChild( 1 ), 0 )
  52. );
  53. } );
  54. const modelContent = '<heading1>Heading 1</heading1><paragraph>[]Paragraph</paragraph><heading2>Heading 2</heading2>';
  55. const viewContent = '<h2>Heading 1</h2><p>{}Paragraph</p><h3>Heading 2</h3>';
  56. expect( getModelData( model ) ).to.equal( modelContent );
  57. expect( getViewData( view ) ).to.equal( viewContent );
  58. // 2. Create mutations which are result of changing HTML to '<h2>Heading 1{}Paragraph</h2><h3>Heading 2</h3>'.
  59. const mutations = [ {
  60. // `heading1` new text mutation
  61. type: 'children',
  62. newChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
  63. oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
  64. node: viewRoot.getChild( 0 )
  65. }, {
  66. // `paragraph` text removal mutation
  67. type: 'children',
  68. newChildren: [],
  69. oldChildren: [ viewRoot.getChild( 1 ).getChild( 0 ) ],
  70. node: viewRoot.getChild( 1 )
  71. }, {
  72. // `paragraph` removal mutation
  73. type: 'children',
  74. newChildren: [ viewRoot.getChild( 0 ) ],
  75. oldChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ) ],
  76. node: viewRoot
  77. } ];
  78. // 3. Simulate 'Backspace' flow on Android.
  79. simulateBackspace( mutations );
  80. expect( mutationsSpy.callCount ).to.equal( 0 );
  81. expect( getModelData( model ) ).to.equal( '<heading1>Heading 1[]Paragraph</heading1><heading2>Heading 2</heading2>' );
  82. expect( getViewData( view ) ).to.equal( '<h2>Heading 1{}Paragraph</h2><h3>Heading 2</h3>' );
  83. // Due ot `Undo` issue the selection is after paragraph after undoing changes (ckeditor5-undo/issues/64).
  84. expectContentAfterUndo(
  85. '<heading1>Heading 1[]</heading1><paragraph>Paragraph</paragraph><heading2>Heading 2</heading2>',
  86. '<h2>Heading 1{}</h2><p>Paragraph</p><h3>Heading 2</h3>' );
  87. } );
  88. it( 'should handle two entire blocks removal', () => {
  89. // 1. Set selection to '<h2>{Heading 1</h2><p>Paragraph}</p><h3>Heading 2</h3>'.
  90. model.change( writer => {
  91. writer.setSelection(
  92. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 1 ), 9 )
  93. );
  94. } );
  95. const modelContent = '<heading1>[Heading 1</heading1><paragraph>Paragraph]</paragraph><heading2>Heading 2</heading2>';
  96. const viewContent = '<h2>{Heading 1</h2><p>Paragraph}</p><h3>Heading 2</h3>';
  97. expect( getModelData( model ) ).to.equal( modelContent );
  98. expect( getViewData( view ) ).to.equal( viewContent );
  99. // 2. Create mutations which are result of changing HTML to '<h2>[]</h2><h3>Heading 2</h3>'.
  100. const mutations = [ {
  101. // `heading1` text removal mutation
  102. type: 'children',
  103. newChildren: [ new ViewElement( 'br' ) ],
  104. oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
  105. node: viewRoot.getChild( 0 )
  106. }, {
  107. // `paragraph` removal mutation
  108. type: 'children',
  109. newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 2 ) ],
  110. oldChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ), viewRoot.getChild( 2 ) ],
  111. node: viewRoot
  112. } ];
  113. // 3. Create selection which simulate Android behaviour where upon pressing `Backspace`
  114. // selection is changed to `<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>`.
  115. const newSelection = ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 9, modelRoot.getChild( 1 ), 9 );
  116. // 4. Simulate 'Backspace' flow on Android.
  117. simulateBackspace( mutations, newSelection );
  118. expect( mutationsSpy.callCount ).to.equal( 0 );
  119. expect( getModelData( model ) ).to.equal( '<heading1>[]</heading1><heading2>Heading 2</heading2>' );
  120. expect( getViewData( view ) ).to.equal( '<h2>[]</h2><h3>Heading 2</h3>' );
  121. expectContentAfterUndo(
  122. '<heading1>[Heading 1</heading1><paragraph>Paragraph]</paragraph><heading2>Heading 2</heading2>',
  123. '<h2>{Heading 1</h2><p>Paragraph}</p><h3>Heading 2</h3>' );
  124. } );
  125. it( 'should handle two partially selected blocks removal', () => {
  126. // 1. Set selection to '<h2>Hea{ding 1</h2><p>Paragraph}</p><h3>Heading 2</h3>'.
  127. model.change( writer => {
  128. writer.setSelection(
  129. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 1 ), 9 )
  130. );
  131. } );
  132. const modelContent = '<heading1>Hea[ding 1</heading1><paragraph>Paragraph]</paragraph><heading2>Heading 2</heading2>';
  133. const viewContent = '<h2>Hea{ding 1</h2><p>Paragraph}</p><h3>Heading 2</h3>';
  134. expect( getModelData( model ) ).to.equal( modelContent );
  135. expect( getViewData( view ) ).to.equal( viewContent );
  136. // 2. Create mutations which are result of changing HTML to '<h2>Hea{}</h2><h3>Heading 2</h3>'.
  137. const mutations = [ {
  138. // `heading1` text partial removal mutation
  139. type: 'text',
  140. newText: 'Hea',
  141. oldText: 'Heading 1',
  142. node: viewRoot.getChild( 0 ).getChild( 0 )
  143. }, {
  144. // `paragraph` removal mutation
  145. type: 'children',
  146. newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 2 ) ],
  147. oldChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ), viewRoot.getChild( 2 ) ],
  148. node: viewRoot
  149. } ];
  150. // 3. Create selection which simulate Android behaviour where upon pressing `Backspace`
  151. // selection is changed to `<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>`.
  152. const newSelection = ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 9, modelRoot.getChild( 1 ), 9 );
  153. // 4. Simulate 'Backspace' flow on Android.
  154. simulateBackspace( mutations, newSelection );
  155. expect( mutationsSpy.callCount ).to.equal( 0 );
  156. expect( getModelData( model ) ).to.equal( '<heading1>Hea[]</heading1><heading2>Heading 2</heading2>' );
  157. expect( getViewData( view ) ).to.equal( '<h2>Hea{}</h2><h3>Heading 2</h3>' );
  158. expectContentAfterUndo( modelContent, viewContent );
  159. } );
  160. it( 'should handle blocks removal if selection ends on the boundary of inline element', () => {
  161. editor.setData( '<h2>Heading 1</h2><p>Paragraph</p><h3><em>Heading</em> 2</h3>' );
  162. // 1. Set selection to '<h2>{Heading 1</h2><p>Paragraph</p><h3>]<i>Heading</i> 2</h3>'.
  163. model.change( writer => {
  164. writer.setSelection(
  165. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 2 ), 0 )
  166. );
  167. } );
  168. const modelContent = '<heading1>[Heading 1</heading1><paragraph>Paragraph</paragraph>' +
  169. '<heading2>]<$text italic="true">Heading</$text> 2</heading2>';
  170. const viewContent = '<h2>{Heading 1</h2><p>Paragraph</p><h3>]<i>Heading</i> 2</h3>';
  171. expect( getModelData( model ) ).to.equal( modelContent );
  172. expect( getViewData( view ) ).to.equal( viewContent );
  173. // 2. Create mutations which are result of changing HTML to '<h2><i>{}Heading</i> 2</h2>'.
  174. const mutations = [ {
  175. // `heading1` text to children mutation
  176. type: 'children',
  177. newChildren: Array.from( viewRoot.getChild( 2 ).getChildren() ),
  178. oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
  179. node: viewRoot.getChild( 0 )
  180. }, {
  181. // `heading2` children removal mutation
  182. type: 'children',
  183. newChildren: [],
  184. oldChildren: Array.from( viewRoot.getChild( 2 ).getChildren() ),
  185. node: viewRoot.getChild( 2 )
  186. }, { // `paragraph` and `heading2` removal mutation
  187. type: 'children',
  188. newChildren: [ viewRoot.getChild( 0 ) ],
  189. oldChildren: Array.from( viewRoot.getChildren() ),
  190. node: viewRoot
  191. } ];
  192. // 3. Create selection which simulate Android behaviour where upon pressing `Backspace`
  193. // selection is changed to `<h2>Heading 1</h2><p>Paragraph</p><h3><em>{}Heading</em> 2</h3>`.
  194. const newSelection = ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 2 ), 0, modelRoot.getChild( 2 ), 0 );
  195. // 4. Simulate 'Backspace' flow on Android.
  196. simulateBackspace( mutations, newSelection );
  197. expect( mutationsSpy.callCount ).to.equal( 0 );
  198. expect( getModelData( model ) ).to.equal( '<heading1><$text italic="true">[]Heading</$text> 2</heading1>' );
  199. expect( getViewData( view ) ).to.equal( '<h2><i>{}Heading</i> 2</h2>' );
  200. // https://github.com/ckeditor/ckeditor5-undo/issues/89
  201. expectContentAfterUndo(
  202. '<heading1>[Heading 1</heading1><paragraph>Paragraph]</paragraph><heading2><$text italic="true">Heading</$text> 2</heading2>',
  203. '<h2>{Heading 1</h2><p>Paragraph}</p><h3><i>Heading</i> 2</h3>'
  204. );
  205. } );
  206. it( 'should handle selection changed by the user before `backspace` on block merging', () => {
  207. // 1. Stub `Date.now` so we can simulate user selection change timing.
  208. let dateNowValue = 0;
  209. dateNowStub = sinon.stub( Date, 'now' ).callsFake( () => {
  210. dateNowValue += 500;
  211. return dateNowValue;
  212. } );
  213. editor.setData( '<h2>Heading 1</h2><p>Paragraph</p><h3><em>Heading</em> 2</h3>' );
  214. // 2. Set selection to '<h2>{Heading 1</h2><p>Paragraph</p><h3>]<i>Heading</i> 2</h3>'.
  215. model.change( writer => {
  216. writer.setSelection(
  217. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 2 ), 0 )
  218. );
  219. } );
  220. const modelContent = '<heading1>[Heading 1</heading1><paragraph>Paragraph</paragraph>' +
  221. '<heading2>]<$text italic="true">Heading</$text> 2</heading2>';
  222. const viewContent = '<h2>{Heading 1</h2><p>Paragraph</p><h3>]<i>Heading</i> 2</h3>';
  223. expect( getModelData( model ) ).to.equal( modelContent );
  224. expect( getViewData( view ) ).to.equal( viewContent );
  225. // 3. Create mutations which are result of changing HTML to '<h2>Heading 1</h2><p>Paragraph{}<i>Heading</i> 2</p>'.
  226. // This is still a block container removal so 'injectAndroidBackspaceMutationsHandling' will get triggered.
  227. const mutations = [ {
  228. // `paragraph` children added mutation
  229. type: 'children',
  230. newChildren: [ viewRoot.getChild( 1 ).getChild( 0 ) ].concat( Array.from( viewRoot.getChild( 2 ).getChildren() ) ),
  231. oldChildren: Array.from( viewRoot.getChild( 1 ).getChildren() ),
  232. node: viewRoot.getChild( 1 )
  233. }, {
  234. // `heading2` children removal mutation
  235. type: 'children',
  236. newChildren: [],
  237. oldChildren: Array.from( viewRoot.getChild( 2 ).getChildren() ),
  238. node: viewRoot.getChild( 2 )
  239. }, { // `heading2` removal mutation
  240. type: 'children',
  241. newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ) ],
  242. oldChildren: Array.from( viewRoot.getChildren() ),
  243. node: viewRoot
  244. } ];
  245. // 4. Simulate user selection change which is identical as Android native change on 'Backspace'.
  246. model.change( writer => {
  247. writer.setSelection( ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 2 ), 0, modelRoot.getChild( 2 ), 0 ) );
  248. } );
  249. // 5. Simulate 'Backspace' flow on Android.
  250. simulateBackspace( mutations );
  251. expect( mutationsSpy.callCount ).to.equal( 0 );
  252. expect( getModelData( model ) ).to.equal( '<heading1>Heading 1</heading1><paragraph>Paragraph[]' +
  253. '<$text italic="true">Heading</$text> 2</paragraph>' );
  254. expect( getViewData( view ) ).to.equal( '<h2>Heading 1</h2><p>Paragraph{}<i>Heading</i> 2</p>' );
  255. // Due ot `Undo` issue the selection is after paragraph after undoing changes (ckeditor5-undo/issues/64).
  256. expectContentAfterUndo( '<heading1>Heading 1</heading1><paragraph>Paragraph[]</paragraph>' +
  257. '<heading2><$text italic="true">Heading</$text> 2</heading2>',
  258. '<h2>Heading 1</h2><p>Paragraph{}</p><h3><i>Heading</i> 2</h3>' );
  259. } );
  260. it( 'should not be triggered for container insertion mutations', () => {
  261. // 1. Set selection to '<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>'.
  262. model.change( writer => {
  263. writer.setSelection(
  264. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 9, modelRoot.getChild( 1 ), 9 )
  265. );
  266. } );
  267. const modelContent = '<heading1>Heading 1</heading1><paragraph>Paragraph[]</paragraph><heading2>Heading 2</heading2>';
  268. const viewContent = '<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>';
  269. expect( getModelData( model ) ).to.equal( modelContent );
  270. expect( getViewData( view ) ).to.equal( viewContent );
  271. const viewFoo = new ViewText( 'foo' );
  272. const viewP = new ViewElement( 'p', null, viewFoo );
  273. // 2. Create mutations which are result of changing HTML to '<h2>Heading 1</h2><p>Paragraph{}</p><p>Foo</p><h3>Heading 2</h3>'.
  274. const mutations = [ {
  275. // new paragraph insertion mutation
  276. type: 'children',
  277. newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ), viewP, viewRoot.getChild( 2 ) ],
  278. oldChildren: Array.from( viewRoot.getChildren() ),
  279. node: viewRoot.getChild( 0 )
  280. } ];
  281. // 3. Spy mutations listener calls. It should be called ones
  282. // as it was not stopped by 'injectAndroidBackspaceMutationsHandling' handler.
  283. viewDocument.on( 'mutations', mutationsSpy, { priority: 'lowest' } );
  284. // 4. Fire mutations event.
  285. viewDocument.fire( 'mutations', mutations );
  286. expect( mutationsSpy.callCount ).to.equal( 1 );
  287. expect( getModelData( model ) ).to.equal( modelContent );
  288. expect( getViewData( view ) ).to.equal( viewContent );
  289. } );
  290. function simulateBackspace( mutations, newSelection ) {
  291. // Spy mutations listener calls. If Android handler was triggered it should prevent further calls.
  292. viewDocument.on( 'mutations', mutationsSpy, { priority: 'lowest' } );
  293. // Simulate selection change on Android devices before `keydown` event.
  294. if ( newSelection ) {
  295. model.change( writer => {
  296. writer.setSelection( newSelection );
  297. } );
  298. }
  299. // Fire `keydown` event with `229` key code so it is consistent with what happens on Android devices.
  300. viewDocument.fire( 'keydown', { keyCode: 229 } );
  301. // Fire mutations event.
  302. viewDocument.fire( 'mutations', mutations );
  303. }
  304. function expectContentAfterUndo( modelContent, viewContent ) {
  305. editor.execute( 'undo' );
  306. expect( getModelData( model ) ).to.equal( modelContent );
  307. expect( getViewData( view ) ).to.equal( viewContent );
  308. }
  309. } );