|
13 | 13 |
|
14 | 14 | (function() {
|
15 | 15 |
|
| 16 | + let CSS_URL_RX = /(url\()([^)]*)(\))/g; |
| 17 | + let ABS_URL = /(^\/)|(^#)|(^[\w-\d]*:)/; |
| 18 | + let workingURL; |
| 19 | + let resolveDoc; |
| 20 | + /** |
| 21 | + * Resolves the given URL against the provided `baseUri'. |
| 22 | + * |
| 23 | + * @memberof Polymer.ResolveUrl |
| 24 | + * @param {string} url Input URL to resolve |
| 25 | + * @param {string} baseURI Base URI to resolve the URL against |
| 26 | + * @return {string} resolved URL |
| 27 | + */ |
| 28 | + function resolveUrl(url, baseURI) { |
| 29 | + if (url && ABS_URL.test(url)) { |
| 30 | + return url; |
| 31 | + } |
| 32 | + // Lazy feature detection. |
| 33 | + if (workingURL === undefined) { |
| 34 | + workingURL = false; |
| 35 | + try { |
| 36 | + const u = new URL('b', 'http://a'); |
| 37 | + u.pathname = 'c%20d'; |
| 38 | + workingURL = (u.href === 'http://a/c%20d'); |
| 39 | + } catch (e) { |
| 40 | + // silently fail |
| 41 | + } |
| 42 | + } |
| 43 | + if (!baseURI) { |
| 44 | + baseURI = document.baseURI || window.location.href; |
| 45 | + } |
| 46 | + if (workingURL) { |
| 47 | + return (new URL(url, baseURI)).href; |
| 48 | + } |
| 49 | + // Fallback to creating an anchor into a disconnected document. |
| 50 | + if (!resolveDoc) { |
| 51 | + resolveDoc = document.implementation.createHTMLDocument('temp'); |
| 52 | + resolveDoc.base = resolveDoc.createElement('base'); |
| 53 | + resolveDoc.head.appendChild(resolveDoc.base); |
| 54 | + resolveDoc.anchor = resolveDoc.createElement('a'); |
| 55 | + resolveDoc.body.appendChild(resolveDoc.anchor); |
| 56 | + } |
| 57 | + resolveDoc.base.href = baseURI; |
| 58 | + resolveDoc.anchor.href = url; |
| 59 | + return resolveDoc.anchor.href || url; |
| 60 | + |
| 61 | + } |
| 62 | + |
16 | 63 | /**
|
17 | 64 | * Resolves any relative URL's in the given CSS text against the provided
|
18 | 65 | * `ownerDocument`'s `baseURI`.
|
19 | 66 | *
|
20 | 67 | * @memberof Polymer.ResolveUrl
|
21 | 68 | * @param {string} cssText CSS text to process
|
22 |
| - * @param {Document} ownerDocument Owner document to base URL's on |
| 69 | + * @param {string} baseURI Base URI to resolve the URL against |
23 | 70 | * @return {string} Processed CSS text with resolved URL's
|
24 | 71 | */
|
25 |
| - function resolveCss(cssText, ownerDocument) { |
| 72 | + function resolveCss(cssText, baseURI) { |
26 | 73 | return cssText.replace(CSS_URL_RX, function(m, pre, url, post) {
|
27 | 74 | return pre + '\'' +
|
28 |
| - resolve(url.replace(/["']/g, ''), ownerDocument) + |
| 75 | + resolveUrl(url.replace(/["']/g, ''), baseURI) + |
29 | 76 | '\'' + post;
|
30 | 77 | });
|
31 | 78 | }
|
32 | 79 |
|
33 | 80 | /**
|
34 |
| - * Resolves any relative URL's in `src` or `style` attributes of the given |
35 |
| - * `element` against the provided `ownerDocument`'s `baseURI`. |
| 81 | + * Returns a path from a given `url`. The path includes the trailing |
| 82 | + * `/` from the url. |
36 | 83 | *
|
37 | 84 | * @memberof Polymer.ResolveUrl
|
38 |
| - * @param {HTMLElement} element Element whose attributes will be processed |
39 |
| - * @param {Document} ownerDocument Owner document to base URL's on |
| 85 | + * @param {string} url Input URL to transform |
| 86 | + * @return {string} resolved path |
40 | 87 | */
|
41 |
| - function resolveAttrs(element, ownerDocument) { |
42 |
| - for (var name in URL_ATTRS) { |
43 |
| - var a$ = URL_ATTRS[name]; |
44 |
| - for (var i=0, l=a$.length, a, at, v; (i<l) && (a=a$[i]); i++) { |
45 |
| - if (name === '*' || element.localName === name) { |
46 |
| - at = element.attributes[a]; |
47 |
| - v = at && at.value; |
48 |
| - if (v && (v.search(BINDING_RX) < 0)) { |
49 |
| - at.value = (a === 'style') ? |
50 |
| - resolveCss(v, ownerDocument) : |
51 |
| - resolve(v, ownerDocument); |
52 |
| - } |
53 |
| - } |
54 |
| - } |
55 |
| - } |
| 88 | + function pathFromUrl(url) { |
| 89 | + return url.substring(0, url.lastIndexOf('/') + 1); |
56 | 90 | }
|
57 | 91 |
|
58 |
| - /** |
59 |
| - * Resolves the given URL against the provided `ownerDocument`'s `baseURI'. |
60 |
| - * |
61 |
| - * Does not modify `#` links or absolute URL's. |
62 |
| - * |
63 |
| - * @private |
64 |
| - */ |
65 |
| - function resolve(url, ownerDocument) { |
66 |
| - // do not resolve '#' links, they are used for routing |
67 |
| - if (url && ABS_URL.test(url)) { |
68 |
| - return url; |
69 |
| - } |
70 |
| - var resolver = getUrlResolver(ownerDocument); |
71 |
| - resolver.href = url; |
72 |
| - return resolver.href || url; |
73 |
| - } |
74 |
| - |
75 |
| - var tempDoc; |
76 |
| - var tempDocBase; |
77 |
| - |
78 |
| - /** |
79 |
| - * Resolves the given URL against the provided `baseUri'. |
80 |
| - * |
81 |
| - * @memberof Polymer.ResolveUrl |
82 |
| - * @param {string} url Input URL to resolve |
83 |
| - * @param {string} baseUri Base URI to resolve the URL against |
84 |
| - * @param {type} name Description |
85 |
| - */ |
86 |
| - function resolveUrl(url, baseUri) { |
87 |
| - if (!tempDoc) { |
88 |
| - tempDoc = document.implementation.createHTMLDocument('temp'); |
89 |
| - tempDocBase = tempDoc.createElement('base'); |
90 |
| - tempDoc.head.appendChild(tempDocBase); |
91 |
| - } |
92 |
| - tempDocBase.href = baseUri; |
93 |
| - return resolve(url, tempDoc); |
94 |
| - } |
95 |
| - |
96 |
| - function getUrlResolver(ownerDocument) { |
97 |
| - return ownerDocument.__urlResolver || |
98 |
| - (ownerDocument.__urlResolver = ownerDocument.createElement('a')); |
99 |
| - } |
100 |
| - |
101 |
| - var CSS_URL_RX = /(url\()([^)]*)(\))/g; |
102 |
| - var URL_ATTRS = { |
103 |
| - '*': ['href', 'src', 'style', 'url'], |
104 |
| - form: ['action'] |
105 |
| - }; |
106 |
| - var ABS_URL = /(^\/)|(^#)|(^[\w-\d]*:)/; |
107 |
| - var BINDING_RX = /\{\{|\[\[/; |
108 |
| - |
109 | 92 | /**
|
110 | 93 | * Module with utilities for resolving relative URL's.
|
111 | 94 | *
|
|
114 | 97 | * @summary Module with utilities for resolving relative URL's.
|
115 | 98 | */
|
116 | 99 | Polymer.ResolveUrl = {
|
117 |
| - // exports |
118 | 100 | resolveCss: resolveCss,
|
119 |
| - resolveAttrs: resolveAttrs, |
120 |
| - resolveUrl: resolveUrl |
| 101 | + resolveUrl: resolveUrl, |
| 102 | + pathFromUrl: pathFromUrl |
121 | 103 | };
|
122 | 104 |
|
| 105 | + // NOTE: baseURI is not supported on IE? |
| 106 | + Polymer.rootPath = pathFromUrl(document.baseURI || window.location.href); |
| 107 | + |
123 | 108 | })();
|
124 | 109 |
|
125 | 110 | </script>
|
0 commit comments