Skip to content

Commit 7231b25

Browse files
committed
fixes #642
1 parent 625162a commit 7231b25

File tree

3 files changed

+80
-50
lines changed

3 files changed

+80
-50
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@
613613
'fastcore.xml.Safe.__html__': ('xml.html#safe.__html__', 'fastcore/xml.py'),
614614
'fastcore.xml.__getattr__': ('xml.html#__getattr__', 'fastcore/xml.py'),
615615
'fastcore.xml._escape': ('xml.html#_escape', 'fastcore/xml.py'),
616+
'fastcore.xml._fix_k': ('xml.html#_fix_k', 'fastcore/xml.py'),
616617
'fastcore.xml._flatten_tuple': ('xml.html#_flatten_tuple', 'fastcore/xml.py'),
617618
'fastcore.xml._is_whitespace_significant': ('xml.html#_is_whitespace_significant', 'fastcore/xml.py'),
618619
'fastcore.xml._noescape': ('xml.html#_noescape', 'fastcore/xml.py'),

fastcore/xml.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@
2222
from functools import partial
2323
from html import escape
2424

25+
# %% ../nbs/11_xml.ipynb
26+
def _fix_k(k): return k if k=='_' else k.lstrip('_').replace('_', '-')
27+
2528
# %% ../nbs/11_xml.ipynb
2629
_specials = set('@.-!~:[](){}$%^&*+=|/?<>,`')
2730

2831
def attrmap(o):
29-
if o=='_' or (_specials & set(o)): return o
32+
if _specials & set(o): return o
3033
o = dict(htmlClass='class', cls='class', _class='class', klass='class',
3134
_for='for', fr='for', htmlFor='for').get(o, o)
32-
return o.lstrip('_').replace('_', '-')
35+
return _fix_k(o)
3336

3437
# %% ../nbs/11_xml.ipynb
3538
def valmap(o):
@@ -66,8 +69,8 @@ def changed(self):
6669
return self
6770

6871
def __setattr__(self, k, v):
69-
if k.startswith('__') or k[-1]=='_' or k in ('tag','children','attrs','void_'): return super().__setattr__(k,v)
70-
self.attrs[k.lstrip('_').replace('_', '-')] = v
72+
if len(k)>1 and k.startswith('__') or k[-1]=='_' or k in ('tag','children','attrs','void_'): return super().__setattr__(k,v)
73+
self.attrs[_fix_k(k)] = v
7174
self.changed()
7275

7376
def __getattr__(self, k):
@@ -76,7 +79,8 @@ def __getattr__(self, k):
7679

7780
@property
7881
def list(self): return [self.tag,self.children,self.attrs]
79-
def get(self, k, default=None): return self.attrs.get(k.lstrip('_').replace('_', '-'), default)
82+
def get(self, k, default=None): return self.attrs.get(_fix_k(k), default)
83+
8084
def __repr__(self): return f'{self.tag}({self.children},{self.attrs})'
8185
def __iter__(self): return iter(self.children)
8286
def __getitem__(self, idx): return self.children[idx]
@@ -151,7 +155,7 @@ def _to_attr(k,v):
151155

152156
# %% ../nbs/11_xml.ipynb
153157
_block_tags = {'div', 'p', 'ul', 'ol', 'li', 'table', 'thead', 'tbody', 'tfoot',
154-
'html', 'head', 'body', 'meta', '!doctype', 'input', 'script', 'link', 'style',
158+
'html', 'head', 'body', 'meta', 'title', '!doctype', 'input', 'script', 'link', 'style',
155159
'tr', 'th', 'td', 'section', 'article', 'nav', 'aside', 'header',
156160
'footer', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote'}
157161
_inline_tags = {'a', 'span', 'b', 'i', 'u', 'em', 'strong', 'img', 'br', 'small',
@@ -222,6 +226,6 @@ def showtags(s):
222226
# %% ../nbs/11_xml.ipynb
223227
def __getattr__(tag):
224228
if tag.startswith('_') or tag[0].islower(): raise AttributeError
225-
tag = tag.replace("_", "-")
229+
tag = _fix_k(tag)
226230
def _f(*c, target_id=None, **kwargs): return ft(tag, *c, target_id=target_id, **kwargs)
227231
return _f

0 commit comments

Comments
 (0)