Class: HTMLProofer::Check::Scripts

Inherits:
HTMLProofer::Check show all
Defined in:
lib/html_proofer/check/scripts.rb

Instance Attribute Summary

Attributes inherited from HTMLProofer::Check

#external_urls, #failures, #internal_urls, #options

Instance Method Summary collapse

Methods inherited from HTMLProofer::Check

#add_failure, #add_to_external_urls, #add_to_internal_urls, #create_element, #initialize, #short_name, short_name, subchecks

Methods included from Utils

#blank?, #create_nokogiri, #pluralize

Constructor Details

This class inherits a constructor from HTMLProofer::Check

Instance Method Details

#check_sriObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/html_proofer/check/scripts.rb', line 35

def check_sri
  if blank?(@script.node["integrity"]) && blank?(@script.node["crossorigin"])
    add_failure("SRI and CORS not provided in: #{@script.url.raw_attribute}", line: @script.line,
      content: @script.content)
  elsif blank?(@script.node["integrity"])
    add_failure("Integrity is missing in: #{@script.url.raw_attribute}", line: @script.line,
      content: @script.content)
  elsif blank?(@script.node["crossorigin"])
    add_failure("CORS not provided for external resource in: #{@script.url.raw_attribute}", line: @script.line,
      content: @script.content)
  end
end

#missing_src?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/html_proofer/check/scripts.rb', line 31

def missing_src?
  @script.node["src"].nil?
end

#runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/html_proofer/check/scripts.rb', line 6

def run
  @html.css("script").each do |node|
    @script = create_element(node)

    next if @script.ignore?
    next unless @script.content.strip.empty?

    # does the script exist?
    if missing_src?
      add_failure("script is empty and has no src attribute", line: @script.line, content: @script.content)
    elsif @script.url.protocol_relative?
      add_failure("script link #{@script.url} is a protocol-relative URL, use explicit https:// instead",
        line: @script.line, content: @script.content)
    elsif @script.url.remote?
      add_to_external_urls(@script.url, @script.line)
      check_sri if @runner.check_sri?
    elsif !@script.url.exists?
      add_failure("internal script reference #{@script.src} does not exist", line: @script.line,
        content: @script.content)
    end
  end

  external_urls
end