Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down
20 changes: 20 additions & 0 deletions tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }',
'<h1 id="python-support">Python support</h1>',
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(
Expand Down