Преглед изворни кода

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).
Piotrek Koszuliński пре 7 година
родитељ
комит
baf21792df

+ 8 - 3
packages/ckeditor5-core/src/editor/utils/dataapimixin.js

@@ -24,8 +24,8 @@ const DataApiMixin = {
 	/**
 	 * @inheritDoc
 	 */
-	getData() {
-		return this.data.get();
+	getData( options ) {
+		return this.data.get( options );
 	}
 };
 
@@ -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 - 0
packages/ckeditor5-core/tests/editor/utils/dataapimixin.js

@@ -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';
 
@@ -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' );
 		} );
@@ -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 );
+		} );
 	} );
 } );