-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
52 lines (47 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const isValidString = param => typeof param === 'string' && param.length > 0;
const startsWith = (string, start) => string[0] === start;
const isSelector = param =>
isValidString(param) && (startsWith(param, '.') || startsWith(param, '#'));
const node = h => tagName => (first, ...rest) => {
if (isSelector(first)) {
return h(tagName + first, ...rest);
} else if (typeof first === 'undefined') {
return h(tagName);
} else {
return h(tagName, first, ...rest);
}
};
// The tag names are verified against html-tag-names in the tests
// See https://github.com/ohanhi/hyperscript-helpers/issues/34 for the reason
// why the tags aren't simply required from html-tag-names
const TAG_NAMES = [
'a', 'abbr', 'acronym', 'address', 'applet', 'area', 'article', 'aside',
'audio', 'b', 'base', 'basefont', 'bdi', 'bdo', 'bgsound', 'big', 'blink',
'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite',
'code', 'col', 'colgroup', 'command', 'content', 'data', 'datalist', 'dd',
'del', 'details', 'dfn', 'dialog', 'dir', 'div', 'dl', 'dt', 'element', 'em',
'embed', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form',
'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header',
'hgroup', 'hr', 'html', 'i', 'iframe', 'image', 'img', 'input', 'ins',
'isindex', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'listing',
'main', 'map', 'mark', 'marquee', 'math', 'menu', 'menuitem', 'meta',
'meter', 'multicol', 'nav', 'nextid', 'nobr', 'noembed', 'noframes',
'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param',
'picture', 'plaintext', 'pre', 'progress', 'q', 'rb', 'rbc', 'rp', 'rt',
'rtc', 'ruby', 's', 'samp', 'script', 'section', 'select', 'shadow', 'slot',
'small', 'source', 'spacer', 'span', 'strike', 'strong', 'style', 'sub',
'summary', 'sup', 'svg', 'table', 'tbody', 'td', 'template', 'textarea',
'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u', 'ul',
'var', 'video', 'wbr', 'xmp'
];
export default h => {
const createTag = node(h);
const exported = { TAG_NAMES, isSelector, createTag };
TAG_NAMES.forEach(n => {
// Also support a first-letter-uppercase spelling to help avoid conflicts
// with other variables or Javascript reserved keywords such as 'var'
exported[n] = exported[n.charAt(0).toUpperCase() + n.slice(1)] =
createTag(n);
});
return exported;
};