-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to check if editor has no content/is empty? #401
Comments
This is an important feature. In v4 we simply discard the data on output if it looks like empty. @mlewand, can you point us to this piece of code in v4? |
There's this config option called When calling editor's As for content that's true that some elements might be recognized as part of empty content, e.g. empty paragraph, div, section etc. However IMHO having an empty table should not mark editor as empty. |
Thanks, @mlewand! I propose to KISS on this and have an approach similar to v4 at data output processing. I would not make it configurable for now. |
I really don't like that regexp solution (and most regexp solutions :P). Besides, it would need to be implemented in HtmlDP, because only it works with HTML strings. So, we may need a similar solution for the MarkdownDP later on (e.g. to not print empty list items) and for any other DP. So, first we need to answer whether this is HTML related feature and can be implemented in HtmlDP because it doesn't concern other formats. Or that it's a generic feature and is required for all formats. The other question is whether we may need to make it in any way adjustable in the future. We could, perhaps, base this on simple schema checks (checking whether elements are blocks and if so ignoring them if empty). If someone would need to filter out more empty content, then they could do this based on their own checks (you can always have your own The last argument means that having a separate |
My 2c: right now we could do this using view, but it won't be 100% accurate (I mean, it depends on what we will want to achieve). In view, we have several different element types, but only two of them are "real" data: Other idea proposed by @pjasiun was to enhance schema with possibility to mark which elements are data and which are storing data. In other words, which elements should be treated as empty if they have no content. For example, image is always a content, no matter if it is empty or not. Paragraph is empty if it does not have content. But table is more complicated, maybe it should not be treated as empty even if it is. |
We have two options:
I'm definitely for option 1, which can at any moment by replaced by 2, if 1 will be revealed to be inefficient. That's my point on KISS. |
+1 it makes a lot of sense
Keep in mind that you also want to check for number of such "empty elements" as 2 paragraphs should not be identified as empty content. |
I would look what this issue reporter expected. This may drive the expectation of other developers. If they look to an empty editor, they expect that editor to be empty. By not doing this with |
Also, keep in mind that such checks are many times done at the server side, once data is posted. |
We won't be able to change this once developers rely on it. And if we plan to make such a non-BC changes it, then there's no point in making them in the first place. We were meant to have a quite stable API after 1.0, weren't we?
An It's actually quite depressing because they proposed getting a raw text from the editor and it's the most popular answer there ;|. The right answer is 3x less popular. An SRP rule tells us that a single method should do one thing. This is KISS. Moreover, it lets you compose the methods differently. If the default E.g., I'd KISS regarding |
Then go around explaining that |
As we can see, you need to explain how to check if the editor is empty in CKE4's case too. I, personally, believe that a more explicit API is more verbose. As a result, the system is less magical and less confusing. I guess we can continue this discussion, but I'd like to hear an opinion of people less accustomed to CKE4. After all, this is not for us. |
Definition – we call an editor empty if it contains an empty paragraph, list item, heading or another kind of flow element. No text, no images, not sure about tables or other rich content like e.g. simplebox widget. Pick one:
Note: Consider the fact that definition of the empty editor is not definitive. It's our current assumption and may differ per case. |
I'd go with second because first option gives us nothing and Additionally, I'd consider naming the method Edit: I mean, above is pretty obvious, but what I want to highlight is a fact that specific implementation may have it's own rules and we should treat "default content" or "no content" as something "configurable" and solution-specific. |
I'm for option 1 (surprise!) as it is closer to the way humans think and also to enable out of the box server side empty check.
|
I'm for option number 2. - Also it's going to be much easier to find in docs, and for that reason I'm for staying with |
I repeat myself here, to underline this problem... please do not ignore the server side empty check problem. |
Also, it would be great to see reports of issues like "getData() is empty when the editor is not empty" in CKEditor 4. @mlewand, do you have anything? |
Not a problem. This is an integration aspect. The data must not be sent to the server (or empty string/whatever must be sent) if the editor is empty. It's JS sending the data most of the time. And even if we consider textarea replacement case, the "is empty" check can be considered in this case. But then it's only an optional end-feature's trait. Not a hardcoded logic which affects every possible use case. |
It's still a pity that I can't easily trim data returned by the editor. I want to get data from the form element and send to the server without empty paragraphs or/and white spaces at the end of content. I don't have too much experience with using RTEs I'm more familiar with pure form elements and for me it is natural to trim the content. |
If we have a helper to "trim" the data it will solve the problem with checking if data is empty. const isEmpty = trimmedData == ''; exactly the same as checking if form element value is empty. |
This is a very interesting point, in fact. You're right that a normal |
I think that @oskarwrobel's point answers the question and our entire discussion went in the wrong direction :D. Trimming what would be trailing whitespaces and new lines in plain text or Markdown is indeed necessary. What's important – it can't be implemented in a reasonable way as a separate step to be executed after
WDYT? |
Both fine for me.
Hard to say. On one hand, it would be more correct on the model, since it's a model what have a semantic information in the scheme. On the other, the view we can modify in the
For sure there should be a separate |
After reviewing some code which do: editor.document.on( 'change', () => {
const data = editor.getData();
saveButtonView.isEnabled = isNotEmpty( data );
} ); I realized that the Note that checking if the document is empty can be done very quickly, without involving the whole data processing. |
This is a separate requirement and I don't think that it's any important now. You can easily throttle the change event to not cause any serious performance issues. So, to conclude, we need this now:
In the future, we can add:
PS. Some ideas regarding implementation. The trimming mechanism could be a function which gets model element (root) and returns a cloned tree with the trimmed content. Such function will be super useful – e.g. the future isEmpty( rootName ) {
return isEmpty( trim( this.getRoot( rootName ) ) );
} It may also be possible for this method to cache the result until any |
At the current stage, making |
But we need to enable it now (as @oskarwrobel requested), so we can easily make it configurable already. This is extremely cheap and will prevent situations when someone wants to configure it, but needs to wait a month until we release a new version. In fact, having many topics on our heads we usually need to delay such little fixes for a longer time which is certainly super frustrating for the community (we're talking here about hardcoding a certain behaviour, without an option to workaround this; I wouldn't oppose in a different case). Hence, since we're already thinking about this, let's not force people to wait. The only question is whether this should be on or off by default. I'm fine with both, @pjasiun is too, but I can see that the pressure to just trim the data by default is high, so let's enable it. I updated my above proposal to match this change. |
@Reinmar has this been implemented yet? We have a problem where we want to keep the contents of an empty editor, because it has formatting applied that we want to keep, eg. |
Sorry to hear that. We actually stumbled upon this ourselves that's why this issue was discussed already. But there's always something more important to work on. I bumped up this ticket to the "next" milestone. It does not mean we'll be able to work on this in the next iteration but it should speed up things anyway. |
It would be useful also a config prop like: Using it with React would be amazing! Thanks! |
SO question for the CKEditor5: https://stackoverflow.com/questions/52536974/how-to-get-ckeditor5-data-with-out-nbsp-filler. |
Is there a date where a bugfix for this gets released? I see this bug is quite critical, as the content repository get "dirty" like that. |
@f1ames has it as a high-priority ticket in the current iteration together with https://github.com/ckeditor/ckeditor5-utils/issues/269. I can't promise anything, but it seems that we'll finally get it done and released in mid-February. |
I just stumbled upon this, until it gets released my dirty hack is to const isEmpty = (data) => {
const div = document.createElement('div');
div.innerHTML = data;
return !div.textContent.trim();
}; Works for me because I don't want to save anything in database unless there's at least one readable character, I don't care about images or tables or whatnot. I do hope a proper fix will be implemented in mid Feb, thanks guys |
I started to wonder how trimming should actually work - what content should be trimmed. There are many cases when content is empty-like or is "visually" empty. Here are some examples: Empty content
1 - 3 should be considered empty and trimmed so Multiple empty blocks / empty blocks on content boundariesContent mentioned in Empty content section is considered empty (and should be trimmed). What about cases when such content is repeated/duplicated:
If a single such element is treated as empty, multiple empty elements are still empty (something like multiplying by 0) so should be trimmed IMHO. or appears on the beginning/end of the content:
It is a little similar to cases with multiple spaces on the beginning/end of the content, which should be trimmed:
so maybe elements considered empty should be also trimmed from beginning/end of the content. I'm not sure TBH. SpacingShould whitespace sequences (marked as
such can appear also on the beginning/end of the content:
Again from
I assume that in all cases above spaces or entire elements should be trimmed. Other elementsThere are some elements which should not be trimmed even if they are empty and only elements in the editor content such as:
I just wanted to point out that the origin of this issue is |
I'd define a non-empty content differently – it's content which has:
And that's it. Anything else is noise – any attribute/contrainer element with no content or not being a content itself can be trimmed. So, the only problem is how to define "non-plain container/attribute elements". This may require setting some property on them. |
If you have jQuery you can use:- var message = window.editor.getData(), textOnly = $(message).text().trim(); if(textOnly != ''){ //save your data } else { //show error message } |
When discussing how to implement this functionality we started having doubts regarding trimming whitespaces. Returning empty string when the editor has no visible content (e.g. a single empty paragraph) is one thing and it was implemented in ckeditor/ckeditor5-engine#1656, but returning content with whitespaces trimmed at the start and end is more tricky. It's tricky because we need to either modify some data structure on which we work to remove those whitespaces from it or do it on the fly when converting model to the view or view to the DOM. Doing this on the fly during m->v conversion is hard because of markers and other ranges and positions that might be affected. It seems a bit easier to clone the model with all markers, trim it and them convert. Still – it's a challenge because there may be a logic tracking those markers and the model structure and this logic would need to be cloned too (i.e. applied to the cloned structure). Finally, this could theoretically be done on the view, but doing this there means losing a lot of information. The quality and stability of such a solution would be lower than if this was done on the model. Therefore, taken these issues, we decided to focus now on simply returning this empty data string when there's no visible content in the editor. We won't work on trimming for now. |
Feature: Introduced whitespace trimming to `Model#hasContent()`. `DataController#get()` method can now trim empty data (so it returns empty string instead of `<p> </p>`). Closes [ckeditor/ckeditor5#401](ckeditor/ckeditor5#401). BREAKING CHANGE: `DataController#get()` method now returns an empty string when the editor content is empty (instead of returning e.g. `<p> </p>`).
config.ignoreEmptyParagraph = is not working in Django CKEditor I'm using ckeditor in my django project. I added the following code at the end of html file:
I want to stop users from submitting empty messages. |
@minaee CKEditor 4 and CKEditor 5 are two different apps. If you have a problem with CKE4, please report your issue in CKEditor 4 issue tracker. |
In most of cases editor will have an empty paragraph on init. So
getData() == ''
will befalse
almost all the time. What if someone adds some spaces or new lines?For example, you use CKE as an input to write posts or comments and you want to prevent user from submitting form until
content.trim() == ''
. How to do it?My first thought was to add an option
getData( { trim: true } )
or create methodisEmpty( document )
but @pjasiun and @scofalik pointed out that it won't be so easy because there is no common rule for every feature that defines when feature is empty. Eg. Can table be empty? or image?The text was updated successfully, but these errors were encountered: