Skip to content

Commit 456543a

Browse files
ClearlyClaireGargron
authored andcommitted
Fix HTML validation (mastodon#10354)
* Fix HTML validation * Report first HTML error instead on validation error
1 parent d23dad5 commit 456543a

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

app/validators/html_validator.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
class HtmlValidator < ActiveModel::EachValidator
44
def validate_each(record, attribute, value)
55
return if value.blank?
6-
record.errors.add(attribute, I18n.t('html_validator.invalid_markup')) unless valid_html?(value)
6+
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
710
end
811

912
private
1013

11-
def valid_html?(str)
12-
Nokogiri::HTML.fragment(str).to_s == str
14+
def html_errors(str)
15+
fragment = Nokogiri::HTML.fragment(str)
16+
fragment.errors
1317
end
1418
end

config/locales/en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ en:
638638
one: Something isn't quite right yet! Please review the error below
639639
other: Something isn't quite right yet! Please review %{count} errors below
640640
html_validator:
641-
invalid_markup: contains invalid HTML markup
641+
invalid_markup: 'contains invalid HTML markup: %{error}'
642642
identity_proofs:
643643
active: Active
644644
authorize: Yes, authorize

0 commit comments

Comments
 (0)