diff --git a/CHANGES b/CHANGES index 4343ffb3e..b7feb8fa1 100644 --- a/CHANGES +++ b/CHANGES @@ -15,6 +15,9 @@ Incompatible changes * #9672: the signature of :py:meth:`domains.py.PyObject.get_signature_prefix` has changed to return a list of nodes instead of a plain string. +* ``domains.js.JSObject.display_prefix`` has been changed into a method + ``get_display_prefix`` which now returns a list of nodes + instead of a plain string. Deprecated ---------- diff --git a/sphinx/domains/javascript.py b/sphinx/domains/javascript.py index 565d681dc..7abfad482 100644 --- a/sphinx/domains/javascript.py +++ b/sphinx/domains/javascript.py @@ -41,9 +41,6 @@ class JSObject(ObjectDescription[Tuple[str, str]]): #: added has_arguments = False - #: what is displayed right before the documentation entry - display_prefix: str = None - #: If ``allow_nesting`` is ``True``, the object prefixes will be accumulated #: based on directive nesting allow_nesting = False @@ -53,6 +50,10 @@ class JSObject(ObjectDescription[Tuple[str, str]]): 'noindexentry': directives.flag, } + def get_display_prefix(self) -> List[nodes.Node]: + #: what is displayed right before the documentation entry + return [] + def handle_signature(self, sig: str, signode: desc_signature) -> Tuple[str, str]: """Breaks down construct signatures @@ -91,9 +92,9 @@ class JSObject(ObjectDescription[Tuple[str, str]]): signode['object'] = prefix signode['fullname'] = fullname - if self.display_prefix: - signode += addnodes.desc_annotation(self.display_prefix, - self.display_prefix) + display_prefix = self.get_display_prefix() + if display_prefix: + signode += addnodes.desc_annotation('', '', *display_prefix) if prefix: signode += addnodes.desc_addname(prefix + '.', prefix + '.') elif mod_name: @@ -227,9 +228,13 @@ class JSCallable(JSObject): class JSConstructor(JSCallable): """Like a callable but with a different prefix.""" - display_prefix = 'class ' + allow_nesting = True + def get_display_prefix(self) -> List[nodes.Node]: + return [addnodes.desc_sig_keyword('class', 'class'), + addnodes.desc_sig_space()] + class JSModule(SphinxDirective): """ diff --git a/tests/test_domain_js.py b/tests/test_domain_js.py index 1fb865d4b..535321d9f 100644 --- a/tests/test_domain_js.py +++ b/tests/test_domain_js.py @@ -15,7 +15,8 @@ from docutils import nodes from sphinx import addnodes from sphinx.addnodes import (desc, desc_annotation, desc_content, desc_name, desc_parameter, - desc_parameterlist, desc_signature) + desc_parameterlist, desc_sig_keyword, desc_sig_space, + desc_signature) from sphinx.domains.javascript import JavaScriptDomain from sphinx.testing import restructuredtext from sphinx.testing.util import assert_node @@ -198,7 +199,8 @@ def test_js_class(app): text = ".. js:class:: Application" doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ([desc_sig_keyword, 'class'], + desc_sig_space)], [desc_name, "Application"], [desc_parameterlist, ()])], [desc_content, ()])]))