diff --git a/CHANGES b/CHANGES index a16ce6b8c..fcd09f1b2 100644 --- a/CHANGES +++ b/CHANGES @@ -53,6 +53,8 @@ Features added in the documentation. * #1027: Support backslash line continuation in :rst:dir:`productionlist`. * #7108: config: Allow to show an error message from conf.py via ``ConfigError`` +* #7032: html: :confval:`html_scaled_image_link` will be disabled for images having + ``no-scaled-link`` class Bugs fixed ---------- diff --git a/doc/usage/configuration.rst b/doc/usage/configuration.rst index 7fc09c037..cfc5db065 100644 --- a/doc/usage/configuration.rst +++ b/doc/usage/configuration.rst @@ -1357,8 +1357,21 @@ that use Sphinx's HTMLWriter class. 'target' option or scale related options: 'scale', 'width', 'height'. The default is ``True``. + Document authors can this feature manually with giving ``no-scaled-link`` + class to the image: + + .. code-block:: rst + + .. image:: sphinx.png + :scale: 50% + :class: no-scaled-link + .. versionadded:: 1.3 + .. versionchanged:: 2.4 + + It is disabled for images having ``no-scaled-link`` class + .. confval:: html_math_renderer The name of math_renderer extension for HTML output. The default is diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 80c99d3b8..a0f6a9e55 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -807,13 +807,17 @@ class StandaloneHTMLBuilder(Builder): if self.config.html_scaled_image_link and self.html_scaled_image_link: for node in doctree.traverse(nodes.image): - scale_keys = ('scale', 'width', 'height') - if not any((key in node) for key in scale_keys) or \ - isinstance(node.parent, nodes.reference): - # docutils does unfortunately not preserve the - # ``target`` attribute on images, so we need to check - # the parent node here. + if not any((key in node) for key in ['scale', 'width', 'height']): + # resizing options are not given. scaled image link is available + # only for resized images. continue + elif isinstance(node.parent, nodes.reference): + # A image having hyperlink target + continue + elif 'no-scaled-link' in node['classes']: + # scaled image link is disabled for this node + continue + uri = node['uri'] reference = nodes.reference('', '', internal=True) if uri in self.images: diff --git a/tests/roots/test-html_scaled_image_link/conf.py b/tests/roots/test-html_scaled_image_link/conf.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/roots/test-html_scaled_image_link/img.png b/tests/roots/test-html_scaled_image_link/img.png new file mode 100644 index 000000000..a97e86d66 Binary files /dev/null and b/tests/roots/test-html_scaled_image_link/img.png differ diff --git a/tests/roots/test-html_scaled_image_link/index.rst b/tests/roots/test-html_scaled_image_link/index.rst new file mode 100644 index 000000000..0e4794058 --- /dev/null +++ b/tests/roots/test-html_scaled_image_link/index.rst @@ -0,0 +1,11 @@ +test-html_scaled_image_link +=========================== + +.. image:: img.png + +.. image:: img.png + :scale: 50% + +.. image:: img.png + :scale: 50% + :class: no-scaled-link diff --git a/tests/test_build_html.py b/tests/test_build_html.py index ac44ca7e4..25ef87644 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -1542,3 +1542,22 @@ def test_validate_html_static_path(app): ] validate_html_static_path(app, app.config) assert app.config.html_static_path == ['_static'] + + +@pytest.mark.sphinx(testroot='html_scaled_image_link') +def test_html_scaled_image_link(app): + app.build() + context = (app.outdir / 'index.html').text() + + # no scaled parameters + assert re.search('\n_images/img.png', context) + + # scaled_image_link + assert re.search('\n' + '_images/img.png', + context) + + # no-scaled-link class disables the feature + assert re.search('\n_images/img.png', + context)