Skip to content

Commit 9994651

Browse files
authored
Merge pull request #797 from AnswerDotAI/html2ft_space
only strip newlines when parsing strings
2 parents bc64dc2 + ea012f9 commit 9994651

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

fasthtml/components.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,14 @@ def _f(*c, target_id=None, **kwargs): return ft_hx(tag, *c, target_id=target_id,
207207
def html2ft(html, attr1st=False):
208208
"""Convert HTML to an `ft` expression"""
209209
rev_map = {'class': 'cls', 'for': 'fr'}
210-
210+
211211
def _parse(elm, lvl=0, indent=4):
212-
if isinstance(elm, str): return repr(elm.strip()) if elm.strip() else ''
212+
if isinstance(elm, str): return repr(elm.strip("\n")) if elm.strip() else ''
213213
if isinstance(elm, list): return '\n'.join(_parse(o, lvl) for o in elm)
214214
tag_name = elm.name.capitalize().replace("-", "_")
215215
if tag_name=='[document]': return _parse(list(elm.children), lvl)
216216
cts = elm.contents
217-
cs = [repr(c.strip()) if isinstance(c, str) else _parse(c, lvl+1)
217+
cs = [repr(c.strip("\n")) if isinstance(c, str) else _parse(c, lvl+1)
218218
for c in cts if str(c).strip()]
219219
attrs, exotic_attrs = [], {}
220220
for key, value in sorted(elm.attrs.items(), key=lambda x: x[0]=='class'):

nbs/api/01_components.ipynb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,14 +1120,14 @@
11201120
"def html2ft(html, attr1st=False):\n",
11211121
" \"\"\"Convert HTML to an `ft` expression\"\"\"\n",
11221122
" rev_map = {'class': 'cls', 'for': 'fr'}\n",
1123-
" \n",
1123+
"\n",
11241124
" def _parse(elm, lvl=0, indent=4):\n",
1125-
" if isinstance(elm, str): return repr(elm.strip()) if elm.strip() else ''\n",
1125+
" if isinstance(elm, str): return repr(elm.strip(\"\\n\")) if elm.strip() else ''\n",
11261126
" if isinstance(elm, list): return '\\n'.join(_parse(o, lvl) for o in elm)\n",
11271127
" tag_name = elm.name.capitalize().replace(\"-\", \"_\")\n",
11281128
" if tag_name=='[document]': return _parse(list(elm.children), lvl)\n",
11291129
" cts = elm.contents\n",
1130-
" cs = [repr(c.strip()) if isinstance(c, str) else _parse(c, lvl+1)\n",
1130+
" cs = [repr(c.strip(\"\\n\")) if isinstance(c, str) else _parse(c, lvl+1)\n",
11311131
" for c in cts if str(c).strip()]\n",
11321132
" attrs, exotic_attrs = [], {}\n",
11331133
" for key, value in sorted(elm.attrs.items(), key=lambda x: x[0]=='class'):\n",
@@ -1299,7 +1299,9 @@
12991299
"#|hide\n",
13001300
"def test_html2ft(html: str, attr1st=False):\n",
13011301
" # html -> ft -> html\n",
1302-
" assert html == to_xml(eval(html2ft(html, attr1st))).strip()"
1302+
" soup1 = BeautifulSoup(html, 'html.parser')\n",
1303+
" soup2 = BeautifulSoup(to_xml(eval(html2ft(html, attr1st))).strip(), 'html.parser')\n",
1304+
" assert soup1.prettify() == soup2.prettify()\n"
13031305
]
13041306
},
13051307
{
@@ -1313,6 +1315,7 @@
13131315
"test_html2ft('<input value=\"Profit\" name=\"title\" id=\"title\" class=\"char\">')\n",
13141316
"test_html2ft('<div id=\"foo\"></div>')\n",
13151317
"test_html2ft('<div id=\"foo\">hi</div>')\n",
1318+
"test_html2ft('<div>Howdy <a href=\"https://answer.ai\">answer</a> how are you?</div>')\n",
13161319
"test_html2ft('<div x-show=\"open\" x-transition:enter=\"transition duration-300\" x-transition:enter-start=\"opacity-0 scale-90\">Hello 👋</div>')\n",
13171320
"test_html2ft('<div x-transition:enter.scale.80 x-transition:leave.scale.90>hello</div>')"
13181321
]
@@ -1361,13 +1364,7 @@
13611364
"source": []
13621365
}
13631366
],
1364-
"metadata": {
1365-
"kernelspec": {
1366-
"display_name": "python3",
1367-
"language": "python",
1368-
"name": "python3"
1369-
}
1370-
},
1367+
"metadata": {},
13711368
"nbformat": 4,
13721369
"nbformat_minor": 5
13731370
}

0 commit comments

Comments
 (0)