Skip to content

A platform for community discussion. Free, open, simple.

FIX: Make truncation of -- consistent across tag creations (#38013)

Natalie Tayf546a3a403b2
diff --git a/app/services/tags/bulk_create.rb b/app/services/tags/bulk_create.rb
index d600c79be6a..2de71e367ce 100644
--- a/app/services/tags/bulk_create.rb
+++ b/app/services/tags/bulk_create.rb
@@ -67,27 +67,18 @@ class Tags::BulkCreate
raw_tag_names.each do |raw_name|
next if raw_name.blank?
- normalized_input = raw_name.strip
- normalized_input = normalized_input.downcase if SiteSetting.force_lowercase_tags
- normalized_input = normalized_input.gsub(/[[:space:]]+/, "-")
-
- if normalized_input.length > SiteSetting.max_tag_length
- results[:failed][raw_name] = I18n.t(
- "tags.bulk_create.tag_too_long",
- count: SiteSetting.max_tag_length,
- )
- next
- end
-
- tag_name = DiscourseTagging.clean_tag(raw_name)
+ tag_name = DiscourseTagging.clean_tag(raw_name, truncate: false)
if tag_name.blank?
results[:failed][raw_name] = I18n.t("tags.bulk_create.invalid_name")
next
end
- if tag_name != normalized_input
- results[:failed][raw_name] = I18n.t("tags.bulk_create.invalid_name")
+ if tag_name.length > SiteSetting.max_tag_length
+ results[:failed][raw_name] = I18n.t(
+ "tags.bulk_create.tag_too_long",
+ count: SiteSetting.max_tag_length,
+ )
next
end
diff --git a/frontend/discourse/select-kit/services/tag-utils.js b/frontend/discourse/select-kit/services/tag-utils.js
index d132d9ff2fc..8ce746709b8 100644
--- a/frontend/discourse/select-kit/services/tag-utils.js
+++ b/frontend/discourse/select-kit/services/tag-utils.js
@@ -74,6 +74,7 @@ export default class TagUtils extends Service {
.trim()
.replace(/\s+/g, "-")
.replace(/[\/\?#\[\]@!\$&'\(\)\*\+,;=\.%\\`^\s|\{\}"<>]+/g, "")
+ .replace(/-{2,}/g, "-")
.substring(0, this.siteSettings.max_tag_length);
if (this.siteSettings.force_lowercase_tags) {
diff --git a/frontend/discourse/tests/unit/services/tag-utils-test.js b/frontend/discourse/tests/unit/services/tag-utils-test.js
new file mode 100644
index 00000000000..8eeca77ca03
--- /dev/null
+++ b/frontend/discourse/tests/unit/services/tag-utils-test.js
@@ -0,0 +1,41 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+
+module("Unit | Service | tag-utils", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.tagUtils = getOwner(this).lookup("service:tag-utils");
+ });
+
+ test("createContentFromInput squeezes consecutive dashes", function (assert) {
+ assert.strictEqual(
+ this.tagUtils.createContentFromInput("test--tag"),
+ "test-tag"
+ );
+ assert.strictEqual(
+ this.tagUtils.createContentFromInput("multi---dash"),
+ "multi-dash"
+ );
+ assert.strictEqual(
+ this.tagUtils.createContentFromInput("a--b--c"),
+ "a-b-c"
+ );
+ });
+
+ test("createContentFromInput handles basic normalization", function (assert) {
+ assert.strictEqual(
+ this.tagUtils.createContentFromInput(" spaced "),
+ "spaced"
+ );
+ assert.strictEqual(
+ this.tagUtils.createContentFromInput("has spaces"),
+ "has-spaces"
+ );
+ assert.strictEqual(
+ this.tagUtils.createContentFromInput("special!@#chars"),
+ "specialchars"
+ );
+ });
+});
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index 21a24d40fb5..3f9b1ba1de3 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -804,7 +804,7 @@ module DiscourseTagging
Discourse.cache.delete(TAGS_STAFF_CACHE_KEY)
end
- def self.clean_tag(tag)
+ def self.clean_tag(tag, truncate: true)
tag = tag.dup
tag.downcase! if SiteSetting.force_lowercase_tags
tag.strip!
@@ -812,7 +812,7 @@ module DiscourseTagging
tag.gsub!(/[^[:word:][:punct:]]+/, "")
tag.gsub!(TAGS_FILTER_REGEXP, "")
tag.squeeze!("-")
- tag[0...SiteSetting.max_tag_length]
+ truncate ? tag[0...SiteSetting.max_tag_length] : tag
end
def self.tags_for_saving(tags_arg, guardian, opts = {})
diff --git a/spec/services/tags/bulk_create_spec.rb b/spec/services/tags/bulk_create_spec.rb
index b113cda3a9f..2a19bbdf914 100644
--- a/spec/services/tags/bulk_create_spec.rb
+++ b/spec/services/tags/bulk_create_spec.rb
@@ -155,8 +155,40 @@ RSpec.describe(Tags::BulkCreate) do
end
end
- context "when some tags are invalid" do
- let(:tag_names) { ["", "valid-tag", " ", "%^&ui^&*", "!!!test"] }
+ context "when tags contain consecutive dashes" do
+ let(:tag_names) { %w[test--tag multi---dash a--b--c] }
+
+ it { is_expected.to run_successfully }
+
+ it "normalizes consecutive dashes and creates tags" do
+ expect { result }.to change { Tag.count }.by(3)
+ expect(Tag.where(name: %w[test-tag multi-dash a-b-c]).count).to eq(3)
+ end
+
+ it "returns normalized names" do
+ expect(result.results[:created]).to contain_exactly("test-tag", "multi-dash", "a-b-c")
+ expect(result.results[:failed]).to be_empty
+ end
+ end
+
+ context "when tags contain characters that get stripped by clean_tag" do
+ let(:tag_names) { %w[valid-tag %^&ui^&* !!!test] }
+
+ it { is_expected.to run_successfully }
+
+ it "cleans and creates tags" do
+ expect { result }.to change { Tag.count }.by(3)
+ expect(Tag.where(name: %w[valid-tag ui test]).count).to eq(3)
+ end
+
+ it "returns cleaned names" do
+ expect(result.results[:created]).to contain_exactly("valid-tag", "ui", "test")
+ expect(result.results[:failed]).to be_empty
+ end
+ end
+
+ context "when tags are entirely invalid" do
+ let(:tag_names) { ["", "valid-tag", " ", "@!#$%^&*"] }
it { is_expected.to run_successfully }
@@ -166,7 +198,7 @@ RSpec.describe(Tags::BulkCreate) do
it "reports failed tags" do
expect(result.results[:created]).to contain_exactly("valid-tag")
- expect(result.results[:failed].keys).to contain_exactly("%^&ui^&*", "!!!test")
+ expect(result.results[:failed].keys).to contain_exactly("@!#$%^&*")
end
end
@@ -202,7 +234,7 @@ RSpec.describe(Tags::BulkCreate) do
context "when mixing valid, existing, and invalid tags" do
fab!(:existing_tag) { Fabricate(:tag, name: "existing") }
- let(:tag_names) { ["new-tag", "existing", "", "invalid@@@", "another-new"] }
+ let(:tag_names) { ["new-tag", "existing", "", "@!#$%", "another-new"] }
it { is_expected.to run_successfully }
@@ -210,7 +242,7 @@ RSpec.describe(Tags::BulkCreate) do
expect { result }.to change { Tag.count }.by(2)
expect(result.results[:created]).to contain_exactly("new-tag", "another-new")
expect(result.results[:existing]).to contain_exactly("existing")
- expect(result.results[:failed].keys).to contain_exactly("invalid@@@")
+ expect(result.results[:failed].keys).to contain_exactly("@!#$%")
end
end
end