from abc import ABC, abstractmethod
from bs4 import BeautifulSoup, Tag # Ensure Tag is imported
class AccessibilityRule(ABC):
"""Abstract base class for an accessibility rule checker."""
@property
@abstractmethod
def issue_key(self) -> str:
"""A unique string key identifying the type of issue this rule checks for.
This should match the keys used in the 'issues_to_fix' list in your dataset.
"""
pass
@abstractmethod
def check(self, soup: BeautifulSoup, original_html_info: dict) -> bool:
"""
Checks the provided HTML (parsed as a BeautifulSoup soup) for compliance
with this specific accessibility rule.
Args:
soup: The BeautifulSoup object representing the LLM's modified HTML.
original_html_info: A dictionary containing information about the original
HTML snippet, potentially including the original HTML string
if needed for context by some rules.
Returns:
True if the HTML passes this rule (i.e., the issue is fixed or wasn't present).
False if the HTML fails this rule (i.e., the issue persists or was introduced).
"""
pass
class MissingAltTextRule(AccessibilityRule):
@property
def issue_key(self) -> str:
return "missing_alt_text"
def check(self, soup: BeautifulSoup, original_html_info: dict) -> bool:
# Check if images were relevant in the first place for this item based on original_html_info
# This helps decide if an absence of images now is a failure or if the check is moot.
original_had_images = " str:
return "missing_label_for" # Or "label_association" if you prefer
def check(self, soup: BeautifulSoup, original_html_info: dict) -> bool:
original_had_labels = "