Commas in autocomplete textfield give incorrect behaviour

Created on 28 March 2017, about 8 years ago
Updated 31 March 2023, about 2 years 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

Closed: works as designed

Version

9.5

Component
Field 

Last updated 2 days ago

Live updates comments and jobs are added and updated live.
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 almost 2 years 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 over 1 year ago
  • 🇮🇳India tanmayk Maharashtra

    Patch for 10.1.x.

  • Status changed to Needs work over 1 year 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.

  • 🇺🇸United States charles belov San Francisco, CA, US

    This has turned out to be an issue for us when a page title has a comma in it. Outlook uses semicolon as a delimiter in autocomplete fields.

    If you want to keep it as a comma, maybe add an instruction below the field to quote strings containing commas. Now if a string has both comma and quote marks, that might be a problem.

Production build 0.71.5 2024