about summary refs log tree commit diff
path: root/app/validators/html_validator.rb
diff options
context:
space:
mode:
authorReverite <github@reverite.sh>2019-04-12 01:38:18 -0700
committerReverite <github@reverite.sh>2019-04-12 01:38:18 -0700
commite10a9794f4ed7c90e3190f285359f55dd00da435 (patch)
tree579ebf95d6bbf091d05e66907a9c8168c926e0af /app/validators/html_validator.rb
parentff736905fa534f7189e57c1d0c14fbac45f239a1 (diff)
parentbb50ec2e6687238ad8b2ec545a73270fee7a7b09 (diff)
Merge branch 'glitch' into production
Diffstat (limited to 'app/validators/html_validator.rb')
-rw-r--r--app/validators/html_validator.rb12
1 files changed, 7 insertions, 5 deletions
diff --git a/app/validators/html_validator.rb b/app/validators/html_validator.rb
index b7caee5a9..1c9cd303c 100644
--- a/app/validators/html_validator.rb
+++ b/app/validators/html_validator.rb
@@ -1,18 +1,20 @@
 # frozen_string_literal: true
 
 class HtmlValidator < ActiveModel::EachValidator
+  ERROR_RE = /Opening and ending tag mismatch|Unexpected end tag/
+
   def validate_each(record, attribute, value)
     return if value.blank?
+
     errors = html_errors(value)
-    unless errors.empty?
-      record.errors.add(attribute, I18n.t('html_validator.invalid_markup', error: errors.first.to_s))
-    end
+
+    record.errors.add(attribute, I18n.t('html_validator.invalid_markup', error: errors.first.to_s)) unless errors.empty?
   end
 
   private
 
   def html_errors(str)
-    fragment = Nokogiri::HTML.fragment(str)
-    fragment.errors
+    fragment = Nokogiri::HTML.fragment(options[:wrap_with] ? "<#{options[:wrap_with]}>#{str}</#{options[:wrap_with]}>" : str)
+    fragment.errors.select { |error| ERROR_RE =~ error.message }
   end
 end