8
0

injectandroidbackspacemutationshandling.js 14 KB

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