Skip to content

Commit 307486c

Browse files
authored
Merge pull request #709 from AnswerDotAI/erikgaas/xml_null_values
Zero and empty string treated as nullable for xml creation
2 parents 045f35c + 5b8ca61 commit 307486c

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

fastcore/xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _to_xml(elm, lvl=0, indent=True, do_escape=True):
188188

189189
stag = tag
190190
if attrs:
191-
sattrs = ' '.join(_to_attr(k, v) for k, v in attrs.items() if v not in (False, None, '') and (k=='_' or k[-1]!='_'))
191+
sattrs = ' '.join(_to_attr(k, v) for k, v in attrs.items() if v is not False and v is not None and (k=='_' or k[-1]!='_'))
192192
if sattrs: stag += f' {sattrs}'
193193

194194
cltag = '' if is_void else f'</{tag}>'

nbs/09_xml.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@
440440
"\n",
441441
" stag = tag\n",
442442
" if attrs:\n",
443-
" sattrs = ' '.join(_to_attr(k, v) for k, v in attrs.items() if v not in (False, None, '') and (k=='_' or k[-1]!='_'))\n",
443+
" sattrs = ' '.join(_to_attr(k, v) for k, v in attrs.items() if v is not False and v is not None and (k=='_' or k[-1]!='_'))\n",
444444
" if sattrs: stag += f' {sattrs}'\n",
445445
"\n",
446446
" cltag = '' if is_void else f'</{tag}>'\n",
@@ -511,7 +511,9 @@
511511
" \"<div>&lt;script&gt;alert('XSS')&lt;/script&gt;</div>\\n\")\n",
512512
"test_eq(to_xml(Div(\"<script>alert('XSS')</script>\"), do_escape=False),\n",
513513
" \"<div><script>alert('XSS')</script></div>\\n\")\n",
514-
"test_eq(to_xml(Div(foo=False), indent=False), '<div></div>')"
514+
"test_eq(to_xml(Div(foo=False), indent=False), '<div></div>')\n",
515+
"test_eq(to_xml(Input(type=\"number\", min=0, max=100)), '<input type=\"number\" min=\"0\" max=\"100\">\\n')\n",
516+
"test_eq(to_xml(Option(\"select a value\", value=\"\")), '<option value=\"\">select a value</option>')"
515517
]
516518
},
517519
{

0 commit comments

Comments
 (0)