FIX: Make category slugs lowercase (#11277)

Admins could specify category slug with upper case characters and same slug,
but with different cases could be used simultaneously.
This commit is contained in:
Bianca Nenciu
2021-01-12 17:28:33 +02:00
committed by GitHub
parent e80332a2bc
commit ec0212e56b
7 changed files with 118 additions and 30 deletions

View File

@ -6,28 +6,22 @@ module Slug
CHAR_FILTER_REGEXP = /[:\/\?#\[\]@!\$&'\(\)\*\+,;=_\.~%\\`^\s|\{\}"<>]+/ # :/?#[]@!$&'()*+,;=_.~%\`^|{}"<>
MAX_LENGTH = 255
def self.for(string, default = 'topic', max_length = MAX_LENGTH)
def self.for(string, default = 'topic', max_length = MAX_LENGTH, method: nil)
string = string.gsub(/:([\w\-+]+(?::t\d)?):/, '') if string.present? # strip emoji strings
if SiteSetting.slug_generation_method == 'encoded'
max_length = 9999 # do not truncate encoded slugs
end
method = (method || SiteSetting.slug_generation_method || :ascii).to_sym
max_length = 9999 if method == :encoded # do not truncate encoded slugs
slug =
case (SiteSetting.slug_generation_method || :ascii).to_sym
case method
when :ascii then self.ascii_generator(string)
when :encoded then self.encoded_generator(string)
when :none then self.none_generator(string)
end
slug = self.prettify_slug(slug, max_length: max_length)
(slug.blank? || slug_is_only_numbers?(slug)) ? default : slug
end
def self.sanitize(string, downcase: false, max_length: MAX_LENGTH)
slug = self.encoded_generator(string, downcase: downcase)
self.prettify_slug(slug, max_length: max_length)
end
private
def self.slug_is_only_numbers?(slug)