Skip to content

Commit f5e2375

Browse files
committed
fix: title override and add tests
- Fix _xt_cts to respect manually passed Title components - Add tests for default title - Add tests for custom title - Add tests for title override behavior
1 parent d15379f commit f5e2375

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

fasthtml/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ def _xt_cts(req, resp):
392392
hdr_tags = 'title','meta','link','style','base'
393393
heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)
394394
if resp and 'hx-request' not in req.headers and not any(getattr(o, 'tag', '')=='html' for o in resp):
395-
resp = respond(req, heads or [Title(req.app.title)], bdy)
395+
title = [] if any(getattr(o, 'tag', '')=='title' for o in heads) else [Title(req.app.title)]
396+
resp = respond(req, [*heads, *title], bdy)
396397
return _to_xml(req, resp, indent=fh_cfg.indent), http_hdrs, ts
397398

398399
# %% ../nbs/api/00_core.ipynb

nbs/api/00_core.ipynb

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2024, 12, 10, 14, 0)"
134+
"datetime.datetime(2024, 12, 14, 14, 0)"
135135
]
136136
},
137137
"execution_count": null,
@@ -1085,7 +1085,8 @@
10851085
" hdr_tags = 'title','meta','link','style','base'\n",
10861086
" heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)\n",
10871087
" if resp and 'hx-request' not in req.headers and not any(getattr(o, 'tag', '')=='html' for o in resp):\n",
1088-
" resp = respond(req, heads or [Title(req.app.title)], bdy)\n",
1088+
" title = [] if any(getattr(o, 'tag', '')=='title' for o in heads) else [Title(req.app.title)]\n",
1089+
" resp = respond(req, [*heads, *title], bdy)\n",
10891090
" return _to_xml(req, resp, indent=fh_cfg.indent), http_hdrs, ts"
10901091
]
10911092
},
@@ -1229,7 +1230,7 @@
12291230
{
12301231
"data": {
12311232
"text/plain": [
1232-
"'7eb16e0e-f1b9-4266-98fa-2d84c34f86fc'"
1233+
"'af1b5dfe-6a32-4348-b1de-692f979f20fb'"
12331234
]
12341235
},
12351236
"execution_count": null,
@@ -1667,6 +1668,59 @@
16671668
"app,cli,rt = get_cli(FastHTML(secret_key='soopersecret'))"
16681669
]
16691670
},
1671+
{
1672+
"cell_type": "code",
1673+
"execution_count": null,
1674+
"id": "421262a8",
1675+
"metadata": {},
1676+
"outputs": [
1677+
{
1678+
"name": "stdout",
1679+
"output_type": "stream",
1680+
"text": [
1681+
"[Route(path='/foo', name='foo', methods=['GET', 'HEAD'])]\n"
1682+
]
1683+
},
1684+
{
1685+
"data": {
1686+
"text/plain": [
1687+
"'/foo?param=value'"
1688+
]
1689+
},
1690+
"execution_count": null,
1691+
"metadata": {},
1692+
"output_type": "execute_result"
1693+
}
1694+
],
1695+
"source": [
1696+
"app,cli,rt = get_cli(FastHTML(title=\"My Custom Title\"))\n",
1697+
"@app.get\n",
1698+
"def foo(): return Div(\"Hello World\")\n",
1699+
"\n",
1700+
"print(app.routes)\n",
1701+
"\n",
1702+
"response = cli.get('/foo')\n",
1703+
"assert '<title>My Custom Title</title>' in response.text\n",
1704+
"\n",
1705+
"foo.to(param='value')"
1706+
]
1707+
},
1708+
{
1709+
"cell_type": "code",
1710+
"execution_count": null,
1711+
"id": "2ebd6270",
1712+
"metadata": {},
1713+
"outputs": [],
1714+
"source": [
1715+
"app,cli,rt = get_cli(FastHTML())\n",
1716+
"\n",
1717+
"@rt('/xt2')\n",
1718+
"def get(): return H1('bar')\n",
1719+
"\n",
1720+
"txt = cli.get('/xt2').text\n",
1721+
"assert '<title>FastHTML page</title>' in txt and '<h1>bar</h1>' in txt and '<html>' in txt"
1722+
]
1723+
},
16701724
{
16711725
"cell_type": "code",
16721726
"execution_count": null,
@@ -2424,13 +2478,13 @@
24242478
"name": "stdout",
24252479
"output_type": "stream",
24262480
"text": [
2427-
"Set to 2024-12-10 09:54:42.331681\n"
2481+
"Set to 2024-12-14 12:38:57.886589\n"
24282482
]
24292483
},
24302484
{
24312485
"data": {
24322486
"text/plain": [
2433-
"'Session time: 2024-12-10 09:54:42.331681'"
2487+
"'Session time: 2024-12-14 12:38:57.886589'"
24342488
]
24352489
},
24362490
"execution_count": null,
@@ -2950,7 +3004,7 @@
29503004
{
29513005
"data": {
29523006
"text/plain": [
2953-
"'Cookie was set at time 09:54:43.255286'"
3007+
"'Cookie was set at time 12:38:58.050326'"
29543008
]
29553009
},
29563010
"execution_count": null,

0 commit comments

Comments
 (0)