Class: Codeqa::Checkers::HtmlValidator

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

Constant Summary collapse

REMOVED_NOKOGIRI_ERRORS =
Regexp.union(
  /Opening and ending tag mismatch: (special line 1|\w+ line \d* and special)/,
  /Premature end of data in tag special/,
  /Extra content at the end of the document/,
  /xmlParseEntityRef: no name/,
  /Entity 'nbsp' not defined/
)

Instance Attribute Summary

Attributes inherited from Codeqa::Checker

#errors, #sourcefile

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Codeqa::Checker

#initialize

Constructor Details

This class inherits a constructor from Codeqa::Checker

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


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

def self.available?
  nokogiri?
end

.check?(sourcefile) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.nokogiri?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/codeqa/checkers/html_validator.rb', line 49

def self.nokogiri?
  @loaded ||= begin
                require 'nokogiri'
                true
              end
end

Instance Method Details

#checkObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/codeqa/checkers/html_validator.rb', line 29

def check
  return unless self.class.nokogiri?
  doc = Nokogiri::XML "<special>#{stripped_html}</special>"

  doc.errors.delete_if{ |e| e.message =~ REMOVED_NOKOGIRI_ERRORS }
  errors.add(:source, sourcefile.content) unless doc.errors.empty?
  doc.errors.each do |error|
    errors.add(error.line, error.message) unless error.warning?
  end
end

#hintObject



18
19
20
# File 'lib/codeqa/checkers/html_validator.rb', line 18

def hint
  'Nokogiri found XHTML errors, please fix them.'
end

#nameObject



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

def name
  'html'
end

#stripped_htmlObject



40
41
42
43
44
45
46
47
# File 'lib/codeqa/checkers/html_validator.rb', line 40

def stripped_html
  @stripped_html ||= ErbSanitizer.
                      new(sourcefile.content).
                      result.
                      gsub(%r{<script[ >](.*?)</script>}m) do
                        "<!-- script#{"\n" * $1.scan("\n").count} /script -->"
                      end
end