From 778013f91a650f585bd7bf346d781d5bd9392e46 Mon Sep 17 00:00:00 2001 From: James Addison <55152140+jayaddison@users.noreply.github.com> Date: Wed, 3 Jul 2024 02:16:43 +0100 Subject: [PATCH] Regression: restore extraction of data-URI images from source for builders whose output formats do not support them natively (#12344) --- CHANGES.rst | 3 +++ sphinx/builders/__init__.py | 4 ++-- sphinx/transforms/post_transforms/images.py | 4 +--- tests/roots/test-images/index.rst | 5 +++++ tests/test_builders/test_build_epub.py | 1 + tests/test_builders/test_build_html_image.py | 1 + tests/test_builders/test_build_latex.py | 1 + tests/test_builders/test_build_texinfo.py | 1 + 8 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 675c7f77c..39a6d2c6a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -73,6 +73,9 @@ Bugs fixed * #12459: Add valid-type arguments to the ``linkcheck_rate_limit_timeout`` configuration setting. Patch by James Addison. +* #12331: Resolve data-URI-image-extraction regression from v7.3.0 affecting + builders without native support for data-URIs in their output format. + Patch by James Addison. Improvements ------------ diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py index bfbf11657..de25ab06a 100644 --- a/sphinx/builders/__init__.py +++ b/sphinx/builders/__init__.py @@ -71,9 +71,9 @@ class Builder: #: The list of MIME types of image formats supported by the builder. #: Image files are searched in the order in which they appear here. supported_image_types: list[str] = [] - #: The builder supports remote images or not. + #: The builder can produce output documents that may fetch external images when opened. supported_remote_images = False - #: The builder supports data URIs or not. + #: The file format produced by the builder allows images to be embedded using data-URIs. supported_data_uri_images = False def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: diff --git a/sphinx/transforms/post_transforms/images.py b/sphinx/transforms/post_transforms/images.py index 589a8d6ab..b679481e8 100644 --- a/sphinx/transforms/post_transforms/images.py +++ b/sphinx/transforms/post_transforms/images.py @@ -115,10 +115,8 @@ class DataURIExtractor(BaseImageConverter): default_priority = 150 def match(self, node: nodes.image) -> bool: - if not self.app.builder.supported_remote_images: - return False if self.app.builder.supported_data_uri_images is True: - return False + return False # do not transform the image; data URIs are valid in the build output return node['uri'].startswith('data:') def handle(self, node: nodes.image) -> None: diff --git a/tests/roots/test-images/index.rst b/tests/roots/test-images/index.rst index 9b9aac1e5..f6d7160da 100644 --- a/tests/roots/test-images/index.rst +++ b/tests/roots/test-images/index.rst @@ -27,3 +27,8 @@ test-image .. non-exist remote image .. image:: http://localhost:7777/NOT_EXIST.PNG + +.. a self-contained image within a data URI + This image was generated using ImageMagick 6.9 with the command ``convert -pointsize 32 -font Noto-Sans-Egyptian-Hieroglyphs-Regular caption:$(printf '\U13080') -trim -border 2 -monochrome eoh.png`` +.. image:: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACoAAAAjAQAAAADKt6U+AAAAAmJLR0QAAd2KE6QAAAAHdElNRQfoBQIVBgOBlOMTAAAAEGNhTnYAAAAtAAAAOwAAAAEAAAATst46RgAAAJtJREFUCNdNz70KwkAMAOA8iOhjuGh9HB9BCtoTHHwMH0Mc7KWTmx0dHDpovUk6HCil3sUmATHLR/4IAeJA+LEWPmbEeHJMWbTMZDA0CNFn8x1COFPaIHQ55R7hlZGdIjwj2aovRjJbhPvMLNN+r0g2vB7ByIWbHqqVh3LR3lhZWM0qYV8qjU6+lc4J7ZVx4SjEINBKOSinv/+YL1xvsJE6ztdqAAAADHRFWHRjYXB0aW9uAPCTgoD4hdKUAAAAD3RFWHRjYXB0aW9uOmxpbmVzADGoBz2RAAAAAElFTkSuQmCC + :alt: The Eye of Horus in a black font on a white background. diff --git a/tests/test_builders/test_build_epub.py b/tests/test_builders/test_build_epub.py index 6829f22dd..461ff8cc6 100644 --- a/tests/test_builders/test_build_epub.py +++ b/tests/test_builders/test_build_epub.py @@ -409,6 +409,7 @@ def test_copy_images(app, status, warning): images = {image.name for image in images_dir.rglob('*')} images.discard('python-logo.png') assert images == { + # 'ba30773957c3fe046897111afd65a80b81cad089.png', # epub: image from data:image/png URI in source 'img.png', 'rimg.png', 'rimg1.png', diff --git a/tests/test_builders/test_build_html_image.py b/tests/test_builders/test_build_html_image.py index f385d1189..365b0110f 100644 --- a/tests/test_builders/test_build_html_image.py +++ b/tests/test_builders/test_build_html_image.py @@ -72,6 +72,7 @@ def test_copy_images(app, status, warning): images_dir = Path(app.outdir) / '_images' images = {image.name for image in images_dir.rglob('*')} assert images == { + # 'ba30773957c3fe046897111afd65a80b81cad089.png', # html: image from data:image/png URI in source 'img.png', 'rimg.png', 'rimg1.png', diff --git a/tests/test_builders/test_build_latex.py b/tests/test_builders/test_build_latex.py index 2c3cae18b..88c83560b 100644 --- a/tests/test_builders/test_build_latex.py +++ b/tests/test_builders/test_build_latex.py @@ -1711,6 +1711,7 @@ def test_copy_images(app, status, warning): } images.discard('sphinx.png') assert images == { + 'ba30773957c3fe046897111afd65a80b81cad089.png', # latex: image from data:image/png URI in source 'img.pdf', 'rimg.png', 'testimäge.png', diff --git a/tests/test_builders/test_build_texinfo.py b/tests/test_builders/test_build_texinfo.py index f9effb2c0..3b8e27662 100644 --- a/tests/test_builders/test_build_texinfo.py +++ b/tests/test_builders/test_build_texinfo.py @@ -124,6 +124,7 @@ def test_copy_images(app, status, warning): images = {image.name for image in images_dir.rglob('*')} images.discard('python-logo.png') assert images == { + 'ba30773957c3fe046897111afd65a80b81cad089.png', # texinfo: image from data:image/png URI in source 'img.png', 'rimg.png', 'testimäge.png',