From 950b9a0f7445adef3dcb1740daed8a596e71b01e Mon Sep 17 00:00:00 2001 From: lidongze1002 Date: Tue, 17 Nov 2026 16:22:47 +0800 Subject: [PATCH] Fix TypeError for a valueless data-toc-label The attr_list extension writes a bare attribute with a value of None, and toc read data-toc-label without allowing for that, so a heading written as "# Heading {: data-toc-label }" raised TypeError: expected string or bytes-like object, got 'NoneType' from unescape. A valueless label is no label at all: the heading text is used and the attribute is dropped, which is how a valueless id has been handled since #1705. Closes #1712. --- docs/changelog.md | 5 +++++ markdown/extensions/toc.py | 11 ++++++++--- tests/test_syntax/extensions/test_toc.py | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 043bfa2d7..ecaf078ca 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -20,6 +20,11 @@ See the [Contributing Guide](contributing.md) for details. attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{id}`) writes it: like a heading with no `id` at all, it now gets a generated slug (#1705). +* The `toc` extension no longer raises `TypeError` for a heading whose + `data-toc-label` attribute is present but valueless, as the `attr_list` + extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a + heading with no label at all, the heading text is used and the attribute is + dropped (#1712). * Fix for complex cases in new emphasis handling where text could be unexpectedly dropped (#1694). * Fix rule of 3 handling in new emphasis handling where an opener skipped by the rule could still be paired diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 261f2af03..5e17dd7c8 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -408,9 +408,14 @@ def run(self, doc: etree.Element) -> None: data_toc_label = '' if 'data-toc-label' in el.attrib: - data_toc_label = run_postprocessors(unescape(el.attrib['data-toc-label']), self.md) - # Overwrite name with sanitized value of `data-toc-label`. - name = escape_cdata(strip_tags(data_toc_label)) + # The `attr_list` extension writes a bare attribute with a + # value of None. A valueless label is no label at all, so + # the heading text is used and the attribute is dropped, + # as a valueless id is treated above. + if el.attrib['data-toc-label'] is not None: + data_toc_label = run_postprocessors(unescape(el.attrib['data-toc-label']), self.md) + # Overwrite name with sanitized value of `data-toc-label`. + name = escape_cdata(strip_tags(data_toc_label)) # Remove the data-toc-label attribute as it is no longer needed del el.attrib['data-toc-label'] diff --git a/tests/test_syntax/extensions/test_toc.py b/tests/test_syntax/extensions/test_toc.py index 60fa87dad..e1479f8e8 100644 --- a/tests/test_syntax/extensions/test_toc.py +++ b/tests/test_syntax/extensions/test_toc.py @@ -203,6 +203,26 @@ def testCustomLabelAttributeWithAngleBracket(self): extensions=[TocExtension(), 'attr_list'] ) + def testValuelessLabelAttribute(self): + # `attr_list` stores a bare attribute as None. A valueless + # `data-toc-label` is no label at all, so the heading text is used and + # the attribute is dropped instead of the render raising. + self.assertMarkdownRenders( + '# Python support {: data-toc-label }', + '

Python support

', + expected_attrs={ + 'toc_tokens': [{ + 'level': 1, + 'id': 'python-support', + 'name': 'Python support', + 'html': 'Python support', + 'data-toc-label': '', + 'children': [] + }] + }, + extensions=[TocExtension(), 'attr_list'] + ) + def testUniqueIds(self): self.assertMarkdownRenders( self.dedent(