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 }', + '