FIX: Sync client & server rules regarding tag names. (#6400)

This commit is contained in:
Bianca Nenciu 2018-10-02 06:45:44 +02:00 committed by Guo Xiang Tan
parent da9eee5262
commit 719a433c03
2 changed files with 45 additions and 2 deletions

View File

@ -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));

View File

@ -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"
);
}