Skip to content

Commit

Permalink
Merge pull request #2560 from archivesspace/ANW-1201
Browse files Browse the repository at this point in the history
ANW-1201: bugfix for UTF-8 chars in slugs breaking the PUI
  • Loading branch information
Brian Hoffman committed Jan 18, 2022
2 parents e057615 + a4885f1 commit 28185c5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
12 changes: 9 additions & 3 deletions backend/app/lib/slugs/slug_helpers_generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def self.reset_autogenerated_slugs
# eg. migration 129
def self.clean_slug(slug)
if slug
encoding_options = {
:invalid => :replace,
:undef => :replace,
:replace => '',
:universal_newline => true
}

slug = slug.encode(Encoding.find('ASCII'), encoding_options)

# if the slug contains two slashes (forward or backward) next to each other, completely zero it out.
# this is intended to revert an entity to use the URI if the ID or name the slug was generated from is a URI.
slug = "" if slug =~ /\/\// || slug =~ /\\/
Expand All @@ -59,9 +68,6 @@ def self.clean_slug(slug)
# remove double hypens
slug = slug.gsub("--", "")

# remove en and em dashes
slug = slug.gsub(/[\u2013-\u2014]/, "")

# remove single quotes
slug = slug.gsub("'", "")

Expand Down
2 changes: 1 addition & 1 deletion backend/spec/model_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
expect(resource[:slug]).to eq(expected_slug)
end
it "cleans slug" do
resource = Resource.create_from_json(build(:json_resource, :is_slug_auto => true, :id_0 => "Foo Bar Baz&&&&", :id_1 => "", :id_2 => "", :id_3 => ""))
resource = Resource.create_from_json(build(:json_resource, :is_slug_auto => true, :id_0 => "Foo Bar Baz&&&&", :id_1 => "", :id_2 => "", :id_3 => ""))
expect(resource[:slug]).to eq("foo_bar_baz")
end
it "dedupes slug" do
Expand Down
13 changes: 10 additions & 3 deletions backend/spec/spec_slugs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

def clean_slug(slug)
if slug
# remove all non-ASCII chars
encoding_options = {
:invalid => :replace,
:undef => :replace,
:replace => '',
:universal_newline => true
}

slug = slug.encode(Encoding.find('ASCII'), encoding_options)

# if the slug contains a slash, completely zero it out.
# this is intended to revert an entity to use the URI if the ID or name the slug was generated from is a URL.
slug = "" if slug =~ /\//
Expand All @@ -16,9 +26,6 @@ def clean_slug(slug)
# remove double hypens
slug = slug.gsub("--", "")

# remove en and em dashes
slug = slug.gsub(/[\u2013-\u2014]/, "")

# remove single quotes
slug = slug.gsub("'", "")

Expand Down

0 comments on commit 28185c5

Please sign in to comment.