Class: Codeqa::Checkers::CheckErbHtml

Inherits:
Codeqa::Checker show all
Defined in:
lib/codeqa/checkers/check_erb_html.rb

Instance Attribute Summary

Attributes inherited from Codeqa::Checker

#errors, #sourcefile

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Codeqa::Checker

available?, #initialize

Constructor Details

This class inherits a constructor from Codeqa::Checker

Class Method Details

.check?(sourcefile) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/codeqa/checkers/check_erb_html.rb', line 6

def self.check?(sourcefile)
  sourcefile.html?
end

Instance Method Details

#checkObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/codeqa/checkers/check_erb_html.rb', line 18

def check
  result = nil
  with_existing_file(html) do |filename|
    Open3.popen3("tidy -q -e -xml '#{filename}'") do |_in_stream, _out_stream, err_stream|
      message = err_stream.read
      result = message if message =~ /(Error:|missing trailing quote|end of file while parsing attributes)/m
    end # IO.popen
  end # Tempfile

  return unless result
  errors.add(:source, html)
  errors.add(nil, result)
end

#hintObject



14
15
16
# File 'lib/codeqa/checkers/check_erb_html.rb', line 14

def hint
  'The html I see after removing the erb stuff is not valid (find the unclosed tags and attributes).'
end

#htmlObject



32
33
34
35
36
37
38
39
# File 'lib/codeqa/checkers/check_erb_html.rb', line 32

def html
  @html ||= begin
              html = ErbSanitizer.new(sourcefile.content).result
              html = html.force_encoding('UTF-8') if html.respond_to?(:force_encoding)
              html.gsub(%r{<script[ >].*?</script>|<style[ >].*?</style>}m,
                        '<!--removed script/style tag-->')
            end
end

#nameObject



10
11
12
# File 'lib/codeqa/checkers/check_erb_html.rb', line 10

def name
  'erb html'
end