/
xml.coffee
362 lines (315 loc) · 10.2 KB
/
xml.coffee
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
{ EventEmitter } = require 'events'
{ deep_merge, new_attrs, safe } = require './util'
EVENTS = ['add', 'attr', 'data', 'text', 'raw', 'show', 'hide', 'remove',
'replace', 'close']
# could be boosted up by using a c lib → https://github.com/wezm/node-genx
# TODO add clone method to tag so tags can be cloned (param: deep for deep copy)
# TODO partials?
parse_args = (name, attrs, children, opts) ->
unless typeof attrs is 'object'
[opts, children, attrs] = [children, attrs, {}]
else
# if attrs is an object and you want to use opts, make children null
attrs ?= {}
opts ?= {}
return [name, attrs, children, opts]
connect_tags = (parent, child) ->
listeners = {}
pipe = (event) ->
return if listeners[event]?
child.on? event, listeners[event] = ->
parent.emit(event, arguments...)
wire = ->
pipe event for event in EVENTS
dispose = ->
for event in EVENTS
if (listener = listeners[event])?
child.removeListener?(event, listener)
listeners[event] = undefined
remove = (soft) ->
if this is child
parent.removeListener('removed', remove)
parent.removeListener('replaced', replace)
child.removeListener('replaced', replace)
dispose()
else if soft
parent.once('removed', remove)
else
child.removeListener('removed', remove)
parent.removeListener('replaced', replace)
child.removeListener('replaced', replace)
dispose()
replace = (tag) ->
if this is child
remove.call(parent)
child = tag
wire()
else
parent.removeListener('removed', remove)
parent = tag
tag.once('replaced', replace)
tag.once('removed', remove)
wire() # add
child.once('removed', remove)
parent.once('removed', remove)
child.once('replaced', replace)
parent.once('replaced', replace)
add_tag = (newtag, callback) ->
return callback?.call(this) unless newtag?
# only when the builder approves the new tag we can proceed with announcing
# the new tag to the parent and to the tree and apply the childrenscope
wire_tag = (_, tag) =>
tag.builder ?= @builder
tag.parent ?= this
tag.builder.opts.pretty = @builder.opts.pretty
tag.builder.level = @level
connect_tags(this, tag)
@emit 'add', this, tag
@emit 'new', tag
@isempty = no
tag.emit? 'close', tag if tag.closed
callback?.call(this, tag)
newtag.parent = this
if @builder?
@builder.approve('new', this, newtag, wire_tag)
else
wire_tag(this, newtag)
new_tag = ->
[name, attrs, children, opts] = parse_args arguments...
opts.level ?= @level+1
# possibility to overwrite existing opts, like pretty
opts = deep_merge (@builder?.opts ? {}), opts
opts.builder = @builder
TagInstance = @builder?.Tag ? Tag
newtag = new TagInstance name, attrs, null, opts
add_tag.call this, newtag, (tag) ->
tag.children children, opts if children?
return newtag # hopefully this is still the same after the approval
sync_tag = ->
[name, attrs, children, opts] = parse_args arguments...
self_ending_children_scope = ->
@children children
@end()
new_tag.call this, name, attrs, self_ending_children_scope, opts
class Tag extends EventEmitter
constructor: ->
[@name, @attrs, children, opts] = parse_args arguments...
@pretty = opts.pretty ? off
@level = opts.level ? 0
@builder = opts.builder # inheritence
@setMaxListeners(0)
@parent = @builder
@closed = no
@writable = true
@hidden = no
@isready = no
@isempty = yes
@content = ""
@children children, opts
@$tag = sync_tag
@tag = new_tag
attr: (key, value) =>
if typeof key is 'string'
if not value?
attr = @builder?.query('attr', this, key)
# attr is not defined if attr is undefined
# attr doesn't have a value when attr is null
unless attr is undefined
# sync it and return value
@attrs[key] = attr
return attr
@attrs[key] = value
@emit 'attr', this, key, value
else
for own k, v of key
unless v is undefined
@attrs[k] = v
else
delete @attr[key]
@emit 'attr', this, k, v
this
removeAttr: (keys...) =>
for key in keys
delete @attrs[key]
@emit 'attr', this, key, undefined
this
children: (children) =>
return this unless children?
if typeof children is 'function'
children.call this
else
@text children
this
text: (content, opts = {}) =>
unless content?
return @content = @builder?.query 'text', this
content = safe(content) if opts.escape
if opts.append
@content += content
else
@content = content
@emit('text', this, content)
@isempty = no
this
raw: (html, opts = {}) =>
@emit('raw', this, html)
@isempty = no
this
write: (content, {escape, append} = {}) =>
content = safe(content) if escape
@emit 'data', this, "#{content}" if content
if append ? yes
@content += content
else
@content = content
@isempty = no
true
up: (opts = {}) =>
opts.end ?= true
@end arguments... if opts.end
@parent
show: () =>
@hidden = no
@emit 'show', this
this
hide: () =>
@hidden = yes
@emit 'hide', this
this
end: () =>
if not @closed
@closed = 'approving'
close_tag = =>
if @isempty
@closed = 'self'
else
@closed = yes
@emit 'close', this
@emit 'end'
@writable = false
set_ready = =>
@isready = yes
@emit 'ready'
if @builder?
@builder.approve('ready', this, set_ready)
else
set_ready()
if @builder?
@builder.approve('end', this, close_tag)
else
close_tag(this, this)
else if @closed is 'approving'
# just wait
else if @closed is 'removed'
@emit 'end'
@writable = false
else
@closed = yes
@writable = false
this
toString: () =>
"<#{@name}#{new_attrs @attrs}" +
if @closed is 'self'
"/>"
else if @closed
">#{@content}</#{@name}>"
else
">#{@content}"
add: (rawtag, callback) =>
tag = @builder?.query 'tag', this, rawtag
tag = rawtag unless tag? or @builder?
# TODO query result validation
add_tag.call(this, tag, callback)
this
replace: (rawtag) =>
tag = @builder?.query 'tag', this, rawtag
tag = rawtag unless tag? or @builder?
return this if this is tag
tag.parent ?= @parent
tag.builder ?= @builder
@emit 'replace', this, tag
if @builder is tag.builder
@builder = null
@parent = null
@emit 'replaced', tag # internal
tag
remove: (opts = {}) =>
@closed = 'removed' unless @closed or opts.soft
@emit 'remove', this, opts
@builder = null unless this is @builder
@parent = null
@emit 'removed', opts.soft # internal
@removeAllListeners() unless opts.soft
this
ready: (callback) =>
if @isready
callback?.call(this)
return this
@once 'ready', callback
this
class Builder extends EventEmitter
constructor: (@opts = {}) ->
# values
@builder = this
@checkers = {} # all the middlewares that have to approve a new tag
# states
@closed = no
@isempty = yes
# defaults
@opts.pretty ?= off
@level = @opts.level ? -1
@setMaxListeners(0)
# api
@Tag = Tag
@tag = new_tag
@$tag = sync_tag
show: Tag::show
hide: Tag::hide
remove: Tag::remove
toString: ->
"[object AsyncXMLBuilder]"
add: (rawtag, callback) =>
tag = @query 'tag', this, rawtag
tag = rawtag unless tag?
# TODO query result validation
add_tag.call(this, tag, callback)
this
end: () =>
@closed = yes
@emit 'close', this # tag api
@emit 'end'
this
ready: (callback) =>
return callback?.call(this) if @closed is yes
@once 'end', callback
# intern getter to let intern tag structure stay in sync with eg dom
query: (type, tag, key) ->
if type is 'attr'
tag.attrs[key]
else if type is 'text'
tag.content
else if type is 'tag'
key # this should be a tag
register: (type, checker) ->
unless type is 'new' or type is 'end' or type is 'ready'
throw new Error "only type 'ready', 'new' or 'end' allowed."
@checkers[type] ?= []
@checkers[type].push checker
approve: (type, parent, tag, callback) ->
checkers = @checkers[type]?.slice?() ? []
switch type
when 'new'
next = (tag) ->
checker = checkers.shift() ? callback
checker(parent, tag, next)
when 'ready', 'end'
[callback, tag] = [tag, parent] # shift arguments
next = (tag) ->
checker = checkers.shift() ? callback
checker(tag, next)
else
throw new Error "type '#{type}' not supported."
# start
next(tag)
# exports
module.exports = { Tag, Builder }