diff --git a/docs/changelog.md b/docs/changelog.md index 043bfa2d7..828d283cb 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -16,6 +16,10 @@ See the [Contributing Guide](contributing.md) for details. ### Fixed +* 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 `data-toc-label`, its text is used as the TOC name (#1712). * The `toc` extension no longer raises `TypeError` for a heading whose `id` attribute is present but valueless, as the `attr_list` extension's bare-attribute syntax (`{id}`) writes it: like a heading with no `id` at diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 261f2af03..57960ad89 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -408,11 +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)) # Remove the data-toc-label attribute as it is no longer needed - del el.attrib['data-toc-label'] + label = el.attrib.pop('data-toc-label') + # The `attr_list` extension writes bare attributes with a value of + # None. A valueless label is treated like a missing one. + if label is not None: + data_toc_label = run_postprocessors(unescape(label), self.md) + # Overwrite name with sanitized value of `data-toc-label`. + name = escape_cdata(strip_tags(data_toc_label)) if int(el.tag[-1]) >= self.toc_top and int(el.tag[-1]) <= self.toc_bottom: toc_tokens.append({ diff --git a/tests/test_syntax/extensions/test_toc.py b/tests/test_syntax/extensions/test_toc.py index 60fa87dad..7072b4bcc 100644 --- a/tests/test_syntax/extensions/test_toc.py +++ b/tests/test_syntax/extensions/test_toc.py @@ -1578,7 +1578,7 @@ def testDontStripHTMLEntities(self): class TestTOCWithAttrList(TestCase): - """The `toc` extension together with `attr_list` (#1705).""" + """The `toc` extension together with `attr_list` (#1705, #1712).""" maxDiff = None default_kwargs = { @@ -1611,3 +1611,38 @@ def testBareIdAttributeWithTOCMarker(self): '\n' '

Heading One

' ) + + def testBareDataTocLabelAttribute(self): + # A valueless `data-toc-label` is treated like a missing one: the + # heading text is used as the TOC name and the attribute is removed. + self.assertMarkdownRenders( + '# Hello {: data-toc-label }', + '

Hello

', + expected_attrs={ + 'toc_tokens': [{ + 'level': 1, + 'id': 'hello', + 'name': 'Hello', + 'html': 'Hello', + 'data-toc-label': '', + 'children': [] + }] + } + ) + + def testBareDataTocLabelAttributeWithTOCMarker(self): + self.assertMarkdownRenders( + self.dedent( + ''' + [TOC] + + # Heading One {: data-toc-label } + ''' + ), + '
\n' + '\n' # noqa + '
\n' + '

Heading One

' + )