Problem/Motivation

The Maxlength character limit appears, but does not update as the author is typing.

Steps to reproduce

  1. Enable the CKEditor 5 module
  2. Configure a text format (eg: Basic HTML) to use CKEditor 5
  3. Configure a formatted text field to have a Maxlength setting.
  4. Type into the field with a Maxlength setting.

Screen recording of character count not updating as author types.

Proposed resolution

Add support for CKEditor 5 by working with the CKEditor Wordcount feature. Preserving the current configuration workflow and character count behavior is preferred. We would only rely on CKEditor Wordcount's calculating of the character count (at least for now).

Remaining tasks

  1. ✅ Recruit a contributor to work on this issue
  2. ✅ Finalize approach with maintainers
  3. ✅ Implement bug fix
  4. ✅ Community Test
  5. ✅ Maintainer Test
  6. ✅ Maintainer code review
  7. ✅ Merge into dev branch

Additional Resources

User interface changes

None

API changes

TBD

Data model changes

TBD

Issue fork maxlength-3267413

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

vlyalko created an issue. See original summary.

cedewey’s picture

Version: 2.0.0-rc1 » 2.0.x-dev
andrei.vesterli’s picture

FileSize
151.31 KB

Hello @cedewey

The issue I see here is that the plugin "Character Count Plugin - jQuery plugin" provided by the module says:

"Dynamic character count for text areas and input fields"...

What do we have is:
1. A hidden textarea that is actually counting very good
2. The ckeditor5 has not any input or textarea and the plugin is not applied well.

In conclusion...we probably need to look at: https://ckeditor.com/docs/ckeditor5/latest/features/word-count.html

dwebpoint’s picture

I've reproduced the issue for Administrator role. And I don't have hidden textarea per #3
It looks interesting that issue is not reproducible for other custom roles.
Drupal 9.3.16, maxlength 2.0.0-rc2

cedewey’s picture

Issue summary: View changes
FileSize
1.34 MB

Vera, thanks for reporting this issue.

Andrei and Dzianis, thank you for investigating the issue further.

I've elaborated on the steps to reproduce, what I'm seeing on my end and links to resources that might be helpful.

If any of you (or anyone else reading this) is interested in working on this, let me know. Myself and the other maintainers don't have time at the moment to work on this, but would be happy to support others as best we can.

zcht’s picture

Priority: Normal » Major

Now that Drupal 9.5 & 10 have been released, this issue seems to have become even more important. I can confirm that under Drupal 9.5 and CKEditor 5 the function is no longer given. It would be nice if we could address this issue a bit more actively now that CKEditor 4 is deprecated and sooner or later everyone will switch to version 5.

cedewey’s picture

Issue summary: View changes

Hi all,

We agree this is a major issue. Unfortunately us maintainers don't have capacity at the moment to actively work on this issue. I've added a note about the bug on the project page so people are aware of MaxLength's current incompatibility with CKEditor 5.

If anyone from the broader Drupal community has time to work on this we would greatly appreciate it.

Feuerwagen’s picture

Both my time and knowledge of CKEditor 5 are limited, so I can't contribute anything substantial right now. But at least I added this module to CKEditor 5 Upgrade Coordination docs page for visibility.

cedewey’s picture

Thank you, that's helpful!

hipp2bsquare made their first commit to this issue’s fork.

hbrokmeier made their first commit to this issue’s fork.

fisherman90 made their first commit to this issue’s fork.

fisherman90’s picture

If you want, please have a look at my approach in https://git.drupalcode.org/project/maxlength/-/merge_requests/25 .

I'm repsecting the "Hard limit" option as well as syncing the textfield with the contents of the editor.
This way, maxlength counting works again.

I'm sure there's better approaches to solving this, but I didn't find any better.

bogdanstamate06’s picture

Hi @fisherman90,

I tested out your commit, and it works fine.
I have a question though. I am trying to remove from the total count number, the spaces and the line breaks.
Do you have any idea how to achieve this?

Thanks!

fisherman90’s picture

@bogdanstamate06 I've tried removing the total count number from the "Count down message" (is that what you meant?).
Also in case I understood it differently, I reduced the "Max length" Number for an already existing field, which also seems to work with spaces / lines.

Are you sure this has something to do with this issue? Or is it maybe another Problem you are running into?
Sounds like maybe your text formats need to have a look at.

At least for now I cannot reproduce that the spaces / line breaks get broken by the change.
If this is still an issue for you and does not come from text formats or so: Can you post a reproductible scenario for me, so I can try to get the error on my setup, too?

fisherman90’s picture

Status: Active » Needs review
hipp2bsquare’s picture

Status: Needs review » Reviewed & tested by the community

Excellent! I've tested fisherman90's merge request and it's working great on both CKE5 and 4. I believe all our test coverage is CKE-version agnostic, but I will confirm with the other maintainers.

