|
72 | 72 | "source": [ |
73 | 73 | "#|export\n", |
74 | 74 | "class XT(list):\n", |
75 | | - " def __init__(self, tag, cs, attrs): super().__init__([tag, cs, attrs])\n", |
| 75 | + " def __init__(self, tag, cs, attrs=None, **kwargs): super().__init__([tag, cs, {**(attrs or {}), **kwargs}])\n", |
76 | 76 | " @property\n", |
77 | 77 | " def tag(self): return self[0]\n", |
78 | 78 | " @property\n", |
79 | 79 | " def children(self): return self[1]\n", |
80 | 80 | " @property\n", |
81 | | - " def attrs(self): return self[2]" |
| 81 | + " def attrs(self): return self[2]\n", |
| 82 | + "\n", |
| 83 | + " def __setattr__(self, k, v):\n", |
| 84 | + " if k.startswith('__') or k in ('tag','cs','attrs'): return super().__setattr__(k,v)\n", |
| 85 | + " self.attrs[k.lstrip('_').replace('_', '-')] = v\n", |
| 86 | + "\n", |
| 87 | + " def __getattr__(self, k):\n", |
| 88 | + " if k.startswith('__') or k not in self.attrs: raise AttributeError(k)\n", |
| 89 | + " return self.attrs[k.lstrip('_').replace('_', '-')]" |
82 | 90 | ] |
83 | 91 | }, |
84 | 92 | { |
|
144 | 152 | " (['div',\n", |
145 | 153 | " ('Some text',\n", |
146 | 154 | " ['input', (), {'name': 'me'}],\n", |
147 | | - " ['img', (), {'data': {'a': 1}, 'src': 'filename'}]),\n", |
| 155 | + " ['img', (), {'data': 1, 'src': 'filename'}]),\n", |
148 | 156 | " {'class': 'myclass'}],),\n", |
149 | 157 | " {}]),\n", |
150 | 158 | " {}]\n" |
|
154 | 162 | "source": [ |
155 | 163 | "samp = Html(\n", |
156 | 164 | " Head(Title('Some page')),\n", |
157 | | - " Body(Div('Some text', Input(name='me'), Img(src=\"filename\", data={'a':1}), klass='myclass'))\n", |
| 165 | + " Body(Div('Some text', Input(name='me'), Img(src=\"filename\", data=1), klass='myclass'))\n", |
158 | 166 | ")\n", |
159 | 167 | "pprint(samp)" |
160 | 168 | ] |
|
190 | 198 | "print(elem.attrs)" |
191 | 199 | ] |
192 | 200 | }, |
| 201 | + { |
| 202 | + "cell_type": "markdown", |
| 203 | + "id": "bb61f88e", |
| 204 | + "metadata": {}, |
| 205 | + "source": [ |
| 206 | + "You can also get and set attrs directly:" |
| 207 | + ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "cell_type": "code", |
| 211 | + "execution_count": null, |
| 212 | + "id": "5c7f175d", |
| 213 | + "metadata": {}, |
| 214 | + "outputs": [ |
| 215 | + { |
| 216 | + "name": "stdout", |
| 217 | + "output_type": "stream", |
| 218 | + "text": [ |
| 219 | + "newid\n" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "data": { |
| 224 | + "text/plain": [ |
| 225 | + "['p', ('Some text',), {'id': 'newid'}]" |
| 226 | + ] |
| 227 | + }, |
| 228 | + "execution_count": null, |
| 229 | + "metadata": {}, |
| 230 | + "output_type": "execute_result" |
| 231 | + } |
| 232 | + ], |
| 233 | + "source": [ |
| 234 | + "elem.id = 'newid'\n", |
| 235 | + "print(elem.id)\n", |
| 236 | + "elem" |
| 237 | + ] |
| 238 | + }, |
193 | 239 | { |
194 | 240 | "cell_type": "code", |
195 | 241 | "execution_count": null, |
|
282 | 328 | " <div class=\"myclass\">\n", |
283 | 329 | "Some text\n", |
284 | 330 | " <input name=\"me\">\n", |
285 | | - " <img src=\"filename\" data='{\"a\": 1}'>\n", |
| 331 | + " <img src=\"filename\" data=\"1\">\n", |
286 | 332 | " </div>\n", |
287 | 333 | " </body>\n", |
288 | 334 | "</html>\n", |
|
291 | 337 | } |
292 | 338 | ], |
293 | 339 | "source": [ |
294 | | - "print(to_xml(samp))" |
| 340 | + "h = to_xml(samp)\n", |
| 341 | + "print(h)" |
295 | 342 | ] |
296 | 343 | }, |
297 | 344 | { |
|
0 commit comments