diff --git a/app/assets/javascripts/discourse/app/lib/autocomplete.js b/app/assets/javascripts/discourse/app/lib/autocomplete.js index 974856e5c29..6c4bfc1a2be 100644 --- a/app/assets/javascripts/discourse/app/lib/autocomplete.js +++ b/app/assets/javascripts/discourse/app/lib/autocomplete.js @@ -618,7 +618,7 @@ export default function (options) { break; } } - prevIsGood = !allowedLettersRegex.test(prev); + prevIsGood = !/\s/.test(prev); if (completeTermOption) { prevIsGood ||= prev === " "; } diff --git a/app/assets/javascripts/discourse/tests/unit/lib/autocomplete-test.js b/app/assets/javascripts/discourse/tests/unit/lib/autocomplete-test.js index acfa61dfb2a..eb33b62f11b 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/autocomplete-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/autocomplete-test.js @@ -66,6 +66,55 @@ module("Unit | Utility | autocomplete", function (hooks) { ); } + test("Autocomplete can complete really short terms correctly", async function (assert) { + let element = textArea(""); + let $element = $(element); + + autocomplete.call($element, { + key: ":", + transformComplete: () => "sad:", + dataSource: () => [":sad:"], + template: compile(`
+ +
`), + }); + + simulateKey(element, "a"); + simulateKey(element, " "); + + simulateKey(element, ":"); + simulateKey(element, ")"); + simulateKey(element, "\r"); + + let sleep = (millisecs) => + new Promise((promise) => setTimeout(promise, millisecs)); + // completeTerm awaits transformComplete + // we need to wait for it to be done + // Note: this is somewhat questionable given that when people + // press ENTER on an autocomplete they do not want to be beholden + // to an async function. + let inputEquals = async function (value) { + let count = 3000; + while (count > 0 && element.value !== value) { + count -= 1; + await sleep(1); + } + }; + + await inputEquals("a :sad: "); + assert.strictEqual(element.value, "a :sad: "); + assert.strictEqual(element.selectionStart, 8); + assert.strictEqual(element.selectionEnd, 8); + }); + test("Autocomplete can account for cursor drift correctly", function (assert) { let element = textArea(""); let $element = $(element);