Sha256: 131bd7e6f08c32fd7b16445dc230325b9324045db81c7ec6aa731c986b4a56c8

Contents?: true

Size: 740 Bytes

Versions: 6

Compression:

Stored size: 740 Bytes

Contents

# frozen_string_literal: true

module PlatformosCheck
  class ImgLazyLoading < HtmlCheck
    severity :suggestion
    categories :html, :performance
    doc docs_url(__FILE__)

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

    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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
platformos-check-0.4.14 lib/platformos_check/checks/img_lazy_loading.rb
platformos-check-0.4.13 lib/platformos_check/checks/img_lazy_loading.rb
platformos-check-0.4.12 lib/platformos_check/checks/img_lazy_loading.rb
platformos-check-0.4.11 lib/platformos_check/checks/img_lazy_loading.rb
platformos-check-0.4.10 lib/platformos_check/checks/img_lazy_loading.rb
platformos-check-0.4.9 lib/platformos_check/checks/img_lazy_loading.rb