Skip to content

Commit 5d6ae35

Browse files
authored
Pass queries and fragments to redirection (#16)
* Pass queries and fragments to redirection * Add tests * Add dirhtml tests
1 parent 84aa2a3 commit 5d6ae35

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

sphinxext/rediraffe.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@
2121
"""
2222
<html>
2323
<head>
24-
<meta http-equiv="refresh" content="0; url={{rel_url}}"/>
24+
<noscript>
25+
<meta http-equiv="refresh" content="0; url={{rel_url}}"/>
26+
</noscript>
2527
</head>
2628
<body>
2729
<p>You should have been redirected.</p>
2830
<a href="{{rel_url}}">If not, click here to continue.</a>
31+
<script>
32+
window.location.href = '{{rel_url}}' + (window.location.search || '') + (window.location.hash || '');
33+
</script>
2934
</body>
3035
</html>
3136
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extensions = ["sphinxext.rediraffe"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
8+
rediraffe_redirects = "redirects.txt"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Index File
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
another.rst index.rst

tests/test_ext.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,55 @@ def test_jinja_bad_path(self, app: Sphinx, ensure_redirect):
209209

210210
ensure_redirect("another.html", "index.html")
211211

212+
@pytest.mark.sphinx("html", testroot="pass_url_fragments_queries")
213+
def test_pass_url_fragments(self, app: Sphinx, _sb: BaseCase, ensure_redirect):
214+
app.build()
215+
216+
ensure_redirect("another.html", "index.html")
217+
_sb.open(rel2url(app.outdir, "another.html") + "#haha")
218+
# check url
219+
assert Path(rel2url(app.outdir, "index.html")) == Path(
220+
_sb.execute_script(
221+
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
222+
)
223+
)
224+
# check hash
225+
assert "#haha" == _sb.execute_script("return window.location.hash")
226+
227+
@pytest.mark.sphinx("html", testroot="pass_url_fragments_queries")
228+
def test_pass_url_queries(self, app: Sphinx, _sb: BaseCase, ensure_redirect):
229+
app.build()
230+
231+
ensure_redirect("another.html", "index.html")
232+
_sb.open(rel2url(app.outdir, "another.html") + "?phrase=haha")
233+
# check url
234+
assert Path(rel2url(app.outdir, "index.html")) == Path(
235+
_sb.execute_script(
236+
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
237+
)
238+
)
239+
# check query
240+
assert "?phrase=haha" == _sb.execute_script("return window.location.search")
241+
242+
@pytest.mark.sphinx("html", testroot="pass_url_fragments_queries")
243+
def test_pass_url_fragment_and_query(
244+
self, app: Sphinx, _sb: BaseCase, ensure_redirect
245+
):
246+
app.build()
247+
248+
ensure_redirect("another.html", "index.html")
249+
_sb.open(rel2url(app.outdir, "another.html") + "?phrase=haha#giraffe")
250+
# check url
251+
assert Path(rel2url(app.outdir, "index.html")) == Path(
252+
_sb.execute_script(
253+
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
254+
)
255+
)
256+
# check query
257+
assert "?phrase=haha" == _sb.execute_script("return window.location.search")
258+
# check hash
259+
assert "#giraffe" == _sb.execute_script("return window.location.hash")
260+
212261

213262
class TestExtDirHtml:
214263
@pytest.mark.sphinx("dirhtml", testroot="no_redirects")
@@ -402,3 +451,52 @@ def test_jinja_bad_path(self, app: Sphinx, ensure_redirect):
402451
assert app.statuscode == 0
403452

404453
ensure_redirect("another/index.html", "index.html")
454+
455+
@pytest.mark.sphinx("dirhtml", testroot="pass_url_fragments_queries")
456+
def test_pass_url_fragments(self, app: Sphinx, _sb: BaseCase, ensure_redirect):
457+
app.build()
458+
459+
ensure_redirect("another/index.html", "index.html")
460+
_sb.open(rel2url(app.outdir, "another/index.html") + "#haha")
461+
# check url
462+
assert Path(rel2url(app.outdir, "index.html")) == Path(
463+
_sb.execute_script(
464+
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
465+
)
466+
)
467+
# check hash
468+
assert "#haha" == _sb.execute_script("return window.location.hash")
469+
470+
@pytest.mark.sphinx("dirhtml", testroot="pass_url_fragments_queries")
471+
def test_pass_url_queries(self, app: Sphinx, _sb: BaseCase, ensure_redirect):
472+
app.build()
473+
474+
ensure_redirect("another/index.html", "index.html")
475+
_sb.open(rel2url(app.outdir, "another/index.html") + "?phrase=haha")
476+
# check url
477+
assert Path(rel2url(app.outdir, "index.html")) == Path(
478+
_sb.execute_script(
479+
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
480+
)
481+
)
482+
# check query
483+
assert "?phrase=haha" == _sb.execute_script("return window.location.search")
484+
485+
@pytest.mark.sphinx("dirhtml", testroot="pass_url_fragments_queries")
486+
def test_pass_url_fragment_and_query(
487+
self, app: Sphinx, _sb: BaseCase, ensure_redirect
488+
):
489+
app.build()
490+
491+
ensure_redirect("another/index.html", "index.html")
492+
_sb.open(rel2url(app.outdir, "another/index.html") + "?phrase=haha#giraffe")
493+
# check url
494+
assert Path(rel2url(app.outdir, "index.html")) == Path(
495+
_sb.execute_script(
496+
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
497+
)
498+
)
499+
# check query
500+
assert "?phrase=haha" == _sb.execute_script("return window.location.search")
501+
# check hash
502+
assert "#giraffe" == _sb.execute_script("return window.location.hash")

0 commit comments

Comments
 (0)