Class: ThemeCheck::AssetSizeCSS

Inherits:
HtmlCheck show all
Includes:
RegexHelpers
Defined in:
lib/theme_check/checks/asset_size_css.rb

Constant Summary

Constants included from RegexHelpers

RegexHelpers::HTML_LIQUID_PLACEHOLDER, RegexHelpers::LIQUID_TAG, RegexHelpers::LIQUID_TAG_OR_VARIABLE, RegexHelpers::LIQUID_VARIABLE, RegexHelpers::START_OR_END_QUOTE

Constants inherited from HtmlCheck

HtmlCheck::START_OR_END_QUOTE

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES

Instance Attribute Summary collapse

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #theme

Instance Method Summary collapse

Methods included from RegexHelpers

#matches

Methods included from ChecksTracking

#inherited

Methods inherited from Check

#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_theme?

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Constructor Details

#initialize(threshold_in_bytes: 100_000) ⇒ AssetSizeCSS

Returns a new instance of AssetSizeCSS.



11
12
13
# File 'lib/theme_check/checks/asset_size_css.rb', line 11

def initialize(threshold_in_bytes: 100_000)
  @threshold_in_bytes = threshold_in_bytes
end

Instance Attribute Details

#threshold_in_bytesObject (readonly)

Returns the value of attribute threshold_in_bytes.



9
10
11
# File 'lib/theme_check/checks/asset_size_css.rb', line 9

def threshold_in_bytes
  @threshold_in_bytes
end

Instance Method Details

#href_to_file_size(href) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/theme_check/checks/asset_size_css.rb', line 26

def href_to_file_size(href)
  # asset_url (+ optional stylesheet_tag) variables
  if href =~ /^#{LIQUID_VARIABLE}$/o && href =~ /asset_url/ && href =~ Liquid::QuotedString
    asset_id = Regexp.last_match(0).gsub(START_OR_END_QUOTE, "")
    asset = @theme.assets.find { |a| a.name.end_with?("/" + asset_id) }
    return if asset.nil?
    asset.gzipped_size

  # remote URLs
  elsif href =~ %r{^(https?:)?//}
    asset = RemoteAssetFile.from_src(href)
    asset.gzipped_size
  end
end


15
16
17
18
19
20
21
22
23
24
# File 'lib/theme_check/checks/asset_size_css.rb', line 15

def on_link(node)
  return if node.attributes['rel'] != "stylesheet"
  file_size = href_to_file_size(node.attributes['href'])
  return if file_size.nil?
  return if file_size <= threshold_in_bytes
  add_offense(
    "CSS on every page load exceeding compressed size threshold (#{threshold_in_bytes} Bytes)",
    node: node
  )
end