From c7eb36d28655b6b08ccc4d56de639815676d5d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Furga=C5=82a?= <83299832+00200200@users.noreply.github.com> Date: Tue, 29 Sep 2026 17:54:56 +0200 Subject: [PATCH] Treat picture as a block-level HTML element HTML comments are stored as raw blocks. When picture was treated as inline, a comment inside a line-started picture split the element across paragraphs and produced invalid HTML. picture belongs with video and other media tags whose contents Markdown should not rewrite. An inline picture in mid-paragraph is unchanged. Fixes #1643. --- docs/changelog.md | 7 +++++ docs/extensions/md_in_html.md | 4 +-- markdown/util.py | 2 +- tests/test_syntax/blocks/test_html_blocks.py | 29 ++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 661161c65..1d8531a87 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -12,6 +12,13 @@ and this project adheres to the [Python Version Specification](https://packaging.python.org/en/latest/specifications/version-specifiers/). See the [Contributing Guide](contributing.md) for details. +## [unreleased] + +### Fixed + +* Treat `picture` as a block-level HTML element so comments inside it do not + split the element into invalid paragraphs (#1643). + ## [3.11.0] - 2026-09-25 ### Changed diff --git a/docs/extensions/md_in_html.md b/docs/extensions/md_in_html.md index 13a9a88b3..6181f1dc7 100644 --- a/docs/extensions/md_in_html.md +++ b/docs/extensions/md_in_html.md @@ -45,8 +45,8 @@ When the `markdown` attribute is set to `"1"`, then the parser will use the defa The following tags have the `block` behavior by default: `article`, `aside`, `blockquote`, `body`, `colgroup`, `details`, `div`, `dl`, `fieldset`, `figcaption`, `figure`, `footer`, `form`, `group`, `header`, `hgroup`, `hr`, -`iframe`, `main`, `map`, `menu`, `nav`, `noscript`, `object`, `ol`, `output`, `progress`, `section`, `table`, -`tbody`, `tfoot`, `thead`, `tr`, `ul` and `video`. +`iframe`, `main`, `map`, `menu`, `nav`, `noscript`, `object`, `ol`, `output`, `picture`, `progress`, `section`, +`table`, `tbody`, `tfoot`, `thead`, `tr`, `ul` and `video`. For example: diff --git a/markdown/util.py b/markdown/util.py index 29ffa247d..439e66dd2 100644 --- a/markdown/util.py +++ b/markdown/util.py @@ -53,7 +53,7 @@ 'p', 'pre', 'section', 'table', 'ul', # Other elements which Markdown should not be mucking up the contents of. 'canvas', 'colgroup', 'dd', 'body', 'dt', 'group', 'html', 'iframe', 'li', 'legend', - 'math', 'map', 'noscript', 'output', 'object', 'option', 'progress', 'script', + 'math', 'map', 'noscript', 'output', 'object', 'option', 'picture', 'progress', 'script', 'style', 'summary', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'tr', 'video', 'center' ] diff --git a/tests/test_syntax/blocks/test_html_blocks.py b/tests/test_syntax/blocks/test_html_blocks.py index ecc10f456..52da2a102 100644 --- a/tests/test_syntax/blocks/test_html_blocks.py +++ b/tests/test_syntax/blocks/test_html_blocks.py @@ -1004,6 +1004,35 @@ def test_raw_comment_nested(self): ) ) + def test_raw_picture_with_comment(self): + # HTML comments inside must not split the element (see #1643). + self.assertMarkdownRenders( + self.dedent( + """ + + + + Example + + """ + ), + self.dedent( + """ + + + + Example + + """ + ) + ) + + def test_inline_picture_remains_inline(self): + self.assertMarkdownRenders( + 'See Example here.', + '

See Example here.

' + ) + def test_comment_in_code_block(self): self.assertMarkdownRenders( ' ',