On non-HTML fields, two consecutive line breaks are miscounted as one character

Created on 21 December 2023, 11 months ago
Updated 16 March 2024, 8 months ago

Problem/Motivation

We have a non-HTML textarea containing using both #maxlength and maxlength.js.
The way maxlength.js counts line breaks is confusing and is not consistent with how the browser counts with the maxlength attribute or how the serverside validation counts with #maxlength.

(The reason we are using a maxlength attribute instead of the strict limit option from maxlength.js is because this field can containing existing data longer than the limit.
In that case maxlength.js strict mode will remove the extra characters at the end automatically whereas #maxlength will keep it, prevent the user from typing more and return an error if the user submits the form. So it lets the user remove the extra characters without deciding for them that the end should be removed.)

Steps to reproduce

Type the following text in a textarea:



foo



bar


maxlength.js will count only 7 characters.

Proposed resolution

This is the combination of 2 problems:
Line breaks at the start and the end are trimmed before counting.
ml.unify_lineending() causes two consecutive line breaks to be counted only once.

The attached patch makes it work for us but I suppose it will mess up count in HTML fields?
Maybe we could add a setting to skip calling ml.strip_tags() on plaintext textareas?

Remaining tasks

  • ✅ Get maintainer approval for the proposed resolution
  • ✅ Implement resolution
  • ❌ Write test coverage (contact a maintainer if you need help)
  • ❌ Maintainer review via the UI
  • ❌ Maintainer Code Review #1
  • ❌ Maintainer code review #2
  • ❌ Merge into dev branch, with credit to author and participants
🐛 Bug report
Status

Needs work

Version

3.0

Component

Code

Created by

🇫🇷France prudloff Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024