Skip to content

Commit

Permalink
Do not resolve hash-only urls used for routing
Browse files Browse the repository at this point in the history
Add tests for imported documents url resolution

Fixes #651
  • Loading branch information
dfreedm committed Jan 14, 2015
1 parent 2ff1e4a commit c716964
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ function resolveRelativeUrl(baseUrl, url, keepAbsolute) {
if (url && url[0] === '/') {
return url;
}
// do not resolve '#' links, they are used for routing
if (url && url[0] === '#') {
return url;
}
var u = new URL(url, baseUrl);
return keepAbsolute ? u.href : makeDocumentRelPath(u.href);
}
Expand Down
17 changes: 17 additions & 0 deletions test/html/url-import/url-import.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<!--
Copyright (c) 2014 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
-->
<template>
<b id="a" href="/1/2"></b>
<b id="b" href="2/../3/4"></b>
<b id="c" href="{{ path }}/baz"></b>
<b id="d" href="#"></b>
<b id="e" href="?foo"></b>
<b id="f" href="#path"></b>
</template>
13 changes: 13 additions & 0 deletions test/html/url.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<script src="../../../tools/test/chai/chai.js"></script>
<script src="../../../webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="../../polymer.html">
<link rel="import" href="url-import/url-import.html" id="url-import">
</head>
<body>
<template>
Expand All @@ -29,13 +30,25 @@
function getUrl(template, id, attr) {
return template.content.querySelector('#' + id).getAttribute(attr);
}

var t = document.querySelector('template');
Polymer.urlResolver.resolveTemplate(t);

chai.assert.equal(getUrl(t, 'a', 'href'), '/1/2');
chai.assert.equal(getUrl(t, 'b', 'href'), '3/4');
chai.assert.equal(getUrl(t, 'c', 'href'), '{{ path }}/baz');
chai.assert.equal(getUrl(t, 'd', 'href'), '#');
chai.assert.equal(getUrl(t, 'e', 'href'), '?foo');

t = document.getElementById('url-import').import.querySelector('template');
Polymer.urlResolver.resolveTemplate(t);

chai.assert.equal(getUrl(t, 'a', 'href'), '/1/2');
chai.assert.equal(getUrl(t, 'b', 'href'), 'url-import/3/4');
chai.assert.equal(getUrl(t, 'c', 'href'), '{{ path }}/baz');
chai.assert.equal(getUrl(t, 'd', 'href'), '#');
chai.assert.equal(getUrl(t, 'e', 'href'), 'url-import/url-import.html?foo');

done();
});
</script>
Expand Down

0 comments on commit c716964

Please sign in to comment.