Repository: sphinx-doc/sphinx

Issue:
Wrong internal href fragment links in epub output
Let's take for example the Django settings documentation (https://docs.djangoproject.com/en/2.0/ref/settings).

In the HTML output, a text like ```:setting:`STATICFILES_FINDERS` ``` is translated to `<a class="reference internal" href="#std:setting-STATICFILES_FINDERS">` and the anchor is indeed matching that link.

In epub output, the link is `<a class="reference internal" href="#std-setting-STA     TICFILES_FINDERS">`, while the anchor is still `<span id="std:setting-STATICFILES_FINDERS">` (like for HTML). Therefore, the link doesn't work, because of the `-`/`:` mismatch in the link name.

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734?src=pr&el=h1) Report
> Merging [#6734](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734?src=pr&el=desc) into [master](https://codecov.io/gh/sphinx-doc/sphinx/commit/618cc26c6463b1ad29a1568bfd123b016fc870c4?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734/graphs/tree.svg?width=650&token=VFlFSELbkw&height=150&src=pr)](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master   #6734      +/-   ##
=========================================
- Coverage   84.41%   84.4%   -0.01%     
=========================================
  Files         266     267       +1     
  Lines       40448   40472      +24     
  Branches     5931    5934       +3     
=========================================
+ Hits        34144   34161      +17     
- Misses       4997    5000       +3     
- Partials     1307    1311       +4
```


| [Impacted Files](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_build\_epub.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9idWlsZF9lcHViLnB5) | `92.82% <100%> (+0.03%)` | :arrow_up: |
| [sphinx/builders/\_epub\_base.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734/diff?src=pr&el=tree#diff-c3BoaW54L2J1aWxkZXJzL19lcHViX2Jhc2UucHk=) | `64.9% <100%> (-0.23%)` | :arrow_down: |
| [sphinx/\_\_init\_\_.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734/diff?src=pr&el=tree#diff-c3BoaW54L19faW5pdF9fLnB5) | `73.91% <0%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734?src=pr&el=footer). Last update [618cc26...68ad00b](https://codecov.io/gh/sphinx-doc/sphinx/pull/6734?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGES b/CHANGES
index cad76cd50..472d20ebe 100644
--- a/CHANGES
+++ b/CHANGES
@@ -33,6 +33,7 @@ Bugs fixed
 * #6584: i18n: Error when compiling message catalogs on Hindi
 * #6708: mathbase: Some deprecated functions have removed
 * #6709: autodoc: mock object does not work as a class decorator
+* #5070: epub: Wrong internal href fragment links
 * #6712: Allow not to install sphinx.testing as runtime (mainly for ALT Linux)
 
 Testing
diff --git a/sphinx/builders/_epub_base.py b/sphinx/builders/_epub_base.py
index 75e79936b..9bdf856a6 100644
--- a/sphinx/builders/_epub_base.py
+++ b/sphinx/builders/_epub_base.py
@@ -272,7 +272,7 @@ def fix_ids(self, tree: nodes.document) -> None:
                 if ':' in node_id:
                     target['ids'][i] = self.fix_fragment('', node_id)
 
-            next_node = target.next_node(siblings=True)  # type: Node
+            next_node = target.next_node(ascend=True)  # type: Node
             if isinstance(next_node, nodes.Element):
                 for i, node_id in enumerate(next_node['ids']):
                     if ':' in node_id:


Test patch:
diff --git a/tests/roots/test-epub-anchor-id/index.rst b/tests/roots/test-epub-anchor-id/index.rst
index f3064615c..75e3c6054 100644
--- a/tests/roots/test-epub-anchor-id/index.rst
+++ b/tests/roots/test-epub-anchor-id/index.rst
@@ -5,4 +5,9 @@ test-epub-anchor-id
 
 blah blah blah
 
+.. setting:: STATICFILES_SECTION
+
+blah blah blah
+==============
+
 see :setting:`STATICFILES_FINDERS`
diff --git a/tests/test_build_epub.py b/tests/test_build_epub.py
index bace00ea7..9436ac020 100644
--- a/tests/test_build_epub.py
+++ b/tests/test_build_epub.py
@@ -321,6 +321,7 @@ def test_epub_anchor_id(app):
 
     html = (app.outdir / 'index.xhtml').text()
     assert '<p id="std-setting-STATICFILES_FINDERS">blah blah blah</p>' in html
+    assert '<span id="std-setting-STATICFILES_SECTION"></span><h1>blah blah blah</h1>' in html
     assert 'see <a class="reference internal" href="#std-setting-STATICFILES_FINDERS">' in html