Skip to content

Commit

Permalink
Do not resolve urls with leading slash and other prototcols
Browse files Browse the repository at this point in the history
Fixes #2448
  • Loading branch information
dfreedm committed Jul 29, 2016
1 parent c34fff9 commit 94f95ec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/lib/resolve-url.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

(function() {

// path fixup for urls in cssText that's expected to
// path fixup for urls in cssText that's expected to
// come from a given ownerDocument
function resolveCss(cssText, ownerDocument) {
return cssText.replace(CSS_URL_RX, function(m, pre, url, post) {
return pre + '\'' +
resolve(url.replace(/["']/g, ''), ownerDocument) +
return pre + '\'' +
resolve(url.replace(/["']/g, ''), ownerDocument) +
'\'' + post;
});
}

// url fixup for urls in an element's attributes made relative to
// url fixup for urls in an element's attributes made relative to
// ownerDoc's base url
function resolveAttrs(element, ownerDocument) {
for (var name in URL_ATTRS) {
Expand All @@ -41,10 +41,10 @@
}

function resolve(url, ownerDocument) {
// do not resolve '#' links, they are used for routing
if (url && url[0] === '#') {
// do not modify absolute urls
if (url && ABS_URL.test(url)) {
return url;
}
}
var resolver = getUrlResolver(ownerDocument);
resolver.href = url;
return resolver.href || url;
Expand All @@ -63,7 +63,7 @@
}

function getUrlResolver(ownerDocument) {
return ownerDocument.__urlResolver ||
return ownerDocument.__urlResolver ||
(ownerDocument.__urlResolver = ownerDocument.createElement('a'));
}

Expand All @@ -72,6 +72,7 @@
'*': ['href', 'src', 'style', 'url'],
form: ['action']
};
var ABS_URL = /(^\/)|(^#)|(^[\w-\d]*:)/;
var BINDING_RX = /\{\{|\[\[/;

// exports
Expand Down
2 changes: 2 additions & 0 deletions test/unit/resolveurl.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
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.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');
});

test('resolveUrl api', function() {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/sub/resolveurl-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<a id="action" action="foo.z">Foo</a>
<form id="formAction" action="foo.z"></form>
<a id="hash" href="#foo.z">Foo</a>
<a id="absolute" href="/foo.z">Foo</a>
<a id="protocol" href="data:foo.z">Foo</a>
</template>
</dom-module>
<script>
Expand Down

0 comments on commit 94f95ec

Please sign in to comment.