UNPKG

fractal-core

Version:

A minimalist and well crafted app, content or component is our conviction

72 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const parseSelector = require("parse-sel"); const elements_1 = require("./elements"); const elements_2 = require("./elements"); function init(modules) { function parse(vnode, node) { var result = []; var attributes = new Map([ // These can be overwritten because that’s what happens in snabbdom ['id', node.id], ['class', node.className] ]); modules.forEach(function (fn, index) { fn(vnode, attributes); }); attributes.forEach(function (value, key) { if (value && value !== '') { result.push(key + '="' + value + '"'); } }); return result.join(' '); } return function renderToString(vnode) { if (!vnode.sel && vnode.text) { return vnode.text; } vnode.data = vnode.data || {}; // Support thunks if (vnode.data.hook && typeof vnode.data.hook.init === 'function' && typeof vnode.data.fn === 'function') { vnode.data.hook.init(vnode); } var node = parseSelector(vnode.sel); var tagName = node.tagName; var attributes = parse(vnode, node); var svg = vnode.data.ns === 'http://www.w3.org/2000/svg'; var tag = []; if (tagName === '!') { return '<!--' + vnode.text + '-->'; } // Open tag tag.push('<' + tagName); if (attributes.length) { tag.push(' ' + attributes); } if (svg && elements_2.CONTAINER[tagName] !== true) { tag.push(' /'); } tag.push('>'); // Close tag, if needed if ((elements_1.VOID[tagName] !== true && !svg) || (svg && elements_2.CONTAINER[tagName] === true)) { if (vnode.data.props && vnode.data.props.innerHTML) { tag.push(vnode.data.props.innerHTML); } else if (vnode.text) { tag.push(vnode.text); } else if (vnode.children) { vnode.children.forEach(function (child) { tag.push(renderToString(child)); }); } tag.push('</' + tagName + '>'); } return tag.join(''); }; } exports.init = init; //# sourceMappingURL=init.js.map