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

Introduced model.isEmpty() method. DataController.get() method can now trim empty data #1656

Merged
merged 13 commits into from
Feb 14, 2019

Conversation

f1ames
Copy link
Contributor

@f1ames f1ames commented Jan 31, 2019

Suggested merge commit message (convention)

Feature: Introduced model.isEmpty() method. DataController.get() method can now trim empty data. Closes ckeditor/ckeditor5#401.

BREAKING CHANGE: DataController.get() method now returns an empty string by default when editor content is empty.


Additional information

See ckeditor/ckeditor5#401. I have also created https://github.com/ckeditor/ckeditor5/tree/t/401 branch for easier testing.

There are a few related PRs which should be closed together with this one:

@coveralls
Copy link

coveralls commented Jan 31, 2019

Coverage Status

Coverage increased (+2.0e-05%) to 99.992% when pulling 55a07ef on t/ckeditor5/401 into f579c93 on master.

@@ -145,14 +154,21 @@ export default class DataController {
*
* @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
* Element whose content will be stringified.
* @param {Boolean} [skipEmpty=false] Whether content considered empty should be skipped. The method
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this logic should stay in get(). Less API changes is better.

* @returns {String} Output data.
*/
get( rootName = 'main' ) {
get( options ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done with destructuring.

* {@link module:engine/model/element~Element element} is considered empty.
*
* The range or element is considered non-empty if it contains any:
* * EmptyElement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a model range contain a view empty element? ;)

Copy link
Member

@Reinmar Reinmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation and documentation of isEmpty() needs a small... rework ;)

* @param {module:engine/model/range~Range|module:engine/model/element~Element} rangeOrElement Range or element to check.
* @returns {Boolean}
*/
isEmpty( rangeOrElement ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relation between isEmpty() and hasContent() needs to be clarified. Both these methods are defined on Model but they have different semantics. I wonder if we shouldn't rename hasContent() or isEmpty() to somehow differentiate between these two.

cc @pjasiun

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge isEmpty() into hasContent() (i.e. we will have a single method for that called hasContent()).

Right now there are 2 differences between them – check for markers and /\S+/. The former can be done automatically by hasContent(). The latter could be enabled by an option.trimWhitespaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// Get model range.
return this.stringify( this.model.document.getRoot( rootName ) );
return trim === 'empty' && !this.model.hasContent( root, { trimWhitespaces: true } ) ? '' : this.stringify( root );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment... ;)

@Reinmar Reinmar merged commit 2b95dc3 into master Feb 14, 2019
@Reinmar Reinmar deleted the t/ckeditor5/401 branch February 14, 2019 09:48
@Serzhs
Copy link

Serzhs commented Mar 26, 2019

I think the probelm in here is that there are no samples how to implement this in my project

ClassicEditor.model returns undefined

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to check if editor has no content/is empty?
4 participants