Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

T/61: Backspace on a fully selected content or at the beginning of the first block should ensure that paragraph is left #106

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/deletecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import Command from '@ckeditor/ckeditor5-core/src/command';
import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
import ChangeBuffer from './changebuffer';
import Position from '@ckeditor/ckeditor5-engine/src/model/position';
import Element from '@ckeditor/ckeditor5-engine/src/model/element';
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
import count from '@ckeditor/ckeditor5-utils/src/count';

/**
Expand Down Expand Up @@ -60,6 +63,8 @@ export default class DeleteCommand extends Command {
execute( options = {} ) {
const doc = this.editor.document;
const dataController = this.editor.data;
const root = doc.getRoot();
const schema = doc.schema;

doc.enqueueChanges( () => {
this._buffer.lock();
Expand Down Expand Up @@ -87,6 +92,25 @@ export default class DeleteCommand extends Command {
dataController.deleteContent( selection, this._buffer.batch );
this._buffer.input( changeCount );

// Check whether the whole content has been removed and whether schema allows inserting a paragraph.
// If the user will typing after removing the whole content, the user's input should be wrapped in the paragraph
// instead of any other element. See https://github.com/ckeditor/ckeditor5-typing/issues/61.
if (
Position.createAt( root ).isTouching( selection.getFirstPosition() ) &&
Position.createAt( root, 'end' ).isTouching( selection.getLastPosition() ) &&
schema.check( { name: 'paragraph', inside: root.name } )
) {
dataController.deleteContent( selection, this._buffer.batch );

this._buffer.batch.remove(
new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) )
);

this._buffer.batch.insert(
new Position( root, [ 0 ] ), new Element( 'paragraph' )
);
}

doc.selection.setRanges( selection.getRanges(), selection.isBackward );

this._buffer.unlock();
Expand Down
104 changes: 104 additions & 0 deletions tests/tickets/61.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* globals document */

import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';

import Typing from '../../src/typing';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';

import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';

describe( 'Bug ckeditor5-typing#61', () => {
let editor, doc, viewDocument, editorElement;

beforeEach( () => {
editorElement = document.createElement( 'div' );
document.body.appendChild( editorElement );

return ClassicTestEditor.create( editorElement, {
plugins: [ Typing, Paragraph, Undo, Bold, Italic, Enter, Heading ],
typing: { undoStep: 3 }
} )
.then( newEditor => {
editor = newEditor;
doc = editor.document;
viewDocument = editor.editing.view;
} );
} );

afterEach( () => {
editorElement.remove();

return editor.destroy();
} );

function expectOutput( modelOutput, viewOutput ) {
expect( getModelData( editor.document ) ).to.equal( modelOutput );
expect( getViewData( viewDocument ) ).to.equal( viewOutput );
}

function simulateTyping( text ) {
// While typing, every character is an atomic change.
text.split( '' ).forEach( character => {
editor.execute( 'input', {
text: character
} );
} );
}

it( 'leaves an empty paragraph after removing the whole content from editor #1', () => {
setModelData( doc, '[<heading1>Header 1</heading1><paragraph>Some text.</paragraph>]' );

editor.execute( 'delete' );

expectOutput( '<paragraph>[]</paragraph>', '<p>[]</p>' );
} );

it( 'leaves an empty paragraph after removing the whole content from editor #2', () => {
setModelData( doc, '<heading1>[Header 1</heading1><paragraph>Some text.]</paragraph>' );

editor.execute( 'delete' );

expectOutput( '<paragraph>[]</paragraph>', '<p>[]</p>' );
} );

it( 'wraps inserted text in a paragraph after typing in editor with selected the whole content #1', () => {
setModelData( doc, '[<heading1>Header 1</heading1><paragraph>Some text.</paragraph>]' );

editor.execute( 'delete' );

simulateTyping( '123' );

expectOutput( '<paragraph>123[]</paragraph>', '<p>123{}</p>' );
} );

it( 'wraps inserted text in a paragraph after typing in editor with selected the whole content #2', () => {
setModelData( doc, '<heading1>[Header 1</heading1><paragraph>Some text.]</paragraph>' );

editor.execute( 'delete' );

simulateTyping( '123' );

expectOutput( '<paragraph>123[]</paragraph>', '<p>123{}</p>' );
} );

it( 'paragraph created after removing the whole content should not have any attribute', () => {
setModelData( doc, '<paragraph><$text bold="true">[Some text.]</$text></paragraph>' );

editor.execute( 'delete' );

simulateTyping( '123' );

expectOutput( '<paragraph>123[]</paragraph>', '<p>123{}</p>' );
} );
} );