Class: PlatformosCheck::ImgLazyLoading

Inherits:
HtmlCheck show all
Defined in:
lib/platformos_check/checks/img_lazy_loading.rb

Constant Summary collapse

ACCEPTED_LOADING_VALUES =
Set.new(%w[lazy eager]).freeze
LOADING_DEFAULT_ATTRIBUTE =
' loading="eager"'

Constants inherited from HtmlCheck

HtmlCheck::START_OR_END_QUOTE

Constants inherited from Check

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

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #platformos_app

Instance Method Summary collapse

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_platformos_app?

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Instance Method Details

#on_img(node) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/platformos_check/checks/img_lazy_loading.rb', line 12

def on_img(node)
  loading = node.attributes["loading"]&.downcase
  return if ACCEPTED_LOADING_VALUES.include?(loading)

  add_offense("Use loading=\"eager\" for images visible in the viewport on load and loading=\"lazy\" for others", node:) do |corrector|
    start_pos = node.start_index + node.markup.index('>')
    corrector.insert_after(node, LOADING_DEFAULT_ATTRIBUTE, start_pos...start_pos)
  end
end