Commas in autocomplete textfield give incorrect behaviour

Created on 28 March 2017, over 7 years ago
Updated 15 December 2023, 7 months ago

What happens:

When typing something like this into an autocomplete textfield:
be,re,lon

The autocomplete code will only show suggestions containing the text lon. Sometimes commas are important to the user. For example if you were matching items as follows:

  • mammals, apes, chimpanzees
  • mammals, apes, monkeys
  • mammals, canines, poodle

If the user has already typed 'mammals' they might want to list only 'mammals, apes', so would type that, to list all results like that. Anyway, that's just one example.

At least one function that causes this issue is here: (in /core/misc/autocomplete.js)

  /**
   * Helper splitting terms from the autocomplete value.
   *
   * @function Drupal.autocomplete.splitValues
   *
   * @param {string} value
   *   The value being entered by the user.
   *
   * @return {Array}
   *   Array of values, split by comma.
   */
  function autocompleteSplitValues(value) {
    // We will match the value against comma-separated terms.
    var result = [];
    var quote = false;
    var current = '';
    var valueLength = value.length;
    var character;

    for (var i = 0; i < valueLength; i++) {
      character = value.charAt(i);
      if (character === '"') {
        current += character;
        quote = !quote;
      }
      else if (character === ',' && !quote) {
        result.push(current.trim());
        current = '';
      }
      else {
        current += character;
      }
    }
    if (value.length > 0) {
      result.push($.trim(current));
    }

    return result;
  }
🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Field 

Last updated about 8 hours ago

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India tanmayk Maharashtra

    I've encountered with this problem & we need commas as a part of label. Adding patch here for 9.x to use in composer.

  • Status changed to Active about 1 year ago
  • 🇩🇪Germany hexabinaer Berlin, Germany

    Sorry for re-opening but this might not even be a minor issue and should not be classified as "works as designed".

    @moritzsteinhauer observed that some of his users create user names containing a comma. Now when he uses the Authored by autocomplete filter on admin/content, he can select the suggested author but on filter receives an error.

    Making people conclude that it's their fault should only happen when we're sure it is ;-)

  • Status changed to Needs review 7 months ago
  • 🇮🇳India tanmayk Maharashtra

    Patch for 10.1.x.

  • Status changed to Needs work 7 months ago
  • 🇺🇸United States smustgrave

    Issue summary should follow standard issue template.

    Will need a test case

    Changing this behavior may also have backward compatibility concerns for contrib.

Production build 0.69.0 2024