Skip to content

Commit b52b69a

Browse files
Gargronhiyuki2578
authored andcommitted
Change HTML validator to ignore all errors except unmatched tags (mastodon#10534)
1 parent f416c82 commit b52b69a

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

app/models/form/admin_settings.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class Form::AdminSettings
4949

5050
attr_accessor(*KEYS)
5151

52-
validates :site_short_description, :site_description, :site_extended_description, :site_terms, :closed_registrations_message, html: true
52+
validates :site_short_description, :site_description, html: { wrap_with: :p }
53+
validates :site_extended_description, :site_terms, :closed_registrations_message, html: true
5354
validates :registrations_mode, inclusion: { in: %w(open approved none) }
5455
validates :min_invite_role, inclusion: { in: %w(disabled user moderator admin) }
5556
validates :site_contact_email, :site_contact_username, presence: true

app/validators/html_validator.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# frozen_string_literal: true
22

33
class HtmlValidator < ActiveModel::EachValidator
4+
ERROR_RE = /Opening and ending tag mismatch|Unexpected end tag/
5+
46
def validate_each(record, attribute, value)
57
return if value.blank?
8+
69
errors = html_errors(value)
7-
unless errors.empty?
8-
record.errors.add(attribute, I18n.t('html_validator.invalid_markup', error: errors.first.to_s))
9-
end
10+
11+
record.errors.add(attribute, I18n.t('html_validator.invalid_markup', error: errors.first.to_s)) unless errors.empty?
1012
end
1113

1214
private
1315

1416
def html_errors(str)
15-
fragment = Nokogiri::HTML.fragment(str)
16-
fragment.errors
17+
fragment = Nokogiri::HTML.fragment(options[:wrap_with] ? "<#{options[:wrap_with]}>#{str}</#{options[:wrap_with]}>" : str)
18+
fragment.errors.select { |error| ERROR_RE =~ error.message }
1719
end
1820
end

0 commit comments

Comments
 (0)