Debian Codegraph @

test/test_writers/test_html5_polyglot_misc.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_html5_polyglot_misc.py 9928 2024-09-11 07:20:07Z milde $
3 # Authors: Lea Wiemann, Dmitry Shachnev, Günter Milde
4 # Maintainer: [email protected]
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Miscellaneous HTML writer tests.
9 """
10
11 import os
12 from pathlib import Path
13 import sys
14 import unittest
15
16 if __name__ == '__main__':
17 # prepend the "docutils root" to the Python library path
18 # so we import the local `docutils` package.
19 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
20
21 from docutils import core, frontend, nodes, utils
22 from docutils.writers import html5_polyglot, _html_base
23
24 # TEST_ROOT is ./test/ from the docutils root
25 TEST_ROOT = Path(__file__).parents[1]
26 DATA_ROOT = TEST_ROOT / 'data'
27
28
29 def relpath(path):
30 return os.path.relpath(path, os.getcwd()).replace('\\', '/')
31
32
33 class EncodingTestCase(unittest.TestCase):
34
35 def test_xmlcharrefreplace(self):
36 # Test that xmlcharrefreplace is the default output encoding
37 # error handler.
38 settings_overrides = {
39 'output_encoding': 'latin1',
40 'stylesheet_path': '',
41 '_disable_config': True}
42 result = core.publish_string(
43 'EUR = \u20ac', writer=html5_polyglot.Writer(),
44 settings_overrides=settings_overrides)
45 # Encoding a euro sign with latin1 doesn't work, so the
46 # xmlcharrefreplace handler is used.
47 self.assertIn(b'EUR = €', result)
48
49
50 class MovingArgsTestCase(unittest.TestCase):
51
52 mys = {'stylesheet_path': '',
53 '_disable_config': True,
54 }
55
56 def test_definition_list_item_classes(self):
57 # Do not drop class arguments for the definition list item.
58 # Pass them to the term node instead.
59 data = """\
60 first term:
61 fist def
62
63 .. class:: for the second item
64
65 second term:
66 second def
67 """
68 result = core.publish_string(data, writer=html5_polyglot.Writer(),
69 settings_overrides=self.mys).decode()
70 self.assertIn('<dt class="for the second item">second term:</dt>',
71 result)
72
73 def test_definition_list_item_name(self):
74 # Do not drop the "name" of the definition list item.
75 # Pass it to to the term node instead.
76 data = """\
77 first term:
78 first def
79
80 .. _second item:
81
82 second term:
83 second def
84 """
85 result = core.publish_string(data, writer=html5_polyglot.Writer(),
86 settings_overrides=self.mys).decode()
87 self.assertIn('<dt id="second-item">second term:</dt>',
88 result)
89
90
91 ham_css = relpath(DATA_ROOT / 'ham.css')
92
93
94 class SettingsTestCase(unittest.TestCase):
95 data = 'test'
96
97 def test_default_stylesheet(self):
98 # default style sheet, embedded
99 mys = {'_disable_config': True}
100 styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
101 settings_overrides=mys)['stylesheet']
102 self.assertIn('Minimal style sheet for the HTML output of Docutils.',
103 styles)
104
105 def test_default_stylesheet_linked(self):
106 # default style sheet, linked
107 mys = {'_disable_config': True,
108 'embed_stylesheet': False}
109 styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
110 settings_overrides=mys)['stylesheet']
111 self.assertIn('docutils/writers/html5_polyglot/minimal.css', styles)
112
113 def test_math_stylesheet_linked(self):
114 # default + math style sheet, linked
115 mys = {'_disable_config': True,
116 'embed_stylesheet': False,
117 'stylesheet_path': 'minimal.css, math.css'}
118 styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
119 settings_overrides=mys)['stylesheet']
120 self.assertIn('docutils/writers/html5_polyglot/minimal.css', styles)
121 self.assertIn('docutils/writers/html5_polyglot/math.css', styles)
122
123 def test_custom_stylesheet_linked(self):
124 # default + custom style sheet, linked
125 mys = {'_disable_config': True,
126 'embed_stylesheet': False,
127 'stylesheet_path': 'minimal.css, '
128 'data/ham.css'}
129 styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
130 settings_overrides=mys)['stylesheet']
131 self.assertIn('docutils/writers/html5_polyglot/minimal.css', styles)
132 self.assertIn('href="data/ham.css"', styles)
133
134 def test_custom_stylesheet_dir(self):
135 mys = {'_disable_config': True,
136 'embed_stylesheet': False,
137 'stylesheet_dirs': (
138 TEST_ROOT / '../docutils/writers/html5_polyglot/',
139 DATA_ROOT),
140 'stylesheet_path': 'minimal.css, ham.css'}
141 styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
142 settings_overrides=mys)['stylesheet']
143 if (TEST_ROOT / '../docutils/writers/html5_polyglot/').is_dir():
144 self.assertIn('docutils/writers/html5_polyglot/minimal.css',
145 styles)
146 self.assertIn(f'href="{ham_css}"', styles)
147
148 def test_custom_stylesheet_dir_embedded(self):
149 mys = {'_disable_config': True,
150 'embed_stylesheet': True,
151 'stylesheet_dirs': (
152 TEST_ROOT / '../docutils/writers/html5_polyglot/',
153 DATA_ROOT),
154 'stylesheet_path': 'ham.css'}
155 styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
156 settings_overrides=mys)['stylesheet']
157 self.assertIn('dl.docutils dd {\n margin-bottom: 0.5em }', styles)
158
159 def test_future_warnings(self):
160 """Warn about deprecated setting name."""
161 my_settings = {'_disable_config': True,
162 'embed_images': False,
163 'output_encoding': 'unicode',
164 }
165 with self.assertWarnsRegex(FutureWarning,
166 '"embed_images"\n will be removed'):
167 core.publish_string('warnings test',
168 writer=html5_polyglot.Writer(),
169 settings_overrides=my_settings)
170
171
172 minimal_css = relpath(TEST_ROOT / 'functional/input/data/minimal.css')
173 plain_css = relpath(TEST_ROOT / 'functional/input/data/plain.css')
174 math_css = relpath(TEST_ROOT / 'functional/input/data/math.css')
175
176
177 class MathTestCase(unittest.TestCase):
178
179 """Provisional: This class tests the current implementation of maths
180 support which is open to change in future Docutils releases. """
181
182 mathjax_script = '<script type="text/javascript" src="%s">'
183 default_mathjax_url = ('file:/usr/share/javascript/mathjax/MathJax.js'
184 '?config=TeX-AMS_CHTML')
185 custom_mathjax_url = '/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
186 data = ':math:`42`'
187
188 def test_math_output_default(self):
189 # default math output is MathML (since 0.22)
190 mys = {'_disable_config': True}
191 fragment = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
192 settings_overrides=mys)['fragment']
193 self.assertIn('<mn>42</mn>', fragment)
194
195 def test_math_output_mathjax(self):
196 # Explicitly specifying math_output=MathJax, case insensitively
197 # use default MathJax URL
198 mys = {'_disable_config': True,
199 'report_level': 3,
200 'math_output': 'MathJax'}
201 head = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
202 settings_overrides=mys)['head']
203 self.assertIn(self.mathjax_script % self.default_mathjax_url, head)
204
205 def test_math_output_mathjax_custom(self):
206 # Customizing MathJax URL
207 mys = {'_disable_config': True,
208 'math_output':
209 'mathjax %s' % self.custom_mathjax_url}
210 head = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
211 settings_overrides=mys)['head']
212 self.assertIn(self.mathjax_script % self.custom_mathjax_url, head)
213
214 def test_math_output_html(self):
215 mys = {'_disable_config': True,
216 'math_output': 'HTML'}
217 head = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
218 settings_overrides=mys)['head']
219 # There should be no MathJax script when math_output is not MathJax
220 self.assertNotIn('MathJax.js', head)
221
222 def test_math_output_html_stylesheet(self):
223 mys = {'_disable_config': True,
224 'math_output': 'HTML math.css,custom/style.css',
225 'stylesheet_dirs': (
226 TEST_ROOT,
227 TEST_ROOT / 'functional/input/data'),
228 'embed_stylesheet': False}
229 styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(),
230 settings_overrides=mys)['stylesheet']
231 self.assertEqual(f"""\
232 <link rel="stylesheet" href="{minimal_css}" type="text/css" />
233 <link rel="stylesheet" href="{plain_css}" type="text/css" />
234 <link rel="stylesheet" href="{math_css}" type="text/css" />
235 <link rel="stylesheet" href="custom/style.css" type="text/css" />
236 """, styles)
237
238 def test_math_output_mathjax_no_math(self):
239 # There should be no math script when text does not contain math
240 head = core.publish_parts('No math.', writer=html5_polyglot.Writer())['head']
241 self.assertNotIn('MathJax', head)
242
243
244 class ImagesTestCase(unittest.TestCase):
245 """Test image handling routines."""
246 maxDiff = None
247 settings = frontend.get_default_settings(_html_base.Writer)
248 document = utils.new_document('test data', settings)
249 translator = _html_base.HTMLTranslator(document)
250 svg_sample = """\
251 <?xml version="1.0" encoding="UTF-8"?>
252 <svg xmlns="http://www.w3.org/2000/svg"
253 style="background: blue; width: 5ex; height: 3ex" viewBox="0 0 10 10">
254 <circle cx="5" cy="5" r="4" fill="lightblue" />
255 <!-- comments are ignored -->
256 </svg>
257 """
258 expected = """\
259 <svg xmlns="http://www.w3.org/2000/svg" style="background: blue; width: 4em; height: 3ex;" viewBox="0 0 10 10" height="32" class="test me">
260 <title>blue circle</title><circle cx="5" cy="5" r="4" fill="lightblue" />
261 \n\
262 </svg>"""
263
264 def test_image_size(self):
265 # HTML attributes "width" and "height" take only integer values:
266 # -> height value is passed as "height" attribute,
267 # value with unit is passed via "style".
268 image = nodes.image(width='4em', height='3', scale=200)
269 self.assertEqual(self.translator.image_size(image),
270 {'height': '6', 'style': 'width: 8em;'})
271 # "style" declaration also used for unitless non-interger value
272 image = nodes.image(width='4em', height='3', scale=50)
273 self.assertEqual(self.translator.image_size(image),
274 {'style': 'width: 2em;', 'height': '2'})
275
276 def test_prepare_svg(self):
277 # Internal method: the test is no guaranty for stability,
278 # interface and behaviour may change without notice.
279 image = nodes.image(height='32', width='4em', alt='blue circle',
280 align='left', classes=['test', 'me'])
281 atts = self.translator.image_size(image)
282 rv = self.translator.prepare_svg(self.svg_sample, image, atts)
283 self.assertEqual(rv, self.expected)
284
285 def test_prepare_svg_syntax_variants(self):
286 # parsing "style" declarations must be robust:
287 svg_sample = '<svg style="width:3em"></svg>'
288
289 atts = {'style': 'colon:in:value'}
290 rv = self.translator.prepare_svg(svg_sample, nodes.image(), atts)
291 self.assertEqual(rv, '<svg style="width: 3em; colon: in:value;" />')
292
293 atts = {'style': 'no-colon;'}
294 rv = self.translator.prepare_svg(svg_sample, nodes.image(), atts)
295 self.assertEqual(rv, '<svg style="width: 3em; no-colon: ;" />')
296
297
298 if __name__ == '__main__':
299 unittest.main()