-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
187 lines (149 loc) · 3.41 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
IncrementalDOM api mock
*/
function elementOpen() {
var propertyValuePairs = Array.prototype.slice.call(arguments, 3, arguments.length)
elementOpenStart(arguments[0], arguments[1], arguments[2])
if (propertyValuePairs.length > 0) {
_attrsArray(propertyValuePairs)
}
return elementOpenEnd()
}
function elementOpenStart(tagname, key, staticPropertyValuePairs) {
_push('<' + tagname, true)
if (staticPropertyValuePairs && staticPropertyValuePairs.length > 0) {
_attrsArray(staticPropertyValuePairs)
}
}
function elementOpenEnd() {
_push('>', false)
_nestingCount++
return _elementDummy
}
function elementClose(tagname) {
_nestingCount--
_push('</' + tagname + '>', true)
return _elementDummy
}
function elementVoid() {
elementOpen.apply(null, arguments)
return elementClose(arguments[0])
}
function elementPlaceholder() {
elementOpen.apply(null, arguments)
return elementClose(arguments[0])
}
function attr(name, value) {
_push(' ' + name + '="' + value + '"', false)
}
function text(value, formatters) {
var formatted = value
for (var i = 1; i < arguments.length; i += 1) {
formatted = arguments[i](formatted)
}
_push('' + formatted, true)
}
function patch(node, description, data) {
if (typeof node === 'function') {
node(function () {
description(data)
})
} else {
description(data)
}
if (!_isStreamOutput) {
_output.rendered = _buffer.join('')
// the following two line are in browser fun only (usefull for testing)
// console.log(_output.rendered)
if (node.innerHTML) node.innerHTML = _output.rendered
}
if (_doneCallback) _doneCallback(_output)
if (!_keepOpen) _reset()
}
/*
Internal methods
*/
function _attrsArray(attrsArray) {
for (var i = 0; i < attrsArray.length; i += 2) {
attr(attrsArray[i], attrsArray[i + 1])
}
}
function _nestingSpace() {
return Array(_nestingCount).join(_prettyPrint)
}
function _push2Array(token, needsFormatting) {
if (_prettyPrint && needsFormatting) {
_buffer.push('\n')
_buffer.push(_nestingSpace())
_buffer.push(token)
} else {
_buffer.push(token)
}
}
function _push2Stream(token, needsFormatting) {
if (_prettyPrint && needsFormatting) {
_output.write('\n')
_output.write(_nestingSpace())
_output.write(token)
} else {
_output.write(token)
}
}
function _reset() {
setOutput()
_buffer = []
_nestingCount = 1
}
/*
Configuration api
*/
function setOutput(prettyPrint, output, doneCallback, keepOpen) {
_prettyPrint = prettyPrint
_output = output ? output : {}
_isStreamOutput = output && output.write ? true : false
_doneCallback = typeof doneCallback === 'function' ? doneCallback : undefined
_keepOpen = keepOpen
if (_isStreamOutput) {
_push = _push2Stream
} else {
_push = _push2Array
}
}
function setElementDummy(obj, merge) {
if (merge) {
Object.assign(_elementDummy, obj)
} else {
_elementDummy = obj
}
}
/*
Internal state
*/
var _elementDummy = {
addEventListener: function () {}
}
var _prettyPrint;
var _output;
var _isStreamOutput;
var _doneCallback;
var _keepOpen;
var _buffer;
var _nestingCount;
var _push;
_reset()
module.exports = {
// IncrementalDOM api mock
elementOpen: elementOpen,
elementOpenStart: elementOpenStart,
elementOpenEnd: elementOpenEnd,
elementClose: elementClose,
elementVoid: elementVoid,
elementPlaceholder: elementPlaceholder,
attr: attr,
text: text,
patch: patch,
// Configuration api
forceReset: _reset,
setOutput: setOutput,
setElementDummy: setElementDummy
}