From 0d8e4cb1b0c0c0ea46d688c16a003ee956e1310b Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Mon, 13 Oct 2014 12:17:46 -0700 Subject: [PATCH] Prevent rewriting # with document query params All urls will use baseUrl to setup a "clean" URL object Fixes #110 Don't bleed document search param into hash-only links --- src/lib/url.js | 20 +++++++++++++++----- test/html/url.html | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/lib/url.js b/src/lib/url.js index 7c0f9b1361..90de75558e 100644 --- a/src/lib/url.js +++ b/src/lib/url.js @@ -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 @@ -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'); @@ -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) { @@ -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; @@ -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, ''); @@ -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) { @@ -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('/'); diff --git a/test/html/url.html b/test/html/url.html index 1f2837bbe2..c89d727d2f 100644 --- a/test/html/url.html +++ b/test/html/url.html @@ -22,6 +22,7 @@ +