injectandroidbackspacemutationshandling.js 15 KB

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