Skip to content
Merged
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
13 changes: 11 additions & 2 deletions tests/test_badge_tooltips.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'<a class="[^"]*\bsd-badge\b[^"]*" href="#my-target"[ >]', html
)
assert len(badge_anchors) == 2
# the tooltipped one carries the title; it survived resolution
assert (
'<a class="sd-sphinx-override sd-badge sd-bg-primary sd-bg-text-primary'
Expand Down
24 changes: 20 additions & 4 deletions tests/test_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Callable
from pathlib import Path

from docutils import nodes
import pytest

from .conftest import SphinxBuilder
Expand All @@ -25,6 +26,21 @@
]


def sort_ids(doctree: nodes.document) -> nodes.document:
"""Sort the ``ids`` of every element, in place.

The order of an element's ids is a parser detail, not sphinx-design output:
for a section with an explicit target, docutils lists the title slug first,
while MyST-Parser's heading-anchor rework (executablebooks/MyST-Parser#1158)
lists the explicit target first. Sorting keeps the RST and MyST snippets
comparing against the same fixture, and still checks the full set of ids.
"""
for node in doctree.findall(nodes.Element):
if len(node["ids"]) > 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")
Expand All @@ -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()),
Expand Down Expand Up @@ -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()),
Expand All @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_snippets/snippet_post_card-link.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_snippets/snippet_pre_card-link.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Loading