-
Notifications
You must be signed in to change notification settings - Fork 17
/
html.py
603 lines (385 loc) · 11 KB
/
html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
"""
Copyright (c) 2017, Jairus Martin.
Distributed under the terms of the MIT License.
The full license is in the file LICENSE.txt, distributed with this software.
Created on Apr 2, 2017
@author: jrm
"""
from __future__ import print_function
from atom.api import (
Event, Enum, ContainerList, Value, Int, Unicode, Dict, Instance,
ForwardTyped, Typed, Coerced, observe
)
from enaml.core.declarative import d_
from enaml.widgets.toolkit_object import ToolkitObject, ProxyToolkitObject
class ProxyTag(ProxyToolkitObject):
declaration = ForwardTyped(lambda: Tag)
def xpath(self, *args, **kwargs):
""" Perform an xpath lookup on the node """
raise NotImplementedError
class Tag(ToolkitObject):
#: Reference to the proxy object
proxy = Typed(ProxyTag)
#: Object ID
id = d_(Unicode())
#: Object ID
ref = d_(Unicode())
#: Tag name (leave blank for class name)
tag = d_(Unicode()).tag(attr=False)
#: CSS classes
cls = d_(Instance((list, object))).tag(attr=False)
#: CSS styles
style = d_(Instance((dict, object))).tag(attr=False)
#: Node text
text = d_(Unicode()).tag(attr=False)
#: Node tail text
tail = d_(Unicode()).tag(attr=False)
#: Alt attribute
alt = d_(Unicode())
#: Custom attributes not explicitly defined
attrs = d_(Dict()).tag(attr=False)
#: Event from JS
onclick = d_(Unicode())
#: Whether this is clickable via websockets
clickable = d_(Coerced(bool))
#: Event from JS
clicked = d_(Event())
def _default_tag(self):
return self.__class__.__name__.lower()
def _default_ref(self):
return u"{}".format(id(self))
@observe('id', 'tag', 'cls', 'style', 'text', 'tail', 'alt', 'attrs',
'onclick', 'clickable')
def _update_proxy(self, change):
""" Update the proxy widget when the Widget data
changes.
"""
#: Try default handler
if change['type'] == 'update' and self.proxy_is_active:
handler = getattr(self.proxy, 'set_' + change['name'], None)
if handler is not None:
handler(change['value'])
else:
self.proxy.set_attribute(change['name'], change['value'])
self._notify_modified(change)
def _notify_modified(self, change):
""" If a change occurs when we have a websocket connection active
notify the websocket client of the change.
"""
root = self.root_object()
if isinstance(root, Html):
name = change['name']
change = {
'ref': self.ref,
'type': change['type'],
'name': change['name'],
'value': change['value']
}
root.modified(change)
def child_added(self, child):
super(Tag, self).child_added(child)
if isinstance(child, Tag) and self.proxy_is_active:
change = {
'type': 'added',
'name': 'children',
#'before':self.ch #: TODO: Handle placement?
'value': child.render().decode()
}
self._notify_modified(change)
def child_removed(self, child):
super(Tag, self).child_removed(child)
if isinstance(child, Tag) and self.proxy_is_active:
change = {
'type': 'removed',
'name': 'children',
'value': child.ref,
}
self._notify_modified(change)
def xpath(self, *args, **kwargs):
if not nodes:
return []
refs = [node.attrib.get('ref') for node in nodes]
if not refs:
return []
return [CACHE[ref] for ref in refs if ref and ref in CACHE]
def xpath(self, query, **kwargs):
""" Find nodes matching the given xpath query """
if not self.proxy:
return
nodes = self.proxy.find(query, **kwargs)
return [n.declaration for n in nodes]
def prepare(self, **kwargs):
""" Prepare for rendering """
for k, v in kwargs.items():
setattr(self, k, v)
if not self.is_initialized:
self.initialize()
if not self.proxy_is_active:
self.activate_proxy()
def render(self, **kwargs):
""" Render to a string"""
self.prepare(**kwargs)
return self.proxy.render()
class Html(Tag):
#: Dom modified event. This will fire when any child node is updated, added
#: or removed. Observe this event to handle updating websockets.
modified = d_(Event(dict), writable=False).tag(attr=False)
def _default_tag(self):
return 'html'
class Head(Tag):
pass
class Body(Tag):
pass
class Title(Tag):
pass
class P(Tag):
pass
class H1(Tag):
pass
class H2(Tag):
pass
class H3(Tag):
pass
class H4(Tag):
pass
class H5(Tag):
pass
class H6(Tag):
pass
class Hr(Tag):
pass
class Br(Tag):
pass
class Pre(Tag):
pass
#class Code(Tag):
# pass
class Kbd(Tag):
pass
class Samp(Tag):
pass
class Var(Tag):
pass
class Div(Tag):
pass
class Span(Tag):
pass
class A(Tag):
href = d_(Unicode())
target = d_(Enum("", "_blank", "_self", "_parent", "_top", "framename"))
@observe('href', 'target')
def _update_proxy(self, change):
super(A, self)._update_proxy(change)
class B(Tag):
pass
class I(Tag):
pass
class Strong(Tag):
pass
class Em(Tag):
pass
class Mark(Tag):
pass
class Small(Tag):
pass
class Del(Tag):
pass
class Ins(Tag):
pass
class Sub(Tag):
pass
class Sup(Tag):
pass
class Q(Tag):
pass
class Blockquote(Tag):
cite = d_(Unicode())
@observe('cite')
def _update_proxy(self, change):
super(Blockquote, self)._update_proxy(change)
class Abbr(Tag):
pass
class Address(Tag):
pass
class Cite(Tag):
pass
class Bdo(Tag):
dir = d_(Unicode())
class Img(Tag):
src = d_(Unicode())
width = d_(Unicode())
height = d_(Unicode())
@observe('src', 'width', 'height')
def _update_proxy(self, change):
super(Img, self)._update_proxy(change)
class Style(Tag):
pass
class Link(Tag):
type = d_(Unicode())
rel = d_(Unicode())
href = d_(Unicode())
media = d_(Unicode())
@observe('type', 'rel', 'href', 'media')
def _update_proxy(self, change):
super(Link, self)._update_proxy(change)
class Map(Tag):
name = d_(Unicode())
@observe('name')
def _update_proxy(self, change):
super(Map, self)._update_proxy(change)
class Area(Tag):
shape = d_(Unicode())
coords = d_(Unicode())
href = d_(Unicode())
@observe('shape', 'coords', 'href')
def _update_proxy(self, change):
super(Area, self)._update_proxy(change)
class Table(Tag):
pass
class Tr(Tag):
pass
class Td(Tag):
colspan = d_(Unicode())
rowspan = d_(Unicode())
@observe('colspan', 'rowspan')
def _update_proxy(self, change):
super(Td, self)._update_proxy(change)
class Th(Tag):
colspan = d_(Unicode())
rowspan = d_(Unicode())
@observe('colspan', 'rowspan')
def _update_proxy(self, change):
super(Th, self)._update_proxy(change)
class THead(Tag):
pass
class TBody(Tag):
pass
class TFoot(Tag):
pass
class Caption(Tag):
pass
class Ul(Tag):
pass
class Ol(Tag):
type = d_(Enum("", "1", "A", "a", "I", "i"))
@observe('type')
def _update_proxy(self, change):
super(Ol, self)._update_proxy(change)
class Li(Tag):
pass
class Dl(Tag):
pass
class Dt(Tag):
pass
class Dd(Tag):
pass
class IFrame(Tag):
src = d_(Unicode())
height = d_(Unicode())
width = d_(Unicode())
target = d_(Unicode())
@observe('src', 'height', 'width', 'target')
def _update_proxy(self, change):
super(IFrame, self)._update_proxy(change)
class Script(Tag):
src = d_(Unicode())
type = d_(Unicode())
@observe('type', 'src')
def _update_proxy(self, change):
super(Script, self)._update_proxy(change)
class NoScript(Tag):
pass
class Meta(Tag):
name = d_(Unicode())
content = d_(Unicode())
@observe('name', 'content')
def _update_proxy(self, change):
super(Meta, self)._update_proxy(change)
class Base(Tag):
href = d_(Unicode())
target = d_(Enum("", "_blank", "_self", "_parent", "_top", "framename"))
@observe('href', 'target')
def _update_proxy(self, change):
super(Base, self)._update_proxy(change)
class Header(Tag):
pass
class Nav(Tag):
pass
class Section(Tag):
pass
class Aside(Tag):
pass
class Article(Tag):
pass
class Footer(Tag):
pass
class Summary(Tag):
pass
class Details(Tag):
pass
class Form(Tag):
action = d_(Unicode())
method = d_(Enum("post", "get"))
@observe('action', 'method')
def _update_proxy(self, change):
super(Form, self)._update_proxy(change)
class Fieldset(Tag):
pass
class Legend(Tag):
pass
class Label(Tag):
pass
class Select(Tag):
name = d_(Unicode())
value = d_(Unicode())
def _default_name(self):
return u'{}'.format(self.ref)
@observe('name', 'value')
def _update_proxy(self, change):
super(Select, self)._update_proxy(change)
class Option(Tag):
value = d_(Unicode())
selected = d_(Coerced(bool))
@observe('value', 'selected')
def _update_proxy(self, change):
super(Option, self)._update_proxy(change)
class Input(Tag):
name = d_(Unicode())
type = d_(Unicode())
disabled = d_(Coerced(bool))
checked = d_(Coerced(bool))
value = d_(Value())
def _default_name(self):
return u'{}'.format(self.ref)
@observe('name', 'type', 'disabled', 'checked', 'value')
def _update_proxy(self, change):
super(Input, self)._update_proxy(change)
class Textarea(Tag):
name = d_(Unicode())
rows = d_(Unicode())
cols = d_(Unicode())
def _default_name(self):
return u'{}'.format(self.ref)
@observe('name', 'rows', 'cols')
def _update_proxy(self, change):
super(Textarea, self)._update_proxy(change)
class Button(Tag):
name = d_(Unicode())
type = d_(Unicode())
value = d_(Unicode('1'))
def _default_name(self):
return u'{}'.format(self.ref)
@observe('type')
def _update_proxy(self, change):
super(Button, self)._update_proxy(change)
class Video(Tag):
controls = d_(Coerced(bool))
@observe('controls')
def _update_proxy(self, change):
super(Video, self)._update_proxy(change)
class Source(Tag):
src = d_(Unicode())
type = d_(Unicode())
@observe('src', 'type')
def _update_proxy(self, change):
super(Source, self)._update_proxy(change)