|
134 | 134 | " def __init__(self, tag:str, cs:tuple, attrs:dict=None, void_=False, **kwargs):\n",
|
135 | 135 | " assert isinstance(cs, tuple)\n",
|
136 | 136 | " self.tag,self.children,self.attrs,self.void_ = tag,cs,attrs,void_\n",
|
| 137 | + " self.listeners_ = []\n", |
| 138 | + " \n", |
| 139 | + " def on(self, f): self.listeners_.append(f)\n", |
| 140 | + " def changed(self):\n", |
| 141 | + " [f(self) for f in self.listeners_]\n", |
| 142 | + " return self\n", |
137 | 143 | "\n",
|
138 | 144 | " def __setattr__(self, k, v):\n",
|
139 |
| - " if k.startswith('__') or k in ('tag','children','attrs','void_'): return super().__setattr__(k,v)\n", |
| 145 | + " if k.startswith('__') or k[-1]=='_' or k in ('tag','children','attrs','void_'): return super().__setattr__(k,v)\n", |
140 | 146 | " self.attrs[k.lstrip('_').replace('_', '-')] = v\n",
|
| 147 | + " self.changed()\n", |
141 | 148 | "\n",
|
142 | 149 | " def __getattr__(self, k):\n",
|
143 | 150 | " if k.startswith('__'): raise AttributeError(k)\n",
|
|
147 | 154 | " def list(self): return [self.tag,self.children,self.attrs]\n",
|
148 | 155 | " def get(self, k, default=None): return self.attrs.get(k.lstrip('_').replace('_', '-'), default)\n",
|
149 | 156 | " def __repr__(self): return f'{self.tag}({self.children},{self.attrs})'\n",
|
150 |
| - "\n", |
151 |
| - " def __add__(self, b):\n", |
152 |
| - " self.children = self.children + tuplify(b)\n", |
153 |
| - " return self\n", |
| 157 | + " def __iter__(self): return iter(self.children)\n", |
| 158 | + " def __getitem__(self, idx): return self.children[idx]\n", |
154 | 159 | " \n",
|
155 | 160 | " def __setitem__(self, i, o):\n",
|
156 | 161 | " self.children = self.children[:i] + (o,) + self.children[i+1:]\n",
|
157 |
| - "\n", |
158 |
| - " def __getitem__(self, idx): return self.children[idx]\n", |
159 |
| - " def __iter__(self): return iter(self.children)\n", |
| 162 | + " self.changed()\n", |
160 | 163 | "\n",
|
161 | 164 | " def __call__(self, *c, **kw):\n",
|
162 | 165 | " c,kw = _preproc(c,kw)\n",
|
163 |
| - " if c: self = self+c\n", |
| 166 | + " if c: self.children = self.children+c\n", |
164 | 167 | " if kw: self.attrs = {**self.attrs, **kw}\n",
|
165 |
| - " return self\n", |
| 168 | + " return self.changed()\n", |
166 | 169 | "\n",
|
167 |
| - " def set(self, *o, **k):\n", |
168 |
| - " \"Set children and/or attributes—chainable\"\n", |
169 |
| - " if o: self.children = o\n", |
170 |
| - " for k,v in k.items(): setattr(self,k,v)\n", |
171 |
| - " return self" |
| 170 | + " def set(self, *c, **kw):\n", |
| 171 | + " \"Set children and/or attributes (chainable)\"\n", |
| 172 | + " c,kw = _preproc(c,kw)\n", |
| 173 | + " if c: self.children = c\n", |
| 174 | + " if kw:\n", |
| 175 | + " self.attrs = {k:v for k,v in self.attrs.items() if k in ('id','name')}\n", |
| 176 | + " self.attrs = {**self.attrs, **kw}\n", |
| 177 | + " return self.changed()" |
172 | 178 | ]
|
173 | 179 | },
|
174 | 180 | {
|
|
207 | 213 | "for o in _all_: _g[o] = partial(ft, o.lower(), void_=o.lower() in voids)"
|
208 | 214 | ]
|
209 | 215 | },
|
210 |
| - { |
211 |
| - "cell_type": "code", |
212 |
| - "execution_count": null, |
213 |
| - "id": "306844ba", |
214 |
| - "metadata": {}, |
215 |
| - "outputs": [ |
216 |
| - { |
217 |
| - "data": { |
218 |
| - "text/plain": [ |
219 |
| - "body((div(('hi',),{'a': 1, 'b': True, 'class': None}), p(('hi',),{'class': 'a 1', 'style': 'a:1; b:2'})),{})" |
220 |
| - ] |
221 |
| - }, |
222 |
| - "execution_count": null, |
223 |
| - "metadata": {}, |
224 |
| - "output_type": "execute_result" |
225 |
| - } |
226 |
| - ], |
227 |
| - "source": [ |
228 |
| - "a = Body(Div('hi', a=1, b=True, cls=()), P('hi', cls=['a',1], style=dict(a=1,b=2)))\n", |
229 |
| - "a" |
230 |
| - ] |
231 |
| - }, |
232 |
| - { |
233 |
| - "cell_type": "code", |
234 |
| - "execution_count": null, |
235 |
| - "id": "500f358a", |
236 |
| - "metadata": {}, |
237 |
| - "outputs": [ |
238 |
| - { |
239 |
| - "data": { |
240 |
| - "text/plain": [ |
241 |
| - "body((div(('hi',),{'a': 1, 'b': True, 'class': None}), p(('hi',),{'class': 'a 1', 'style': 'a:1; b:2'}), p(('a',),{}), p(('b',),{})),{})" |
242 |
| - ] |
243 |
| - }, |
244 |
| - "execution_count": null, |
245 |
| - "metadata": {}, |
246 |
| - "output_type": "execute_result" |
247 |
| - } |
248 |
| - ], |
249 |
| - "source": [ |
250 |
| - "a + (P('a'),P('b'))" |
251 |
| - ] |
252 |
| - }, |
253 | 216 | {
|
254 | 217 | "cell_type": "markdown",
|
255 | 218 | "id": "732e44ab",
|
|
462 | 425 | "\n",
|
463 | 426 | " stag = tag\n",
|
464 | 427 | " if attrs:\n",
|
465 |
| - " sattrs = ' '.join(_to_attr(k, v) for k, v in attrs.items() if v not in (False, None, ''))\n", |
| 428 | + " sattrs = ' '.join(_to_attr(k, v) for k, v in attrs.items() if v not in (False, None, '') and k[-1]!='_')\n", |
466 | 429 | " if sattrs: stag += f' {sattrs}'\n",
|
467 | 430 | "\n",
|
468 | 431 | " cltag = '' if is_void else f'</{tag}>'\n",
|
|
0 commit comments