File: test_build_html_image.py

package info (click to toggle)
sphinx 8.2.3-12
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 26,960 kB
  • sloc: python: 105,864; javascript: 6,474; perl: 449; makefile: 178; sh: 37; xml: 19; ansic: 2
file content (116 lines) | stat: -rw-r--r-- 3,951 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from __future__ import annotations

import re
from pathlib import Path
from typing import TYPE_CHECKING

import docutils
import pytest

if TYPE_CHECKING:
    from sphinx.testing.util import SphinxTestApp


@pytest.mark.usefixtures('_http_teapot')
@pytest.mark.sphinx('html', testroot='images')
def test_html_remote_images(app: SphinxTestApp) -> None:
    app.build(force_all=True)

    result = (app.outdir / 'index.html').read_text(encoding='utf8')
    assert (
        '<img alt="http://localhost:7777/sphinx.png" '
        'src="http://localhost:7777/sphinx.png" />'
    ) in result
    assert not (app.outdir / 'sphinx.png').exists()


@pytest.mark.sphinx('html', testroot='image-escape')
def test_html_encoded_image(app: SphinxTestApp) -> None:
    app.build(force_all=True)

    result = (app.outdir / 'index.html').read_text(encoding='utf8')
    assert '<img alt="_images/img_%231.png" src="_images/img_%231.png" />' in result
    assert (app.outdir / '_images/img_#1.png').exists()


@pytest.mark.sphinx('html', testroot='remote-logo')
def test_html_remote_logo(app: SphinxTestApp) -> None:
    app.build(force_all=True)

    result = (app.outdir / 'index.html').read_text(encoding='utf8')
    assert (
        '<img class="logo" src="https://www.python.org/static/img/python-logo.png" alt="Logo of Project name not set"/>'
    ) in result
    assert (
        '<link rel="icon" href="https://www.python.org/static/favicon.ico"/>'
    ) in result
    assert not (app.outdir / 'python-logo.png').exists()


@pytest.mark.sphinx('html', testroot='local-logo')
def test_html_local_logo(app: SphinxTestApp) -> None:
    app.build(force_all=True)

    result = (app.outdir / 'index.html').read_text(encoding='utf8')
    assert (
        '<img class="logo" src="_static/img.png" alt="Logo of Project name not set"/>'
    ) in result
    assert (app.outdir / '_static/img.png').exists()


@pytest.mark.sphinx('html', testroot='html_scaled_image_link')
def test_html_scaled_image_link(app: SphinxTestApp) -> None:
    app.build()
    context = (app.outdir / 'index.html').read_text(encoding='utf8')

    # no scaled parameters
    assert re.search('\n<img alt="_images/img.png" src="_images/img.png" />', context)

    # scaled_image_link
    if docutils.__version_info__[:2] >= (0, 22):
        assert re.search(
            '\n<a class="reference internal image-reference" href="_images/img.png">'
            '<img alt="_images/img.png" height="[^"]+" src="_images/img.png" width="[^"]+" />'
            '\n</a>',
            context,
        )
    else:
        # Docutils 0.21 adds a newline before the closing </a> tag
        closing_space = '\n' if docutils.__version_info__[:2] >= (0, 21) else ''
        assert re.search(
            '\n<a class="reference internal image-reference" href="_images/img.png">'
            '<img alt="_images/img.png" src="_images/img.png" style="[^"]+" />'
            f'{closing_space}</a>',
            context,
        )

    # no-scaled-link class disables the feature
    if docutils.__version_info__[:2] >= (0, 22):
        assert re.search(
            '\n<img alt="_images/img.png" class="no-scaled-link"'
            ' height="[^"]+" src="_images/img.png" width="[^"]+" />',
            context,
        )
    else:
        assert re.search(
            '\n<img alt="_images/img.png" class="no-scaled-link"'
            ' src="_images/img.png" style="[^"]+" />',
            context,
        )


@pytest.mark.usefixtures('_http_teapot')
@pytest.mark.sphinx('html', testroot='images')
def test_copy_images(app: SphinxTestApp) -> None:
    app.build()

    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',
        'svgimg.svg',
        'testimäge.png',
    }