This repository was archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbundle.js
More file actions
635 lines (562 loc) · 17.8 KB
/
Copy pathbundle.js
File metadata and controls
635 lines (562 loc) · 17.8 KB
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
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
(function (global){
var hyperHTMLApp = (function () {'use strict';
var onpushstate = require('onpushstate');
var path2regexp = require('path-to-regexp');
var app = HyperHTMLApplication.prototype;
app.get = function get(path) {
for (var
keys = [],
re = asPath2RegExp(path, keys),
info = this._paths[re] || (this._paths[re] = {
keys: keys,
cb: [],
re: re
}),
i = 1, length = arguments.length;
i < length; i++
) {
info.cb.push(arguments[i]);
}
return this;
};
app.delete = function del(path) {
for (var
re = asPath2RegExp(path, []),
info = this._paths[re],
i = 1, length = arguments.length;
i < length; i++
) {
var cb = arguments[i];
var index = info ? info.cb.lastIndexOf(cb) : -1;
if (-1 < index) info.cb.splice(index, 1);
}
return this;
};
app.use = function use(mount, cb) {
if (typeof mount === 'function') {
cb = mount;
mount = '(.*)';
}
for (var
paths = [].concat(mount),
i = 0, length = paths.length;
i < length; i++
) {
this.get(paths[i], cb);
}
return this;
};
app.param = function param(name, cb) {
for (var
names = [].concat(name),
i = 0, length = names.length;
i < length; i++
) {
this._params[names[i]] = cb;
}
return this;
};
app.navigate = function navigate(pathname, options) {
switch (true) {
case !!options:
switch (true) {
case !!options.replace:
case !!options.replaceState:
history.replaceState(history.state, document.title, pathname);
break;
}
break;
case pathname === (location.pathname + location.search):
this.handleEvent({type: 'samestate'});
break;
default:
var doc = document;
var html = doc.documentElement;
var navigator = doc.createElement('a');
navigator.href = pathname;
navigator.onclick = remove;
html.insertBefore(navigator, html.firstChild);
navigator.click();
break;
}
return this;
};
app.handleEvent = function handleEvent(e) {
var paths = this._paths;
for (var key in paths) {
if (paths.hasOwnProperty(key)) {
var info = paths[key];
var match = info.re.exec(location.pathname);
if (match) {
var invoked = [];
var keys = [];
var params = this._params;
var ctx = {
params: createParams(match, info.keys),
type: e.type
};
var i = 0;
var length = info.cb.length;
for (key in ctx.params) {
if (params.hasOwnProperty(key)) {
keys.push(key);
}
}
(function param() {
if (keys.length) {
key = keys.shift();
params[key](ctx, param, ctx.params[key]);
} else {
(function next() {
if (i < length) {
var cb = info.cb[i++];
if (invoked.lastIndexOf(cb) < 0) {
invoked.push(cb);
cb(ctx, next);
} else {
next();
}
}
}());
}
}());
return;
}
}
}
};
function asPath2RegExp(path, keys) {
if (typeof path !== 'string') {
path = path.toString();
path = path.slice(1, path.lastIndexOf('/'));
}
return path2regexp(path, keys);
}
function createParams(match, keys) {
for (var
value,
params = {},
i = 1,
length = match.length;
i < length; i++
) {
value = match[i];
if (value) params[keys[i - 1].name] = value;
}
return params;
}
function remove() {
this.parentNode.removeChild(this);
}
function HyperHTMLApplication() {
this._params = {};
this._paths = {};
global.addEventListener('popstate', this, false);
global.addEventListener('pushstate', this, false);
}
return function hyperHTMLApp() {
return new HyperHTMLApplication();
};
}());
module.exports = (global.hyperHTML || {}).app = hyperHTMLApp;
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"onpushstate":2,"path-to-regexp":3}],2:[function(require,module,exports){
/*! (C) 2017 Andrea Giammarchi - @WebReflection - ISC License */
/**
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
document.addEventListener('click', function (e) {
// find the link node (even if inside an opened Shadow DOM)
var target = e.target.shadowRoot ? e.path[0] : e.target;
// find the anchor
var anchor = (target.closest || function (A) {
while (target && target.nodeName !== A) target = target.parentNode;
return target;
}).call(target, 'A');
if (
// it was found
anchor &&
// it's for the current page
/^(?:_self)?$/i.test(anchor.target) &&
// it's not a download
!anchor.hasAttribute('download') &&
// it's not a resource handled externally
anchor.getAttribute('rel') !== 'external' &&
// it's not a click with ctrl/shift/alt keys pressed
// => (let the browser do it's job instead)
!e.ctrlKey && !e.metaKey && !e.shiftKey && !e.altKey
) {
// all states are simply fully resolved URLs
// pushstate will be the new page with old one as state
// popstate will be old page with previous one as state.
var next = new URL(anchor.href);
var curr = location;
// only if in the same origin
if (next.origin === curr.origin) {
// verify it's not just an anchor change
var redirect = next.pathname + next.search;
var hash = next.hash;
var scrollIntoView = true;
// in every case prevent the default action
e.preventDefault();
// but don't stop propagation, other listeners
// might want to be triggered regardless the history
if (redirect === (curr.pathname + curr.search)) {
// anchors should do what anchors do, only if valid
// https://www.w3.org/TR/html4/types.html#type-name
if (/^#[a-z][a-z0-9.:_-]+$/i.test(hash)) {
var target = document.querySelector(
hash + ',[name="' + hash.slice(1) + '"]'
);
if (target) {
// verify if other listeners tried to prevent the default
e.preventDefault = function () { scrollIntoView = false; };
// after this event has captured and bubbled the DOM
setTimeout(function () {
// if nobody else prevented the default
// simulate what an anchor would've done
if (scrollIntoView) target.scrollIntoView(true);
});
}
}
// replace the history to ignore the popstate on anchor
history.replaceState(history.state, document.title, redirect + hash);
} else {
// trigger a new pushstate notification
var evt = new CustomEvent('pushstate');
evt.state = curr.href;
// being sure it happens after so the new location should be available
setTimeout(function () {
// dispatch the event
dispatchEvent(evt);
// also trigger Level 0 if possible
if (window.onpushstate) onpushstate(evt);
});
history.pushState(evt.state, document.title, redirect + hash);
}
}
}
}, true);
},{}],3:[function(require,module,exports){
/**
* Expose `pathToRegexp`.
*/
module.exports = pathToRegexp
module.exports.parse = parse
module.exports.compile = compile
module.exports.tokensToFunction = tokensToFunction
module.exports.tokensToRegExp = tokensToRegExp
/**
* Default configs.
*/
var DEFAULT_DELIMITER = '/'
/**
* The main path matching regexp utility.
*
* @type {RegExp}
*/
var PATH_REGEXP = new RegExp([
// Match escaped characters that would otherwise appear in future matches.
// This allows the user to escape special characters that won't transform.
'(\\\\.)',
// Match Express-style parameters and un-named parameters with a prefix
// and optional suffixes. Matches appear as:
//
// ":test(\\d+)?" => ["test", "\d+", undefined, "?"]
// "(\\d+)" => [undefined, undefined, "\d+", undefined]
'(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?'
].join('|'), 'g')
/**
* Parse a string for the raw tokens.
*
* @param {string} str
* @param {Object=} options
* @return {!Array}
*/
function parse (str, options) {
var tokens = []
var key = 0
var index = 0
var path = ''
var defaultDelimiter = (options && options.delimiter) || DEFAULT_DELIMITER
var whitelist = (options && options.whitelist) || undefined
var pathEscaped = false
var res
while ((res = PATH_REGEXP.exec(str)) !== null) {
var m = res[0]
var escaped = res[1]
var offset = res.index
path += str.slice(index, offset)
index = offset + m.length
// Ignore already escaped sequences.
if (escaped) {
path += escaped[1]
pathEscaped = true
continue
}
var prev = ''
var name = res[2]
var capture = res[3]
var group = res[4]
var modifier = res[5]
if (!pathEscaped && path.length) {
var k = path.length - 1
var c = path[k]
var matches = whitelist ? whitelist.indexOf(c) > -1 : true
if (matches) {
prev = c
path = path.slice(0, k)
}
}
// Push the current path onto the tokens.
if (path) {
tokens.push(path)
path = ''
pathEscaped = false
}
var repeat = modifier === '+' || modifier === '*'
var optional = modifier === '?' || modifier === '*'
var pattern = capture || group
var delimiter = prev || defaultDelimiter
tokens.push({
name: name || key++,
prefix: prev,
delimiter: delimiter,
optional: optional,
repeat: repeat,
pattern: pattern
? escapeGroup(pattern)
: '[^' + escapeString(delimiter === defaultDelimiter ? delimiter : (delimiter + defaultDelimiter)) + ']+?'
})
}
// Push any remaining characters.
if (path || index < str.length) {
tokens.push(path + str.substr(index))
}
return tokens
}
/**
* Compile a string to a template function for the path.
*
* @param {string} str
* @param {Object=} options
* @return {!function(Object=, Object=)}
*/
function compile (str, options) {
return tokensToFunction(parse(str, options))
}
/**
* Expose a method for transforming tokens into the path function.
*/
function tokensToFunction (tokens) {
// Compile all the tokens into regexps.
var matches = new Array(tokens.length)
// Compile all the patterns before compilation.
for (var i = 0; i < tokens.length; i++) {
if (typeof tokens[i] === 'object') {
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$')
}
}
return function (data, options) {
var path = ''
var encode = (options && options.encode) || encodeURIComponent
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
if (typeof token === 'string') {
path += token
continue
}
var value = data ? data[token.name] : undefined
var segment
if (Array.isArray(value)) {
if (!token.repeat) {
throw new TypeError('Expected "' + token.name + '" to not repeat, but got array')
}
if (value.length === 0) {
if (token.optional) continue
throw new TypeError('Expected "' + token.name + '" to not be empty')
}
for (var j = 0; j < value.length; j++) {
segment = encode(value[j], token)
if (!matches[i].test(segment)) {
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"')
}
path += (j === 0 ? token.prefix : token.delimiter) + segment
}
continue
}
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
segment = encode(String(value), token)
if (!matches[i].test(segment)) {
throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but got "' + segment + '"')
}
path += token.prefix + segment
continue
}
if (token.optional) continue
throw new TypeError('Expected "' + token.name + '" to be ' + (token.repeat ? 'an array' : 'a string'))
}
return path
}
}
/**
* Escape a regular expression string.
*
* @param {string} str
* @return {string}
*/
function escapeString (str) {
return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1')
}
/**
* Escape the capturing group by escaping special characters and meaning.
*
* @param {string} group
* @return {string}
*/
function escapeGroup (group) {
return group.replace(/([=!:$/()])/g, '\\$1')
}
/**
* Get the flags for a regexp from the options.
*
* @param {Object} options
* @return {string}
*/
function flags (options) {
return options && options.sensitive ? '' : 'i'
}
/**
* Pull out keys from a regexp.
*
* @param {!RegExp} path
* @param {Array=} keys
* @return {!RegExp}
*/
function regexpToRegexp (path, keys) {
if (!keys) return path
// Use a negative lookahead to match only capturing groups.
var groups = path.source.match(/\((?!\?)/g)
if (groups) {
for (var i = 0; i < groups.length; i++) {
keys.push({
name: i,
prefix: null,
delimiter: null,
optional: false,
repeat: false,
pattern: null
})
}
}
return path
}
/**
* Transform an array into a regexp.
*
* @param {!Array} path
* @param {Array=} keys
* @param {Object=} options
* @return {!RegExp}
*/
function arrayToRegexp (path, keys, options) {
var parts = []
for (var i = 0; i < path.length; i++) {
parts.push(pathToRegexp(path[i], keys, options).source)
}
return new RegExp('(?:' + parts.join('|') + ')', flags(options))
}
/**
* Create a path regexp from string input.
*
* @param {string} path
* @param {Array=} keys
* @param {Object=} options
* @return {!RegExp}
*/
function stringToRegexp (path, keys, options) {
return tokensToRegExp(parse(path, options), keys, options)
}
/**
* Expose a function for taking tokens and returning a RegExp.
*
* @param {!Array} tokens
* @param {Array=} keys
* @param {Object=} options
* @return {!RegExp}
*/
function tokensToRegExp (tokens, keys, options) {
options = options || {}
var strict = options.strict
var start = options.start !== false
var end = options.end !== false
var delimiter = options.delimiter || DEFAULT_DELIMITER
var endsWith = [].concat(options.endsWith || []).map(escapeString).concat('$').join('|')
var route = start ? '^' : ''
// Iterate over the tokens and create our regexp string.
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
if (typeof token === 'string') {
route += escapeString(token)
} else {
var capture = token.repeat
? '(?:' + token.pattern + ')(?:' + escapeString(token.delimiter) + '(?:' + token.pattern + '))*'
: token.pattern
if (keys) keys.push(token)
if (token.optional) {
if (!token.prefix) {
route += '(' + capture + ')?'
} else {
route += '(?:' + escapeString(token.prefix) + '(' + capture + '))?'
}
} else {
route += escapeString(token.prefix) + '(' + capture + ')'
}
}
}
if (end) {
if (!strict) route += '(?:' + escapeString(delimiter) + ')?'
route += endsWith === '$' ? '$' : '(?=' + endsWith + ')'
} else {
var endToken = tokens[tokens.length - 1]
var isEndDelimited = typeof endToken === 'string'
? endToken[endToken.length - 1] === delimiter
: endToken === undefined
if (!strict) route += '(?:' + escapeString(delimiter) + '(?=' + endsWith + '))?'
if (!isEndDelimited) route += '(?=' + escapeString(delimiter) + '|' + endsWith + ')'
}
return new RegExp(route, flags(options))
}
/**
* Normalize the given path string, returning a regular expression.
*
* An empty array can be passed in for the keys, which will hold the
* placeholder key descriptions. For example, using `/user/:id`, `keys` will
* contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
*
* @param {(string|RegExp|Array)} path
* @param {Array=} keys
* @param {Object=} options
* @return {!RegExp}
*/
function pathToRegexp (path, keys, options) {
if (path instanceof RegExp) {
return regexpToRegexp(path, keys)
}
if (Array.isArray(path)) {
return arrayToRegexp(/** @type {!Array} */ (path), keys, options)
}
return stringToRegexp(/** @type {string} */ (path), keys, options)
}
},{}]},{},[1]);