@cycle/react-dom
Version:
Cycle.js driver that uses React DOM to render the view
268 lines • 5.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("@cycle/react");
function parseShortcut(param) {
if (typeof param === 'symbol') {
return { id: undefined, className: undefined, sel: param };
}
if (typeof param !== 'string' || param.length === 0) {
return { id: undefined, className: undefined, sel: undefined };
}
const parts = param
.split(/(?=[\#\.])/g)
.filter(part => part.length >= 2);
const possiblyIds = parts.filter(part => part[0] === '#');
const possiblyClasses = parts.filter(part => part[0] === '.');
const removeFirstChar = part => part.slice(1);
const id = possiblyIds.map(removeFirstChar).find(() => true);
const className = possiblyClasses.map(removeFirstChar).join(' ');
const sel = parts.find(part => part[0] !== '#' && part[0] !== '.');
return { id, className, sel };
}
function createTagFunction(tagName) {
return function hyperscript(a, b, c) {
const hasA = typeof a !== 'undefined';
const hasB = typeof b !== 'undefined';
const hasBChildren = Array.isArray(b) || typeof b === 'string';
const hasC = typeof c !== 'undefined';
const { id, className, sel } = parseShortcut(a);
const hasId = !!id;
const hasClassName = !!className;
const hasSelector = !!sel;
if (hasId || hasClassName) {
if (hasB && hasC) {
return react_1.h(tagName, Object.assign({}, b, { id, className, sel }), c);
}
else if (hasB && hasBChildren) {
return react_1.h(tagName, { id, className, sel }, b);
}
else if (hasB) {
return react_1.h(tagName, Object.assign({}, b, { id, className, sel }));
}
else {
return react_1.h(tagName, { id, className, sel });
}
}
else if (hasC) {
return react_1.h(tagName, Object.assign({ sel: a }, b), c);
}
else if (hasB && hasSelector && hasBChildren) {
return react_1.h(tagName, { sel }, b);
}
else if (hasB && hasSelector) {
return react_1.h(tagName, Object.assign({ sel }, b));
}
else if (hasB) {
return react_1.h(tagName, a, b);
}
else if (hasA && typeof sel === 'symbol') {
return react_1.h(tagName, { sel });
}
else if (hasA) {
return react_1.h(tagName, a);
}
else {
return react_1.h(tagName, {});
}
};
}
const SVG_TAG_NAMES = [
'a',
'altGlyph',
'altGlyphDef',
'altGlyphItem',
'animate',
'animateColor',
'animateMotion',
'animateTransform',
'circle',
'clipPath',
'colorProfile',
'cursor',
'defs',
'desc',
'ellipse',
'feBlend',
'feColorMatrix',
'feComponentTransfer',
'feComposite',
'feConvolveMatrix',
'feDiffuseLighting',
'feDisplacementMap',
'feDistantLight',
'feFlood',
'feFuncA',
'feFuncB',
'feFuncG',
'feFuncR',
'feGaussianBlur',
'feImage',
'feMerge',
'feMergeNode',
'feMorphology',
'feOffset',
'fePointLight',
'feSpecularLighting',
'feSpotlight',
'feTile',
'feTurbulence',
'filter',
'font',
'fontFace',
'fontFaceFormat',
'fontFaceName',
'fontFaceSrc',
'fontFaceUri',
'foreignObject',
'g',
'glyph',
'glyphRef',
'hkern',
'image',
'line',
'linearGradient',
'marker',
'mask',
'metadata',
'missingGlyph',
'mpath',
'path',
'pattern',
'polygon',
'polyline',
'radialGradient',
'rect',
'script',
'set',
'stop',
'style',
'switch',
'symbol',
'text',
'textPath',
'title',
'tref',
'tspan',
'use',
'view',
'vkern',
];
const svg = createTagFunction('svg');
SVG_TAG_NAMES.forEach(tag => {
svg[tag] = createTagFunction(tag);
});
const TAG_NAMES = [
'a',
'abbr',
'address',
'area',
'article',
'aside',
'audio',
'b',
'base',
'bdi',
'bdo',
'blockquote',
'body',
'br',
'button',
'canvas',
'caption',
'cite',
'code',
'col',
'colgroup',
'dd',
'del',
'dfn',
'dir',
'div',
'dl',
'dt',
'em',
'embed',
'fieldset',
'figcaption',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'head',
'header',
'hgroup',
'hr',
'html',
'i',
'iframe',
'img',
'input',
'ins',
'kbd',
'keygen',
'label',
'legend',
'li',
'link',
'main',
'map',
'mark',
'menu',
'meta',
'nav',
'noscript',
'object',
'ol',
'optgroup',
'option',
'p',
'param',
'pre',
'progress',
'q',
'rp',
'rt',
'ruby',
's',
'samp',
'script',
'section',
'select',
'small',
'source',
'span',
'strong',
'style',
'sub',
'sup',
'table',
'tbody',
'td',
'textarea',
'tfoot',
'th',
'thead',
'time',
'title',
'tr',
'u',
'ul',
'video',
];
const exported = {
SVG_TAG_NAMES,
TAG_NAMES,
svg,
parseShortcut,
createTagFunction,
};
TAG_NAMES.forEach(n => {
exported[n] = createTagFunction(n);
});
exports.default = exported;
//# sourceMappingURL=hyperscript-helpers.js.map