From abbac9918ff46853d2ea1f6ee9f0be9572252194 Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Tue, 24 Jan 2023 15:44:19 +0100 Subject: [PATCH 1/5] Add configuration option `jquery_cors_enable` and bump to 4.0.0 --- CHANGES.rst | 9 +++++++++ README.rst | 9 +++++++++ sphinxcontrib/jquery/__init__.py | 22 ++++++++++++++++++---- tests/test_jquery_installed.py | 18 +++++++++++++++++- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 79d6d3e..850fdb9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,12 @@ +Release 4.0.0 (24/01/2023) +========================== + +* Enforcing CORS check breaks documentation builds displayed directly from local filesystem (``file:///``). + Make CORS checks optional with setting ``jquery_cors_enable``, default is ``False``. + See `sphinx_rtd_theme#1420`_. + +.. _sphinx_rtd_theme#1420: https://github.com/readthedocs/sphinx_rtd_theme/issues/1420 + Release 3.0.0 (03/11/2022) ========================== diff --git a/README.rst b/README.rst index 7e955b6..ddbee61 100644 --- a/README.rst +++ b/README.rst @@ -19,3 +19,12 @@ To use it, add ``sphinxcontrib.jquery`` as a Sphinx extension: "sphinxcontrib.jquery", ] ... + +Configuration +------------- + +``` +# Enable CORS integrity="" on included JS files +# Default: False +jquery_cors_enable = True +``` diff --git a/sphinxcontrib/jquery/__init__.py b/sphinxcontrib/jquery/__init__.py index f38224f..578b4e5 100644 --- a/sphinxcontrib/jquery/__init__.py +++ b/sphinxcontrib/jquery/__init__.py @@ -3,8 +3,8 @@ import sphinx -__version__ = "3.0.0" -version_info = (3, 0, 0) +__version__ = "4.0.0" +version_info = (4, 0, 0) _ROOT_DIR = path.abspath(path.dirname(__file__)) _FILES = ( @@ -19,18 +19,32 @@ ) -def setup(app): +def add_js_files(app, config): jquery_installed = getattr(app, "_sphinxcontrib_jquery_installed", False) + if sphinx.version_info[:2] >= (6, 0) and not jquery_installed: makedirs(path.join(app.outdir, '_static'), exist_ok=True) for (filename, integrity) in _FILES: - app.add_js_file(filename, integrity=integrity, priority=100) + # The default is not not enable CORS because it does not trigger the hash + # check but instead blocks the request when viewing documentation locally + # through the file:// "protocol". + if config.jquery_cors_enable: + app.add_js_file(filename, integrity=integrity, priority=100) + else: + app.add_js_file(filename, priority=100) shutil.copyfile( path.join(_ROOT_DIR, filename), path.join(app.outdir, '_static', filename) ) app._sphinxcontrib_jquery_installed = True + +def setup(app): + # Configuration value for enabling CORS checks + app.add_config_value("jquery_cors_enable", default=False, rebuild="html", types=[bool]) + + app.connect('config-inited', add_js_files) + return { "parallel_read_safe": True, "parallel_write_safe": True, diff --git a/tests/test_jquery_installed.py b/tests/test_jquery_installed.py index 92e9fdc..9d7eff4 100644 --- a/tests/test_jquery_installed.py +++ b/tests/test_jquery_installed.py @@ -32,7 +32,7 @@ def inner(**kwargs): @pytest.mark.skipif(sphinx.version_info[:2] < (6, 0), reason="Requires Sphinx 6.0 or greater") def test_jquery_installed_sphinx_ge_60(blank_app): - out_dir = blank_app(confoverrides={"extensions": ["sphinxcontrib.jquery"]}) + out_dir = blank_app(confoverrides={"extensions": ["sphinxcontrib.jquery"], "jquery_cors_enable": True}) text = out_dir.joinpath("index.html").read_text(encoding="utf-8") assert ('') in text + assert ('') in text + + static_dir = out_dir / '_static' + assert static_dir.joinpath('jquery.js').is_file() + assert static_dir.joinpath('_sphinx_javascript_frameworks_compat.js').is_file() + + @pytest.mark.skipif(sphinx.version_info[:2] >= (6, 0), reason="Requires Sphinx older than 6.0") def test_jquery_installed_sphinx_lt_60(blank_app): From 339baa374a28c4a84c76812f186807c3ba1277bf Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Mon, 30 Jan 2023 11:09:31 +0100 Subject: [PATCH 2/5] Rename CORS to SRI since it's SRI-specific --- CHANGES.rst | 4 ++-- README.rst | 7 +++++-- sphinxcontrib/jquery/__init__.py | 8 ++++---- tests/test_jquery_installed.py | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 850fdb9..96e68ea 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,8 +1,8 @@ Release 4.0.0 (24/01/2023) ========================== -* Enforcing CORS check breaks documentation builds displayed directly from local filesystem (``file:///``). - Make CORS checks optional with setting ``jquery_cors_enable``, default is ``False``. +* Enforcing SRI check broke documentation builds displayed directly from local filesystem (``file:///``). + Make SRI checks optional with setting ``jquery_sri_enable``, default is ``False``. See `sphinx_rtd_theme#1420`_. .. _sphinx_rtd_theme#1420: https://github.com/readthedocs/sphinx_rtd_theme/issues/1420 diff --git a/README.rst b/README.rst index ddbee61..cabf5b5 100644 --- a/README.rst +++ b/README.rst @@ -24,7 +24,10 @@ Configuration ------------- ``` -# Enable CORS integrity="" on included JS files +# Enable Subresource Integrity (SRI) such that +#