-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvhtml.js
More file actions
60 lines (52 loc) · 1.41 KB
/
vhtml.js
File metadata and controls
60 lines (52 loc) · 1.41 KB
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
53
54
55
56
57
58
59
60
import emptyTags from './empty-tags';
// escape an attribute
let esc = str => String(str).replace(/[&<>"']/g, s=>`&${map[s]};`);
let map = {'&':'amp','<':'lt','>':'gt','"':'quot',"'":'apos'};
let setInnerHTMLAttr = 'dangerouslySetInnerHTML';
let DOMAttributeNames = {
className: 'class',
htmlFor: 'for'
};
let sanitized = {};
/** Hyperscript reviver that constructs a sanitized HTML string. */
export default function h(name, attrs) {
let stack=[], s = '';
attrs = attrs || {};
for (let i=arguments.length; i-- > 2; ) {
stack.push(arguments[i]);
}
// Sortof component support!
if (typeof name==='function') {
attrs.children = stack.reverse();
return name(attrs);
// return name(attrs, stack.reverse());
}
if (name) {
s += '<' + name;
if (attrs) for (let i in attrs) {
if (attrs[i]!==false && attrs[i]!=null && i !== setInnerHTMLAttr) {
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`;
}
}
s += '>';
}
if (emptyTags.indexOf(name) === -1) {
if (attrs[setInnerHTMLAttr]) {
s += attrs[setInnerHTMLAttr].__html;
}
else while (stack.length) {
let child = stack.pop();
if (child !== undefined && child !== null) {
if (child.pop) {
for (let i=child.length; i--; ) stack.push(child[i]);
}
else {
s += sanitized[child]===true ? child : esc(child);
}
}
}
s += name ? `</${name}>` : '';
}
sanitized[s] = true;
return s;
}