Describe the bug
Using the singlehtml builder to generate index.html from the example below produces invalid internal HTML anchors having multiple # characters in them with certain combinations of glossaries and toctree inclusions. In the reproducer below, there is an index.rst file with a toctree that includes two glossary files in separate documents, each with their own glossary item definition. An equivalent behavior can be observed also by using .. include to include the files directly. The build command is sphinx-build -b singlehtml . _build/foo and the HTML file is tested with html5validator --root _build/foo --show-warnings. Issue #13037 might be related.
How to Reproduce
conf.py:
project = 'Glossary Test'
extensions = []
index.rst:
Main Documentation
==================
Welcome to the documentation.
.. toctree::
:maxdepth: 2
ch1
glossary/term1
glossary/term2
with the glossary files being trivial items:
.. glossary::
API
Lorem Ipsum.
The generate document contains:
<a class="reference internal" href="#document-glossary/term1#term-API"><span class="xref std std-term">API</span></a>
Environment Information
Platform: linux; (Linux-6.14.0-37-generic-x86_64-with-glibc2.39)
Python version: 3.12.3 (main, Nov 6 2025, 13:44:16) [GCC 13.3.0])
Python implementation: CPython
Sphinx version: 9.0.4
Docutils version: 0.21.2
Jinja2 version: 3.1.4
Pygments version: 2.19.2
Sphinx extensions
Additional context
Some build warnings are generated, but they should not affect the outcome of the test
writing... /home/nicola/test-sphinx/index.rst:6: WARNING: toctree contains reference to document 'glossary/term1' that doesn't have a title: no link will be generated [toc.no_title]
/home/nicola/test-sphinx/index.rst:6: WARNING: toctree contains reference to document 'glossary/term2' that doesn't have a title: no link will be generated [toc.no_title]
/home/nicola/test-sphinx/index.rst:6: WARNING: toctree contains reference to document 'glossary/term1' that doesn't have a title: no link will be generated [toc.no_title]
/home/nicola/test-sphinx/index.rst:6: WARNING: toctree contains reference to document 'glossary/term2' that doesn't have a title: no link will be generated [toc.no_title]
Describe the bug
Using the singlehtml builder to generate index.html from the example below produces invalid internal HTML anchors having multiple
#characters in them with certain combinations of glossaries and toctree inclusions. In the reproducer below, there is anindex.rstfile with a toctree that includes two glossary files in separate documents, each with their own glossary item definition. An equivalent behavior can be observed also by using.. includeto include the files directly. The build command issphinx-build -b singlehtml . _build/fooand the HTML file is tested withhtml5validator --root _build/foo --show-warnings. Issue #13037 might be related.How to Reproduce
conf.py:
index.rst:
with the glossary files being trivial items:
The generate document contains:
Environment Information
Sphinx extensions
NoneAdditional context
Some build warnings are generated, but they should not affect the outcome of the test