Repository navigation
🐛 FIX: Preserve literal alt text in HTML images - #1214
Merged
chrisjsewell merged 7 commits intoNov 9, 2026
Merged
Conversation
`<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.
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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1213.
With
html_imageenabled,<img src="example.png" alt="A # sign">currently produces alt textAand 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_textand resulting standard image node'srawsourcenow become empty instead of containing generated:key: valuelines. 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 inrenderInlineAsText.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 BeautifulSoupfindAlldeprecation warnings in the Sphinx test helpers.pre-commit run --all-files: all seven configured checks passed, including Ruff and mypy.git diff --check: passed.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.