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