forked from krux/postscribe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostscribe.js
735 lines (532 loc) · 17.4 KB
/
postscribe.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
// postscribe.js 1.0.5
// (c) Copyright 2012 to the present, Krux
// postscribe is freely distributable under the MIT license.
// For all details and documentation:
// http://krux.github.com/postscribe
(function() {
var globals = this;
if(globals.postscribe) {
return;
}
// Debug write tasks.
var DEBUG = true;
// Turn on to debug how each chunk affected the DOM.
var DEBUG_CHUNK = false;
// # Helper Functions
var slice = Array.prototype.slice;
// A function that intentionally does nothing.
function doNothing() {}
// Is this a function?
function isFunction(x) {
return "function" == typeof x;
}
// Loop over each item in an array-like value.
function each(arr, fn, _this) {
var i, len = (arr && arr.length) || 0;
for(i = 0; i < len; i++) {
fn.call(_this, arr[i], i);
}
}
// Loop over each key/value pair in a hash.
function eachKey(obj, fn, _this) {
var key;
for(key in obj) {
if(obj.hasOwnProperty(key)) {
fn.call(_this, key, obj[key]);
}
}
}
// Set properties on an object.
function set(obj, props) {
eachKey(props, function(key, value) {
obj[key] = value;
});
return obj;
}
// Set default options where some option was not specified.
function defaults(options, _defaults) {
options = options || {};
eachKey(_defaults, function(key, val) {
if(options[key] == null) {
options[key] = val;
}
});
return options;
}
// Convert value (e.g., a NodeList) to an array.
function toArray(obj) {
try {
return slice.call(obj);
} catch(e) {
var ret = [];
each(obj, function(val) {
ret.push(val);
});
return ret;
}
}
// Test if token is a script tag.
function isScript(tok) {
return (/^script$/i).test(tok.tagName);
}
// # Class WriteStream
// Stream static html to an element, where "static html" denotes "html without scripts".
// This class maintains a *history of writes devoid of any attributes* or "proxy history".
// Injecting the proxy history into a temporary div has no side-effects,
// other than to create proxy elements for previously written elements.
// Given the `staticHtml` of a new write, a `tempDiv`'s innerHTML is set to `proxy_history + staticHtml`.
// The *structure* of `tempDiv`'s contents, (i.e., the placement of new nodes beside or inside of proxy elements),
// reflects the DOM structure that would have resulted if all writes had been squashed into a single write.
// For each descendent `node` of `tempDiv` whose parentNode is a *proxy*, `node` is appended to the corresponding *real* element within the DOM.
// Proxy elements are mapped to *actual* elements in the DOM by injecting a data-id attribute into each start tag in `staticHtml`.
var WriteStream = (function(){
// Prefix for data attributes on DOM elements.
var BASEATTR = 'data-ps-';
// get / set data attributes
function data(el, name, value) {
var attr = BASEATTR + name;
if(arguments.length === 2) {
// Get
var val = el.getAttribute(attr);
// IE 8 returns a number if it's a number
return val == null ? val : String(val);
} else if( value != null && value !== '') {
// Set
el.setAttribute(attr, value);
} else {
// Remove
el.removeAttribute(attr);
}
}
function WriteStream(root) {
// Actual elements by id.
this.actuals = [root];
// Embodies the "structure" of what's been written so far, devoid of attributes.
this.proxyHistory = '';
// Create a proxy of the root element.
this.proxyRoot = root.ownerDocument.createElement(root.nodeName);
data(this.proxyRoot, 'proxyof', 0);
}
WriteStream.prototype.buildChunk = function (tokens) {
var nextId = this.actuals.length,
// The raw html of this chunk.
raw = [],
// The html to create the nodes in the tokens (with id's injected).
actual = [],
// Html that can later be used to proxy the nodes in the tokens.
proxy = [];
each(tokens, function(tok) {
raw.push(tok.text);
if(tok.attrs) {
// Visit a token with attributes (startTag or atomicTag).
// Ignore noscript tags. They are atomic, so we don't have to worry about children.
if(!(/^noscript$/i).test(tok.tagName)) {
var id = nextId++;
// Actual: inject id attribute: replace '>' at end of start tag with id attribute + '>'
actual.push(
tok.text.replace(/(\/?>)/, ' '+BASEATTR+'id='+id+' $1')
);
// Proxy: strip all attributes and inject proxyof attribute
proxy.push(
// ignore atomic tags (e.g., style): they have no "structural" effect
tok.type === 'atomicTag' ? '' :
'<'+tok.tagName+' '+BASEATTR+'proxyof='+id+(tok.unary ? '/>' : '>')
);
}
} else {
// Visit any other type of token
// Actual: append.
actual.push(tok.text);
// Proxy: append endTags. Ignore everything else.
proxy.push(tok.type === 'endTag' ? tok.text : '');
}
});
return {
tokens: tokens,
raw: raw.join(''),
actual: actual.join(''),
proxy: proxy.join('')
};
};
WriteStream.prototype.write = function(tokens) {
var chunk = this.buildChunk(tokens);
if(!chunk.actual) {
// e.g., no tokens, or a noscript that got ignored
return;
}
chunk.html = this.proxyHistory + chunk.actual;
this.proxyHistory += chunk.proxy;
this.proxyRoot.innerHTML = chunk.html;
if(DEBUG_CHUNK) {
chunk.proxyInnerHTML = this.proxyRoot.innerHTML;
}
this.walkNodes();
if(DEBUG_CHUNK) {
chunk.actualInnerHTML = this.actuals[0].innerHTML; //root
}
return chunk;
};
WriteStream.prototype.walkNodes = function() {
var node, stack = [this.proxyRoot];
// use shift/unshift so that children are walked in document order
while((node = stack.shift()) != null) {
var isElement = node.nodeType === 1;
var isProxy = isElement && data(node, 'proxyof');
// Ignore proxies
if(!isProxy) {
if(isElement) {
// New actual element: register it and remove the the id attr.
this.actuals[data(node, 'id')] = node;
data(node, 'id', null);
}
// Is node's parent a proxy?
var parentIsProxyOf = node.parentNode && data(node.parentNode, 'proxyof');
if(parentIsProxyOf) {
// Move node under actual parent.
this.actuals[parentIsProxyOf].appendChild(node);
}
}
// prepend childNodes to stack
stack.unshift.apply(stack, toArray(node.childNodes));
}
};
return WriteStream;
}());
// # Class Worker
// Perform tasks in the context of an element.
var Worker = (function() {
function Worker(el, options) {
// Default options
var doc = el.ownerDocument;
set(this, {
root: el,
options: defaults(options, { error: doNothing }),
stream: new WriteStream(el),
parser: globals.htmlParser('', { autoFix: true }),
doc: doc,
win: doc.defaultView|| doc.parentWindow
});
}
Worker.prototype.exec = function(task, done) {
task.run.call(this.win, this.doc);
delete task.run;
done();
};
// The method on the window object used for 'eval'
var EVAL = globals.execScript ? 'execScript' : 'eval';
Worker.prototype.script_inline = function(task, done) {
try {
this.win[EVAL](task.expr);
} catch(e) {
this.options.error(e);
}
done();
};
Worker.prototype.script_remote = function(task, done) {
var _this = this;
var s = this.doc.createElement('script');
var props = {
src: task.src,
async: true,
// Handlers
onload: function() {
s = s.onload = s.onreadystatechange = s.onerror = null;
done();
},
onreadystatechange: function() {
if(/^(loaded|complete)$/.test( s.readyState )) {
s.onload();
}
},
onerror: function() {
_this.options.error({ message: 'remote script failed ' + task.src });
s.onload();
}
};
// Set attributes
eachKey(task.tok.attrs, function(name, value) {
if(!props.hasOwnProperty(name)) {
s.setAttribute(name, value);
}
});
set(s, props);
this.root.appendChild(s);
};
// Write task
Worker.prototype.write = function(task, done, flow) {
this.parser.append(task.html);
var tok, tokens = [];
// stop if we see a script token
while((tok = this.parser.readToken()) != null && !isScript(tok)) {
tokens.push(tok);
}
// Write out this chunk of tokens
var chunk = this.stream.write(tokens);
if(DEBUG_CHUNK) {
task.chunk = chunk;
}
if(tok) {
this.onScriptToken(tok, flow);
}
done();
};
// We encountered a script token while writing.
Worker.prototype.onScriptToken = function(tok, flow) {
// Stash remainder of parser for during this script.
var remainder = this.parser.clear();
// Subtask: Run this script.
var src = tok.attrs.src || tok.attrs.SRC;
flow.subtask( src ?
// Remote script: cannot be inlined.
{ type: 'script_remote', src: src, tok: tok } :
// Inline script.
{ type: 'script_inline', inlinable: true, tok: tok, expr: (tok.content)
// remove CDATA and HTML comments
.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, "$1")
.replace(/<!--([\s\S]*?)-->/g, "$1")
}
);
// Subtask: Write remainder behind script.
if(remainder) {
flow.subtask({ type: 'write', html: remainder, inlinable: true });
}
};
return Worker;
}());
// ## Class Flow
// Controls the flow of a tree of tasks with subtasks
// Subtasks of a task are those tasks that are added while that task is the active task.
// 1. task _A_ and all its "subtasks" are done before any tasks following _A_ (in tree order)
// 2. A task is inlined if it is inlinable and there are no deferred tasks (because of point #1). Else it is defered.
// Special task properties:
// * type
// * start
// * complete
var Flow = (function() {
// param worker[task.type](task, done): an object with async callbacks to execute each task type.
function Flow(worker, options) {
var deferred = [];
set(this, {
// The worker performs the tasks.
worker: worker,
options: defaults(options, {
taskAdd: doNothing,
taskStart: doNothing,
taskDone: doNothing
}),
// The active (currently executing) task.
active: null,
// The list of deferred tasks.
// Only done when idle.
deferred: deferred,
// The active task's deferred decendant subtasks.
_deferred: deferred
});
}
// Add a "root" task.
Flow.prototype.task = function(task, done) {
this.options.taskAdd(task);
this.deferred.push(task);
if(done) {
this.deferred.push(done);
}
this.nextIfIdle();
return this;
};
// Add a subtask of active task.
Flow.prototype.subtask = function(child) {
this.options.taskAdd(child);
if(child.inlinable && !this._deferred.length) {
// Inline this child.
this.startTask(child);
} else {
// Defer this child.
this._deferred.push(child);
}
};
// Start a task.
Flow.prototype.startTask = function(task) {
var _this = this;
if(this.stopRequested) {
return this._deferred.unshift(task);
}
// Functions are light-weight tasks.
if(isFunction(task)) {
task.call(this);
return this.nextIfIdle();
}
// Stash the active task and queue
var stash = { active: this.active, _deferred: this._deferred };
// Collect deferred subtasks for this task.
set(this, { active: task, _deferred: [] });
this.options.taskStart(task);
// Worker's method is passed the task, done callback, and this flow.
this.worker[task.type](task, function() {
_this.doneTask(stash);
}, this);
};
// Called when active task is done.
Flow.prototype.doneTask = function(stash) {
this.options.taskDone(this.active);
// Prepend deferred to stashed deferred in-place.
// When idle, this.deferred will hold all _deferred in the right order.
[].unshift.apply(stash._deferred, this._deferred);
// Restore stashed state.
set(this, stash);
// Are we are waiting to stop?
if( this.onStop && !this.active ) {
this.onStop(); delete this.onStop;
}
this.nextIfIdle();
};
// Run the next deferred task if no other task is running.
Flow.prototype.nextIfIdle = function() {
// !this.active <==> (this._deferred === this.deferred)
var task = !this.active && this.deferred.shift();
if(task) {
this.startTask(task);
}
};
// Stop this flow
Flow.prototype.stop = function(onStop) {
// Callback when flow has actually stopped.
onStop = onStop || doNothing;
this.stopRequested = true;
if(!this.active) {
// We are ready to stop now.
onStop();
} else {
// We will stop when next we are idle.
this.onStop = onStop;
}
};
// Start this flow.
Flow.prototype.start = function() {
this.stopRequested = false;
delete this.onStop;
this.nextIfIdle();
return this;
};
return Flow;
}());
// ## Class Tracer (Debugging)
// Traces the relationships between tasks.
var Tracer = (function() {
function Tracer() {
set(this, {
// All tasks by id.
tasks: [],
// Tasks with no parent.
roots: [],
// The active task.
active: null
});
}
Tracer.prototype.taskAdd = function(task) {
task.id = this.tasks.length;
this.tasks.push(task);
task.state = 'waiting';
if(this.active) {
task.cause = this.active.id;
(this.active.effects = this.active.effects || []).push(task.id);
}
return task;
};
Tracer.prototype.taskStart = function(task) {
var parent = this.active;
if(parent) {
task.parent = parent.id;
(parent.childIds = parent.childIds || []).push(task.id);
(parent.children = parent.children || []).push(task);
} else {
this.roots.push(task);
}
task.state = 'started';
this.active = task;
};
Tracer.prototype.taskDone = function(task) {
task.state = 'done';
this.active = task.parent != null ? this.tasks[task.parent] : null;
};
return Tracer;
}());
// Public-facing interface and queuing
var postscribe = (function() {
var nextId = 0;
function start(el, rootTask, options, done) {
options = defaults(options, {
afterWrite: doNothing,
done: doNothing
});
// Create the flow.
var worker = new Worker(el, options);
var flow = new Flow(worker, DEBUG && new Tracer());
flow.id = nextId++;
flow.name = options.name || flow.id;
postscribe.flows[flow.name] = flow;
// Override document.write.
var doc = el.ownerDocument;
var stash = { write: doc.write, writeln: doc.writeln };
function write(str) {
flow.subtask({ type: 'write', html: str, inlinable: true });
options.afterWrite(str);
}
set(doc, { write: write, writeln: function(str) { write(str + '\n'); } });
// Start the flow
flow.task(rootTask, function() {
// restore document.write
set(doc, stash);
options.done();
done();
});
return flow;
}
var queue = new Flow({
rootTask: function(args, done) {
args.push(done);
args.flow = start.apply(null, args);
}
});
function postscribe(el, html, options) {
el =
// id selector
(/^#/).test(el) ? globals.document.getElementById(el.substr(1)) :
// jquery object. TODO: loop over all elements.
el.jquery ? el[0] : el;
options = options || {};
var rootTask = isFunction(html) ?
{ type: 'exec', run: html } :
{ type: 'write', html: html };
var args = set([el, rootTask, options], { type: 'rootTask' });
queue.task(args);
return (el.postscribe = {
stop: function() {
if(args.flow) {
args.flow.stop();
} else {
// Set a root task that does nothing.
args[1] = { type: "exec", run: doNothing };
}
}
});
}
return set(postscribe, {
flows: {},
queue: queue,
// Expose internal classes.
Worker: Worker,
Flow: Flow,
Tracer: Tracer,
WriteStream: WriteStream,
json: function() {
var ret = {};
eachKey(this.flows, function(name, flow) {
ret[name] = flow.options.roots;
});
return ret;
}
});
}());
// export postscribe
globals.postscribe = postscribe;
}());