Class: Uniword::Quality::ImageAltTextRule

Inherits:
QualityRule
  • Object
show all
Defined in:
lib/uniword/quality/rules/image_alt_text_rule.rb

Overview

Checks that images have accessibility text.

Responsibility: Validate image accessibility. Single Responsibility - only checks image alt text.

Validates:

  • Images have alt text

  • Alt text meets minimum length requirements

  • Document is accessible for screen readers

Examples:

Configuration

image_alt_text:
  enabled: true
  require_alt_text: true
  min_length: 10

Instance Attribute Summary

Attributes inherited from QualityRule

#config, #enabled

Instance Method Summary collapse

Methods inherited from QualityRule

#enabled?, #name

Constructor Details

#initialize(config = {}) ⇒ ImageAltTextRule

Returns a new instance of ImageAltTextRule.



21
22
23
24
25
# File 'lib/uniword/quality/rules/image_alt_text_rule.rb', line 21

def initialize(config = {})
  super
  @require_alt_text = @config.fetch(:require_alt_text, true)
  @min_length = @config[:min_length] || 10
end

Instance Method Details

#check(document) ⇒ Array<QualityViolation>

Check document for image alt text violations

Parameters:

  • document (Document)

    The document to check

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/uniword/quality/rules/image_alt_text_rule.rb', line 31

def check(document)
  violations = []

  return violations unless @require_alt_text

  image_count = 0
  document.paragraphs.each_with_index do |para, para_index|
    # Images are in runs as drawings
    para.runs.each do |run|
      run.drawings.each do |drawing|
        image_count += 1

        alt_text = extract_alt_text(drawing)

        if alt_text.nil? || alt_text.empty?
          violations << create_violation(
            severity: :error,
            message: "Image #{image_count} is missing alt text. " \
                     "Alt text is required for accessibility.",
            location: "Paragraph #{para_index + 1}, Image #{image_count}",
            element: drawing,
          )
        elsif alt_text.length < @min_length
          violations << create_violation(
            severity: :warning,
            message: "Image #{image_count} has alt text that is too short " \
                     "(#{alt_text.length} characters, minimum: #{@min_length}). " \
                     "Provide more descriptive alt text.",
            location: "Paragraph #{para_index + 1}, Image #{image_count}",
            element: drawing,
          )
        end
      end
    end
  end

  violations
end