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

Commit 4f8abd1

Browse files
authored
Merge pull request #161 from ckeditor/t/ckeditor5/401
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).
2 parents 8efe0b9 + 35c0e62 commit 4f8abd1

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/editor/utils/dataapimixin.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const DataApiMixin = {
2424
/**
2525
* @inheritDoc
2626
*/
27-
getData() {
28-
return this.data.get();
27+
getData( options ) {
28+
return this.data.get( options );
2929
}
3030
};
3131

@@ -67,9 +67,14 @@ export default DataApiMixin;
6767
* See the {@glink features/markdown Markdown output} guide for more details.
6868
*
6969
* Note: Not only is the format of the data configurable, but the type of the `getData()`'s return value does not
70-
* have to be a string either. You can e.g. return an object or a DOM `DocumentFragment` if you consider this
70+
* have to be a string either. You can e.g. return an object or a DOM `DocumentFragment` if you consider this
7171
* the right format for you.
7272
*
7373
* @method #getData
74+
* @param {Object} [options]
75+
* @param {String} [options.rootName='main'] Root name.
76+
* @param {String} [options.trim='empty'] Whether returned data should be trimmed. This option is set to `'empty'` by default,
77+
* which means that whenever editor content is considered empty, an empty string is returned. To turn off trimming
78+
* use `'none'`. In such cases exact content will be returned (for example `'<p>&nbsp;</p>'` for an empty editor).
7479
* @returns {String} Output data.
7580
*/

tests/editor/utils/dataapimixin.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import DataApiMixin from '../../../src/editor/utils/dataapimixin';
77
import Editor from '../../../src/editor/editor';
88
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
9+
import testUtils from '../../../tests/_utils/utils';
910
import mix from '@ckeditor/ckeditor5-utils/src/mix';
1011
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
1112

@@ -40,6 +41,8 @@ describe( 'DataApiMixin', () => {
4041
} );
4142

4243
describe( 'getData()', () => {
44+
testUtils.createSinonSandbox();
45+
4346
it( 'should be added to editor interface', () => {
4447
expect( editor ).have.property( 'getData' ).to.be.a( 'function' );
4548
} );
@@ -49,5 +52,23 @@ describe( 'DataApiMixin', () => {
4952

5053
expect( editor.getData() ).to.equal( 'foo' );
5154
} );
55+
56+
it( 'should get data of the second root', () => {
57+
setData( editor.model, 'bar', { rootName: 'secondRoot' } );
58+
59+
expect( editor.getData( { rootName: 'secondRoot' } ) ).to.equal( 'bar' );
60+
} );
61+
62+
it( 'should pass options object to data.get() method internally', () => {
63+
const spy = testUtils.sinon.spy( editor.data, 'get' );
64+
const options = { rootName: 'main', trim: 'none' };
65+
66+
setData( editor.model, 'foo' );
67+
68+
expect( editor.getData( options ) ).to.equal( 'foo' );
69+
70+
testUtils.sinon.assert.calledOnce( spy );
71+
testUtils.sinon.assert.calledWith( spy, options );
72+
} );
5273
} );
5374
} );

0 commit comments

Comments
 (0)