diff --git a/app/assets/javascripts/select-kit/components/mini-tag-chooser.js.es6 b/app/assets/javascripts/select-kit/components/mini-tag-chooser.js.es6 index 3c8f11d948..f44fdb33e2 100644 --- a/app/assets/javascripts/select-kit/components/mini-tag-chooser.js.es6 +++ b/app/assets/javascripts/select-kit/components/mini-tag-chooser.js.es6 @@ -220,6 +220,29 @@ export default ComboBox.extend(TagsMixin, { this.destroyTags(tags); }, + _sanitizeContent(content, property) { + switch (typeof content) { + case "string": + // See lib/discourse_tagging#clean_tag. + return content + .toLowerCase() + .trim() + .replace(/\s+/, "-") + .replace(/[\/\?#\[\]@!\$&'\(\)\*\+,;=\.%\\`^\s|\{\}"<>]+/, "") + .substring(0, this.siteSettings.max_tag_length); + default: + return get(content, this.get(property)); + } + }, + + valueForContentItem(content) { + return this._sanitizeContent(content, "valueAttribute"); + }, + + _nameForContent(content) { + return this._sanitizeContent(content, "nameProperty"); + }, + actions: { onSelect(tag) { this.set("tags", makeArray(this.get("tags")).concat(tag)); diff --git a/test/javascripts/components/mini-tag-chooser-test.js.es6 b/test/javascripts/components/mini-tag-chooser-test.js.es6 index 062aa37cbf..69eae80f9e 100644 --- a/test/javascripts/components/mini-tag-chooser-test.js.es6 +++ b/test/javascripts/components/mini-tag-chooser-test.js.es6 @@ -11,6 +11,8 @@ componentTest("default", { template: "{{mini-tag-chooser allowAny=true filterable=true tags=tags}}", beforeEach() { + this.siteSettings.max_tag_length = 24; + this.site.set("can_create_tag", true); this.set("tags", ["jeff", "neil", "arpit"]); @@ -26,7 +28,7 @@ componentTest("default", { }); } - if (params.queryParams.q === "joffrey") { + if (params.queryParams.q === "joffrey" || params.queryParams.q === "invalid'tag" || params.queryParams.q === "01234567890123456789012345") { return response({results: []}); } @@ -72,6 +74,24 @@ componentTest("default", { "it creates the tag" ); + await this.get("subject").expand(); + await this.get("subject").fillInFilter("invalid'tag"); + await this.get("subject").keyboard("enter"); + assert.deepEqual( + this.get("tags"), + ["jeff", "neil", "arpit", "régis", "joffrey", "invalidtag"], + "it strips invalid characters in tag" + ); + + await this.get("subject").expand(); + await this.get("subject").fillInFilter("01234567890123456789012345"); + await this.get("subject").keyboard("enter"); + assert.deepEqual( + this.get("tags"), + ["jeff", "neil", "arpit", "régis", "joffrey", "invalidtag"], + "it does not allow creating long tags" + ); + await click( this.get("subject") .el() @@ -80,7 +100,7 @@ componentTest("default", { ); assert.deepEqual( this.get("tags"), - ["jeff", "neil", "arpit", "régis"], + ["jeff", "neil", "arpit", "régis", "joffrey"], "it removes the tag" ); }