Class: Codeqa::Checkers::HtmlValidator
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
#errors, #sourcefile
Class Method Summary
collapse
Instance Method Summary
collapse
#initialize
Class Method Details
.available? ⇒ Boolean
10
11
12
|
# File 'lib/codeqa/checkers/html_validator.rb', line 10
def self.available?
nokogiri?
end
|
.check?(sourcefile) ⇒ Boolean
6
7
8
|
# File 'lib/codeqa/checkers/html_validator.rb', line 6
def self.check?(sourcefile)
sourcefile.html?
end
|
.nokogiri? ⇒ 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
#check ⇒ Object
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
|
#hint ⇒ Object
18
19
20
|
# File 'lib/codeqa/checkers/html_validator.rb', line 18
def hint
'Nokogiri found XHTML errors, please fix them.'
end
|
#name ⇒ Object
14
15
16
|
# File 'lib/codeqa/checkers/html_validator.rb', line 14
def name
'html'
end
|
#stripped_html ⇒ Object
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
|