Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/src/lib/elementmaker.py
1398 views
1
# vim:fileencoding=utf-8
2
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
3
4
html_elements = {
5
'a', 'abbr', 'acronym', 'address', 'area',
6
'article', 'aside', 'audio', 'b', 'base', 'big', 'body', 'blockquote', 'br', 'button',
7
'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup',
8
'command', 'datagrid', 'datalist', 'dd', 'del', 'details', 'dfn',
9
'dialog', 'dir', 'div', 'dl', 'dt', 'em', 'event-source', 'fieldset',
10
'figcaption', 'figure', 'footer', 'font', 'form', 'header', 'h1',
11
'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'head', 'i', 'iframe', 'img', 'input', 'ins',
12
'keygen', 'kbd', 'label', 'legend', 'li', 'm', 'map', 'menu', 'meter',
13
'multicol', 'nav', 'nextid', 'ol', 'output', 'optgroup', 'option',
14
'p', 'pre', 'progress', 'q', 's', 'samp', 'script', 'section', 'select',
15
'small', 'sound', 'source', 'spacer', 'span', 'strike', 'strong', 'style',
16
'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'time', 'tfoot',
17
'th', 'thead', 'tr', 'tt', 'u', 'ul', 'var', 'video'
18
}
19
20
mathml_elements = {
21
'maction', 'math', 'merror', 'mfrac', 'mi',
22
'mmultiscripts', 'mn', 'mo', 'mover', 'mpadded', 'mphantom',
23
'mprescripts', 'mroot', 'mrow', 'mspace', 'msqrt', 'mstyle', 'msub',
24
'msubsup', 'msup', 'mtable', 'mtd', 'mtext', 'mtr', 'munder',
25
'munderover', 'none'
26
}
27
28
svg_elements = {
29
'a', 'animate', 'animateColor', 'animateMotion',
30
'animateTransform', 'clipPath', 'circle', 'defs', 'desc', 'ellipse',
31
'font-face', 'font-face-name', 'font-face-src', 'g', 'glyph', 'hkern',
32
'linearGradient', 'line', 'marker', 'metadata', 'missing-glyph',
33
'mpath', 'path', 'polygon', 'polyline', 'radialGradient', 'rect',
34
'set', 'stop', 'svg', 'switch', 'text', 'title', 'tspan', 'use'
35
}
36
37
html5_tags = html_elements.union(mathml_elements).union(svg_elements)
38
39
def _makeelement(tag, *args, **kwargs):
40
ans = this.createElement(tag)
41
42
for attr in kwargs:
43
vattr = str.replace(str.rstrip(attr, '_'), '_', '-')
44
val = kwargs[attr]
45
if callable(val):
46
if str.startswith(attr, 'on'):
47
attr = attr[2:]
48
ans.addEventListener(attr, val)
49
elif val is True:
50
ans.setAttribute(vattr, vattr)
51
elif jstype(val) is 'string':
52
ans.setAttribute(vattr, val)
53
54
for arg in args:
55
if jstype(arg) is 'string':
56
arg = this.createTextNode(arg)
57
ans.appendChild(arg)
58
return ans
59
60
def maker_for_document(document):
61
# Create an elementmaker to be used with the specified document
62
E = _makeelement.bind(document)
63
Object.defineProperties(E, {
64
tag: {
65
'value':_makeelement.bind(document, tag)
66
} for tag in html5_tags
67
})
68
return E
69
70
if jstype(document) is 'undefined':
71
E = maker_for_document({
72
'createTextNode': def(value): return value;,
73
'createElement': def(name):
74
return {
75
'name':name,
76
'children':[],
77
'attributes':{},
78
'setAttribute': def(name, val): this.attributes[name] = val;,
79
'appendChild': def(child): this.children.push(child);,
80
}
81
})
82
else:
83
E = maker_for_document(document)
84
85