From 7ae92e23aa789235599199820e33228559f05563 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Mon, 9 Nov 2026 18:50:02 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=AA=20TEST:=20count=20only=20the?= =?UTF-8?q?=20badge=20anchors=20in=20the=20bdg-ref=20tooltip=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `test_ref_badge_tooltip_survives_resolution` counted every `href="#my-target"` in the page. With MyST-Parser master (heading-anchor rework, executablebooks/MyST-Parser#1158) the explicit `(my-target)=` becomes the section's primary id, so the theme's headerlink and sidebar TOC also link to `#my-target` and the count is 4, not 2. Count only the anchors carrying the `sd-badge` class, which is what the assertion is about; it holds on MyST-Parser 5.1.0 and on master. --- tests/test_badge_tooltips.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/test_badge_tooltips.py b/tests/test_badge_tooltips.py index 57d505a7..7e2bb73d 100644 --- a/tests/test_badge_tooltips.py +++ b/tests/test_badge_tooltips.py @@ -22,6 +22,8 @@ reStructuredText, and the MyST variants are guarded by ``MYST_PARAM``. """ +import re + from docutils import nodes import pytest from sphinx import addnodes @@ -349,8 +351,15 @@ def test_ref_badge_tooltip_survives_resolution(fmt, sphinx_builder): builder = _build(sphinx_builder, fmt, REF["rst"], REF["myst"]) html = (builder.out_path / "index.html").read_text(encoding="utf8") - # both badges resolve to the target anchor - assert html.count('href="#my-target"') == 2 + # both badges resolve to the target anchor; only the badge anchors are + # counted, since the theme's own links to the section (its headerlink, + # the sidebar TOC) depend on which id the parser makes the section's + # primary one, and MyST-Parser's heading-anchor rework + # (executablebooks/MyST-Parser#1158) makes it the explicit target + badge_anchors = re.findall( + r']', html + ) + assert len(badge_anchors) == 2 # the tooltipped one carries the title; it survived resolution assert ( ' 1: + node["ids"] = sorted(node["ids"]) + return doctree + + def write_assets(src_path: Path): """Write additional assets to the src directory.""" src_path.joinpath("snippet.py").write_text("a = 1") @@ -50,7 +66,7 @@ def test_snippets_rst( builder.src_path.joinpath("index.rst").write_text(content, encoding="utf8") write_assets(builder.src_path) builder.build() - doctree = builder.get_doctree("index", post_transforms=False) + doctree = sort_ids(builder.get_doctree("index", post_transforms=False)) doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 file_regression.check( normalize_doctree_xml(doctree.pformat()), @@ -78,7 +94,7 @@ def test_snippets_myst( builder.src_path.joinpath("index.md").write_text(content, encoding="utf8") write_assets(builder.src_path) builder.build() - doctree = builder.get_doctree("index", post_transforms=False) + doctree = sort_ids(builder.get_doctree("index", post_transforms=False)) doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 file_regression.check( normalize_doctree_xml(doctree.pformat()), @@ -105,7 +121,7 @@ def test_snippets_rst_post( builder.src_path.joinpath("index.rst").write_text(content, encoding="utf8") write_assets(builder.src_path) builder.build() - doctree = builder.get_doctree("index", post_transforms=True) + doctree = sort_ids(builder.get_doctree("index", post_transforms=True)) doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 file_regression.check( normalize_doctree_xml(doctree.pformat()), @@ -133,7 +149,7 @@ def test_snippets_myst_post( builder.src_path.joinpath("index.md").write_text(content, encoding="utf8") write_assets(builder.src_path) builder.build() - doctree = builder.get_doctree("index", post_transforms=True) + doctree = sort_ids(builder.get_doctree("index", post_transforms=True)) doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 file_regression.check( normalize_doctree_xml(doctree.pformat()), diff --git a/tests/test_snippets/snippet_post_card-link.xml b/tests/test_snippets/snippet_post_card-link.xml index 3d74e127..59ec5e47 100644 --- a/tests/test_snippets/snippet_post_card-link.xml +++ b/tests/test_snippets/snippet_post_card-link.xml @@ -3,7 +3,7 @@ Heading <target refid="cards-clickable"> - <section ids="clickable-cards cards-clickable" names="clickable\ cards cards-clickable"> + <section ids="cards-clickable clickable-cards" names="clickable\ cards cards-clickable"> <title> Clickable cards <paragraph> diff --git a/tests/test_snippets/snippet_pre_card-link.xml b/tests/test_snippets/snippet_pre_card-link.xml index 86461813..2f7b35bf 100644 --- a/tests/test_snippets/snippet_pre_card-link.xml +++ b/tests/test_snippets/snippet_pre_card-link.xml @@ -3,7 +3,7 @@ <title> Heading <target refid="cards-clickable"> - <section ids="clickable-cards cards-clickable" names="clickable\ cards cards-clickable"> + <section ids="cards-clickable clickable-cards" names="clickable\ cards cards-clickable"> <title> Clickable cards <paragraph>