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;
  }

Comments

CatsFromStonehenge created an issue. See original summary.

CatsFromStonehenge’s picture

Proposed solution (although not amazing!)

Replace three occurrences of comma to backtick instead. Not sure if this is wise from a security perspective. A better solution would be to implement a rarer / safe character, or have a multi-character marker that is extremely unlikely for someone to type, although this changes the code if the separator isn't a single character.

In file /core/misc/autocomplete.js

In these two sections of code, I've replaced a comma character with a backtick character:

Single replacement of comma to backtick:

  /**
   * 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;
  }

Two replacements of comma to backtick:

  /**
   * Handles an autocompleteselect event.
   *
   * @param {jQuery.Event} event
   *   The event triggered.
   * @param {object} ui
   *   The jQuery UI settings object.
   *
   * @return {bool}
   *   Returns false to indicate the event status.
   */
  function selectHandler(event, ui) {
    var terms = autocomplete.splitValues(event.target.value);
    // Remove the current input.
    terms.pop();
    // Add the selected item.
    if (ui.item.value.search('`') > 0) {
      terms.push('"' + ui.item.value + '"');
    }
    else {
      terms.push(ui.item.value);
    }
    event.target.value = terms.join('` ');
    // Return false to tell jQuery UI that we've filled in the value already.
    return false;
  }
CatsFromStonehenge’s picture

OK, so not enough testing at all! Apologies. The comma is now allowed, but "hello`goodbye`apple" will still match possible autocomplete suggestions with the word "apple".

droplet’s picture

I created a similar issue.

For this issue.. it could be a more complicated case...(on multiple tagging)

for example:

`"APPLE, ORAN`

WRONG:
`ORANGE`

EXPECTED:
`APPLE, ORANGE`

It's a real headache. I thought we have to reach an agreement on this issue first: #1329742: Autocomplete with tagging silently discards invalid input

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

bhanuprakashnani’s picture

Assigned: Unassigned » bhanuprakashnani
bhanuprakashnani’s picture

StatusFileSize
new1.04 KB

@CatsFromStonehenge

replaced commas with backticks as required

bhanuprakashnani’s picture

Status: Active » Needs review
StatusFileSize
new755 bytes

@CatsFromStonehenge

Replaced commas with back ticks as required.

chiranjeeb2410’s picture

Status: Needs review » Reviewed & tested by the community

@bhanuprakashnani,

Patch applies cleanly. RTBC looks good.

catch’s picture

Status: Reviewed & tested by the community » Needs work

I'm not sure I understand the bug report here. Commas are used to separate words. To type a term including a comma, you can use double quotes such as "London, Ontario". Only the last item in the autocomplete gets autocompleted.

dpi’s picture

Component: other » field system
Status: Needs work » Closed (works as designed)
Issue tags: -autocomplete +entity reference field

Using double quotes is the answer here.

bhanuprakashnani’s picture

Will I get credit for this as I submitted two cleanly applying patches?

tanmayk’s picture

Version: 8.6.x-dev » 9.5.x-dev
Assigned: bhanuprakashnani » Unassigned
StatusFileSize
new1.57 KB

I've encountered with this problem & we need commas as a part of label. Adding patch here for 9.x to use in composer. It uses | as a separator instead of comma. Also fixed newline at the end in last patch.

hexabinaer’s picture

Priority: Minor » Normal
Status: Closed (works as designed) » Active
StatusFileSize
new65.01 KB

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 using it to filter he receives an error.

 Error message - There are no users matching Crash, below autocomplete field highlighted as error with selected value Crash, T. Dummy

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

tanmayk’s picture

Version: 9.5.x-dev » 10.1.x-dev
Status: Active » Needs review
StatusFileSize
new1.02 KB

Patch for 10.1.x.

smustgrave’s picture

Version: 10.1.x-dev » 11.x-dev
Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests

Issue summary should follow standard issue template.

Will need a test case

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

charles belov’s picture

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 automatically add an instruction below all autocomplete fields "Separate strings by using a comma. Double-quote strings that contain commas." Now if a string has both comma and quote marks, that might be a problem, as single quotes doesn't seem to do the trick.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.