hbrokmeier’s picture

I also tested fisherman90's merge request with CKE5, and it looks good, the countdown is working as expected.

joevagyok’s picture

Version: 2.0.x-dev » 2.1.x-dev
joevagyok’s picture

Assigned: Unassigned » joevagyok
Status: Reviewed & tested by the community » Needs work

All works fine for me as well however, I'm working on implementing test coverage for both CKE versions.

joevagyok’s picture

Status: Needs work » Needs review

Implemented tests for CKEditor5.

joevagyok’s picture

Status: Needs review » Needs work

Seems there is a problem over D10. As the tests are failing and I confirm after manual testing too that the character count doesn't work over D10. On D9 it works fine with CKEditor5, as you can see on the tests as well.

joevagyok’s picture

Status: Needs work » Needs review

Alright, D10 is passing now as well after the fix.

cedewey’s picture

Status: Needs review » Needs work
FileSize
2.19 MB

Thanks for updating the merge request Joe.

This fix needs a bit more work. If I set this to be a Hard Limit, when I reach the end of the limit and I keep typing, the text I type ends up being added to the beginning of the body field.

Screen recording of text being added to the beginning of the body field when typing past a hard limit.

joevagyok’s picture

Over D9.5.2+CKE4 and D10+CKE5 I could reproduce the issue and I can make characters appear at the first position only if I spam the keyboard, but thats due to the fact that the JS re-inserts the value and at that point the cursor is not at the end position because the wysiwyg is empty. This issue was present from before, not related to the CKE5 integration code. We might need a different issue for that to handle.

joevagyok’s picture

I pushed a potential fix for the problem described in #25.

However, I found a problem when typing and pressing SHIFT+ENTER for new line I get the
alert('Expected end tag: ' + expected_tag_name + '; Found end tag: '+tag_name);
kick in making the browser tab unusable, need to close and reopen the page. Not sure if it is due to my text filter + editor configuration or it is an actual issue. Need to look into it.

joevagyok’s picture

I made a commit that adds annotations to the functions we have in the JS. Sorry for the noise, but I really needed to document the code properly because it is very difficult to get around the legacy code without proper documentation.

cedewey’s picture

Unfortunately with the latest version of the merge request the counter is not updating at all. I've tested this with Drupal 9.5.3 and Drupal 9.5.4. I also tried flushing all caches and testing again.

Screencast showing the counter not updating with CKEditor 5.

joevagyok’s picture

For me it works fine, I reinstalled the whole thing and can't reproduce the problem. And the automated tests are passing too.

Chewie made their first commit to this issue’s fork.

joevagyok’s picture

Status: Needs work » Needs review

Fixed the issue with the single closing element thanks to @Chewie. Adapted the ckeditor tests accordingly.

cedewey’s picture

Confirmed that the page breaks and other singleton HTML tags now work as expected.

I was able to figure out why the counter was not working in some cases.

If my text format starts in CKEditor 4 and I switch to CKEditor 5, then the counter no longer updates. If I save the node and then return to the edit form then the counter and limit method works as expected.

I think we can open a separate issue for that and proceed with this fix, but curious what other maintainers thinks. This also needs a code review from one other maintainer.

cedewey’s picture

Status: Needs review » Needs work

Upon further testing if you include an image or video within a text field with MaxLength is triggers an infinite loop. The single closing element issue still needs some more work.

Rajab Natshah’s picture

I had a further testing round on the fix from the MR 25
Still having issue of not updated remaining: count status.

Rajab Natshah’s picture

Tested MR 25 for the 2nd round with

Maximum length: 200
Maximum summary length: 200

It's working well

Rajab Natshah’s picture

Had a 3rd round with

Maximum length: 200

But no

Maximum summary length: 200

Title, Body field with summary limit
Title, Body field with summary limit

Title, Body field with no summary limit
Title, Body field with no summary limit

Anaconda777’s picture

Hi,

Works for me with ckeditor 5

Thank you!

joevagyok’s picture

Status: Needs work » Needs review
cedewey’s picture

Assigned: joevagyok » hipp2bsquare
Issue summary: View changes

I just tested this with an image in the text field and it's now working as expected. I also tested with
and tags. Those too are working as expected.

Once this receives a code review from a maintainer this will be ready to merge into 2.1.x and we can issue a new release. Exciting!

cedewey’s picture

Assigned: hipp2bsquare » hbrokmeier
hbrokmeier’s picture

Assigned: hbrokmeier » cedewey
Issue summary: View changes

Looks good to me! Elements with attributes are working and nothing stood out to me in the code review.

joevagyok’s picture

Status: Needs review » Reviewed & tested by the community

joevagyok’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kevinquillen’s picture