Skip to content

fix(html): use srcset when an img has no usable src - #2505

Open
Jia-ben00 wants to merge 1 commit into
microsoft:mainfrom
Jia-ben00:fix-img-srcset
Open

Jia-ben00 wants to merge 1 commit into
microsoft:mainfrom
Jia-ben00:fix-img-srcset

Conversation

@Jia-ben00

Copy link
Copy Markdown

_CustomMarkdownify.convert_img reads src and data-src but not srcset, so a responsive image that carries its URL only in srcset loses it:

<img srcset="photo-1x.jpg 1x, photo-2x.jpg 2x" alt="A photo">

renders as ![A photo]() - an empty destination, with no indication that anything was dropped. The existing data-src fallback already covers the same situation for lazy-loaded images, where a placeholder data URI would otherwise win over the real URL; srcset and data-srcset are the responsive equivalents of that case, and neither was consulted.

This adds a srcset parser and extends the fallback chain to src (when usable) -> data-src -> data-srcset -> srcset. Candidates are split on a comma followed by whitespace, so commas inside a URL query string (?crop=1,2) are not mistaken for separators, and the descriptor (2x, 800w) is stripped from the selected candidate. Only the first candidate is used, since markitdown emits a single destination and the first candidate is the one a browser picks at the default density. When keep_data_uris is set, an embedded data URI in src is still preferred, as before.

Adds tests/test_img_srcset.py, covering the fallback order, the placeholder case, descriptor and whitespace handling, and the comma-in-URL case.

convert_img reads src and data-src but not srcset, so a responsive image
that carries its URL only in srcset loses it:

    <img srcset="photo-1x.jpg 1x, photo-2x.jpg 2x" alt="A photo">

renders as ![A photo]() - an empty destination with no indication that
anything was dropped. The existing data-src fallback already covers the
same situation for lazy-loaded images, where a placeholder data URI would
otherwise win over the real URL; srcset and data-srcset are the
responsive equivalents of that case and were not consulted.

Candidates are split on a comma followed by whitespace so that commas
inside a URL query string (?crop=1,2) are not mistaken for separators,
and the descriptor (2x, 800w) is stripped from the selected candidate.
Only the first candidate is used, since markitdown emits a single
destination and the first candidate is the one chosen at the default
density. When keep_data_uris is set, an embedded data URI in src is still
preferred, as before.
@Jia-ben00

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

1 participant