From 0c22d9c9ff4a0a6b3ce2f0aa6bc591b4525b4163 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Thu, 17 Aug 2023 15:40:54 +0100 Subject: [PATCH] Restore hidden pass-through calls when adding asset files --- CHANGES | 2 ++ sphinx/application.py | 9 +++++++++ sphinx/builders/html/__init__.py | 6 ++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 420e7e965c2..8af44b050a0 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,8 @@ Bugs fixed * Restored the the :py:class:`str` interface of the asset classes (``_CascadingStyleSheet`` and ``_JavaScript``), which several extensions relied upon. This will be removed in Sphinx 9. +* Restored calls to ``Builder.add_{css,js}_file()``, + which several extensions relied upon. Testing ------- diff --git a/sphinx/application.py b/sphinx/application.py index 6ae2a35a9a8..73c157dea29 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -5,6 +5,7 @@ from __future__ import annotations +import contextlib import os import pickle import sys @@ -1033,6 +1034,10 @@ def add_js_file(self, filename: str | None, priority: int = 500, kwargs['defer'] = 'defer' self.registry.add_js_file(filename, priority=priority, **kwargs) + with contextlib.suppress(AttributeError): + self.builder.add_js_file( # type: ignore[attr-defined] + filename, priority=priority, **kwargs, + ) def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> None: """Register a stylesheet to include in the HTML output. @@ -1093,6 +1098,10 @@ def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> Non """ logger.debug('[app] adding stylesheet: %r', filename) self.registry.add_css_files(filename, priority=priority, **kwargs) + with contextlib.suppress(AttributeError): + self.builder.add_css_file( # type: ignore[attr-defined] + filename, priority=priority, **kwargs, + ) def add_latex_package(self, packagename: str, options: str | None = None, after_hyperref: bool = False) -> None: diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index d26684ad353..3515edd44aa 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -320,7 +320,8 @@ def add_css_file(self, filename: str, **kwargs: Any) -> None: if '://' not in filename: filename = posixpath.join('_static', filename) - self._css_files.append(_CascadingStyleSheet(filename, **kwargs)) + if (asset := _CascadingStyleSheet(filename, **kwargs)) not in self._css_files: + self._css_files.append(asset) @property def script_files(self) -> list[_JavaScript]: @@ -348,7 +349,8 @@ def add_js_file(self, filename: str, **kwargs: Any) -> None: if filename and '://' not in filename: filename = posixpath.join('_static', filename) - self._js_files.append(_JavaScript(filename, **kwargs)) + if (asset := _JavaScript(filename, **kwargs)) not in self._js_files: + self._js_files.append(asset) @property def math_renderer_name(self) -> str | None: