UNPKG

10 kBPlain TextView Raw
1// tslint:disable:max-file-line-count
2import {h, VNode, VNodeData} from 'snabbdom';
3
4function isValidString(param: any): boolean {
5 return typeof param === 'string' && param.length > 0;
6}
7
8function isSelector(param: any): boolean {
9 return isValidString(param) && (param[0] === '.' || param[0] === '#');
10}
11
12function createTagFunction(tagName: string): Function {
13 return function hyperscript(a: any, b?: any, c?: any): VNode {
14 const hasA = typeof a !== 'undefined';
15 const hasB = typeof b !== 'undefined';
16 const hasC = typeof c !== 'undefined';
17 if (isSelector(a)) {
18 if (hasB && hasC) {
19 return h(tagName + a, b, c);
20 } else if (hasB) {
21 return h(tagName + a, b);
22 } else {
23 return h(tagName + a, {});
24 }
25 } else if (hasC) {
26 return h(tagName + a, b, c);
27 } else if (hasB) {
28 return h(tagName, a, b);
29 } else if (hasA) {
30 return h(tagName, a);
31 } else {
32 return h(tagName, {});
33 }
34 };
35}
36
37const SVG_TAG_NAMES = [
38 'a',
39 'altGlyph',
40 'altGlyphDef',
41 'altGlyphItem',
42 'animate',
43 'animateColor',
44 'animateMotion',
45 'animateTransform',
46 'circle',
47 'clipPath',
48 'colorProfile',
49 'cursor',
50 'defs',
51 'desc',
52 'ellipse',
53 'feBlend',
54 'feColorMatrix',
55 'feComponentTransfer',
56 'feComposite',
57 'feConvolveMatrix',
58 'feDiffuseLighting',
59 'feDisplacementMap',
60 'feDistantLight',
61 'feFlood',
62 'feFuncA',
63 'feFuncB',
64 'feFuncG',
65 'feFuncR',
66 'feGaussianBlur',
67 'feImage',
68 'feMerge',
69 'feMergeNode',
70 'feMorphology',
71 'feOffset',
72 'fePointLight',
73 'feSpecularLighting',
74 'feSpotlight',
75 'feTile',
76 'feTurbulence',
77 'filter',
78 'font',
79 'fontFace',
80 'fontFaceFormat',
81 'fontFaceName',
82 'fontFaceSrc',
83 'fontFaceUri',
84 'foreignObject',
85 'g',
86 'glyph',
87 'glyphRef',
88 'hkern',
89 'image',
90 'line',
91 'linearGradient',
92 'marker',
93 'mask',
94 'metadata',
95 'missingGlyph',
96 'mpath',
97 'path',
98 'pattern',
99 'polygon',
100 'polyline',
101 'radialGradient',
102 'rect',
103 'script',
104 'set',
105 'stop',
106 'style',
107 'switch',
108 'symbol',
109 'text',
110 'textPath',
111 'title',
112 'tref',
113 'tspan',
114 'use',
115 'view',
116 'vkern',
117];
118
119const svg = createTagFunction('svg');
120
121SVG_TAG_NAMES.forEach(tag => {
122 svg[tag] = createTagFunction(tag);
123});
124
125const TAG_NAMES = [
126 'a',
127 'abbr',
128 'address',
129 'area',
130 'article',
131 'aside',
132 'audio',
133 'b',
134 'base',
135 'bdi',
136 'bdo',
137 'blockquote',
138 'body',
139 'br',
140 'button',
141 'canvas',
142 'caption',
143 'cite',
144 'code',
145 'col',
146 'colgroup',
147 'dd',
148 'del',
149 'details',
150 'dfn',
151 'dir',
152 'div',
153 'dl',
154 'dt',
155 'em',
156 'embed',
157 'fieldset',
158 'figcaption',
159 'figure',
160 'footer',
161 'form',
162 'h1',
163 'h2',
164 'h3',
165 'h4',
166 'h5',
167 'h6',
168 'head',
169 'header',
170 'hgroup',
171 'hr',
172 'html',
173 'i',
174 'iframe',
175 'img',
176 'input',
177 'ins',
178 'kbd',
179 'keygen',
180 'label',
181 'legend',
182 'li',
183 'link',
184 'main',
185 'map',
186 'mark',
187 'menu',
188 'meta',
189 'nav',
190 'noscript',
191 'object',
192 'ol',
193 'optgroup',
194 'option',
195 'p',
196 'param',
197 'pre',
198 'progress',
199 'q',
200 'rp',
201 'rt',
202 'ruby',
203 's',
204 'samp',
205 'script',
206 'section',
207 'select',
208 'small',
209 'source',
210 'span',
211 'strong',
212 'style',
213 'sub',
214 'summary',
215 'sup',
216 'table',
217 'tbody',
218 'td',
219 'textarea',
220 'tfoot',
221 'th',
222 'thead',
223 'time',
224 'title',
225 'tr',
226 'u',
227 'ul',
228 'video',
229];
230
231const exported = {
232 SVG_TAG_NAMES,
233 TAG_NAMES,
234 svg,
235 isSelector,
236 createTagFunction,
237};
238TAG_NAMES.forEach(n => {
239 exported[n] = createTagFunction(n);
240});
241export default (exported as any) as HyperScriptHelpers;
242
243export type Children = Array<VNode | string | null>;
244export type Properties = any; //VNodeData // does not work yet, because of https://github.com/snabbdom/snabbdom/pull/325
245export interface HyperScriptHelperFn {
246 (): VNode;
247 (textOrSelector: string): VNode;
248 (children: Children): VNode;
249 (properties: Properties): VNode;
250 (selector: string, children: Children): VNode;
251 (selector: string, text: string): VNode;
252 (selector: string, properties: Properties): VNode;
253 (properties: Properties, children: Children): VNode;
254 (properties: Properties, text: string): VNode;
255 (selector: string, properties: Properties, text: string): VNode;
256 (selector: string, properties: Properties, children: Children): VNode;
257}
258
259export interface SVGHelperFn extends HyperScriptHelperFn {
260 a: HyperScriptHelperFn;
261 altGlyph: HyperScriptHelperFn;
262 altGlyphDef: HyperScriptHelperFn;
263 altGlyphItem: HyperScriptHelperFn;
264 animate: HyperScriptHelperFn;
265 animateColor: HyperScriptHelperFn;
266 animateMotion: HyperScriptHelperFn;
267 animateTransform: HyperScriptHelperFn;
268 circle: HyperScriptHelperFn;
269 clipPath: HyperScriptHelperFn;
270 colorProfile: HyperScriptHelperFn;
271 cursor: HyperScriptHelperFn;
272 defs: HyperScriptHelperFn;
273 desc: HyperScriptHelperFn;
274 ellipse: HyperScriptHelperFn;
275 feBlend: HyperScriptHelperFn;
276 feColorMatrix: HyperScriptHelperFn;
277 feComponentTransfer: HyperScriptHelperFn;
278 feComposite: HyperScriptHelperFn;
279 feConvolveMatrix: HyperScriptHelperFn;
280 feDiffuseLighting: HyperScriptHelperFn;
281 feDisplacementMap: HyperScriptHelperFn;
282 feDistantLight: HyperScriptHelperFn;
283 feFlood: HyperScriptHelperFn;
284 feFuncA: HyperScriptHelperFn;
285 feFuncB: HyperScriptHelperFn;
286 feFuncG: HyperScriptHelperFn;
287 feFuncR: HyperScriptHelperFn;
288 feGaussianBlur: HyperScriptHelperFn;
289 feImage: HyperScriptHelperFn;
290 feMerge: HyperScriptHelperFn;
291 feMergeNode: HyperScriptHelperFn;
292 feMorphology: HyperScriptHelperFn;
293 feOffset: HyperScriptHelperFn;
294 fePointLight: HyperScriptHelperFn;
295 feSpecularLighting: HyperScriptHelperFn;
296 feSpotlight: HyperScriptHelperFn;
297 feTile: HyperScriptHelperFn;
298 feTurbulence: HyperScriptHelperFn;
299 filter: HyperScriptHelperFn;
300 font: HyperScriptHelperFn;
301 fontFace: HyperScriptHelperFn;
302 fontFaceFormat: HyperScriptHelperFn;
303 fontFaceName: HyperScriptHelperFn;
304 fontFaceSrc: HyperScriptHelperFn;
305 fontFaceUri: HyperScriptHelperFn;
306 foreignObject: HyperScriptHelperFn;
307 g: HyperScriptHelperFn;
308 glyph: HyperScriptHelperFn;
309 glyphRef: HyperScriptHelperFn;
310 hkern: HyperScriptHelperFn;
311 image: HyperScriptHelperFn;
312 line: HyperScriptHelperFn;
313 linearGradient: HyperScriptHelperFn;
314 marker: HyperScriptHelperFn;
315 mask: HyperScriptHelperFn;
316 metadata: HyperScriptHelperFn;
317 missingGlyph: HyperScriptHelperFn;
318 mpath: HyperScriptHelperFn;
319 path: HyperScriptHelperFn;
320 pattern: HyperScriptHelperFn;
321 polygon: HyperScriptHelperFn;
322 polyline: HyperScriptHelperFn;
323 radialGradient: HyperScriptHelperFn;
324 rect: HyperScriptHelperFn;
325 script: HyperScriptHelperFn;
326 set: HyperScriptHelperFn;
327 stop: HyperScriptHelperFn;
328 style: HyperScriptHelperFn;
329 switch: HyperScriptHelperFn;
330 symbol: HyperScriptHelperFn;
331 text: HyperScriptHelperFn;
332 textPath: HyperScriptHelperFn;
333 title: HyperScriptHelperFn;
334 tref: HyperScriptHelperFn;
335 tspan: HyperScriptHelperFn;
336 use: HyperScriptHelperFn;
337 view: HyperScriptHelperFn;
338 vkern: HyperScriptHelperFn;
339}
340
341export interface HyperScriptHelpers {
342 svg: SVGHelperFn;
343 a: HyperScriptHelperFn;
344 abbr: HyperScriptHelperFn;
345 address: HyperScriptHelperFn;
346 area: HyperScriptHelperFn;
347 article: HyperScriptHelperFn;
348 aside: HyperScriptHelperFn;
349 audio: HyperScriptHelperFn;
350 b: HyperScriptHelperFn;
351 base: HyperScriptHelperFn;
352 bdi: HyperScriptHelperFn;
353 bdo: HyperScriptHelperFn;
354 blockquote: HyperScriptHelperFn;
355 body: HyperScriptHelperFn;
356 br: HyperScriptHelperFn;
357 button: HyperScriptHelperFn;
358 canvas: HyperScriptHelperFn;
359 caption: HyperScriptHelperFn;
360 cite: HyperScriptHelperFn;
361 code: HyperScriptHelperFn;
362 col: HyperScriptHelperFn;
363 colgroup: HyperScriptHelperFn;
364 dd: HyperScriptHelperFn;
365 del: HyperScriptHelperFn;
366 details: HyperScriptHelperFn;
367 dfn: HyperScriptHelperFn;
368 dir: HyperScriptHelperFn;
369 div: HyperScriptHelperFn;
370 dl: HyperScriptHelperFn;
371 dt: HyperScriptHelperFn;
372 em: HyperScriptHelperFn;
373 embed: HyperScriptHelperFn;
374 fieldset: HyperScriptHelperFn;
375 figcaption: HyperScriptHelperFn;
376 figure: HyperScriptHelperFn;
377 footer: HyperScriptHelperFn;
378 form: HyperScriptHelperFn;
379 h1: HyperScriptHelperFn;
380 h2: HyperScriptHelperFn;
381 h3: HyperScriptHelperFn;
382 h4: HyperScriptHelperFn;
383 h5: HyperScriptHelperFn;
384 h6: HyperScriptHelperFn;
385 head: HyperScriptHelperFn;
386 header: HyperScriptHelperFn;
387 hgroup: HyperScriptHelperFn;
388 hr: HyperScriptHelperFn;
389 html: HyperScriptHelperFn;
390 i: HyperScriptHelperFn;
391 iframe: HyperScriptHelperFn;
392 img: HyperScriptHelperFn;
393 input: HyperScriptHelperFn;
394 ins: HyperScriptHelperFn;
395 kbd: HyperScriptHelperFn;
396 keygen: HyperScriptHelperFn;
397 label: HyperScriptHelperFn;
398 legend: HyperScriptHelperFn;
399 li: HyperScriptHelperFn;
400 link: HyperScriptHelperFn;
401 main: HyperScriptHelperFn;
402 map: HyperScriptHelperFn;
403 mark: HyperScriptHelperFn;
404 menu: HyperScriptHelperFn;
405 meta: HyperScriptHelperFn;
406 nav: HyperScriptHelperFn;
407 noscript: HyperScriptHelperFn;
408 object: HyperScriptHelperFn;
409 ol: HyperScriptHelperFn;
410 optgroup: HyperScriptHelperFn;
411 option: HyperScriptHelperFn;
412 p: HyperScriptHelperFn;
413 param: HyperScriptHelperFn;
414 pre: HyperScriptHelperFn;
415 progress: HyperScriptHelperFn;
416 q: HyperScriptHelperFn;
417 rp: HyperScriptHelperFn;
418 rt: HyperScriptHelperFn;
419 ruby: HyperScriptHelperFn;
420 s: HyperScriptHelperFn;
421 samp: HyperScriptHelperFn;
422 script: HyperScriptHelperFn;
423 section: HyperScriptHelperFn;
424 select: HyperScriptHelperFn;
425 small: HyperScriptHelperFn;
426 source: HyperScriptHelperFn;
427 span: HyperScriptHelperFn;
428 strong: HyperScriptHelperFn;
429 style: HyperScriptHelperFn;
430 sub: HyperScriptHelperFn;
431 summary: HyperScriptHelperFn;
432 sup: HyperScriptHelperFn;
433 table: HyperScriptHelperFn;
434 tbody: HyperScriptHelperFn;
435 td: HyperScriptHelperFn;
436 textarea: HyperScriptHelperFn;
437 tfoot: HyperScriptHelperFn;
438 th: HyperScriptHelperFn;
439 thead: HyperScriptHelperFn;
440 time: HyperScriptHelperFn;
441 title: HyperScriptHelperFn;
442 tr: HyperScriptHelperFn;
443 u: HyperScriptHelperFn;
444 ul: HyperScriptHelperFn;
445 video: HyperScriptHelperFn;
446}