Problem

When a user pastes text more then that Maxlength setting a lot of JS errors appear in the console and prevents the user from editing within the text field..

Console JS errors:

Uncaught TypeError: Cannot set property 'contentEditable' of null
at $.a (ckeditor.js?v=4.6.2:773)
at ckeditor.js?v=4.6.2:26

CKeditor errors

Screen recording of text field flashing after more text pasted than limit allows.

Steps to Recreate

  1. Set a Maxlength limit on a rich text field
  2. Enable the Force Truncate option
  3. Do not enable the Safe truncate HTML option
  4. Paste text that exceeds the character limit set on the field

Proposed Resolution

If we decide to implement #3230544: Apply safe truncate HTML behavior by default on rich text fields then that will resolve this issue.

If we decide not to implement that feature then consider incorporating the patch submitted.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

anpolimus created an issue. See original summary.

Ujval Shah’s picture

Hi anpolimus,

Please provide steps to re-produce the issue.

Thanks,
Ujval

anpolimus’s picture

@Ujval Shah
1. Configure field to have maxlength limit
2. Try to paste large text to this field.
3. Try to edit this field value after pasting.
4. Explore errors at the console and slow browser.

anpolimus’s picture

Issue summary: View changes
mr.baileys’s picture

Status: Active » Postponed (maintainer needs more info)

I'm unable to reproduce this on simplytest.me with the following steps (using maxlength-1.0-beta2):

  1. Add max length and summary max length of 50 to the body-field of the basic page content type included in the standard profile.
  2. Visit node/add/page
  3. Open the summary input
  4. Paste 1000 character lorem ipsum in both the summary- and full fields

No javascript error is reported using the above steps. I tried both with and without the "Bypass maxlength setting"-permission.

@anpolumis: can you still reproduce this issue on a stock D8 with maxlength-1.0-beta2 enabled?

alduya’s picture

Status: Postponed (maintainer needs more info) » Needs work

I experienced the same problem with both the beta1 and current dev version. But it only when using Google Chrome, not in Firefox or Safari.
It looks as if the JS keeps evaluating the length and moving the cursor to the start of the text. Again and again.

It happens both when pasting to long text or typing more than the text limit.

alduya’s picture

Status: Needs work » Active
alduya’s picture

Status: Active » Postponed

Sorry, after coming back to work today and restarting everything I noticed that with the dev version there is also no problem in chrome.
Could someone maybe create a new beta release. The current one is pretty old.

Dafydd Owen’s picture

Tried both the beta and dev versions of the module with this issue present in both for me in chrome. As a quick fix I commented out the lines 314-316 (below) in maxlength.js and all seems well.

e.editor.on('elementsPathUpdate', function(e) {
	setTimeout(function(){ml.ckeditorChange(e)}, 100);
});
i-grou’s picture

Version: 8.x-1.x-dev » 8.x-1.0-beta4
FileSize
109.44 KB

Have another issue here: when user pastes text that exceeds the limit page freezes. No errors in console - just can see how the text editor is shaking and then page freezes and I can't edit any fields:
Pasting long text

After some investigation I've found out that there is an eternal loop caused by lines 323-325 (probably this event is triggered several times and then it becomes eternal loop):

e.editor.on('elementsPathUpdate', function(e) {
  setTimeout(function(){ml.ckeditorChange(e)}, 100);
});

Any ideas how to solve it?

FYI - text field configuration:
Max length: 350
Force truncate: true
Safe truncate html: true

i-grou’s picture

[this comment should be ignored, see the next one]

i-grou’s picture

FileSize
1.35 KB

Here's a patch that fixes eternal check loop.
The idea is this: to avoid eternal loop and freezing let's get time of this update and then stop execution if previous update was executed just now.

P.S. In most cases it works ok, but some times if user would hit all keys possible (including Ctrl+V) very fast and click mouse buttons at the same time - editor would freeze the page again... Would not investigate more for now, as this patch satisfies me, and I'm not sure that this bug is caused by maxlength module only.

i-grou’s picture

[this comment should be ignored, see the next one]

i-grou’s picture

Well there still was another bug - it was not possible to select a text using mouse:
Can not select text

It processes the text on mouse down event and when the character limit is reached the text processing may behave not as expected. I believe that maxlength thinks that text length exceeds the X characters limit (because it has html tags) and then performs processing to reduce the text length. Text is not reduced, because html tags are striped and length is recalculated during processing, but it updates editor content any way. That’s why it was not possible to select a text using mouse.

So previous patch was improved to check if content is different and stop unwanted processing if not.

wombatbuddy’s picture

As a workaround, the 'CKEditor Wordcount' module can be used instead. It works good with the CKEditor.

i-grou’s picture

Thanks @wobatbuddy for suggestion. However it seems to me that module does not provide any validation features, so it may require some extra coding to make it work as expected.

i-grou’s picture

Is there a chance that patch above would be added to the module in the nearest future? Or some other solution would be implemented?

mlncn’s picture

Status: Postponed » Needs review
cedewey’s picture

Status: Needs review » Postponed (maintainer needs more info)
FileSize
135.66 KB

anpolimus,

Thank you for reporting this.

ivanklochko,

Thank you for taking the time to write a patch for this.

I tested Maxlength 8.x-1.0-rc1 with Drupal 8.9.18 on Chrome Version 92.0.4515.159 (Official Build) (64-bit) using Elementary OS 5.1.7 Built on Ubuntu 18.04.4 LTS.

I could not reproduce the error you describe, including the last issue you reported of not being able to select text.

Here is a screencast of what I saw.

Screencast pasting more text than maxlength allows.

cedewey’s picture

cedewey’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

Since I can't reproduce this issue and we haven't received more info from others, I'm going to close this.

cedewey’s picture

Issue summary: View changes
Status: Closed (cannot reproduce) » Postponed
Related issues: +#3230544: Apply safe truncate HTML behavior by default on rich text fields
FileSize
554.15 KB

Ok, I was able to recreate this issue after all. I needed to leave "Safe Truncate HTML" off.

This is postponed because we may just apply that Safe Truncate HTML automatically to rich text fields, which would then resolve this issue.

#3230544: Apply safe truncate HTML behavior by default on rich text fields

I've also updated the original ticket description with more clear steps to reproduce.

cedewey’s picture

Status: Postponed » Closed (outdated)

I'm closing this since we are moving forward with #3230544: Apply safe truncate HTML behavior by default on rich text fields, which renders this issue obsolete.