injectandroidbackspacemutationshandling.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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.skip( '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.skip( '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.skip( '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. }, { // `paragrpah` 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. expectContentAfterUndo( modelContent, viewContent );
  201. } );
  202. it( 'should handle selection changed by the user before `backspace` on block merging', () => {
  203. // 1. Stub `Date.now` so we can simulate user selection change timing.
  204. let dateNowValue = 0;
  205. dateNowStub = sinon.stub( Date, 'now' ).callsFake( () => {
  206. dateNowValue += 500;
  207. return dateNowValue;
  208. } );
  209. editor.setData( '<h2>Heading 1</h2><p>Paragraph</p><h3><em>Heading</em> 2</h3>' );
  210. // 2. Set selection to '<h2>{Heading 1</h2><p>Paragraph</p><h3>]<i>Heading</i> 2</h3>'.
  211. model.change( writer => {
  212. writer.setSelection(
  213. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 2 ), 0 )
  214. );
  215. } );
  216. const modelContent = '<heading1>[Heading 1</heading1><paragraph>Paragraph</paragraph>' +
  217. '<heading2>]<$text italic="true">Heading</$text> 2</heading2>';
  218. const viewContent = '<h2>{Heading 1</h2><p>Paragraph</p><h3>]<i>Heading</i> 2</h3>';
  219. expect( getModelData( model ) ).to.equal( modelContent );
  220. expect( getViewData( view ) ).to.equal( viewContent );
  221. // 3. Create mutations which are result of changing HTML to '<h2>Heading 1</h2><p>Paragraph{}<i>Heading</i> 2</p>'.
  222. // This is still a block container removal so 'injectAndroidBackspaceMutationsHandling' will get triggered.
  223. const mutations = [ {
  224. // `paragraph` children added mutation
  225. type: 'children',
  226. newChildren: [ viewRoot.getChild( 1 ).getChild( 0 ) ].concat( Array.from( viewRoot.getChild( 2 ).getChildren() ) ),
  227. oldChildren: Array.from( viewRoot.getChild( 1 ).getChildren() ),
  228. node: viewRoot.getChild( 1 )
  229. }, {
  230. // `heading2` children removal mutation
  231. type: 'children',
  232. newChildren: [],
  233. oldChildren: Array.from( viewRoot.getChild( 2 ).getChildren() ),
  234. node: viewRoot.getChild( 2 )
  235. }, { // `heading2` removal mutation
  236. type: 'children',
  237. newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ) ],
  238. oldChildren: Array.from( viewRoot.getChildren() ),
  239. node: viewRoot
  240. } ];
  241. // 4. Simulate user selection change which is identical as Android native change on 'Backspace'.
  242. model.change( writer => {
  243. writer.setSelection( ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 2 ), 0, modelRoot.getChild( 2 ), 0 ) );
  244. } );
  245. // 5. Simulate 'Backspace' flow on Android.
  246. simulateBackspace( mutations );
  247. expect( mutationsSpy.callCount ).to.equal( 0 );
  248. expect( getModelData( model ) ).to.equal( '<heading1>Heading 1</heading1><paragraph>Paragraph[]' +
  249. '<$text italic="true">Heading</$text> 2</paragraph>' );
  250. expect( getViewData( view ) ).to.equal( '<h2>Heading 1</h2><p>Paragraph{}<i>Heading</i> 2</p>' );
  251. // Due ot `Undo` issue the selection is after paragraph after undoing changes (ckeditor5-undo/issues/64).
  252. expectContentAfterUndo( '<heading1>Heading 1</heading1><paragraph>Paragraph[]</paragraph>' +
  253. '<heading2><$text italic="true">Heading</$text> 2</heading2>',
  254. '<h2>Heading 1</h2><p>Paragraph{}</p><h3><i>Heading</i> 2</h3>' );
  255. } );
  256. it( 'should not be triggered for container insertion mutations', () => {
  257. // 1. Set selection to '<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>'.
  258. model.change( writer => {
  259. writer.setSelection(
  260. ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 9, modelRoot.getChild( 1 ), 9 )
  261. );
  262. } );
  263. const modelContent = '<heading1>Heading 1</heading1><paragraph>Paragraph[]</paragraph><heading2>Heading 2</heading2>';
  264. const viewContent = '<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>';
  265. expect( getModelData( model ) ).to.equal( modelContent );
  266. expect( getViewData( view ) ).to.equal( viewContent );
  267. const viewFoo = new ViewText( 'foo' );
  268. const viewP = new ViewElement( 'p', null, viewFoo );
  269. // 2. Create mutations which are result of changing HTML to '<h2>Heading 1</h2><p>Paragraph{}</p><p>Foo</p><h3>Heading 2</h3>'.
  270. const mutations = [ {
  271. // new paragraph insertion mutation
  272. type: 'children',
  273. newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ), viewP, viewRoot.getChild( 2 ) ],
  274. oldChildren: Array.from( viewRoot.getChildren() ),
  275. node: viewRoot.getChild( 0 )
  276. } ];
  277. // 3. Spy mutations listener calls. It should be called ones
  278. // as it was not stopped by 'injectAndroidBackspaceMutationsHandling' handler.
  279. viewDocument.on( 'mutations', mutationsSpy, { priority: 'lowest' } );
  280. // 4. Fire mutations event.
  281. viewDocument.fire( 'mutations', mutations );
  282. expect( mutationsSpy.callCount ).to.equal( 1 );
  283. expect( getModelData( model ) ).to.equal( modelContent );
  284. expect( getViewData( view ) ).to.equal( viewContent );
  285. } );
  286. function simulateBackspace( mutations, newSelection ) {
  287. // Spy mutations listener calls. If Android handler was triggered it should prevent further calls.
  288. viewDocument.on( 'mutations', mutationsSpy, { priority: 'lowest' } );
  289. // Simulate selection change on Android devices before `keydown` event.
  290. if ( newSelection ) {
  291. model.change( writer => {
  292. writer.setSelection( newSelection );
  293. } );
  294. }
  295. // Fire `keydown` event with `229` key code so it is consistent with what happens on Android devices.
  296. viewDocument.fire( 'keydown', { keyCode: 229 } );
  297. // Fire mutations event.
  298. viewDocument.fire( 'mutations', mutations );
  299. }
  300. function expectContentAfterUndo( modelContent, viewContent ) {
  301. editor.execute( 'undo' );
  302. expect( getModelData( model ) ).to.equal( modelContent );
  303. expect( getViewData( view ) ).to.equal( viewContent );
  304. }
  305. } );