Sha256: 414825101ee2e5457b5eadae0444f3d788ec3793c313bb54cff673e035e3f430

Contents?: true

Size: 1.38 KB

Versions: 25

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  # Reports errors when trying to use parser-blocking script tags
  class ImgWidthAndHeight < HtmlCheck
    severity :error
    categories :html, :performance
    doc docs_url(__FILE__)

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

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

      record_units_in_field_offenses("width", width, node:)
      record_units_in_field_offenses("height", height, 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:)
    end

    private

    def record_units_in_field_offenses(attribute, value, node:)
      return unless ENDS_IN_CSS_UNIT.match?(value)

      value_without_units = value.gsub(ENDS_IN_CSS_UNIT, '')
      add_offense(
        "The #{attribute} attribute does not take units. Replace with \"#{value_without_units}\"",
        node:
      )
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
platformos-check-0.2.0 lib/platformos_check/checks/img_width_and_height.rb
platformos-check-0.1.0 lib/platformos_check/checks/img_width_and_height.rb
platformos-check-0.0.3 lib/platformos_check/checks/img_width_and_height.rb
platformos-check-0.0.2 lib/platformos_check/checks/img_width_and_height.rb
platformos-check-0.0.1 lib/platformos_check/checks/img_width_and_height.rb