Skip to content

Commit

Permalink
Merge pull request #9337 from ckeditor/i/8974
Browse files Browse the repository at this point in the history
Fix: The editor was not initialized with the empty data for `config.initialData` set to an empty string. Closes #8974.
  • Loading branch information
pomek authored Mar 23, 2021
2 parents 6ebc53f + 2433d61 commit bce8267
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/ckeditor5-editor-balloon/src/ballooneditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export default class BalloonEditor extends Editor {
throw new CKEditorError( 'editor-create-initial-data', null );
}

const initialData = config.initialData || getInitialData( sourceElementOrData );
const initialData = config.initialData !== undefined ? config.initialData : getInitialData( sourceElementOrData );

return editor.data.init( initialData );
} )
Expand Down
17 changes: 17 additions & 0 deletions packages/ckeditor5-editor-balloon/tests/ballooneditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ describe( 'BalloonEditor', () => {
} );
} );

// https://github.com/ckeditor/ckeditor5/issues/8974
it( 'initializes with empty content if config.initialData is set to an empty string', () => {
const editorElement = document.createElement( 'div' );
editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';

return BalloonEditor.create( editorElement, {
initialData: '',
plugins: [ Paragraph ]
} ).then( editor => {
expect( editor.getData() ).to.equal( '' );

return editor.destroy();
} ).then( () => {
editorElement.remove();
} );
} );

it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
BalloonEditor.create( '<p>Hello world!</p>', {
initialData: '<p>I am evil!</p>',
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-editor-classic/src/classiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export default class ClassicEditor extends Editor {
throw new CKEditorError( 'editor-create-initial-data', null );
}

const initialData = config.initialData || getInitialData( sourceElementOrData );
const initialData = config.initialData !== undefined ? config.initialData : getInitialData( sourceElementOrData );

return editor.data.init( initialData );
} )
Expand Down
12 changes: 12 additions & 0 deletions packages/ckeditor5-editor-classic/tests/classiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ describe( 'ClassicEditor', () => {
} );
} );

// https://github.com/ckeditor/ckeditor5/issues/8974
it( 'initializes with empty content if config.initialData is set to an empty string', () => {
return ClassicEditor.create( editorElement, {
initialData: '',
plugins: [ Paragraph ]
} ).then( editor => {
expect( editor.getData() ).to.equal( '' );

editor.destroy();
} );
} );

it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
ClassicEditor.create( '<p>Hello world!</p>', {
initialData: '<p>I am evil!</p>',
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-editor-decoupled/src/decouplededitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export default class DecoupledEditor extends Editor {
throw new CKEditorError( 'editor-create-initial-data', null );
}

const initialData = config.initialData || getInitialData( sourceElementOrData );
const initialData = config.initialData !== undefined ? config.initialData : getInitialData( sourceElementOrData );

return editor.data.init( initialData );
} )
Expand Down
12 changes: 12 additions & 0 deletions packages/ckeditor5-editor-decoupled/tests/decouplededitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ describe( 'DecoupledEditor', () => {
} );
} );

// https://github.com/ckeditor/ckeditor5/issues/8974
it( 'initializes with empty content if config.initialData is set to an empty string', () => {
return DecoupledEditor.create( document.createElement( 'div' ), {
initialData: '',
plugins: [ Paragraph ]
} ).then( editor => {
expect( editor.getData() ).to.equal( '' );

editor.destroy();
} );
} );

// See: https://github.com/ckeditor/ckeditor5/issues/746
it( 'should throw when trying to create the editor using the same source element more than once', done => {
const sourceElement = document.createElement( 'div' );
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-editor-inline/src/inlineeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export default class InlineEditor extends Editor {
throw new CKEditorError( 'editor-create-initial-data', null );
}

const initialData = config.initialData || getInitialData( sourceElementOrData );
const initialData = config.initialData !== undefined ? config.initialData : getInitialData( sourceElementOrData );

return editor.data.init( initialData );
} )
Expand Down
17 changes: 17 additions & 0 deletions packages/ckeditor5-editor-inline/tests/inlineeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ describe( 'InlineEditor', () => {
} );
} );

// https://github.com/ckeditor/ckeditor5/issues/8974
it( 'initializes with empty content if config.initialData is set to an empty string', () => {
const editorElement = document.createElement( 'div' );
editorElement.innerHTML = '<p>Hello world!</p>';

return InlineEditor.create( editorElement, {
initialData: '',
plugins: [ Paragraph ]
} ).then( editor => {
expect( editor.getData() ).to.equal( '' );

return editor.destroy();
} ).then( () => {
editorElement.remove();
} );
} );

it( 'should pass the config.toolbar.shouldNotGroupWhenFull configuration to the view', () => {
const editorElement = document.createElement( 'div' );

Expand Down

0 comments on commit bce8267

Please sign in to comment.