class PrelandsRails::CreateSimpleSource::ValidateZipContent::ValidateHtml::Html

Принимает строку с html разметкой и валидирует её.

Attributes

errors[R]

Public Class Methods

new(string, continue_id) click to toggle source

@param [String] string Содержимое html файла.

# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 14
def initialize(string, continue_id)
  @errors      = []
  @continue_id = continue_id
  @string      = string.gsub(/(\r\n?|\n)/, '') # убираем переносы, мешающие регуляркам
end

Public Instance Methods

valid?() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 20
def valid?
  @errors = [
    check_internal_scripts,
    check_internal_styles,
    check_continue_element,
    check_js_plug,
    check_css_plug,
    check_title_presence,
    check_a_hrefs,
    check_favicon
  ].compact

  !@errors.any?
end

Protected Instance Methods

any(regex) click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 108
def any(regex)
  @string.scan(regex).flatten.map(&:strip).map(&:present?).any?
end

Private Instance Methods

check_a_hrefs() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 92
def check_a_hrefs
  ahref_rx = /<a[^>]+href="[^"]+"/
  return unless @string[ahref_rx]

  'Links with href attribute found.'
end
check_continue_element() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 53
def check_continue_element
  rx  = Regexp.new('id="%s"' % @continue_id)
  res = @string.scan(rx).flatten
  return if res.size == 1

  return 'The continue element with id="%s" must be uniq. Found %s matches.' % [@continue_id, res.size] if res.size > 1

  'Not found continue element with id="%s"' % @continue_id
end
check_css_plug() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 74
def check_css_plug
  head_rx  = /(?<=<head)(.+)(?=<\/head>)/
  css_rx   = 'href="index.css"'

  @string[head_rx]
  res = $1&.[](css_rx)
  return if res.present?

  'Plug index.css before closing tag of head.'
end
check_favicon() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 99
def check_favicon
  favicon_rx = /<link.+href="icon.ico">/
  return if @string[favicon_rx]

  '`link href="icon.ico"` not found.'
end
check_internal_scripts() click to toggle source

@return [nil] Если внутри html НЕ содержится JavaScript код в виде <script>…</script> @return [String] Иначе - вернёт сообщение об ошибке

# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 39
def check_internal_scripts
  rx  = /<script[^>]*>([^<]+)<\/script>/
  return unless any(rx)

  'A JavaScript code is detected inside html-file. Please, move it to index.js.'
end
check_internal_styles() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 46
def check_internal_styles
  rx  = /<style[^>]*>([^<]+)<\/style>/
  return unless any(rx)

  'An internal CSS is detected inside html-file. Please, move it to index.css.'
end
check_js_plug() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 63
def check_js_plug
  body_rx  = /<body[^>]*>(.+)<\/body>/
  js_rx    = '<script src="index.js"'

  @string[body_rx]
  res = $1&.[](js_rx)
  return if res.present?

  'Plug index.js script before closing tag of body.'
end
check_title_presence() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_html/html.rb, line 85
def check_title_presence
  title_rx = /<title>[^<]+<\/title>/
  return if @string[title_rx]

  'Tag `title` not found.'
end