Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change color when hovering over code-formatted text #2007

Merged
merged 7 commits into from
Oct 16, 2024
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
4 changes: 4 additions & 0 deletions src/pydata_sphinx_theme/assets/styles/content/_code.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ code.literal {

a > code {
color: var(--pst-color-inline-code-links);

&:hover {
color: var(--pst-color-link-hover);
}
}

// Minimum opacity needed for linenos to be WCAG AA conformant
Expand Down
19 changes: 19 additions & 0 deletions tests/sites/colors/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Test conf file."""

# -- Project information -----------------------------------------------------

project = "PyData Tests"
copyright = "2020, Pydata community"
author = "Pydata community"

root_doc = "index"

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = []
html_theme = "pydata_sphinx_theme"
html_copy_source = True
html_sourcelink_suffix = ""
28 changes: 28 additions & 0 deletions tests/sites/colors/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Test of colors
==============

Some text with a `text link <https://pydata.org>`__.

Heading 1
---------

Some text with a :ref:`cross reference link <a-cross-reference>`

Heading 2
~~~~~~~~~

Some text with ``inline code``.


.. _a-cross-reference:

Heading 3
`````````

Some text with a |code link|_.


.. 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.

.. |code link| replace:: ``inline code link``
.. _code link: https://pydata.org
86 changes: 69 additions & 17 deletions tests/test_playwright.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""Build minimal test sites with sphinx_build_factory and test them with Playwright."""

from pathlib import Path
from typing import Callable
from urllib.parse import urljoin

import pytest

try:
from pathlib import UnsupportedOperation # added in Py 3.13
except ImportError:
UnsupportedOperation = NotImplementedError

# Using importorskip to ensure these tests are only loaded if Playwright is installed.
playwright = pytest.importorskip("playwright")
from playwright.sync_api import Page, expect # noqa: E402

path_repo = Path(__file__).parents[1]
path_docs_build = path_repo / "docs" / "_build" / "html"
repo_path = Path(__file__).parents[1]
test_sites_dir = repo_path / "docs" / "_build" / "html" / "playwright_tests"


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


def _build_test_site(site_name: str, sphinx_build_factory: Callable) -> None:
"""Helper function for building simple test sites (with no `confoverrides`)."""
sphinx_build = sphinx_build_factory(site_name)
sphinx_build.build()
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*")
return sphinx_build.outdir


def _check_test_site(site_name: str, site_path: Path, test_func: Callable):
"""Make the built test site available to Playwright, then run `test_func` on it."""
test_sites_dir.mkdir(exist_ok=True)
symlink_path = test_sites_dir / site_name
try:
symlink_path.symlink_to(site_path, True)
except UnsupportedOperation:
pytest.xfail("filesystem doesn't support symlinking")
else:
test_func()
finally:
symlink_path.unlink()
test_sites_dir.rmdir()


def test_version_switcher_highlighting(page: Page, url_base: str) -> None:
"""In sidebar and topbar - version switcher should apply highlight color to currently selected version."""
page.goto(url=url_base)
Expand Down Expand Up @@ -60,28 +89,51 @@ def test_breadcrumb_expansion(page: Page, url_base: str) -> None:


def test_breadcrumbs_everywhere(
sphinx_build_factory, page: Page, url_base: str
sphinx_build_factory: Callable, page: Page, url_base: str
) -> None:
"""Test building the base html template and config."""
sphinx_build = sphinx_build_factory("breadcrumbs")
"""Test breadcrumbs truncate properly when placed in various parts of the layout."""
site_name = "breadcrumbs"
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory)

# Basic build with defaults
sphinx_build.build()
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*")
symlink_path = path_docs_build / "playwright_tests" / "breadcrumbs"
symlink_path.parent.mkdir(exist_ok=True)
try:
symlink_path.symlink_to(sphinx_build.outdir, True)
def check_breadcrumb_truncation():
page.goto(
urljoin(url_base, "playwright_tests/breadcrumbs/hansel/gretel/house.html")
urljoin(url_base, f"playwright_tests/{site_name}/hansel/gretel/house.html")
)
# sidebar should overflow
text = "In the oven with my sister, so hot right now. Soooo. Hotttt."
el = page.locator("#main-content").get_by_text(text).last
assert _is_overflowing(el)
# footer containers will never trigger ellipsis overflow because... their min-width is content? TODO
# footer containers never trigger ellipsis overflow because min-width is content
el = page.locator(".footer-items__center > .footer-item")
assert not _is_overflowing(el)
finally:
symlink_path.unlink()
symlink_path.parent.rmdir()

_check_test_site(site_name, site_path, check_breadcrumb_truncation)


def test_colors(sphinx_build_factory: Callable, page: Page, url_base: str) -> None:
"""Test that things get colored the way we expect them to.

Comment on lines +113 to +115
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem to be the "light theme" colours; should we be testing for both light and dark?

My gut tells me yes WDYT @drammock @gabalafou ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that as long as we know that mode switching works, tests like this one (i.e. "did the CSS selector do what we intended" tests) only need to test one mode.

The exception would be any a11y test that checks for color contrast --- obv. we should run that test in both modes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are running those a11y tests on both modes so then we should be covered

Note: this is not comprehensive! Please feel free to add to this test by editing
`../sites/colors/index.rst` and adding more `expect` statements below.
"""
site_name = "colors"
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory)

def check_colors():
page.goto(urljoin(url_base, f"playwright_tests/{site_name}/index.html"))
primary_color = "rgb(10, 125, 145)"
hover_color = "rgb(128, 69, 229)"
spans = {
"text link": primary_color,
"cross reference link": primary_color,
"inline code": "rgb(145, 37, 131)",
"code link": "rgb(8, 93, 108)", # teal-600, AKA #085d6c
}
for text, color in spans.items():
el = page.get_by_text(text).first
expect(el).to_have_css("color", color)
if "link" in text:
el.hover()
expect(el).to_have_css("color", hover_color)

_check_test_site(site_name, site_path, check_colors)
Loading