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

Commit

Permalink
Merge pull request #161 from ckeditor/t/ckeditor5/401
Browse files Browse the repository at this point in the history
Other: The `Editor#getData()` method now accepts `options.trim` parameter. By default it will now return an empty string when the editor is empty (instead of returning `'<p>&nbsp;</p>'` as before).

BREAKING CHANGE: The `Editor#getData()` method now returns an empty string by default when editor content is empty (instead of returning `'<p>&nbsp;</p>'` as before).
  • Loading branch information
Reinmar authored Feb 14, 2019
2 parents 8efe0b9 + 35c0e62 commit 4f8abd1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/editor/utils/dataapimixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const DataApiMixin = {
/**
* @inheritDoc
*/
getData() {
return this.data.get();
getData( options ) {
return this.data.get( options );
}
};

Expand Down Expand Up @@ -67,9 +67,14 @@ export default DataApiMixin;
* See the {@glink features/markdown Markdown output} guide for more details.
*
* Note: Not only is the format of the data configurable, but the type of the `getData()`'s return value does not
* have to be a string either. You can e.g. return an object or a DOM `DocumentFragment` if you consider this
* have to be a string either. You can e.g. return an object or a DOM `DocumentFragment` if you consider this
* the right format for you.
*
* @method #getData
* @param {Object} [options]
* @param {String} [options.rootName='main'] Root name.
* @param {String} [options.trim='empty'] Whether returned data should be trimmed. This option is set to `'empty'` by default,
* which means that whenever editor content is considered empty, an empty string is returned. To turn off trimming
* use `'none'`. In such cases exact content will be returned (for example `'<p>&nbsp;</p>'` for an empty editor).
* @returns {String} Output data.
*/
21 changes: 21 additions & 0 deletions tests/editor/utils/dataapimixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import DataApiMixin from '../../../src/editor/utils/dataapimixin';
import Editor from '../../../src/editor/editor';
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
import testUtils from '../../../tests/_utils/utils';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

Expand Down Expand Up @@ -40,6 +41,8 @@ describe( 'DataApiMixin', () => {
} );

describe( 'getData()', () => {
testUtils.createSinonSandbox();

it( 'should be added to editor interface', () => {
expect( editor ).have.property( 'getData' ).to.be.a( 'function' );
} );
Expand All @@ -49,5 +52,23 @@ describe( 'DataApiMixin', () => {

expect( editor.getData() ).to.equal( 'foo' );
} );

it( 'should get data of the second root', () => {
setData( editor.model, 'bar', { rootName: 'secondRoot' } );

expect( editor.getData( { rootName: 'secondRoot' } ) ).to.equal( 'bar' );
} );

it( 'should pass options object to data.get() method internally', () => {
const spy = testUtils.sinon.spy( editor.data, 'get' );
const options = { rootName: 'main', trim: 'none' };

setData( editor.model, 'foo' );

expect( editor.getData( options ) ).to.equal( 'foo' );

testUtils.sinon.assert.calledOnce( spy );
testUtils.sinon.assert.calledWith( spy, options );
} );
} );
} );

0 comments on commit 4f8abd1

Please sign in to comment.