Skip to content

Commit 1226761

Browse files
committed
fixes #571
1 parent fe78c1c commit 1226761

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

fastcore/xml.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
# %% ../nbs/11_xml.ipynb 2
1313
from .utils import *
1414

15+
import types,json
16+
1517
from dataclasses import dataclass, asdict
16-
import types
18+
from typing import Mapping
1719
from functools import partial
1820
from html import escape
1921

@@ -37,7 +39,7 @@ def attrs(self): return self[2]
3739
def xt(tag:str, *c, **kw):
3840
"Create an XML tag structure `[tag,children,attrs]` for `toxml()`"
3941
if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])
40-
kw = {_attrmap(k):(v if isinstance(v,bool) else str(v)) for k,v in kw.items() if v is not None}
42+
kw = {_attrmap(k):v for k,v in kw.items() if v is not None}
4143
return XT(tag.lower(),c,kw)
4244

4345
# %% ../nbs/11_xml.ipynb 7
@@ -65,7 +67,12 @@ def _to_attr(k,v):
6567
if isinstance(v,bool):
6668
if v==True : return str(k)
6769
if v==False: return ''
68-
return f'{k}="{escape(str(v), quote=True)}"'
70+
if isinstance(v,str): v = escape(v, quote=True)
71+
elif isinstance(v, Mapping): v = json.dumps(v)
72+
else: v = str(v)
73+
qt = '"'
74+
if qt in v: qt = "'"
75+
return f'{k}={qt}{v}{qt}'
6976

7077
# %% ../nbs/11_xml.ipynb 15
7178
def to_xml(elm, lvl=0):

nbs/11_xml.ipynb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
"#| export\n",
3131
"from fastcore.utils import *\n",
3232
"\n",
33+
"import types,json\n",
34+
"\n",
3335
"from dataclasses import dataclass, asdict\n",
34-
"import types\n",
36+
"from typing import Mapping\n",
3537
"from functools import partial\n",
3638
"from html import escape"
3739
]
@@ -90,7 +92,7 @@
9092
"def xt(tag:str, *c, **kw):\n",
9193
" \"Create an XML tag structure `[tag,children,attrs]` for `toxml()`\"\n",
9294
" if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])\n",
93-
" kw = {_attrmap(k):(v if isinstance(v,bool) else str(v)) for k,v in kw.items() if v is not None}\n",
95+
" kw = {_attrmap(k):v for k,v in kw.items() if v is not None}\n",
9496
" return XT(tag.lower(),c,kw)"
9597
]
9698
},
@@ -142,7 +144,7 @@
142144
" (['div',\n",
143145
" ('Some text',\n",
144146
" ['input', (), {'name': 'me'}],\n",
145-
" ['img', (), {'src': 'filename'}]),\n",
147+
" ['img', (), {'data': {'a': 1}, 'src': 'filename'}]),\n",
146148
" {'class': 'myclass'}],),\n",
147149
" {}]),\n",
148150
" {}]\n"
@@ -152,7 +154,7 @@
152154
"source": [
153155
"samp = Html(\n",
154156
" Head(Title('Some page')),\n",
155-
" Body(Div('Some text', Input(name='me'), Img(src=\"filename\"), klass='myclass'))\n",
157+
" Body(Div('Some text', Input(name='me'), Img(src=\"filename\", data={'a':1}), klass='myclass'))\n",
156158
")\n",
157159
"pprint(samp)"
158160
]
@@ -222,7 +224,12 @@
222224
" if isinstance(v,bool):\n",
223225
" if v==True : return str(k)\n",
224226
" if v==False: return ''\n",
225-
" return f'{k}=\"{escape(str(v), quote=True)}\"'"
227+
" if isinstance(v,str): v = escape(v, quote=True)\n",
228+
" elif isinstance(v, Mapping): v = json.dumps(v)\n",
229+
" else: v = str(v)\n",
230+
" qt = '\"'\n",
231+
" if qt in v: qt = \"'\"\n",
232+
" return f'{k}={qt}{v}{qt}'"
226233
]
227234
},
228235
{
@@ -275,7 +282,7 @@
275282
" <div class=\"myclass\">\n",
276283
"Some text\n",
277284
" <input name=\"me\">\n",
278-
" <img src=\"filename\">\n",
285+
" <img src=\"filename\" data='{\"a\": 1}'>\n",
279286
" </div>\n",
280287
" </body>\n",
281288
"</html>\n",

0 commit comments

Comments
 (0)