Skip to content

Commit c4e7c78

Browse files
authored
Change color when hovering over code-formatted text (#2007)
I decided to assume that #2006 was in fact a bug, so here's a fix. If it's *not* a bug, we can easily alter this PR to keep the test and discard the CSS change. also includes a small tweak to the existing "breadcrumbs-everywhere" test: ~~putting much less in the `try` block so that it might actually fail in cases where we would want it to fail.~~ *being more explicit about what we expect to sometimes fail, by moving some code out of the `try` block and into an `else` block* closes #2006
1 parent c24b4b4 commit c4e7c78

File tree

4 files changed

+120
-17
lines changed

4 files changed

+120
-17
lines changed

src/pydata_sphinx_theme/assets/styles/content/_code.scss

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ code.literal {
5959

6060
a > code {
6161
color: var(--pst-color-inline-code-links);
62+
63+
&:hover {
64+
color: var(--pst-color-link-hover);
65+
}
6266
}
6367

6468
// Minimum opacity needed for linenos to be WCAG AA conformant

tests/sites/colors/conf.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Test conf file."""
2+
3+
# -- Project information -----------------------------------------------------
4+
5+
project = "PyData Tests"
6+
copyright = "2020, Pydata community"
7+
author = "Pydata community"
8+
9+
root_doc = "index"
10+
11+
# -- General configuration ---------------------------------------------------
12+
13+
# Add any Sphinx extension module names here, as strings. They can be
14+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
15+
# ones.
16+
extensions = []
17+
html_theme = "pydata_sphinx_theme"
18+
html_copy_source = True
19+
html_sourcelink_suffix = ""

tests/sites/colors/index.rst

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Test of colors
2+
==============
3+
4+
Some text with a `text link <https://pydata.org>`__.
5+
6+
Heading 1
7+
---------
8+
9+
Some text with a :ref:`cross reference link <a-cross-reference>`
10+
11+
Heading 2
12+
~~~~~~~~~
13+
14+
Some text with ``inline code``.
15+
16+
17+
.. _a-cross-reference:
18+
19+
Heading 3
20+
`````````
21+
22+
Some text with a |code link|_.
23+
24+
25+
.. the below replacement is included to emulate what intersphinx / autodoc / numpydoc generate (links on text formatted as code), which (sadly) can't be done using nesting of standard rST markup.
26+
27+
.. |code link| replace:: ``inline code link``
28+
.. _code link: https://pydata.org

tests/test_playwright.py

+69-17
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
"""Build minimal test sites with sphinx_build_factory and test them with Playwright."""
22

33
from pathlib import Path
4+
from typing import Callable
45
from urllib.parse import urljoin
56

67
import pytest
78

9+
try:
10+
from pathlib import UnsupportedOperation # added in Py 3.13
11+
except ImportError:
12+
UnsupportedOperation = NotImplementedError
13+
814
# Using importorskip to ensure these tests are only loaded if Playwright is installed.
915
playwright = pytest.importorskip("playwright")
1016
from playwright.sync_api import Page, expect # noqa: E402
1117

12-
path_repo = Path(__file__).parents[1]
13-
path_docs_build = path_repo / "docs" / "_build" / "html"
18+
repo_path = Path(__file__).parents[1]
19+
test_sites_dir = repo_path / "docs" / "_build" / "html" / "playwright_tests"
1420

1521

1622
def _is_overflowing(element):
@@ -24,6 +30,29 @@ def _is_overflowing(element):
2430
return element.evaluate("e => e.clientWidth < e.scrollWidth", element)
2531

2632

33+
def _build_test_site(site_name: str, sphinx_build_factory: Callable) -> None:
34+
"""Helper function for building simple test sites (with no `confoverrides`)."""
35+
sphinx_build = sphinx_build_factory(site_name)
36+
sphinx_build.build()
37+
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*")
38+
return sphinx_build.outdir
39+
40+
41+
def _check_test_site(site_name: str, site_path: Path, test_func: Callable):
42+
"""Make the built test site available to Playwright, then run `test_func` on it."""
43+
test_sites_dir.mkdir(exist_ok=True)
44+
symlink_path = test_sites_dir / site_name
45+
try:
46+
symlink_path.symlink_to(site_path, True)
47+
except UnsupportedOperation:
48+
pytest.xfail("filesystem doesn't support symlinking")
49+
else:
50+
test_func()
51+
finally:
52+
symlink_path.unlink()
53+
test_sites_dir.rmdir()
54+
55+
2756
def test_version_switcher_highlighting(page: Page, url_base: str) -> None:
2857
"""In sidebar and topbar - version switcher should apply highlight color to currently selected version."""
2958
page.goto(url=url_base)
@@ -60,28 +89,51 @@ def test_breadcrumb_expansion(page: Page, url_base: str) -> None:
6089

6190

6291
def test_breadcrumbs_everywhere(
63-
sphinx_build_factory, page: Page, url_base: str
92+
sphinx_build_factory: Callable, page: Page, url_base: str
6493
) -> None:
65-
"""Test building the base html template and config."""
66-
sphinx_build = sphinx_build_factory("breadcrumbs")
94+
"""Test breadcrumbs truncate properly when placed in various parts of the layout."""
95+
site_name = "breadcrumbs"
96+
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory)
6797

68-
# Basic build with defaults
69-
sphinx_build.build()
70-
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*")
71-
symlink_path = path_docs_build / "playwright_tests" / "breadcrumbs"
72-
symlink_path.parent.mkdir(exist_ok=True)
73-
try:
74-
symlink_path.symlink_to(sphinx_build.outdir, True)
98+
def check_breadcrumb_truncation():
7599
page.goto(
76-
urljoin(url_base, "playwright_tests/breadcrumbs/hansel/gretel/house.html")
100+
urljoin(url_base, f"playwright_tests/{site_name}/hansel/gretel/house.html")
77101
)
78102
# sidebar should overflow
79103
text = "In the oven with my sister, so hot right now. Soooo. Hotttt."
80104
el = page.locator("#main-content").get_by_text(text).last
81105
assert _is_overflowing(el)
82-
# footer containers will never trigger ellipsis overflow because... their min-width is content? TODO
106+
# footer containers never trigger ellipsis overflow because min-width is content
83107
el = page.locator(".footer-items__center > .footer-item")
84108
assert not _is_overflowing(el)
85-
finally:
86-
symlink_path.unlink()
87-
symlink_path.parent.rmdir()
109+
110+
_check_test_site(site_name, site_path, check_breadcrumb_truncation)
111+
112+
113+
def test_colors(sphinx_build_factory: Callable, page: Page, url_base: str) -> None:
114+
"""Test that things get colored the way we expect them to.
115+
116+
Note: this is not comprehensive! Please feel free to add to this test by editing
117+
`../sites/colors/index.rst` and adding more `expect` statements below.
118+
"""
119+
site_name = "colors"
120+
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory)
121+
122+
def check_colors():
123+
page.goto(urljoin(url_base, f"playwright_tests/{site_name}/index.html"))
124+
primary_color = "rgb(10, 125, 145)"
125+
hover_color = "rgb(128, 69, 229)"
126+
spans = {
127+
"text link": primary_color,
128+
"cross reference link": primary_color,
129+
"inline code": "rgb(145, 37, 131)",
130+
"code link": "rgb(8, 93, 108)", # teal-600, AKA #085d6c
131+
}
132+
for text, color in spans.items():
133+
el = page.get_by_text(text).first
134+
expect(el).to_have_css("color", color)
135+
if "link" in text:
136+
el.hover()
137+
expect(el).to_have_css("color", hover_color)
138+
139+
_check_test_site(site_name, site_path, check_colors)

0 commit comments

Comments
 (0)