forked from MithrilJS/mithril-node-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·136 lines (112 loc) · 3.32 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict'
var VOID_TAGS = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr',
'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track',
'wbr', '!doctype']
function isArray (thing) {
return Object.prototype.toString.call(thing) === '[object Array]'
}
function camelToDash (str) {
return str.replace(/\W+/g, '-')
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
}
function removeEmpties (n) {
return n !== ''
}
// shameless stolen from https://github.com/punkave/sanitize-html
function escapeHtml (s, replaceDoubleQuote) {
if (s === 'undefined') {
s = ''
}
if (typeof (s) !== 'string') {
s = s + ''
}
s = s.replace(/\&/g, '&').replace(/</g, '<').replace(/\>/g, '>')
if (replaceDoubleQuote) {
return s.replace(/\"/g, '"')
}
return s
}
function createAttrString (view, escapeAttributeValue) {
var attrs = view.attrs
if (!attrs || !Object.keys(attrs).length) {
return ''
}
return Object.keys(attrs).map(function (name) {
var value = attrs[name]
if (typeof value === 'undefined' || value === null || typeof value === 'function') {
return
}
if (typeof value === 'boolean') {
return value ? ' ' + name : ''
}
if (name === 'style') {
if (!value) {
return
}
var styles = attrs.style
if (typeof styles === 'object') {
styles = Object.keys(styles).map(function (property) {
return styles[property] !== '' ? [camelToDash(property).toLowerCase(), styles[property]].join(':') : ''
}).filter(removeEmpties).join(';')
}
return styles !== '' ? ' style="' + escapeAttributeValue(styles, true) + '"' : ''
}
// Handle SVG <use> tags specially
if (name === 'href' && view.tag === 'use') {
return ' xlink:href="' + escapeAttributeValue(value, true) + '"'
}
return ' ' + (name === 'className' ? 'class' : name) + '="' + escapeAttributeValue(value, true) + '"'
}).join('')
}
function createChildrenContent (view) {
if (isArray(view.children) && !view.children.length) {
return ''
}
return render(view.children)
}
function render (view, options) {
options = options || {}
var defaultOptions = {
escapeAttributeValue: escapeHtml,
escapeString: escapeHtml
}
Object.keys(defaultOptions).forEach(function (key) {
if (!options.hasOwnProperty(key)) options[key] = defaultOptions[key]
})
var type = typeof view
if (type === 'string') {
return options.escapeString(view)
}
if (type === 'number' || type === 'boolean') {
return view
}
if (!view) {
return ''
}
if (isArray(view)) {
return view.map(function (view) { return render(view, options) }).join('')
}
// compontent
if (view.view) {
var scope = view.controller ? new view.controller() : {}
var result = render(view.view(scope), options)
if (scope.onunload) {
scope.onunload()
}
return result
}
if (view.$trusted) {
return '' + view
}
var children = createChildrenContent(view)
if (!children && VOID_TAGS.indexOf(view.tag.toLowerCase()) >= 0) {
return '<' + view.tag + createAttrString(view, options.escapeAttributeValue) + '>'
}
return [
'<', view.tag, createAttrString(view, options.escapeAttributeValue), '>',
children,
'</', view.tag, '>'
].join('')
}
render.escapeHtml = escapeHtml
module.exports = render