-
Notifications
You must be signed in to change notification settings - Fork 2k
/
resolve-url.js
95 lines (91 loc) · 2.91 KB
/
resolve-url.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
/**
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import './boot.js';
let CSS_URL_RX = /(url\()([^)]*)(\))/g;
let ABS_URL = /(^\/[^\/])|(^#)|(^[\w-\d]*:)/;
let workingURL;
let resolveDoc;
/**
* Resolves the given URL against the provided `baseUri'.
*
* Note that this function performs no resolution for URLs that start
* with `/` (absolute URLs) or `#` (hash identifiers). For general purpose
* URL resolution, use `window.URL`.
*
* @param {string} url Input URL to resolve
* @param {?string=} baseURI Base URI to resolve the URL against
* @return {string} resolved URL
*/
export function resolveUrl(url, baseURI) {
if (url && ABS_URL.test(url)) {
return url;
}
if (url === '//') {
return url;
}
// Lazy feature detection.
if (workingURL === undefined) {
workingURL = false;
try {
const u = new URL('b', 'http://a');
u.pathname = 'c%20d';
workingURL = (u.href === 'http://a/c%20d');
} catch (e) {
// silently fail
}
}
if (!baseURI) {
baseURI = document.baseURI || window.location.href;
}
if (workingURL) {
try {
return (new URL(url, baseURI)).href;
} catch (e) {
// Bad url or baseURI structure. Do not attempt to resolve.
return url;
}
}
// Fallback to creating an anchor into a disconnected document.
if (!resolveDoc) {
resolveDoc = document.implementation.createHTMLDocument('temp');
resolveDoc.base = resolveDoc.createElement('base');
resolveDoc.head.appendChild(resolveDoc.base);
resolveDoc.anchor = resolveDoc.createElement('a');
resolveDoc.body.appendChild(resolveDoc.anchor);
}
resolveDoc.base.href = baseURI;
resolveDoc.anchor.href = url;
return resolveDoc.anchor.href || url;
}
/**
* Resolves any relative URL's in the given CSS text against the provided
* `ownerDocument`'s `baseURI`.
*
* @param {string} cssText CSS text to process
* @param {string} baseURI Base URI to resolve the URL against
* @return {string} Processed CSS text with resolved URL's
*/
export function resolveCss(cssText, baseURI) {
return cssText.replace(CSS_URL_RX, function(m, pre, url, post) {
return pre + '\'' +
resolveUrl(url.replace(/["']/g, ''), baseURI) +
'\'' + post;
});
}
/**
* Returns a path from a given `url`. The path includes the trailing
* `/` from the url.
*
* @param {string} url Input URL to transform
* @return {string} resolved path
*/
export function pathFromUrl(url) {
return url.substring(0, url.lastIndexOf('/') + 1);
}