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;
}
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | commas-autocomplete-behaviour-change-2864549-16.patch | 1.02 KB | tanmayk |
| #14 | commas-autocomplete-behaviour-change-2864549-14.patch | 1.57 KB | tanmayk |
| #9 | 2864549-9.patch | 755 bytes | bhanuprakashnani |
| #8 | 2864549-5.patch | 1.04 KB | bhanuprakashnani |
Comments
Comment #2
CatsFromStonehenge commentedProposed 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.jsIn these two sections of code, I've replaced a comma character with a backtick character:
Single replacement of comma to backtick:
Two replacements of comma to backtick:
Comment #3
CatsFromStonehenge commentedOK, 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".
Comment #4
droplet commentedI 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
Comment #7
bhanuprakashnani commentedComment #8
bhanuprakashnani commented@CatsFromStonehenge
replaced commas with backticks as required
Comment #9
bhanuprakashnani commented@CatsFromStonehenge
Replaced commas with back ticks as required.
Comment #10
chiranjeeb2410 commented@bhanuprakashnani,
Patch applies cleanly. RTBC looks good.
Comment #11
catchI'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.
Comment #12
dpiUsing double quotes is the answer here.
Comment #13
bhanuprakashnani commentedWill I get credit for this as I submitted two cleanly applying patches?
Comment #14
tanmaykI'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.
Comment #15
hexabinaerSorry 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.
Making people conclude that it's their fault should only happen when we're sure it is ;-)
Comment #16
tanmaykPatch for 10.1.x.
Comment #17
smustgrave commentedIssue summary should follow standard issue template.
Will need a test case
Changing this behavior may also have backward compatibility concerns for contrib.
Comment #18
charles belovThis 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.