From 96fe4c110c83a699680448130ee8ff720cf8c757 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 22 Nov 2020 19:01:57 +0900 Subject: [PATCH] Fix #8446: html: consecutive spaces are displayed as single space In HTML, consecutive spaces are considered as single space by HTML browsers. To represent them as is, we have to escape them on rendering them into HTML. This starts to escape the whole of desc_signature node. --- CHANGES | 1 + sphinx/writers/html.py | 2 ++ sphinx/writers/html5.py | 2 ++ tests/test_build_html.py | 10 ++++++---- tests/test_ext_intersphinx.py | 4 ++-- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index eda8709b0fe..efd2c7f3804 100644 --- a/CHANGES +++ b/CHANGES @@ -51,6 +51,7 @@ Bugs fixed based uninitalized variables * #8452: autodoc: autodoc_type_aliases doesn't work when autodoc_typehints is set to "description" +* #8446: html: consecutive spaces are displayed as single space * #8419: html search: Do not load ``language_data.js`` in non-search pages * #8454: graphviz: The layout option for graph and digraph directives don't work * #8437: Makefile: ``make clean`` with empty BUILDDIR is dangerous diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index d3c87076f7e..1fb0a9a988f 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -129,8 +129,10 @@ def depart_desc(self, node: Element) -> None: def visit_desc_signature(self, node: Element) -> None: # the id is set automatically self.body.append(self.starttag(node, 'dt')) + self.protect_literal_text += 1 def depart_desc_signature(self, node: Element) -> None: + self.protect_literal_text -= 1 if not node.get('is_multiline'): self.add_permalink_ref(node, _('Permalink to this definition')) self.body.append('\n') diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index 7824f54f5d1..80458f18424 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -100,8 +100,10 @@ def depart_desc(self, node: Element) -> None: def visit_desc_signature(self, node: Element) -> None: # the id is set automatically self.body.append(self.starttag(node, 'dt')) + self.protect_literal_text += 1 def depart_desc_signature(self, node: Element) -> None: + self.protect_literal_text -= 1 if not node.get('is_multiline'): self.add_permalink_ref(node, _('Permalink to this definition')) self.body.append('\n') diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 6b66a2c67dd..aba9f63b5a0 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -177,8 +177,8 @@ def test_html4_output(app, status, warning): ], 'autodoc.html': [ (".//dl[@class='py class']/dt[@id='autodoc_target.Class']", ''), - (".//dl[@class='py function']/dt[@id='autodoc_target.function']/em/span", r'\*\*'), - (".//dl[@class='py function']/dt[@id='autodoc_target.function']/em/span", r'kwds'), + (".//dl[@class='py function']/dt[@id='autodoc_target.function']/em/span/span", r'\*\*'), + (".//dl[@class='py function']/dt[@id='autodoc_target.function']/em/span/span", r'kwds'), (".//dd/p", r'Return spam\.'), ], 'extapi.html': [ @@ -277,8 +277,10 @@ def test_html4_output(app, status, warning): 'objects.html': [ (".//dt[@id='mod.Cls.meth1']", ''), (".//dt[@id='errmod.Error']", ''), - (".//dt/code", r'long\(parameter,\s* list\)'), - (".//dt/code", 'another one'), + (".//dt/code/span", r'long\(parameter,'), + (".//dt/code/span", r'list\)'), + (".//dt/code/span", 'another'), + (".//dt/code/span", 'one'), (".//a[@href='#mod.Cls'][@class='reference internal']", ''), (".//dl[@class='std userdesc']", ''), (".//dt[@id='userdesc-myobj']", ''), diff --git a/tests/test_ext_intersphinx.py b/tests/test_ext_intersphinx.py index d32970c8b80..718bc942234 100644 --- a/tests/test_ext_intersphinx.py +++ b/tests/test_ext_intersphinx.py @@ -250,10 +250,10 @@ def test_missing_reference_cppdomain(tempdir, app, status, warning): 'Bar' in html) assert ('foons' in html) + ' title="(in foo v2.0)">foons' in html) assert ('bartype' in html) + ' title="(in foo v2.0)">bartype' in html) def test_missing_reference_jsdomain(tempdir, app, status, warning):