diff --git a/ui/app/utils/escape-task-name.js b/ui/app/utils/escape-task-name.js index fff0e4741f8..68d0276f207 100644 --- a/ui/app/utils/escape-task-name.js +++ b/ui/app/utils/escape-task-name.js @@ -1,4 +1,19 @@ +import emojiRegex from 'emoji-regex'; + export default function escapeTaskName(taskName) { + // Store emoji character components differently to bypass escaping: + // "stringšŸ„³" becomes "stringUNICODE55358.UNICODE56691." + const taskNameWithTransformedEmoji = taskName.replace(emojiRegex(), emoji => { + return emoji.split('').map(char => { + return `UNICODE${char.charCodeAt(0)}.`; + }).join(''); + }); + // Regular expression is taken from here: https://stackoverflow.com/a/20053121 - return taskName.replace(/[^a-zA-Z0-9,._+@%/-]/g, '\\$&'); + const escaped = taskNameWithTransformedEmoji.replace(/[^a-zA-Z0-9,._+@%/-]/g, '\\$&'); + + // Restore temporarily-transformed emoji + return escaped.replace(/UNICODE(\d+)./g, (match, digits) => { + return String.fromCharCode(digits); + }); } diff --git a/ui/package.json b/ui/package.json index 6685947e618..343184574e0 100644 --- a/ui/package.json +++ b/ui/package.json @@ -99,6 +99,7 @@ "ember-template-lint": "^2.9.0", "ember-test-selectors": "^2.1.0", "ember-truth-helpers": "^2.0.0", + "emoji-regex": "^9.0.0", "eslint": "^5.16.0", "eslint-plugin-ember": "^7.7.2", "eslint-plugin-ember-a11y-testing": "a11y-tool-sandbox/eslint-plugin-ember-a11y-testing#ca31c9698c7cb105f1c9761d98fcaca7d6874459", diff --git a/ui/tests/unit/utils/escape-task-name-test.js b/ui/tests/unit/utils/escape-task-name-test.js index b34ab2430ea..0d57cfe943a 100644 --- a/ui/tests/unit/utils/escape-task-name-test.js +++ b/ui/tests/unit/utils/escape-task-name-test.js @@ -6,5 +6,6 @@ module('Unit | Utility | escape-task-name', function() { assert.equal(escapeTaskName('plain'), 'plain'); assert.equal(escapeTaskName('a space'), 'a\\ space'); assert.equal(escapeTaskName('dollar $ign'), 'dollar\\ \\$ign'); + assert.equal(escapeTaskName('emojišŸ„³'), 'emojišŸ„³'); }); }); diff --git a/ui/yarn.lock b/ui/yarn.lock index cec575beffd..70b773c81af 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -7733,6 +7733,11 @@ emoji-regex@^8.0.0: resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37" integrity sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A== +emoji-regex@^9.0.0: + version "9.0.0" + resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-9.0.0.tgz#48a2309cc8a1d2e9d23bc6a67c39b63032e76ea4" + integrity sha512-6p1NII1Vm62wni/VR/cUMauVQoxmLVb9csqQlvLz+hO2gk8U2UYDfXHQSUYIBKmZwAKz867IDqG7B+u0mj+M6w== + emojis-list@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"