113 lines
3.5 KiB
HTML
113 lines
3.5 KiB
HTML
|
|
<!--
|
||
|
|
@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
|
||
|
|
-->
|
||
|
|
<link rel="import" href="boot.html">
|
||
|
|
|
||
|
|
<script>
|
||
|
|
|
||
|
|
(function() {
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
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`.
|
||
|
|
*
|
||
|
|
* @memberof Polymer.ResolveUrl
|
||
|
|
* @param {string} url Input URL to resolve
|
||
|
|
* @param {?string=} baseURI Base URI to resolve the URL against
|
||
|
|
* @return {string} resolved URL
|
||
|
|
*/
|
||
|
|
function resolveUrl(url, baseURI) {
|
||
|
|
if (url && ABS_URL.test(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) {
|
||
|
|
return (new URL(url, baseURI)).href;
|
||
|
|
}
|
||
|
|
// 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`.
|
||
|
|
*
|
||
|
|
* @memberof Polymer.ResolveUrl
|
||
|
|
* @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
|
||
|
|
*/
|
||
|
|
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.
|
||
|
|
*
|
||
|
|
* @memberof Polymer.ResolveUrl
|
||
|
|
* @param {string} url Input URL to transform
|
||
|
|
* @return {string} resolved path
|
||
|
|
*/
|
||
|
|
function pathFromUrl(url) {
|
||
|
|
return url.substring(0, url.lastIndexOf('/') + 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Module with utilities for resolving relative URL's.
|
||
|
|
*
|
||
|
|
* @namespace
|
||
|
|
* @memberof Polymer
|
||
|
|
* @summary Module with utilities for resolving relative URL's.
|
||
|
|
*/
|
||
|
|
Polymer.ResolveUrl = {
|
||
|
|
resolveCss: resolveCss,
|
||
|
|
resolveUrl: resolveUrl,
|
||
|
|
pathFromUrl: pathFromUrl
|
||
|
|
};
|
||
|
|
|
||
|
|
})();
|
||
|
|
|
||
|
|
</script>
|