Save these synthetic lines as sample.html: <img src="rule.png" alt="">, <img src="steps.png">, and <img src="door.png" alt="Door 2 on the east side">. A read-only HTMLParser pass should distinguish explicit empty, missing, and present text. You can print each image’s source line using getpos() inside handle_starttag. Preserve duplicate attributes instead of converting attrs straight into a dict.
Next, present an AI with the relevant paragraph and the approved diagram, and ask for one candidate rather than rewriting the HTML. For steps.png, verify that its wording lets a reader name the next step. For rule.png, confirm it really is decoration. Add an image-only button to the fixture as a deliberate manual case, then check its accessible name in the real interface. The parser does not see JavaScript-inserted DOM or prove accessibility.
from html.parser import HTMLParser
class Inventory(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag == "img":
values = [v for k, v in attrs if k == "alt"]
print(self.getpos()[0], values)
p = Inventory()
p.feed('<img alt="">\n<img>\n<img alt="East door">')
p.close()
# Line 1: ['']; line 2: []; line 3: ['East door']
Method reference — W3C WAI, An alt Decision Tree
AI assisted in drafting this article. The example is fictional.
Top comments (0)