Skip to content

Commit dc4dff9

Browse files
committed
fixes #551
1 parent c35e05b commit dc4dff9

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

fastcore/xml.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.
22

33
# %% auto 0
4-
__all__ = ['xt', 'to_xml', 'Html', 'Head', 'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code', 'Div', 'Span', 'P', 'H1',
5-
'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B', 'I', 'U', 'S', 'Strike', 'Sub', 'Sup', 'Hr', 'Br', 'Img',
6-
'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl', 'Dt', 'Dd', 'Table', 'Thead', 'Tbody', 'Tfoot', 'Tr', 'Th', 'Td',
4+
__all__ = ['voids', 'xt', 'to_xml', 'Html', 'Head', 'Title', 'Meta', 'Link', 'Style', 'Body', 'Pre', 'Code', 'Div', 'Span', 'P',
5+
'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B', 'I', 'U', 'S', 'Strike', 'Sub', 'Sup', 'Hr', 'Br',
6+
'Img', 'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl', 'Dt', 'Dd', 'Table', 'Thead', 'Tbody', 'Tfoot', 'Tr', 'Th', 'Td',
77
'Caption', 'Col', 'Colgroup', 'Form', 'Input', 'Textarea', 'Button', 'Select', 'Option', 'Label', 'Fieldset',
88
'Legend', 'Details', 'Summary', 'Main', 'Header', 'Footer', 'Section', 'Article', 'Aside', 'Figure',
99
'Figcaption', 'Mark', 'Small', 'Iframe', 'Object', 'Embed', 'Param', 'Video', 'Audio', 'Source', 'Canvas',
@@ -43,6 +43,9 @@ def xt(tag:str, *c, **kw):
4343
for o in _all_: _g[o] = partial(xt, o.lower())
4444

4545
# %% ../nbs/11_xml.ipynb 9
46+
voids = set('area base br col command embed hr img input keygen link meta param source track wbr'.split())
47+
48+
# %% ../nbs/11_xml.ipynb 10
4649
def to_xml(elm, lvl=0):
4750
"Convert `xt` element tree into an XML string"
4851
if isinstance(elm, tuple): return '\n'.join(to_xml(o) for o in elm)
@@ -58,8 +61,9 @@ def to_xml(elm, lvl=0):
5861
sattrs = (f'{k}="{escape(str(v), quote=False)}"' for k,v in attrs.items())
5962
stag += ' ' + ' '.join(sattrs)
6063

61-
if not cs: return f'{sp}<{stag}></{tag}>\n'
64+
cltag = '' if tag in voids else f'</{tag}>'
65+
if not cs: return f'{sp}<{stag}>{cltag}\n'
6266
res = f'{sp}<{stag}>\n'
6367
res += ''.join(to_xml(c, lvl=lvl+2) for c in cs)
64-
res += f'{sp}</{tag}>\n'
68+
if tag not in voids: res += f'{sp}{cltag}\n'
6569
return res

nbs/11_xml.ipynb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@
118118
" (['head', (['title', ('Some page',), {}],), {}],\n",
119119
" ['body',\n",
120120
" (['div',\n",
121-
" (['p', ('Some text',), {}], ['img', (), {'src': 'filename'}]),\n",
121+
" (['p', ('Some text',), {}],\n",
122+
" ['input', (), {'name': 'me'}],\n",
123+
" ['img', (), {'src': 'filename'}]),\n",
122124
" {'class': 'myclass'}],),\n",
123125
" {}]),\n",
124126
" {}]"
@@ -132,11 +134,22 @@
132134
"source": [
133135
"samp = Html(\n",
134136
" Head(Title('Some page')),\n",
135-
" Body(Div(P('Some text'), Img(src=\"filename\"), klass='myclass'))\n",
137+
" Body(Div(P('Some text'), Input(name='me'), Img(src=\"filename\"), klass='myclass'))\n",
136138
")\n",
137139
"samp"
138140
]
139141
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": null,
145+
"id": "c7de63a4",
146+
"metadata": {},
147+
"outputs": [],
148+
"source": [
149+
"#| export\n",
150+
"voids = set('area base br col command embed hr img input keygen link meta param source track wbr'.split())"
151+
]
152+
},
140153
{
141154
"cell_type": "code",
142155
"execution_count": null,
@@ -160,10 +173,11 @@
160173
" sattrs = (f'{k}=\"{escape(str(v), quote=False)}\"' for k,v in attrs.items())\n",
161174
" stag += ' ' + ' '.join(sattrs)\n",
162175
" \n",
163-
" if not cs: return f'{sp}<{stag}></{tag}>\\n'\n",
176+
" cltag = '' if tag in voids else f'</{tag}>'\n",
177+
" if not cs: return f'{sp}<{stag}>{cltag}\\n'\n",
164178
" res = f'{sp}<{stag}>\\n'\n",
165179
" res += ''.join(to_xml(c, lvl=lvl+2) for c in cs)\n",
166-
" res += f'{sp}</{tag}>\\n'\n",
180+
" if tag not in voids: res += f'{sp}{cltag}\\n'\n",
167181
" return res"
168182
]
169183
},
@@ -188,7 +202,8 @@
188202
" <p>\n",
189203
"Some text\n",
190204
" </p>\n",
191-
" <img src=\"filename\"></img>\n",
205+
" <input name=\"me\">\n",
206+
" <img src=\"filename\">\n",
192207
" </div>\n",
193208
" </body>\n",
194209
"</html>\n",

0 commit comments

Comments
 (0)