-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
526 lines (426 loc) · 15.1 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
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
var domtagger = (function (document) {
'use strict';
/*! (c) Andrea Giammarchi - ISC */
var self = {};
try {
self.WeakMap = WeakMap;
} catch (WeakMap) {
// this could be better but 90% of the time
// it's everything developers need as fallback
self.WeakMap = function (id, Object) {
var dP = Object.defineProperty;
var hOP = Object.hasOwnProperty;
var proto = WeakMap.prototype;
proto["delete"] = function (key) {
return this.has(key) && delete key[this._];
};
proto.get = function (key) {
return this.has(key) ? key[this._] : void 0;
};
proto.has = function (key) {
return hOP.call(key, this._);
};
proto.set = function (key, value) {
dP(key, this._, {
configurable: true,
value: value
});
return this;
};
return WeakMap;
function WeakMap(iterable) {
dP(this, '_', {
value: '_@ungap/weakmap' + id++
});
if (iterable) iterable.forEach(add, this);
}
function add(pair) {
this.set(pair[0], pair[1]);
}
}(Math.random(), Object);
}
var WeakMap$1 = self.WeakMap;
/*! (c) Andrea Giammarchi - ISC */
var createContent = function (document) {
var FRAGMENT = 'fragment';
var TEMPLATE = 'template';
var HAS_CONTENT = ('content' in create(TEMPLATE));
var createHTML = HAS_CONTENT ? function (html) {
var template = create(TEMPLATE);
template.innerHTML = html;
return template.content;
} : function (html) {
var content = create(FRAGMENT);
var template = create(TEMPLATE);
var childNodes = null;
if (/^[^\S]*?<(col(?:group)?|t(?:head|body|foot|r|d|h))/i.test(html)) {
var selector = RegExp.$1;
template.innerHTML = '<table>' + html + '</table>';
childNodes = template.querySelectorAll(selector);
} else {
template.innerHTML = html;
childNodes = template.childNodes;
}
append(content, childNodes);
return content;
};
return function createContent(markup, type) {
return (type === 'svg' ? createSVG : createHTML)(markup);
};
function append(root, childNodes) {
var length = childNodes.length;
while (length--) {
root.appendChild(childNodes[0]);
}
}
function create(element) {
return element === FRAGMENT ? document.createDocumentFragment() : document.createElementNS('http://www.w3.org/1999/xhtml', element);
} // it could use createElementNS when hasNode is there
// but this fallback is equally fast and easier to maintain
// it is also battle tested already in all IE
function createSVG(svg) {
var content = create(FRAGMENT);
var template = create('div');
template.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg">' + svg + '</svg>';
append(content, template.firstChild.childNodes);
return content;
}
}(document);
/*! (c) Andrea Giammarchi - ISC */
var importNode = function (document, appendChild, cloneNode, createTextNode, importNode) {
var _native = (importNode in document); // IE 11 has problems with cloning templates:
// it "forgets" empty childNodes. This feature-detects that.
var fragment = document.createDocumentFragment();
fragment[appendChild](document[createTextNode]('g'));
fragment[appendChild](document[createTextNode](''));
/* istanbul ignore next */
var content = _native ? document[importNode](fragment, true) : fragment[cloneNode](true);
return content.childNodes.length < 2 ? function importNode(node, deep) {
var clone = node[cloneNode]();
for (var
/* istanbul ignore next */
childNodes = node.childNodes || [], length = childNodes.length, i = 0; deep && i < length; i++) {
clone[appendChild](importNode(childNodes[i], deep));
}
return clone;
} :
/* istanbul ignore next */
_native ? document[importNode] : function (node, deep) {
return node[cloneNode](!!deep);
};
}(document, 'appendChild', 'cloneNode', 'createTextNode', 'importNode');
var trim = ''.trim ||
/* istanbul ignore next */
function () {
return String(this).replace(/^\s+|\s+/g, '');
};
/*! (c) Andrea Giammarchi - ISC */
// Custom
var UID = '-' + Math.random().toFixed(6) + '%'; // Edge issue!
var UID_IE = false;
try {
if (!function (template, content, tabindex) {
return content in template && (template.innerHTML = '<p ' + tabindex + '="' + UID + '"></p>', template[content].childNodes[0].getAttribute(tabindex) == UID);
}(document.createElement('template'), 'content', 'tabindex')) {
UID = '_dt: ' + UID.slice(1, -1) + ';';
UID_IE = true;
}
} catch (meh) {}
var UIDC = '<!--' + UID + '-->'; // DOM
var COMMENT_NODE = 8;
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;
var SHOULD_USE_TEXT_CONTENT = /^(?:plaintext|script|style|textarea|title|xmp)$/i;
var VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i;
/*! (c) Andrea Giammarchi - ISC */
function sanitize (template) {
return template.join(UIDC).replace(selfClosing, fullClosing).replace(attrSeeker, attrReplacer);
}
var spaces = ' \\f\\n\\r\\t';
var almostEverything = '[^' + spaces + '\\/>"\'=]+';
var attrName = '[' + spaces + ']+' + almostEverything;
var tagName = '<([A-Za-z]+[A-Za-z0-9:._-]*)((?:';
var attrPartials = '(?:\\s*=\\s*(?:\'[^\']*?\'|"[^"]*?"|<[^>]*?>|' + almostEverything.replace('\\/', '') + '))?)';
var attrSeeker = new RegExp(tagName + attrName + attrPartials + '+)([' + spaces + ']*/?>)', 'g');
var selfClosing = new RegExp(tagName + attrName + attrPartials + '*)([' + spaces + ']*/>)', 'g');
var findAttributes = new RegExp('(' + attrName + '\\s*=\\s*)([\'"]?)' + UIDC + '\\2', 'gi');
function attrReplacer($0, $1, $2, $3) {
return '<' + $1 + $2.replace(findAttributes, replaceAttributes) + $3;
}
function replaceAttributes($0, $1, $2) {
return $1 + ($2 || '"') + UID + ($2 || '"');
}
function fullClosing($0, $1, $2) {
return VOID_ELEMENTS.test($1) ? $0 : '<' + $1 + $2 + '></' + $1 + '>';
}
var umap = (function (_) {
return {
// About: get: _.get.bind(_)
// It looks like WebKit/Safari didn't optimize bind at all,
// so that using bind slows it down by 60%.
// Firefox and Chrome are just fine in both cases,
// so let's use the approach that works fast everywhere 👍
get: function get(key) {
return _.get(key);
},
set: function set(key, value) {
return _.set(key, value), value;
}
};
});
/* istanbul ignore next */
var normalizeAttributes = UID_IE ? function (attributes, parts) {
var html = parts.join(' ');
return parts.slice.call(attributes, 0).sort(function (left, right) {
return html.indexOf(left.name) <= html.indexOf(right.name) ? -1 : 1;
});
} : function (attributes, parts) {
return parts.slice.call(attributes, 0);
};
function find(node, path) {
var length = path.length;
var i = 0;
while (i < length) {
node = node.childNodes[path[i++]];
}
return node;
}
function parse(node, holes, parts, path) {
var childNodes = node.childNodes;
var length = childNodes.length;
var i = 0;
while (i < length) {
var child = childNodes[i];
switch (child.nodeType) {
case ELEMENT_NODE:
var childPath = path.concat(i);
parseAttributes(child, holes, parts, childPath);
parse(child, holes, parts, childPath);
break;
case COMMENT_NODE:
var textContent = child.textContent;
if (textContent === UID) {
parts.shift();
holes.push( // basicHTML or other non standard engines
// might end up having comments in nodes
// where they shouldn't, hence this check.
SHOULD_USE_TEXT_CONTENT.test(node.nodeName) ? Text(node, path) : Any(child, path.concat(i)));
} else {
switch (textContent.slice(0, 2)) {
case '/*':
if (textContent.slice(-2) !== '*/') break;
case "\uD83D\uDC7B":
// ghost
node.removeChild(child);
i--;
length--;
}
}
break;
case TEXT_NODE:
// the following ignore is actually covered by browsers
// only basicHTML ends up on previous COMMENT_NODE case
// instead of TEXT_NODE because it knows nothing about
// special style or textarea behavior
/* istanbul ignore if */
if (SHOULD_USE_TEXT_CONTENT.test(node.nodeName) && trim.call(child.textContent) === UIDC) {
parts.shift();
holes.push(Text(node, path));
}
break;
}
i++;
}
}
function parseAttributes(node, holes, parts, path) {
var attributes = node.attributes;
var cache = [];
var remove = [];
var array = normalizeAttributes(attributes, parts);
var length = array.length;
var i = 0;
while (i < length) {
var attribute = array[i++];
var direct = attribute.value === UID;
var sparse;
if (direct || 1 < (sparse = attribute.value.split(UIDC)).length) {
var name = attribute.name; // the following ignore is covered by IE
// and the IE9 double viewBox test
/* istanbul ignore else */
if (cache.indexOf(name) < 0) {
cache.push(name);
var realName = parts.shift().replace(direct ? /^(?:|[\S\s]*?\s)(\S+?)\s*=\s*('|")?$/ : new RegExp('^(?:|[\\S\\s]*?\\s)(' + name + ')\\s*=\\s*(\'|")[\\S\\s]*', 'i'), '$1');
var value = attributes[realName] || // the following ignore is covered by browsers
// while basicHTML is already case-sensitive
/* istanbul ignore next */
attributes[realName.toLowerCase()];
if (direct) holes.push(Attr(value, path, realName, null));else {
var skip = sparse.length - 2;
while (skip--) {
parts.shift();
}
holes.push(Attr(value, path, realName, sparse));
}
}
remove.push(attribute);
}
}
length = remove.length;
i = 0;
/* istanbul ignore next */
var cleanValue = 0 < length && UID_IE && !('ownerSVGElement' in node);
while (i < length) {
// Edge HTML bug #16878726
var attr = remove[i++]; // IE/Edge bug lighterhtml#63 - clean the value or it'll persist
/* istanbul ignore next */
if (cleanValue) attr.value = ''; // IE/Edge bug lighterhtml#64 - don't use removeAttributeNode
node.removeAttribute(attr.name);
} // This is a very specific Firefox/Safari issue
// but since it should be a not so common pattern,
// it's probably worth patching regardless.
// Basically, scripts created through strings are death.
// You need to create fresh new scripts instead.
// TODO: is there any other node that needs such nonsense?
var nodeName = node.nodeName;
if (/^script$/i.test(nodeName)) {
// this used to be like that
// var script = createElement(node, nodeName);
// then Edge arrived and decided that scripts created
// through template documents aren't worth executing
// so it became this ... hopefully it won't hurt in the wild
var script = document.createElement(nodeName);
length = attributes.length;
i = 0;
while (i < length) {
script.setAttributeNode(attributes[i++].cloneNode(true));
}
script.textContent = node.textContent;
node.parentNode.replaceChild(script, node);
}
}
function Any(node, path) {
return {
type: 'any',
node: node,
path: path
};
}
function Attr(node, path, name, sparse) {
return {
type: 'attr',
node: node,
path: path,
name: name,
sparse: sparse
};
}
function Text(node, path) {
return {
type: 'text',
node: node,
path: path
};
}
// globals
var parsed = umap(new WeakMap$1());
function createInfo(options, template) {
var markup = (options.convert || sanitize)(template);
var transform = options.transform;
if (transform) markup = transform(markup);
var content = createContent(markup, options.type);
cleanContent(content);
var holes = [];
parse(content, holes, template.slice(0), []);
return {
content: content,
updates: function updates(content) {
var updates = [];
var len = holes.length;
var i = 0;
var off = 0;
while (i < len) {
var info = holes[i++];
var node = find(content, info.path);
switch (info.type) {
case 'any':
updates.push({
fn: options.any(node, []),
sparse: false
});
break;
case 'attr':
var sparse = info.sparse;
var fn = options.attribute(node, info.name, info.node);
if (sparse === null) updates.push({
fn: fn,
sparse: false
});else {
off += sparse.length - 2;
updates.push({
fn: fn,
sparse: true,
values: sparse
});
}
break;
case 'text':
updates.push({
fn: options.text(node),
sparse: false
});
node.textContent = '';
break;
}
}
len += off;
return function () {
var length = arguments.length;
if (len !== length - 1) {
throw new Error(length - 1 + ' values instead of ' + len + '\n' + template.join('${value}'));
}
var i = 1;
var off = 1;
while (i < length) {
var update = updates[i - off];
if (update.sparse) {
var values = update.values;
var value = values[0];
var j = 1;
var l = values.length;
off += l - 2;
while (j < l) {
value += arguments[i++] + values[j++];
}
update.fn(value);
} else update.fn(arguments[i++]);
}
return content;
};
}
};
}
function createDetails(options, template) {
var info = parsed.get(template) || parsed.set(template, createInfo(options, template));
return info.updates(importNode.call(document, info.content, true));
}
var empty = [];
function domtagger(options) {
var previous = empty;
var updates = cleanContent;
return function (template) {
if (previous !== template) updates = createDetails(options, previous = template);
return updates.apply(null, arguments);
};
}
function cleanContent(fragment) {
var childNodes = fragment.childNodes;
var i = childNodes.length;
while (i--) {
var child = childNodes[i];
if (child.nodeType !== 1 && trim.call(child.textContent).length === 0) {
fragment.removeChild(child);
}
}
}
return domtagger;
}(document));