diff --git a/CHANGELOG.md b/CHANGELOG.md index c411d126..b11c54d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## Unreleased + +### 🐛 Fixed + +* Respect `xhtmlOut` when rendering task-list checkboxes, so the default + `gfm-like2` output uses self-closing input tags. + ## 4.3.0 - 2026-11-10 This release fixes two quadratic-time parsing paths that let crafted input stall rendering for minutes (security), diff --git a/docs/using.md b/docs/using.md index fd242313..f4a7b8d1 100644 --- a/docs/using.md +++ b/docs/using.md @@ -62,6 +62,10 @@ You can define this configuration *via* directly supplying a dictionary or a pre GitHub-style alerts (`> [!NOTE]`), and single-tilde strikethrough (`~text~`). **Important**, to use this configuration you must have `linkify-it-py` installed. +The `xhtmlOut` option controls self-closing tags, including task-list checkbox inputs. +It defaults to `True` in `commonmark`, `gfm-like`, and `gfm-like2`. +Set it to `False` to render HTML-style void tags such as `` instead of ``. + ```{jupyter-execute} from markdown_it.presets import zero zero.make() diff --git a/markdown_it/renderer.py b/markdown_it/renderer.py index 6ccdee9b..3c46ff75 100644 --- a/markdown_it/renderer.py +++ b/markdown_it/renderer.py @@ -240,9 +240,10 @@ def list_item_open( disabled_attr = ( "" if options.get("tasklists_editable", False) else ' disabled=""' ) + checkbox_end = " />" if options.xhtmlOut else ">" result += ( ' ' + f'{disabled_attr} type="checkbox"{checked_attr}{checkbox_end} ' ) return result diff --git a/tests/test_api/test_main.py b/tests/test_api/test_main.py index 74c7b0f8..6a0dd9f7 100644 --- a/tests/test_api/test_main.py +++ b/tests/test_api/test_main.py @@ -1,6 +1,7 @@ from collections.abc import Iterator from importlib.util import find_spec from typing import TYPE_CHECKING +from xml.etree import ElementTree as ET import pytest @@ -111,7 +112,7 @@ def test_gfm_like2_tasklists_editable(): md_default = MarkdownIt("gfm-like2", GFM_LIKE2_OPTIONS) assert md_default.options["tasklists_editable"] is False assert ( - '' + '' in md_default.render("- [x] done") ) @@ -120,11 +121,40 @@ def test_gfm_like2_tasklists_editable(): ) assert md_editable.options["tasklists_editable"] is True assert ( - '' + '' in md_editable.render("- [x] done") ) +@pytest.mark.parametrize("xhtml_out", [False, True]) +@pytest.mark.parametrize("editable", [False, True]) +@pytest.mark.parametrize("marker", [" ", "x", "X"]) +def test_gfm_like2_tasklist_xhtml(xhtml_out, editable, marker): + """Task checkboxes must follow the output mode and keep their attributes.""" + md = MarkdownIt( + "gfm-like2", + { + **GFM_LIKE2_OPTIONS, + "xhtmlOut": xhtml_out, + "tasklists_editable": editable, + }, + ) + result = md.render(f"- [{marker}] task") + start = result.index("", start) + 1 + checkbox_html = result[start:end] + assert checkbox_html.endswith(" />" if xhtml_out else '">') + if xhtml_out: + # An unclosed HTML input element makes the surrounding XML malformed. + root = ET.fromstring(result) + checkbox = root.find("li/input") + assert checkbox is not None + assert checkbox.attrib["type"] == "checkbox" + assert ("checked" in checkbox.attrib) is (marker != " ") + assert ("disabled" in checkbox.attrib) is (not editable) + assert checkbox.tail == " task" + + def test_gfm_like2_alert_token_map(): md = MarkdownIt("gfm-like2", GFM_LIKE2_OPTIONS) tokens = md.parse("> [!NOTE]\n> body") diff --git a/tests/test_port/fixtures/tasklists.md b/tests/test_port/fixtures/tasklists.md index 7348d328..87c01938 100644 --- a/tests/test_port/fixtures/tasklists.md +++ b/tests/test_port/fixtures/tasklists.md @@ -5,9 +5,9 @@ Bullet unchecked and checked - [x] checked item 3 . . @@ -17,8 +17,8 @@ Uppercase X - [x] checked lowercase . . @@ -28,8 +28,8 @@ Ordered list with tasks 2. [ ] unchecked ordered 2 .
    -
  1. checked ordered 1
  2. -
  3. unchecked ordered 2
  4. +
  5. checked ordered 1
  6. +
  7. unchecked ordered 2
. @@ -55,9 +55,9 @@ Mixed task and non-task items . . @@ -81,8 +81,8 @@ Nested list with tasks @@ -95,9 +95,9 @@ Task with inline formatting - [x] `code` task . . @@ -106,7 +106,7 @@ Single task item - [x] only item . . @@ -116,8 +116,8 @@ Different bullet markers + [ ] plus unchecked . . @@ -127,8 +127,8 @@ Ordered list starting from non-1 4. [ ] task four .
    -
  1. task three
  2. -
  3. task four
  4. +
  5. task three
  6. +
  7. task four
. @@ -223,7 +223,7 @@ Indented code item between task items
[x] literal
 
-
  • b
  • +
  • b
  • . @@ -232,7 +232,7 @@ Four spaces after the marker is still a task - [x] task . . @@ -241,6 +241,6 @@ A tab after the marker is still a task - [x] task . .