Skip to content

HTML image alt text is reparsed as directive option syntax #1213

Open

Description

@AryanRG

What version of myst-parser are you using?

5.1.0, reproduced from current master at 723cffcf84213f0cb58695b27eec9ad72052b53a.

What version dependencies are you using?

Python 3.11.16 on macOS. The regression was exercised with Sphinx 9.0.4 / docutils 0.22.4 and Sphinx 8.2.3 / docutils 0.21.2; markdown-it-py 4.2.0 and mdit-py-plugins 0.6.1.

Describe the bug

With html_image enabled, decoded HTML image attributes are serialized into directive option text and parsed again. Valid literal alt text can therefore be truncated or dropped and produce warnings:

  • A # sign becomes A, with a directive-comments warning.
  • "quoted" text loses its alt attribute, with an invalid-options-format warning.
  • First followed by a newline and second becomes First, with a forbidden-content warning.

This loses accessibility text and can fail warning-strict builds.

To reproduce

from io import StringIO

from docutils import nodes
from docutils.core import publish_doctree

from myst_parser.parsers.docutils_ import Parser

for alt in ("ordinary text", "A # sign", ""quoted" text", "First\nsecond"):
    warnings = StringIO()
    document = publish_doctree(
        f'<img src="example.png" alt="{alt}">\n',
        parser=Parser(),
        settings_overrides={
            "myst_enable_extensions": ["html_image"],
            "warning_stream": warnings,
        },
    )
    image = next(document.findall(nodes.image))
    print(repr(alt), "->", repr(image.get("alt")), repr(warnings.getvalue()))

Expected behavior

Preserve the decoded alt value exactly, without interpreting it as directive option syntax. The same problem occurs for inline HTML images.

This differs from #1210 and #1211, which concern Markdown image text_special tokens in renderInlineAsText. This reproduction starts with an HTML <img> tag and fails during HTML-to-directive conversion.

This report and the accompanying patch were prepared with AI assistance.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions