Module: ValidateHTML

Defined in:
lib/validate_html.rb,
lib/validate_html/railtie.rb,
lib/validate_html/version.rb,
lib/validate_html/configuration.rb,
lib/validate_html/mailer_observer.rb,
lib/validate_html/rack_middleware.rb,
lib/validate_html/active_support_notification_handler.rb

Overview

Validate HTML as it leaves your rails application

Defined Under Namespace

Modules: ActiveSupportNotificationHandler, MailerObserver Classes: Configuration, Error, InvalidHTMLError, NotRememberingMessagesError, RackMiddleware, Railtie

Constant Summary collapse

VERSION =
'0.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationConfiguration (readonly)

Returns:



121
122
123
# File 'lib/validate_html.rb', line 121

def configuration
  @configuration ||= Configuration.new
end

.remembered_messagesArray<String> (readonly)

Returns:

  • (Array<String>)


109
110
111
# File 'lib/validate_html.rb', line 109

def remembered_messages
  @remembered_messages ||= []
end

Class Method Details

.configure {|config| ... } ⇒ void

This method returns an undefined value.

Configure ValidateHTML

Examples:

ValidateHTML.configure do |c|
  c.remember_messages = true
  c.environments = ['test']
end

Yield Parameters:



134
135
136
# File 'lib/validate_html.rb', line 134

def configure
  yield configuration
end

.forget_messagesvoid

This method returns an undefined value.

Clear any remembered messages



115
116
117
# File 'lib/validate_html.rb', line 115

def forget_messages
  @remembered_messages = []
end

.raise_remembered_messagesvoid

This method returns an undefined value.

Raise any remembered messages

Raises:



98
99
100
101
102
103
104
105
# File 'lib/validate_html.rb', line 98

def raise_remembered_messages
  raise NotRememberingMessagesError unless configuration.remember_messages?
  return if remembered_messages.empty?

  messages = remembered_messages
  forget_messages
  raise InvalidHTMLError, messages.uniq.join("---\n")
end

.validate_html(html, name: nil, content_type: nil, raise_on_invalid_html: configuration.raise_on_invalid_html?) ⇒ Boolean

Validate the HTML using by parsing it with nokogiri

skip any errors matching patterns in ValidateHTML::Configuration#ignored_errors

if there are any errors remaining: remember the errors if ValidateHTML::Configuration#remember_messages is true, save the invalid html into the ValidateHTML::Configuration#snapshot_path directory, and raise InvalidHTMLError with the full messages if raise_on_invalid_html is true or return false

if there are no errors, return true

Examples:

ValidateHTML.validate_html('<strong><em>Very Emphasized</strong></em>', name: 'My Emphasized Fragment')
# raises: ValidateHTML::InvalidHTMLError with this message:
#
#   Invalid html from My Emphasized Fragment (ValidateHTML::InvalidHTMLError)
#   Parsed using Nokogiri::HTML5::DocumentFragment
#   Document saved at: [Configuration#snapshot_path]/1a8ce99806ddeccc3a5f2904ba07c7fa5ae4659d.html
#
#   1:28: ERROR: That tag isn't allowed here  Currently open tags: html, strong, em.
#   <strong><em>Very Emphasized</strong></em>
#                              ^
#   1:37: ERROR: That tag isn't allowed here  Currently open tags: html.
#   <strong><em>Very Emphasized</strong></em>
#                                       ^

Parameters:

  • html (String)
  • name (String) (defaults to: nil)

    filename or http path or email subject or etc to print in the error message

  • content_type (String) (defaults to: nil)

    mime type of the document to assist determining encoding

  • raise_on_invalid_html (Boolean) (defaults to: configuration.raise_on_invalid_html?)

Returns:

  • (Boolean)

    true if there are no validation errors

Raises:

  • (InvalidHTMLError)

    if the html is not valid and raise_on_invalid_html is true



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/validate_html.rb', line 79

def validate_html(html, name: nil, content_type: nil, raise_on_invalid_html: configuration.raise_on_invalid_html?)
  return true if html.empty?

  doc = parse_html(html, find_encoding(content_type))

  errors = filter_errors(doc.errors)

  return true if errors.empty?

  handle_errors(name, doc, html, errors, raise_on_invalid_html)

  false
end