calibre/resources/rapydscript/lib/elementmaker.pyj
2016-02-16 15:24:50 +05:30

84 lines
3.0 KiB
Plaintext

# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
html_elements = {
'a', 'abbr', 'acronym', 'address', 'area',
'article', 'aside', 'audio', 'b', 'big', 'blockquote', 'br', 'button',
'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup',
'command', 'datagrid', 'datalist', 'dd', 'del', 'details', 'dfn',
'dialog', 'dir', 'div', 'dl', 'dt', 'em', 'event-source', 'fieldset',
'figcaption', 'figure', 'footer', 'font', 'form', 'header', 'h1',
'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'input', 'ins',
'keygen', 'kbd', 'label', 'legend', 'li', 'm', 'map', 'menu', 'meter',
'multicol', 'nav', 'nextid', 'ol', 'output', 'optgroup', 'option',
'p', 'pre', 'progress', 'q', 's', 'samp', 'script', 'section', 'select',
'small', 'sound', 'source', 'spacer', 'span', 'strike', 'strong', 'style',
'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'time', 'tfoot',
'th', 'thead', 'tr', 'tt', 'u', 'ul', 'var', 'video'
}
mathml_elements = {
'maction', 'math', 'merror', 'mfrac', 'mi',
'mmultiscripts', 'mn', 'mo', 'mover', 'mpadded', 'mphantom',
'mprescripts', 'mroot', 'mrow', 'mspace', 'msqrt', 'mstyle', 'msub',
'msubsup', 'msup', 'mtable', 'mtd', 'mtext', 'mtr', 'munder',
'munderover', 'none'
}
svg_elements = {
'a', 'animate', 'animateColor', 'animateMotion',
'animateTransform', 'clipPath', 'circle', 'defs', 'desc', 'ellipse',
'font-face', 'font-face-name', 'font-face-src', 'g', 'glyph', 'hkern',
'linearGradient', 'line', 'marker', 'metadata', 'missing-glyph',
'mpath', 'path', 'polygon', 'polyline', 'radialGradient', 'rect',
'set', 'stop', 'svg', 'switch', 'text', 'title', 'tspan', 'use'
}
html5_tags = html_elements.union(mathml_elements).union(svg_elements)
def _dummy_create_element(name):
return {
'name':name,
'children':[],
'attributes':{},
'setAttribute': def(name, val): this.attributes[name] = val;,
'appendChild': def(child): this.children.push(child);,
}
def _dummy_create_text(value):
return value
if type(document) == 'undefined':
create_element = _dummy_create_element
create_text_node = _dummy_create_text
else:
create_element = def(name): return document.createElement(name)
create_text_node = def(val): return document.createTextNode(val)
def E(tag, *args, **kwargs):
ans = create_element(tag)
for attr in kwargs:
vattr = str.replace(str.rstrip(attr, '_'), '_', '-')
val = kwargs[attr]
if callable(val):
if str.startswith(attr, 'on'):
attr = attr[2:]
ans.addEventListener(attr, val)
else:
ans.setAttribute(vattr, val)
for arg in args:
if type(arg) == 'string':
ans.appendChild(create_text_node(arg))
else:
ans.appendChild(arg)
return ans
def bind_e(tag):
return def(*args, **kwargs):
return E(tag, *args, **kwargs)
for tag in html5_tags:
Object.defineProperty(E, tag, {'value':bind_e(tag)})