|
|
@@ -0,0 +1,356 @@
|
|
|
+/**
|
|
|
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
|
|
|
+ * For licensing, see LICENSE.md.
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
|
|
|
+ * For licensing, see LICENSE.md.
|
|
|
+ */
|
|
|
+
|
|
|
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
|
|
|
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
|
|
|
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
|
|
|
+import Heading from '@ckeditor/ckeditor5-heading/src/heading';
|
|
|
+import Undo from '@ckeditor/ckeditor5-undo/src/undo';
|
|
|
+import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
|
|
|
+import Input from '../../src/input';
|
|
|
+import Delete from '../../src/delete';
|
|
|
+
|
|
|
+import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
|
|
|
+import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
|
|
|
+import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
|
|
|
+
|
|
|
+import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
|
+import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
|
|
|
+
|
|
|
+/* global document */
|
|
|
+
|
|
|
+describe( 'injectAndroidBackspaceMutationsHandling', () => {
|
|
|
+ let editor, model, modelRoot, view, viewDocument, viewRoot, mutationsSpy, dateNowStub;
|
|
|
+
|
|
|
+ testUtils.createSinonSandbox();
|
|
|
+
|
|
|
+ beforeEach( () => {
|
|
|
+ const domElement = document.createElement( 'div' );
|
|
|
+ document.body.appendChild( domElement );
|
|
|
+
|
|
|
+ mutationsSpy = sinon.spy();
|
|
|
+
|
|
|
+ return ClassicTestEditor.create( domElement, { plugins: [ Input, Delete, Paragraph, Heading, Italic, Undo ] } )
|
|
|
+ .then( newEditor => {
|
|
|
+ editor = newEditor;
|
|
|
+ model = editor.model;
|
|
|
+ modelRoot = model.document.getRoot();
|
|
|
+ view = editor.editing.view;
|
|
|
+ viewDocument = view.document;
|
|
|
+ viewRoot = viewDocument.getRoot();
|
|
|
+
|
|
|
+ editor.setData( '<h2>Heading 1</h2><p>Paragraph</p><h3>Heading 2</h3>' );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ afterEach( () => {
|
|
|
+ if ( dateNowStub ) {
|
|
|
+ dateNowStub.restore();
|
|
|
+ dateNowStub = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return editor.destroy();
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should handle block merging', () => {
|
|
|
+ model.change( writer => {
|
|
|
+ writer.setSelection(
|
|
|
+ ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 0, modelRoot.getChild( 1 ), 0 )
|
|
|
+ );
|
|
|
+ } );
|
|
|
+
|
|
|
+ const modelContent = '<heading1>Heading 1</heading1><paragraph>[]Paragraph</paragraph><heading2>Heading 2</heading2>';
|
|
|
+ const viewContent = '<h2>Heading 1</h2><p>{}Paragraph</p><h3>Heading 2</h3>';
|
|
|
+
|
|
|
+ expect( getModelData( model ) ).to.equal( modelContent );
|
|
|
+ expect( getViewData( view ) ).to.equal( viewContent );
|
|
|
+
|
|
|
+ const mutations = [ {
|
|
|
+ // `heading1` new text mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
|
|
|
+ oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
|
|
|
+ node: viewRoot.getChild( 0 )
|
|
|
+ }, {
|
|
|
+ // `paragraph` text removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [],
|
|
|
+ oldChildren: [ viewRoot.getChild( 1 ).getChild( 0 ) ],
|
|
|
+ node: viewRoot.getChild( 1 )
|
|
|
+ }, {
|
|
|
+ // `paragraph` removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ viewRoot.getChild( 0 ) ],
|
|
|
+ oldChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ) ],
|
|
|
+ node: viewRoot
|
|
|
+ } ];
|
|
|
+
|
|
|
+ simulateBackspace( mutations );
|
|
|
+
|
|
|
+ expect( mutationsSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( getModelData( model ) ).to.equal( '<heading1>Heading 1[]Paragraph</heading1><heading2>Heading 2</heading2>' );
|
|
|
+ expect( getViewData( view ) ).to.equal( '<h2>Heading 1{}Paragraph</h2><h3>Heading 2</h3>' );
|
|
|
+
|
|
|
+ // Due ot `Undo` issue the selection is after paragraph after undoing changes (ckeditor5-undo/issues/64).
|
|
|
+ expectContentAfterUndo(
|
|
|
+ '<heading1>Heading 1</heading1><paragraph>Paragraph[]</paragraph><heading2>Heading 2</heading2>',
|
|
|
+ '<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should handle two entire blocks removal', () => {
|
|
|
+ model.change( writer => {
|
|
|
+ writer.setSelection(
|
|
|
+ ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 1 ), 9 )
|
|
|
+ );
|
|
|
+ } );
|
|
|
+
|
|
|
+ const modelContent = '<heading1>[Heading 1</heading1><paragraph>Paragraph]</paragraph><heading2>Heading 2</heading2>';
|
|
|
+ const viewContent = '<h2>{Heading 1</h2><p>Paragraph}</p><h3>Heading 2</h3>';
|
|
|
+
|
|
|
+ expect( getModelData( model ) ).to.equal( modelContent );
|
|
|
+ expect( getViewData( view ) ).to.equal( viewContent );
|
|
|
+
|
|
|
+ const mutations = [ {
|
|
|
+ // `heading1` text removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ new ViewElement( 'br' ) ],
|
|
|
+ oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
|
|
|
+ node: viewRoot.getChild( 0 )
|
|
|
+ }, {
|
|
|
+ // `paragraph` removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 2 ) ],
|
|
|
+ oldChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ), viewRoot.getChild( 2 ) ],
|
|
|
+ node: viewRoot
|
|
|
+ } ];
|
|
|
+
|
|
|
+ // Simulate change of selection on Android where upon pressing `Backspace`
|
|
|
+ // it is changed to `<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>`.
|
|
|
+ const newSelection = ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 9, modelRoot.getChild( 1 ), 9 );
|
|
|
+
|
|
|
+ simulateBackspace( mutations, newSelection );
|
|
|
+
|
|
|
+ expect( mutationsSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( getModelData( model ) ).to.equal( '<heading1>[]</heading1><heading2>Heading 2</heading2>' );
|
|
|
+ expect( getViewData( view ) ).to.equal( '<h2>[]</h2><h3>Heading 2</h3>' );
|
|
|
+
|
|
|
+ expectContentAfterUndo(
|
|
|
+ '<heading1>[Heading 1</heading1><paragraph>Paragraph]</paragraph><heading2>Heading 2</heading2>',
|
|
|
+ '<h2>{Heading 1</h2><p>Paragraph}</p><h3>Heading 2</h3>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should handle two partially selected blocks removal', () => {
|
|
|
+ model.change( writer => {
|
|
|
+ writer.setSelection(
|
|
|
+ ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 1 ), 9 )
|
|
|
+ );
|
|
|
+ } );
|
|
|
+
|
|
|
+ const modelContent = '<heading1>Hea[ding 1</heading1><paragraph>Paragraph]</paragraph><heading2>Heading 2</heading2>';
|
|
|
+ const viewContent = '<h2>Hea{ding 1</h2><p>Paragraph}</p><h3>Heading 2</h3>';
|
|
|
+
|
|
|
+ expect( getModelData( model ) ).to.equal( modelContent );
|
|
|
+ expect( getViewData( view ) ).to.equal( viewContent );
|
|
|
+
|
|
|
+ const mutations = [ {
|
|
|
+ // `heading1` text partial removal mutation
|
|
|
+ type: 'text',
|
|
|
+ newText: 'Hea',
|
|
|
+ oldText: 'Heading 1',
|
|
|
+ node: viewRoot.getChild( 0 ).getChild( 0 )
|
|
|
+ }, {
|
|
|
+ // `paragraph` removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 2 ) ],
|
|
|
+ oldChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ), viewRoot.getChild( 2 ) ],
|
|
|
+ node: viewRoot
|
|
|
+ } ];
|
|
|
+
|
|
|
+ // Simulate change of selection on Android where upon pressing `Backspace`
|
|
|
+ // it is changed to `<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>`.
|
|
|
+ const newSelection = ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 9, modelRoot.getChild( 1 ), 9 );
|
|
|
+
|
|
|
+ simulateBackspace( mutations, newSelection );
|
|
|
+
|
|
|
+ expect( mutationsSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( getModelData( model ) ).to.equal( '<heading1>Hea[]</heading1><heading2>Heading 2</heading2>' );
|
|
|
+ expect( getViewData( view ) ).to.equal( '<h2>Hea{}</h2><h3>Heading 2</h3>' );
|
|
|
+
|
|
|
+ expectContentAfterUndo( modelContent, viewContent );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should handle blocks removal if selection ends on the boundary of inline element', () => {
|
|
|
+ editor.setData( '<h2>Heading 1</h2><p>Paragraph</p><h3><em>Heading</em> 2</h3>' );
|
|
|
+
|
|
|
+ model.change( writer => {
|
|
|
+ writer.setSelection(
|
|
|
+ ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 2 ), 0 )
|
|
|
+ );
|
|
|
+ } );
|
|
|
+
|
|
|
+ const modelContent = '<heading1>[Heading 1</heading1><paragraph>Paragraph</paragraph>' +
|
|
|
+ '<heading2>]<$text italic="true">Heading</$text> 2</heading2>';
|
|
|
+ const viewContent = '<h2>{Heading 1</h2><p>Paragraph</p><h3>]<i>Heading</i> 2</h3>';
|
|
|
+
|
|
|
+ expect( getModelData( model ) ).to.equal( modelContent );
|
|
|
+ expect( getViewData( view ) ).to.equal( viewContent );
|
|
|
+
|
|
|
+ const mutations = [ {
|
|
|
+ // `heading1` text to children mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: Array.from( viewRoot.getChild( 2 ).getChildren() ),
|
|
|
+ oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
|
|
|
+ node: viewRoot.getChild( 0 )
|
|
|
+ }, {
|
|
|
+ // `heading2` children removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [],
|
|
|
+ oldChildren: Array.from( viewRoot.getChild( 2 ).getChildren() ),
|
|
|
+ node: viewRoot.getChild( 2 )
|
|
|
+ }, { // `paragrpah` and `heading2` removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ viewRoot.getChild( 0 ) ],
|
|
|
+ oldChildren: Array.from( viewRoot.getChildren() ),
|
|
|
+ node: viewRoot
|
|
|
+ } ];
|
|
|
+
|
|
|
+ // Simulate change of selection on Android where upon pressing `Backspace`
|
|
|
+ // it is changed to `<h2>Heading 1</h2><p>Paragraph</p><h3>{}<em>Heading</em> 2</h3>`.
|
|
|
+ const newSelection = ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 2 ), 0, modelRoot.getChild( 2 ), 0 );
|
|
|
+
|
|
|
+ simulateBackspace( mutations, newSelection );
|
|
|
+
|
|
|
+ expect( mutationsSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( getModelData( model ) ).to.equal( '<heading1><$text italic="true">[]Heading</$text> 2</heading1>' );
|
|
|
+ expect( getViewData( view ) ).to.equal( '<h2><i>{}Heading</i> 2</h2>' );
|
|
|
+
|
|
|
+ expectContentAfterUndo( modelContent, viewContent );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should handle selection changed by the user before `backspace` on block merging', () => {
|
|
|
+ // Stub `Date.now` so we can simulate user selection change timing.
|
|
|
+ let dateNowValue = 0;
|
|
|
+ dateNowStub = sinon.stub( Date, 'now' ).callsFake( () => {
|
|
|
+ dateNowValue += 500;
|
|
|
+ return dateNowValue;
|
|
|
+ } );
|
|
|
+
|
|
|
+ editor.setData( '<h2>Heading 1</h2><p>Paragraph</p><h3><em>Heading</em> 2</h3>' );
|
|
|
+
|
|
|
+ model.change( writer => {
|
|
|
+ writer.setSelection(
|
|
|
+ ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 2 ), 0 )
|
|
|
+ );
|
|
|
+ } );
|
|
|
+
|
|
|
+ const modelContent = '<heading1>[Heading 1</heading1><paragraph>Paragraph</paragraph>' +
|
|
|
+ '<heading2>]<$text italic="true">Heading</$text> 2</heading2>';
|
|
|
+ const viewContent = '<h2>{Heading 1</h2><p>Paragraph</p><h3>]<i>Heading</i> 2</h3>';
|
|
|
+
|
|
|
+ expect( getModelData( model ) ).to.equal( modelContent );
|
|
|
+ expect( getViewData( view ) ).to.equal( viewContent );
|
|
|
+
|
|
|
+ const mutations = [ {
|
|
|
+ // `paragraph` children added mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ viewRoot.getChild( 1 ).getChild( 0 ) ].concat( Array.from( viewRoot.getChild( 2 ).getChildren() ) ),
|
|
|
+ oldChildren: Array.from( viewRoot.getChild( 1 ).getChildren() ),
|
|
|
+ node: viewRoot.getChild( 1 )
|
|
|
+ }, {
|
|
|
+ // `heading2` children removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [],
|
|
|
+ oldChildren: Array.from( viewRoot.getChild( 2 ).getChildren() ),
|
|
|
+ node: viewRoot.getChild( 2 )
|
|
|
+ }, { // `heading2` removal mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ) ],
|
|
|
+ oldChildren: Array.from( viewRoot.getChildren() ),
|
|
|
+ node: viewRoot
|
|
|
+ } ];
|
|
|
+
|
|
|
+ // Simulate user selection change which is identical as Android native ones.
|
|
|
+ model.change( writer => {
|
|
|
+ writer.setSelection( ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 2 ), 0, modelRoot.getChild( 2 ), 0 ) );
|
|
|
+ } );
|
|
|
+
|
|
|
+ simulateBackspace( mutations );
|
|
|
+
|
|
|
+ expect( mutationsSpy.callCount ).to.equal( 0 );
|
|
|
+ expect( getModelData( model ) ).to.equal( '<heading1>Heading 1</heading1><paragraph>Paragraph[]' +
|
|
|
+ '<$text italic="true">Heading</$text> 2</paragraph>' );
|
|
|
+ expect( getViewData( view ) ).to.equal( '<h2>Heading 1</h2><p>Paragraph{}<i>Heading</i> 2</p>' );
|
|
|
+
|
|
|
+ // Due ot `Undo` issue the selection is after paragraph after undoing changes (ckeditor5-undo/issues/64).
|
|
|
+ expectContentAfterUndo( '<heading1>Heading 1</heading1><paragraph>Paragraph</paragraph>' +
|
|
|
+ '<heading2><$text italic="true">Heading</$text> 2[]</heading2>',
|
|
|
+ '<h2>Heading 1</h2><p>Paragraph</p><h3><i>Heading</i> 2{}</h3>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should not be triggered for container insertion mutations', () => {
|
|
|
+ model.change( writer => {
|
|
|
+ writer.setSelection(
|
|
|
+ ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 1 ), 9, modelRoot.getChild( 1 ), 9 )
|
|
|
+ );
|
|
|
+ } );
|
|
|
+
|
|
|
+ const modelContent = '<heading1>Heading 1</heading1><paragraph>Paragraph[]</paragraph><heading2>Heading 2</heading2>';
|
|
|
+ const viewContent = '<h2>Heading 1</h2><p>Paragraph{}</p><h3>Heading 2</h3>';
|
|
|
+
|
|
|
+ expect( getModelData( model ) ).to.equal( modelContent );
|
|
|
+ expect( getViewData( view ) ).to.equal( viewContent );
|
|
|
+
|
|
|
+ const viewFoo = new ViewText( 'foo' );
|
|
|
+ const viewP = new ViewElement( 'p', null, viewFoo );
|
|
|
+
|
|
|
+ const mutations = [ {
|
|
|
+ // new paragraph insertion mutation
|
|
|
+ type: 'children',
|
|
|
+ newChildren: [ viewRoot.getChild( 0 ), viewRoot.getChild( 1 ), viewP, viewRoot.getChild( 2 ) ],
|
|
|
+ oldChildren: Array.from( viewRoot.getChildren() ),
|
|
|
+ node: viewRoot.getChild( 0 )
|
|
|
+ } ];
|
|
|
+
|
|
|
+ // Spy mutations listener calls. If Android handler was triggered it should prevent further calls.
|
|
|
+ viewDocument.on( 'mutations', mutationsSpy, { priority: 'lowest' } );
|
|
|
+
|
|
|
+ // Fire mutations event.
|
|
|
+ viewDocument.fire( 'mutations', mutations );
|
|
|
+
|
|
|
+ expect( mutationsSpy.callCount ).to.equal( 1 );
|
|
|
+ expect( getModelData( model ) ).to.equal( modelContent );
|
|
|
+ expect( getViewData( view ) ).to.equal( viewContent );
|
|
|
+ } );
|
|
|
+
|
|
|
+ function simulateBackspace( mutations, newSelection ) {
|
|
|
+ // Spy mutations listener calls. If Android handler was triggered it should prevent further calls.
|
|
|
+ viewDocument.on( 'mutations', mutationsSpy, { priority: 'lowest' } );
|
|
|
+
|
|
|
+ // Simulate selection change on Android devices before `keydown` event.
|
|
|
+ if ( newSelection ) {
|
|
|
+ model.change( writer => {
|
|
|
+ writer.setSelection( newSelection );
|
|
|
+ } );
|
|
|
+ }
|
|
|
+
|
|
|
+ // Fire `keydown` event with `229` key code so it is consistent with what happens on Android devices.
|
|
|
+ viewDocument.fire( 'keydown', { keyCode: 229 } );
|
|
|
+
|
|
|
+ // Fire mutations event.
|
|
|
+ viewDocument.fire( 'mutations', mutations );
|
|
|
+ }
|
|
|
+
|
|
|
+ function expectContentAfterUndo( modelContent, viewContent ) {
|
|
|
+ editor.execute( 'undo' );
|
|
|
+
|
|
|
+ expect( getModelData( model ) ).to.equal( modelContent );
|
|
|
+ expect( getViewData( view ) ).to.equal( viewContent );
|
|
|
+ }
|
|
|
+} );
|