Skip to content

Commit

Permalink
Prevent rewriting # with document query params
Browse files Browse the repository at this point in the history
All urls will use baseUrl to setup a "clean" URL object
Fixes Polymer#110 Don't bleed document search param into hash-only links
  • Loading branch information
dfreedm committed Oct 13, 2014
1 parent fc3b80a commit 0d8e4cb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/lib/url.js
Expand Up @@ -11,7 +11,7 @@

var urlResolver = {
resolveDom: function(root, url) {
url = url || root.ownerDocument.baseURI;
url = url || baseUrl(root);
this.resolveAttributes(root, url);
this.resolveStyles(root, url);
// handle template.content
Expand All @@ -25,7 +25,7 @@ var urlResolver = {
}
},
resolveTemplate: function(template) {
this.resolveDom(template.content, template.ownerDocument.baseURI);
this.resolveDom(template.content, baseUrl(template));
},
resolveStyles: function(root, url) {
var styles = root.querySelectorAll('style');
Expand All @@ -36,7 +36,7 @@ var urlResolver = {
}
},
resolveStyle: function(style, url) {
url = url || style.ownerDocument.baseURI;
url = url || baseUrl(style);
style.textContent = this.resolveCssText(style.textContent, url);
},
resolveCssText: function(cssText, baseUrl, keepAbsolute) {
Expand All @@ -56,7 +56,7 @@ var urlResolver = {
}
},
resolveElementAttributes: function(node, url) {
url = url || node.ownerDocument.baseURI;
url = url || baseUrl(node);
URL_ATTRS.forEach(function(v) {
var attr = node.attributes[v];
var value = attr && attr.value;
Expand All @@ -80,6 +80,13 @@ var URL_ATTRS_SELECTOR = '[' + URL_ATTRS.join('],[') + ']';
var URL_TEMPLATE_SEARCH = '{{.*}}';
var URL_HASH = '#';

function baseUrl(node) {
var u = new URL(node.ownerDocument.baseURI);
u.search = '';
u.hash = '';
return u;
}

function replaceUrlsInCssText(cssText, baseUrl, keepAbsolute, regexp) {
return cssText.replace(regexp, function(m, pre, url, post) {
var urlPath = url.replace(/["']/g, '');
Expand All @@ -98,7 +105,7 @@ function resolveRelativeUrl(baseUrl, url, keepAbsolute) {
}

function makeDocumentRelPath(url) {
var root = new URL(document.baseURI);
var root = baseUrl(document.documentElement);
var u = new URL(url, root);
if (u.host === root.host && u.port === root.port &&
u.protocol === root.protocol) {
Expand All @@ -110,6 +117,9 @@ function makeDocumentRelPath(url) {

// make a relative path from source to target
function makeRelPath(sourceUrl, targetUrl) {
if (targetUrl.href.slice(-1) === '#') {
debugger;
}
var source = sourceUrl.pathname;
var target = targetUrl.pathname;
var s = source.split('/');
Expand Down
2 changes: 2 additions & 0 deletions test/html/url.html
Expand Up @@ -22,6 +22,7 @@
<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>
</template>
<script>
addEventListener('HTMLImportsLoaded', function() {
Expand All @@ -34,6 +35,7 @@
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');
done();
});
</script>
Expand Down

0 comments on commit 0d8e4cb

Please sign in to comment.