Skip to content

Commit

Permalink
Merge pull request Polymer#5081 from Polymer/revert-5024-absolute-url…
Browse files Browse the repository at this point in the history
…-resolve-url

Revert "Move absolute url logic to element-mixin"
  • Loading branch information
kevinpschaaf authored Feb 1, 2018
2 parents 62fd4c5 + d97373d commit 78558b8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
11 changes: 4 additions & 7 deletions lib/mixins/element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
(function() {
'use strict';

const ABS_URL = /(^\/)|(^#)|(^[\w-\d]*:)/;

/**
* Element class mixin that provides the core API for Polymer's meta-programming
* features including template stamping, data-binding, attribute deserialization,
Expand Down Expand Up @@ -629,17 +627,16 @@
* this element. This method will return the same URL before and after
* bundling.
*
* 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 URL to resolve.
* @param {string=} base Optional base URL to resolve against, defaults
* to the element's `importPath`
* @return {string} Rewritten URL relative to base
*/
resolveUrl(url, base) {
// Preserve backward compatibility with `this.resolveUrl('/foo')` resolving
// against the main document per #2448
if (url && ABS_URL.test(url)) {
return url;
}
if (!base && this.importPath) {
base = Polymer.ResolveUrl.resolveUrl(this.importPath);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/resolve-url.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@
'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;
Expand Down
32 changes: 16 additions & 16 deletions test/unit/resolveurl.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,25 @@
test('Urls in styles and attributes', function() {
var el = document.createElement('p-r');
document.body.appendChild(el);
var rx = /sub\/foo\.z/;
var resolvedUrl = /sub\/foo\.z/;
var styleHashUrl = /url\('#bar'\)/;
var styleAbsUrl = /url\('\/zot'\)/;
var style = el.shadowRoot.querySelector('style') || document.querySelector('style[scope=p-r]');
assert.match(style.textContent, rx, 'url not relative to main document');
assert.match(el.$.div.getAttribute('style'), rx, 'style url not relative to main document');
assert.match(el.$.img.src, rx, 'src url not relative to main document');
assert.match(el.$.a.href, rx, 'href url not relative to main document');
assert.match(el.$.import.getAttribute('url'), rx, 'url url not relative to main document');
assert.match(el.$.resolveUrl.getAttribute('url'), rx, 'url url not relative to main document');
assert.notMatch(el.$.root.getAttribute('url'), rx, 'url url not relative to main document');
assert.notMatch(el.$.rel.href, rx, 'relative href url not relative to main document');
assert.match(style.textContent, resolvedUrl, 'url not relative to main document');
assert.match(style.textContent, styleHashUrl, 'hash url incorrectly resolved');
assert.match(style.textContent, styleAbsUrl, 'absolute url incorrectly resolved');
assert.match(el.$.div.getAttribute('style'), resolvedUrl, 'style url not relative to main document');
assert.match(el.$.img.src, resolvedUrl, 'src url not relative to main document');
assert.match(el.$.a.href, resolvedUrl, 'href url not relative to main document');
assert.match(el.$.import.getAttribute('url'), resolvedUrl, 'url url not relative to main document');
assert.match(el.$.resolveUrl.getAttribute('url'), resolvedUrl, 'url url not relative to main document');
assert.equal(el.$.resolveUrlHash.getAttribute('url'), '#foo', 'url url not relative to main document');
assert.equal(el.$.resolveUrlAbs.getAttribute('url'), '/foo', 'url url not relative to main document');
assert.notMatch(el.$.root.getAttribute('url'), resolvedUrl, 'url url not relative to main document');
assert.notMatch(el.$.rel.href, resolvedUrl, 'relative href url not relative to main document');
assert.match(el.$.rel.href, /\?123$/, 'relative href does not preserve query string');
assert.equal(el.$.action.getAttribute('action'), 'foo.z', 'action attribute relativized for incorrect element type');
assert.match(el.$.formAction.action, rx, 'action attribute relativized for incorrect element type');
assert.match(el.$.formAction.action, resolvedUrl, 'action attribute relativized for incorrect element type');
assert.equal(el.$.hash.getAttribute('href'), '#foo.z', 'hash-only url should not be resolved');
assert.equal(el.$.absolute.getAttribute('href'), '/foo.z', 'absolute urls should not be resolved');
assert.equal(el.$.protocol.getAttribute('href'), 'data:foo.z', 'urls with other protocols should not be resolved');
Expand Down Expand Up @@ -157,12 +163,6 @@
assert.equal(actual, expected);
});
});

suite('Polymer.ResolveUrl', function() {
test('appends absolute url to basePath', function() {
assert.equal(Polymer.ResolveUrl.resolveUrl('/foo', 'http://localhost'), 'http://localhost/foo');
});
});
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions test/unit/sub/resolveurl-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
<style>
.logo {
background-image: url(foo.z);
clip-path: url('#bar');
mask-image: url('/zot');
}
</style>
<div id="div" class="logo" style$="background-image: url('[[importPath]]foo.z');"></div>
<img id="img" src$="[[importPath]]foo.z">
<a id="a" href$="[[importPath]]foo.z">Foo</a>
<zonk id="import" url$="[[importPath]]foo.z"></zonk>
<zonk id="resolveUrl" url$="[[resolveUrl('foo.z')]]"></zonk>
<zonk id="resolveUrlHash" url$="[[resolveUrl('#foo')]]"></zonk>
<zonk id="resolveUrlAbs" url$="[[resolveUrl('/foo')]]"></zonk>
<zonk id="root" url$="[[rootPath]]foo.z"></zonk>
<a id="rel" href$="[[importPath]]../foo.z?123">Foo</a>
<a id="action" action="foo.z">Foo</a>
Expand Down
4 changes: 4 additions & 0 deletions types/lib/mixins/element-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ declare namespace Polymer {
* this element. This method will return the same URL before and after
* bundling.
*
* 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 url URL to resolve.
* @param base Optional base URL to resolve against, defaults
* to the element's `importPath`
Expand Down
4 changes: 4 additions & 0 deletions types/lib/utils/resolve-url.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ declare namespace Polymer {
/**
* 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`.
*
* @returns resolved URL
*/
function resolveUrl(url: string, baseURI?: string|null): string;
Expand Down

0 comments on commit 78558b8

Please sign in to comment.