Class: Uniword::Accessibility::Rules::HeadingStructureRule

Inherits:
AccessibilityRule show all
Defined in:
lib/uniword/accessibility/rules/heading_structure_rule.rb

Overview

Heading Structure Rule - WCAG 1.3.1 Info and Relationships

Responsibility: Check proper heading hierarchy Single Responsibility: Heading structure validation only

WCAG 1.3.1 Level A: Headings must follow proper hierarchy

Instance Attribute Summary

Attributes inherited from AccessibilityRule

#config, #level, #rule_id, #wcag_criterion

Instance Method Summary collapse

Methods inherited from AccessibilityRule

#enabled?, #initialize

Constructor Details

This class inherits a constructor from Uniword::Accessibility::AccessibilityRule

Instance Method Details

#check(document) ⇒ Array<AccessibilityViolation>

Check document heading structure

Parameters:

  • document (Document)

    Document to check

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/uniword/accessibility/rules/heading_structure_rule.rb', line 17

def check(document)
  violations = []
  headings = extract_headings(document)

  # Check if document needs headings but has none
  if headings.empty? && requires_headings?(document)
    violations << create_violation(
      message: "Document lacks heading structure (#{document.paragraphs.count} paragraphs without headings)",
      element: document,
      severity: :warning,
      suggestion: @config[:suggestion] ||
        "Add headings to structure the content for screen readers",
    )
    return violations
  end

  # Check heading hierarchy
  violations.concat(check_hierarchy(headings)) if @config[:check_hierarchy]

  # Check for H1 requirement
  if @config[:require_h1] && !has_h1?(headings)
    violations << create_violation(
      message: "Document missing top-level heading (H1)",
      element: document,
      severity: :error,
      suggestion: "Start document with a level 1 heading (H1)",
    )
  end

  violations
end