wikis: normalize Unicode characters in wiki bodies.
* Introduce an abstraction for normalizing attributes. Very loosely modeled after https://github.com/fnando/normalize_attributes. * Normalize wiki bodies to Unicode NFC form. * Normalize Unicode space characters in wiki bodies (strip zero width spaces, normalize line endings to CRLF, normalize Unicode spaces to ASCII spaces). * Trim spaces from the start and end of wiki page bodies. This may cause wiki page diffs to show spaces being removed even when the user didn't explicitly remove the spaces themselves.
This commit is contained in:
18
app/logical/concerns/normalizable.rb
Normal file
18
app/logical/concerns/normalizable.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Normalizable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def normalize(attribute, method_name)
|
||||
define_method("#{attribute}=") do |value|
|
||||
normalized_value = self.class.send(method_name, value)
|
||||
super(normalized_value)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def normalize_text(text)
|
||||
text.unicode_normalize(:nfc).normalize_whitespace.strip
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,6 +3,7 @@ class ApplicationRecord < ActiveRecord::Base
|
||||
|
||||
include Deletable
|
||||
include Mentionable
|
||||
include Normalizable
|
||||
extend HasBitFlags
|
||||
extend Searchable
|
||||
|
||||
|
||||
@@ -3,11 +3,13 @@ class WikiPage < ApplicationRecord
|
||||
|
||||
META_WIKIS = ["list_of_", "tag_group:", "pool_group:", "howto:", "about:", "help:", "template:"]
|
||||
|
||||
before_validation :normalize_title
|
||||
before_validation :normalize_other_names
|
||||
before_save :update_dtext_links, if: :dtext_links_changed?
|
||||
after_save :create_version
|
||||
|
||||
normalize :title, :normalize_title
|
||||
normalize :body, :normalize_text
|
||||
|
||||
validates :title, tag_name: true, presence: true, uniqueness: true, if: :title_changed?
|
||||
validates :body, presence: true, unless: -> { is_deleted? || other_names.present? }
|
||||
validate :validate_rename
|
||||
@@ -151,10 +153,6 @@ class WikiPage < ApplicationRecord
|
||||
title.to_s.downcase.delete_prefix("~").gsub(/[[:space:]]+/, "_").gsub(/__/, "_").gsub(/\A_|_\z/, "")
|
||||
end
|
||||
|
||||
def normalize_title
|
||||
self.title = WikiPage.normalize_title(title)
|
||||
end
|
||||
|
||||
def normalize_other_names
|
||||
self.other_names = other_names.map { |name| WikiPage.normalize_other_name(name) }.uniq
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user