Class: ThemeCheck::ImgWidthAndHeight

Inherits:
HtmlCheck show all
Defined in:
lib/theme_check/checks/img_width_and_height.rb

Overview

Reports errors when trying to use parser-blocking script tags

Constant Summary collapse

ENDS_IN_CSS_UNIT =
/(cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vh|vmin|vmax|%)$/i

Constants inherited from HtmlCheck

HtmlCheck::START_OR_END_QUOTE, HtmlCheck::VARIABLE

Constants inherited from Check

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

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #theme

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

Methods included from JsonHelpers

#format_json_parse_error

Instance Method Details

#on_img(node) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/theme_check/checks/img_width_and_height.rb', line 11

def on_img(node)
  width = node.attributes["width"]&.value
  height = node.attributes["height"]&.value

  record_units_in_field_offenses("width", width, node: node)
  record_units_in_field_offenses("height", height, node: node)

  return if node.attributes["src"].nil? || (width && height)
  missing_width = width.nil?
  missing_height = height.nil?
  error_message = if missing_width && missing_height
    "Missing width and height attributes"
  elsif missing_width
    "Missing width attribute"
  elsif missing_height
    "Missing height attribute"
  end

  add_offense(error_message, node: node)
end