diff --git a/sphinx/addnodes.py b/sphinx/addnodes.py index 2ebc1432ab6..788f9bfa25a 100644 --- a/sphinx/addnodes.py +++ b/sphinx/addnodes.py @@ -119,6 +119,19 @@ def extract_original_messages(self) -> List[str]: # Domain-specific object descriptions (class, function etc.) ############################################################# +class _desc_classes_injector: + """Helper base class for injecting a fixes list of classes. + + Use as the first base class. + """ + + classes = [] # type: List[str] + + def __init__(self, *args, **kwargs) -> None: + super().__init__(*args, **kwargs) + self['classes'].extend(self.classes) + + # Top-level nodes ################# @@ -175,20 +188,26 @@ class desc_content(nodes.General, nodes.Element): # nodes to use within a desc_signature or desc_signature_line -class desc_name(nodes.Part, nodes.Inline, nodes.FixedTextElement): +class desc_name(_desc_classes_injector, nodes.Part, nodes.Inline, nodes.FixedTextElement): """Node for the main object name. For example, in the declaration of a Python class ``MyModule.MyClass``, the main name is ``MyClass``. + + This node always has the class ``sig-name``. """ + classes = ['sig-name', 'descname'] # 'descname' is for backwards compatibility -class desc_addname(nodes.Part, nodes.Inline, nodes.FixedTextElement): +class desc_addname(_desc_classes_injector, nodes.Part, nodes.Inline, nodes.FixedTextElement): """Node for additional name parts for an object. For example, in the declaration of a Python class ``MyModule.MyClass``, the additional name part is ``MyModule.``. + + This node always has the class ``sig-prename``. """ + classes = ['sig-prename', 'descclassname'] # 'descclassname' is for backwards compatibility # compatibility alias @@ -237,7 +256,7 @@ class desc_annotation(nodes.Part, nodes.Inline, nodes.FixedTextElement): # in SigElementFallbackTransform. # When adding a new one, add it to SIG_ELEMENTS. -class desc_sig_element(nodes.inline): +class desc_sig_element(nodes.inline, _desc_classes_injector): """Common parent class of nodes for inline text of a signature.""" classes = [] # type: List[str] diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 52ab359a7f6..1823ed45cc7 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -149,8 +149,14 @@ def depart_desc_content(self, node: Element) -> None: # Nodes for high-level structure in signatures ############################################## + def visit_desc_name(self, node: Element) -> None: + self.body.append(self.starttag(node, 'code', '')) + + def depart_desc_name(self, node: Element) -> None: + self.body.append('') + def visit_desc_addname(self, node: Element) -> None: - self.body.append(self.starttag(node, 'code', '', CLASS='descclassname')) + self.body.append(self.starttag(node, 'code', '')) def depart_desc_addname(self, node: Element) -> None: self.body.append('') @@ -167,12 +173,6 @@ def visit_desc_returns(self, node: Element) -> None: def depart_desc_returns(self, node: Element) -> None: pass - def visit_desc_name(self, node: Element) -> None: - self.body.append(self.starttag(node, 'code', '', CLASS='descname')) - - def depart_desc_name(self, node: Element) -> None: - self.body.append('') - def visit_desc_parameterlist(self, node: Element) -> None: self.body.append('(') self.first_param = 1 diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index 9835a9ee84e..3b2523fddb8 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -121,13 +121,13 @@ def depart_desc_content(self, node: Element) -> None: ############################################## def visit_desc_name(self, node: Element) -> None: - self.body.append(self.starttag(node, 'code', '', CLASS='sig-name descname')) + self.body.append(self.starttag(node, 'code', '')) def depart_desc_name(self, node: Element) -> None: self.body.append('') def visit_desc_addname(self, node: Element) -> None: - self.body.append(self.starttag(node, 'code', '', CLASS='sig-prename descclassname')) + self.body.append(self.starttag(node, 'code', '')) def depart_desc_addname(self, node: Element) -> None: self.body.append('')