Debian Codegraph @

tests/test_builders/test_build_html_5_output.py

raw
1 """Test the HTML builder and check output against XPath."""
2
3 from __future__ import annotations
4
5 import re
6 from typing import TYPE_CHECKING
7
8 import pytest
9 from docutils import nodes
10
11 from tests.test_builders.xpath_util import check_xpath
12
13 if TYPE_CHECKING:
14 from collections.abc import Callable, Iterable
15 from pathlib import Path
16 from typing import Literal
17 from xml.etree.ElementTree import Element, ElementTree
18
19 from sphinx.testing.util import SphinxTestApp
20
21
22 def tail_check(check: str) -> Callable[[Iterable[Element]], Literal[True]]:
23 rex = re.compile(check)
24
25 def checker(nodes: Iterable[Element]) -> Literal[True]:
26 for node in nodes:
27 if node.tail and rex.search(node.tail):
28 return True
29 msg = f'{check!r} not found in tail of any nodes {nodes}'
30 raise AssertionError(msg)
31
32 return checker
33
34
35 @pytest.mark.parametrize(
36 ('fname', 'path', 'check'),
37 [
38 ('images.html', ".//img[@src='_images/img.png']", ''),
39 ('images.html', ".//img[@src='_images/img1.png']", ''),
40 ('images.html', ".//img[@src='_images/simg.png']", ''),
41 ('images.html', ".//img[@src='_images/svgimg.svg']", ''),
42 ('images.html', ".//a[@href='_sources/images.txt']", ''),
43 # Check svg options
44 ('images.html', ".//img[@src='_images/svgimg.svg'][@style='width: 2cm;']", ''),
45 ('images.html', ".//img[@src='_images/svgimg.svg'][@style='height: 2cm;']", ''),
46 ('subdir/images.html', ".//img[@src='../_images/img1.png']", ''),
47 ('subdir/images.html', ".//img[@src='../_images/rimg.png']", ''),
48 ('subdir/includes.html', ".//a[@class='reference download internal']", ''),
49 ('subdir/includes.html', ".//img[@src='../_images/img.png']", ''),
50 ('subdir/includes.html', './/p', 'This is an include file.'),
51 ('subdir/includes.html', './/pre/span', 'line 1'),
52 ('subdir/includes.html', './/pre/span', 'line 2'),
53 ('includes.html', './/pre', 'Max Strauß'),
54 ('includes.html', ".//a[@class='reference download internal']", ''),
55 ('includes.html', './/pre/span', '"quotes"'),
56 ('includes.html', './/pre/span', "'included'"),
57 ('includes.html', ".//pre/span[@class='s2']", 'üöä'),
58 (
59 'includes.html',
60 ".//div[@class='inc-pyobj1 highlight-text notranslate']//pre",
61 r'^class Foo:\n pass\n\s*$',
62 ),
63 (
64 'includes.html',
65 ".//div[@class='inc-pyobj2 highlight-text notranslate']//pre",
66 r'^ def baz\(\):\n pass\n\s*$',
67 ),
68 (
69 'includes.html',
70 ".//div[@class='inc-lines highlight-text notranslate']//pre",
71 r'^class Foo:\n pass\nclass Bar:\n$',
72 ),
73 (
74 'includes.html',
75 ".//div[@class='inc-startend highlight-text notranslate']//pre",
76 '^foo = "Including Unicode characters: üöä"\\n$',
77 ),
78 (
79 'includes.html',
80 ".//div[@class='inc-preappend highlight-text notranslate']//pre",
81 r'(?m)^START CODE$',
82 ),
83 (
84 'includes.html',
85 ".//div[@class='inc-pyobj-dedent highlight-python notranslate']//span",
86 r'def',
87 ),
88 (
89 'includes.html',
90 ".//div[@class='inc-tab3 highlight-text notranslate']//pre",
91 r'-| |-',
92 ),
93 (
94 'includes.html',
95 ".//div[@class='inc-tab8 highlight-python notranslate']//pre/span",
96 r'-| |-',
97 ),
98 ('autodoc.html', ".//dl[@class='py class']/dt[@id='autodoc_target.Class']", ''),
99 (
100 'autodoc.html',
101 ".//dl[@class='py function']/dt[@id='autodoc_target.function']/em/span/span",
102 r'\*\*',
103 ),
104 (
105 'autodoc.html',
106 ".//dl[@class='py function']/dt[@id='autodoc_target.function']/em/span/span",
107 r'kwds',
108 ),
109 ('autodoc.html', './/dd/p', r'Return spam\.'),
110 ('extapi.html', './/strong', 'from class: Bar'),
111 ('markup.html', './/title', 'set by title directive'),
112 ('markup.html', './/p/em', 'Section author: Georg Brandl'),
113 ('markup.html', './/p/em', 'Module author: Georg Brandl'),
114 # created by the meta directive
115 ('markup.html', ".//meta[@name='author'][@content='Me']", ''),
116 ('markup.html', ".//meta[@name='keywords'][@content='docs, sphinx']", ''),
117 # a label created by ``.. _label:``
118 ('markup.html', ".//div[@id='label']", ''),
119 # code with standard code blocks
120 ('markup.html', './/pre', '^some code$'),
121 # an option list
122 ('markup.html', ".//span[@class='option']", '--help'),
123 # admonitions
124 ('markup.html', ".//p[@class='admonition-title']", 'My Admonition'),
125 ('markup.html', ".//div[@class='admonition note']/p", 'Note text.'),
126 ('markup.html', ".//div[@class='admonition warning']/p", 'Warning text.'),
127 # inline markup
128 ('markup.html', './/li/p/strong', r'^command\\n$'),
129 ('markup.html', './/li/p/strong', r'^program\\n$'),
130 ('markup.html', './/li/p/em', r'^dfn\\n$'),
131 ('markup.html', './/li/p/kbd', r'^kbd\\n$'),
132 ('markup.html', './/li/p/span', 'File \N{TRIANGULAR BULLET} Close'),
133 ('markup.html', ".//li/p/code/span[@class='pre']", '^a/$'),
134 ('markup.html', ".//li/p/code/em/span[@class='pre']", '^varpart$'),
135 ('markup.html', ".//li/p/code/em/span[@class='pre']", '^i$'),
136 (
137 'markup.html',
138 ".//a[@href='https://peps.python.org/pep-0008/']"
139 "[@class='pep reference external']/strong",
140 'PEP 8',
141 ),
142 (
143 'markup.html',
144 ".//a[@href='https://peps.python.org/pep-0008/']"
145 "[@class='pep reference external']/strong",
146 'Python Enhancement Proposal #8',
147 ),
148 (
149 'markup.html',
150 ".//a[@href='https://datatracker.ietf.org/doc/html/rfc1.html']"
151 "[@class='rfc reference external']/strong",
152 'RFC 1',
153 ),
154 (
155 'markup.html',
156 ".//a[@href='https://datatracker.ietf.org/doc/html/rfc1.html']"
157 "[@class='rfc reference external']/strong",
158 'Request for Comments #1',
159 ),
160 (
161 'markup.html',
162 ".//a[@href='objects.html#envvar-HOME']"
163 "[@class='reference internal']/code/span[@class='pre']",
164 'HOME',
165 ),
166 (
167 'markup.html',
168 ".//a[@href='#with'][@class='reference internal']/code/span[@class='pre']",
169 '^with$',
170 ),
171 (
172 'markup.html',
173 ".//a[@href='#grammar-token-try_stmt']"
174 "[@class='reference internal']/code/span",
175 '^statement$',
176 ),
177 (
178 'markup.html',
179 ".//a[@href='#some-label'][@class='reference internal']/span",
180 '^here$',
181 ),
182 (
183 'markup.html',
184 ".//a[@href='#some-label'][@class='reference internal']/span",
185 '^there$',
186 ),
187 (
188 'markup.html',
189 ".//a[@href='subdir/includes.html'][@class='reference internal']/span",
190 'Including in subdir',
191 ),
192 (
193 'markup.html',
194 ".//a[@href='objects.html#cmdoption-python-c']"
195 "[@class='reference internal']/code/span[@class='pre']",
196 '-c',
197 ),
198 # abbreviations
199 ('markup.html', ".//abbr[@title='abbreviation']", '^abbr$'),
200 # version stuff
201 (
202 'markup.html',
203 ".//div[@class='versionadded']/p/span",
204 'Added in version 0.6: ',
205 ),
206 (
207 'markup.html',
208 ".//div[@class='versionadded']/p/span",
209 tail_check('First paragraph of version-added'),
210 ),
211 (
212 'markup.html',
213 ".//div[@class='versionadded']/p/span",
214 tail_check('Deprecated alias for version-added'),
215 ),
216 (
217 'markup.html',
218 ".//div[@class='versionchanged']/p/span",
219 tail_check('First paragraph of version-changed'),
220 ),
221 (
222 'markup.html',
223 ".//div[@class='versionchanged']/p",
224 'Second paragraph of version-changed',
225 ),
226 (
227 'markup.html',
228 ".//div[@class='versionchanged']/p/span",
229 tail_check('Deprecated alias for version-changed'),
230 ),
231 (
232 'markup.html',
233 ".//div[@class='deprecated']/p/span",
234 'Deprecated since version 0.6: ',
235 ),
236 (
237 'markup.html',
238 ".//div[@class='deprecated']/p/span",
239 tail_check('Boring stuff.'),
240 ),
241 (
242 'markup.html',
243 ".//div[@class='deprecated']/p/span",
244 tail_check('Deprecated alias for version-deprecated'),
245 ),
246 (
247 'markup.html',
248 ".//div[@class='versionremoved']/p/span",
249 'Removed in version 0.6: ',
250 ),
251 (
252 'markup.html',
253 ".//div[@class='versionremoved']/p/span",
254 tail_check('Deprecated alias for version-removed'),
255 ),
256 # footnote reference
257 ('markup.html', ".//a[@class='footnote-reference brackets']", r'1'),
258 # created by reference lookup
259 ('markup.html', ".//a[@href='index.html#ref1']", ''),
260 # ``seealso`` directive
261 ('markup.html', ".//div/p[@class='admonition-title']", 'See also'),
262 # a ``hlist`` directive
263 ('markup.html', ".//table[@class='hlist']/tr/td/ul/li/p", '^This$'),
264 # a ``centered`` directive
265 ('markup.html', ".//p[@class='centered']/strong", 'LICENSE'),
266 # a glossary
267 ('markup.html', ".//dl/dt[@id='term-boson']", 'boson'),
268 ('markup.html', ".//dl/dt[@id='term-boson']/a", '¶'),
269 # a production list
270 ('markup.html', './/pre/strong', 'try_stmt'),
271 (
272 'markup.html',
273 ".//pre/a[@href='#grammar-token-try1_stmt']/code/span",
274 'try1_stmt',
275 ),
276 # tests for ``only`` directive
277 ('markup.html', './/p', 'A global substitution!'),
278 ('markup.html', './/p', 'In HTML.'),
279 ('markup.html', './/p', 'In both.'),
280 ('markup.html', './/p', 'Always present'),
281 # tests for ``any`` role
282 ('markup.html', ".//a[@href='#with']/span", 'headings'),
283 (
284 'markup.html',
285 ".//a[@href='objects.html#func_without_body']/code/span",
286 'objects',
287 ),
288 # tests for numeric labels
289 (
290 'markup.html',
291 ".//a[@href='#id1'][@class='reference internal']/span",
292 'Testing various markup',
293 ),
294 # tests for smartypants
295 ('markup.html', './/li/p', 'Smart “quotes” in English ‘text’.'),
296 ('markup.html', './/li/p', 'Smart — long and – short dashes.'),
297 ('markup.html', './/li/p', 'Ellipsis…'),
298 ('markup.html', ".//li/p/code/span[@class='pre']", 'foo--"bar"...'),
299 ('markup.html', './/p', 'Этот «абзац» должен использовать „русские“ кавычки.'),
300 ('markup.html', './/p', 'Il dit : « C’est “super” ! »'),
301 ('objects.html', ".//dt[@id='mod.Cls.meth1']", ''),
302 ('objects.html', ".//dt[@id='errmod.Error']", ''),
303 (
304 'objects.html',
305 ".//dt/span[@class='sig-name descname']/span[@class='pre']",
306 r'long\(parameter,',
307 ),
308 (
309 'objects.html',
310 ".//dt/span[@class='sig-name descname']/span[@class='pre']",
311 r'list\)',
312 ),
313 (
314 'objects.html',
315 ".//dt/span[@class='sig-name descname']/span[@class='pre']",
316 'another',
317 ),
318 (
319 'objects.html',
320 ".//dt/span[@class='sig-name descname']/span[@class='pre']",
321 'one',
322 ),
323 ('objects.html', ".//a[@href='#mod.Cls'][@class='reference internal']", ''),
324 ('objects.html', ".//dl[@class='std userdesc']", ''),
325 ('objects.html', ".//dt[@id='userdesc-myobj']", ''),
326 (
327 'objects.html',
328 ".//a[@href='#userdesc-myobj'][@class='reference internal']",
329 '',
330 ),
331 # docfields
332 (
333 'objects.html',
334 ".//a[@class='reference internal'][@href='#TimeInt']/em",
335 'TimeInt',
336 ),
337 ('objects.html', ".//a[@class='reference internal'][@href='#Time']", 'Time'),
338 (
339 'objects.html',
340 ".//a[@class='reference internal'][@href='#errmod.Error']/strong",
341 'Error',
342 ),
343 # C references
344 ('objects.html', ".//span[@class='pre']", 'CFunction()'),
345 ('objects.html', ".//a[@href='#c.Sphinx_DoSomething']", ''),
346 ('objects.html', ".//a[@href='#c.SphinxStruct.member']", ''),
347 ('objects.html', ".//a[@href='#c.SPHINX_USE_PYTHON']", ''),
348 ('objects.html', ".//a[@href='#c.SphinxType']", ''),
349 ('objects.html', ".//a[@href='#c.sphinx_global']", ''),
350 # test global TOC created by toctree()
351 (
352 'objects.html',
353 ".//ul[@class='current']/li[@class='toctree-l1 current']/a[@href='#']",
354 'Testing object descriptions',
355 ),
356 (
357 'objects.html',
358 ".//li[@class='toctree-l1']/a[@href='markup.html']",
359 'Testing various markup',
360 ),
361 # test unknown field names
362 ('objects.html', ".//dt[@class='field-odd']", 'Field_name'),
363 ('objects.html', ".//dt[@class='field-even']", 'Field_name all lower'),
364 ('objects.html', ".//dt[@class='field-odd']", 'FIELD_NAME'),
365 ('objects.html', ".//dt[@class='field-even']", 'FIELD_NAME ALL CAPS'),
366 ('objects.html', ".//dt[@class='field-odd']", 'Field_Name'),
367 ('objects.html', ".//dt[@class='field-even']", 'Field_Name All Word Caps'),
368 # ('objects.html', ".//dt[@class='field-odd']", 'Field_name'), (duplicate)
369 ('objects.html', ".//dt[@class='field-even']", 'Field_name First word cap'),
370 ('objects.html', ".//dt[@class='field-odd']", 'FIELd_name'),
371 ('objects.html', ".//dt[@class='field-even']", 'FIELd_name PARTial caps'),
372 # custom sidebar
373 ('objects.html', './/h4', 'Custom sidebar'),
374 # docfields
375 ('objects.html', ".//dd[@class='field-odd']/p/strong", '^moo$'),
376 (
377 'objects.html',
378 ".//dd[@class='field-odd']/p/strong",
379 tail_check(r'\(Moo\) .* Moo'),
380 ),
381 ('objects.html', ".//dd[@class='field-odd']/ul/li/p/strong", '^hour$'),
382 ('objects.html', ".//dd[@class='field-odd']/ul/li/p/em", '^DuplicateType$'),
383 (
384 'objects.html',
385 ".//dd[@class='field-odd']/ul/li/p/em",
386 tail_check(r'.* Some parameter'),
387 ),
388 # others
389 (
390 'objects.html',
391 ".//a[@class='reference internal'][@href='#cmdoption-perl-arg-p']/code/span",
392 'perl',
393 ),
394 (
395 'objects.html',
396 ".//a[@class='reference internal'][@href='#cmdoption-perl-arg-p']/code/span",
397 '\\+p',
398 ),
399 (
400 'objects.html',
401 ".//a[@class='reference internal'][@href='#cmdoption-perl-ObjC']/code/span",
402 '--ObjC\\+\\+',
403 ),
404 (
405 'objects.html',
406 ".//a[@class='reference internal'][@href='#cmdoption-perl-plugin.option']/code/span",
407 '--plugin.option',
408 ),
409 (
410 'objects.html',
411 ".//a[@class='reference internal'][@href='#cmdoption-perl-arg-create-auth-token']"
412 '/code/span',
413 'create-auth-token',
414 ),
415 (
416 'objects.html',
417 ".//a[@class='reference internal'][@href='#cmdoption-perl-arg-arg']/code/span",
418 'arg',
419 ),
420 (
421 'objects.html',
422 ".//a[@class='reference internal'][@href='#cmdoption-perl-j']/code/span",
423 '-j',
424 ),
425 (
426 'objects.html',
427 ".//a[@class='reference internal'][@href='#cmdoption-hg-arg-commit']/code/span",
428 'hg',
429 ),
430 (
431 'objects.html',
432 ".//a[@class='reference internal'][@href='#cmdoption-hg-arg-commit']/code/span",
433 'commit',
434 ),
435 (
436 'objects.html',
437 ".//a[@class='reference internal'][@href='#cmdoption-git-commit-p']/code/span",
438 'git',
439 ),
440 (
441 'objects.html',
442 ".//a[@class='reference internal'][@href='#cmdoption-git-commit-p']/code/span",
443 'commit',
444 ),
445 (
446 'objects.html',
447 ".//a[@class='reference internal'][@href='#cmdoption-git-commit-p']/code/span",
448 '-p',
449 ),
450 ('index.html', ".//meta[@name='hc'][@content='hcval']", ''),
451 ('index.html', ".//meta[@name='hc_co'][@content='hcval_co']", ''),
452 ('index.html', ".//li[@class='toctree-l1']/a", 'Testing various markup'),
453 ('index.html', ".//li[@class='toctree-l2']/a", 'Inline markup'),
454 ('index.html', './/title', 'Sphinx <Tests>'),
455 ('index.html', ".//div[@class='footer']", 'copyright text credits'),
456 (
457 'index.html',
458 ".//a[@href='https://python.org/'][@class='reference external']",
459 '',
460 ),
461 ('index.html', ".//li/p/a[@href='genindex.html']/span", 'Index'),
462 ('index.html', ".//li/p/a[@href='py-modindex.html']/span", 'Module Index'),
463 # custom sidebar only for contents
464 ('index.html', './/h4', 'Contents sidebar'),
465 # custom JavaScript
466 ('index.html', ".//script[@src='file://moo.js']", ''),
467 # URL in contents
468 (
469 'index.html',
470 ".//a[@class='reference external'][@href='https://sphinx-doc.org/']",
471 'https://sphinx-doc.org/',
472 ),
473 (
474 'index.html',
475 ".//a[@class='reference external'][@href='https://sphinx-doc.org/latest/']",
476 'Latest reference',
477 ),
478 # Indirect hyperlink targets across files
479 (
480 'index.html',
481 ".//a[@href='markup.html#some-label'][@class='reference internal']/span",
482 '^indirect hyperref$',
483 ),
484 ('bom.html', './/title', ' File with UTF-8 BOM'),
485 (
486 'extensions.html',
487 ".//a[@href='https://python.org/dev/']",
488 'https://python.org/dev/',
489 ),
490 (
491 'extensions.html',
492 ".//a[@href='https://bugs.python.org/issue1000']",
493 'issue 1000',
494 ),
495 (
496 'extensions.html',
497 ".//a[@href='https://bugs.python.org/issue1042']",
498 'explicit caption',
499 ),
500 (
501 'extensions.html',
502 ".//a[@class='extlink-pyurl reference external']",
503 'https://python.org/dev/',
504 ),
505 (
506 'extensions.html',
507 ".//a[@class='extlink-issue reference external']",
508 'issue 1000',
509 ),
510 # index entries
511 ('genindex.html', './/a/strong', 'Main'),
512 ('genindex.html', './/a/strong', '[1]'),
513 ('genindex.html', './/a/strong', 'Other'),
514 ('genindex.html', './/a', 'entry'),
515 ('genindex.html', './/li/a', 'double'),
516 ('otherext.html', './/h1', 'Generated section'),
517 ('otherext.html', ".//a[@href='_sources/otherext.foo.txt']", ''),
518 ('search.html', ".//meta[@name='robots'][@content='noindex']", ''),
519 ],
520 )
521 @pytest.mark.sphinx(
522 'html',
523 testroot='root',
524 tags=['testtag'],
525 confoverrides={'html_context.hckey_co': 'hcval_co'},
526 )
527 def test_html5_output(
528 app: SphinxTestApp,
529 cached_etree_parse: Callable[[Path], ElementTree],
530 fname: str,
531 path: str,
532 check: str,
533 ) -> None:
534 app.build()
535 check_xpath(cached_etree_parse(app.outdir / fname), fname, path, check)
536
537
538 @pytest.mark.sphinx('html', testroot='markup-rubric')
539 def test_html5_rubric(app: SphinxTestApp) -> None:
540 def insert_invalid_rubric_heading_level(
541 app: SphinxTestApp,
542 doctree: nodes.document,
543 docname: str,
544 ) -> None:
545 if docname != 'index':
546 return
547 new_node = nodes.rubric('', 'INSERTED RUBRIC')
548 new_node['heading-level'] = 7
549 section = doctree[0]
550 assert isinstance(section, nodes.Element)
551 section.append(new_node)
552
553 app.connect('doctree-resolved', insert_invalid_rubric_heading_level)
554 app.build()
555
556 warnings = app.warning.getvalue()
557 content = (app.outdir / 'index.html').read_text(encoding='utf8')
558 assert '<p class="rubric">This is a rubric</p>' in content
559 assert '<h2 class="myclass rubric">A rubric with a heading level 2</h2>' in content
560
561 # directive warning
562 assert '"7" unknown' in warnings
563
564 # html writer warning
565 assert 'WARNING: unsupported rubric heading level: 7' in warnings
566 assert '</h7>' not in content
567 assert '<p class="rubric">INSERTED RUBRIC</p>' in content