Default maxlength truncate algorithm has week place - even after truncating string can be longer than limit.
This is because javascript substr functions count new lines as one character not two as it have to be.
Example:
After we run obj.val().substr(0, 100) and then calculate string length with count = ml.twochar_lineending(obj.val()).length we could get count to be more that 100 (depends on how many new lines characters are found).
To fix this issue i have created patch - that will apply string truncation untill string will be no longer than limit taking into account new lines characters.
Note - i have created patch only for case when maxlength applied on simple textarea field, not WYSIWYG.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | maxlength_truncate.patch | 670 bytes | Eugene Fidelin |
| maxlength_truncate.patch | 742 bytes | Eugene Fidelin |
Comments
Comment #1
Eugene Fidelin commentedHere is updated version of patch
Comment #2
seandunaway commentedComment #2.0
seandunaway commentedchange description
Comment #3
mpp commentedThis bug now also occurs in Drupal 8.
Comment #4
cedeweyThis is a duplicate of https://www.drupal.org/project/maxlength/issues/2825511 but perhaps some of the code here can be helpful in writing the patch for the other issue.