Skip to content

🐛 FIX: Preserve literal alt text in HTML images - #1214

Merged
chrisjsewell merged 7 commits into
executablebooks:masterfrom
AryanRG:fix/html-image-literal-alt
Nov 9, 2026
Merged

chrisjsewell merged 7 commits into
executablebooks:masterfrom
AryanRG:fix/html-image-literal-alt

Conversation

@AryanRG

@AryanRG AryanRG commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1213.

With html_image enabled, <img src="example.png" alt="A # sign"> currently produces alt text A and a warning. Decoded quotes can drop the alt attribute entirely, and embedded newlines can truncate it. HTML attributes are being serialized into directive option text and parsed a second time.

Pass the existing allowed attributes through run_directive(..., additional_options=...) instead. Directive option validation remains active. Trim width/height values to preserve the former dimension whitespace normalization, while preserving literal alt text, including its leading/trailing spaces.

Regression tests cover block and inline images, quotes, hash signs, embedded newlines, literal alt whitespace, padded numeric/unit/percentage dimensions, and invalid dimensions. The three-file change includes an Unreleased changelog entry.

Compatibility observation: the synthesized directive block_text and resulting standard image node's rawsource now become empty instead of containing generated :key: value lines. This was observed in a local probe. Custom image directives or downstream consumers that inspect those fields may observe a difference; compatibility with all such consumers has not been established.

This addresses HTML attribute conversion in html_to_nodes.py. #1210 / #1211 address a separate Markdown image token path in renderInlineAsText.

Validation on macOS with Python 3.11.16:

  • tox -e py311-sphinx8,py311-sphinx9 -- tests/test_html tests/test_docutils.py tests/test_renderers/test_parse_directives.py -q: 136 passed on each stack (Sphinx 8.2.3 / docutils 0.21.2 and Sphinx 9.0.4 / docutils 0.22.4).
  • tox -e py311-sphinx9 -- -q: 1,265 passed, 11 skipped; 22 existing BeautifulSoup findAll deprecation warnings in the Sphinx test helpers.
  • pre-commit run --all-files: all seven configured checks passed, including Ruff and mypy.
  • git diff --check: passed.
  • Before the production fixes, the original literal-alt regression had six failing cases on Sphinx 9; the subsequent padded-dimension regression had six failing cases on each of Sphinx 8 and 9.

Tox was invoked using uv tool run --from tox --with tox-uv tox. Sphinx 8 received the focused suites, not the full suite. Other Python/platform combinations, the separate docutils-only matrix, standalone documentation/LaTeX builds, and the 11 skipped tests were not exercised locally. Hosted CI results are separate from these local results.

AI assistance was used to prepare the implementation and regression tests.

AryanRG and others added 7 commits October 17, 2026 10:57
`<img src="x.png" width>` gives the attribute the value None, and the
whitespace normalization of width/height called `.strip()` on it
(AttributeError). Skip the normalization for None: the image directive's
converter then rejects the value with a `myst.directive_option` warning,
as it did before this branch.

Tests: valueless width/height, block and inline (fail without the guard);
a valueless alt gives an empty alt, not the text "None".
Passing the attributes as directive options left the image node's
rawsource empty. For an image with no alt, Sphinx's source workaround
then sets its line to 0, so warnings such as "image file not readable"
lost their line number. The rendered tag is now the directive's block
text, which also gives an attribute-less <img src> its line back.
@chrisjsewell
chrisjsewell merged commit e0a12c3 into executablebooks:master Nov 9, 2026
24 checks passed
@chrisjsewell

Copy link
Copy Markdown
Member

Merged in e0a12c3, thanks @AryanRG! Passing the attributes as directive options, rather than re-serialising them, was the right fix. Before merging I pushed:

  • a guard for a valueless width/height (<img src="x.png" width>): the value is None, so the new .strip() raised AttributeError and aborted the build. It is a myst.directive_option warning again, with tests;
  • pins for a valueless alt (now an empty alt; master gave the text None) and class (now warns);
  • HTML images keep their source line in Sphinx warnings: the rendered <img> tag is passed as the directive's block text, which also gives a bare <img src="x.png"> its line (it had lost it on master too);
  • a merge of master, and the CHANGELOG.md hunk dropped (written at release time).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML image alt text is reparsed as directive option syntax

2 participants