From eb0b15386174b8f8cc861c3e9d71422fd2c54e46 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Mon, 22 Jun 2020 18:07:54 +0200 Subject: [PATCH] core/utils rebaseURL: Change a relative base url to an absolute URL using window.location. --- CHANGES.md | 2 +- src/core/utils.js | 7 ++----- src/pat/inject/inject.js | 28 ++++++++++++---------------- tests/specs/core/utils.js | 10 +++++----- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2003d5a47..99d43f6d9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,7 +5,7 @@ Features ~~~~~~~~ -- core/utils rebaseURL: Do not rebase if the base URL isn't absolute or doesn't start with an URL scheme. +- core/utils rebaseURL: Change a relative base url to an absolute URL using window.location. - pat-push: New pattern for replacing html content on push events. - pat-scroll-box: New pattern for scrolling detection. Replaces the previous "scroll detection" module. - pat-inject: Rename undocumented ``selector`` property to ``defaultSelector``. diff --git a/src/core/utils.js b/src/core/utils.js index a7a815241..90098db28 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -182,11 +182,8 @@ define([ // END: Taken from Underscore.js until here. function rebaseURL(base, url) { - if ( - (base.indexOf("://") === -1 && base[0] !== "/") || - url.indexOf("://") !== -1 || - url[0] === "/" || - url.indexOf("data:") === 0) { + base = new URL(base, window.location).href; // If base is relative make it absolute. + if (url.indexOf("://") !== -1 || url[0] === "/" || url.indexOf("data:") === 0) { return url; } return base.slice(0, base.lastIndexOf("/")+1) + url; diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index c2f6270d5..279b667ae 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -814,6 +814,7 @@ define([ A: "href", FORM: "action", IMG: "data-pat-inject-rebase-src", + OBJECT: "data", SOURCE: "data-pat-inject-rebase-src", VIDEO: "data-pat-inject-rebase-src" }, @@ -828,22 +829,17 @@ define([ "$1src=\"\" data-pat-inject-rebase-$2=" ).trim()).wrapAll("
").parent(); - if ( - base.indexOf("://") !== -1 || - base[0] === "/" - ) { - $page.find(Object.keys(inject._rebaseAttrs).join(",")).each(function() { - var $this = $(this), - attrName = inject._rebaseAttrs[this.tagName], - value = $this.attr(attrName); - - if (value && value.slice(0, 2) !== "@@" && value[0] !== "#" && - value.slice(0, 7) !== "mailto:" && value.slice(0, 11) !== "javascript:") { - value = utils.rebaseURL(base, value); - $this.attr(attrName, value); - } - }); - } + $page.find(Object.keys(inject._rebaseAttrs).join(",")).each(function() { + var $this = $(this), + attrName = inject._rebaseAttrs[this.tagName], + value = $this.attr(attrName); + + if (value && value.slice(0, 2) !== "@@" && value[0] !== "#" && + value.slice(0, 7) !== "mailto:" && value.slice(0, 11) !== "javascript:") { + value = utils.rebaseURL(base, value); + $this.attr(attrName, value); + } + }); // XXX: IE8 changes the order of attributes in html. The following // lines move data-pat-inject-rebase-src to src. $page.find("[data-pat-inject-rebase-src]").each(function() { diff --git a/tests/specs/core/utils.js b/tests/specs/core/utils.js index ca5568353..3b060fe7a 100644 --- a/tests/specs/core/utils.js +++ b/tests/specs/core/utils.js @@ -27,14 +27,14 @@ define(["underscore", "pat-utils"], function(_, utils) { it("Rebase with absolute base url", function() { expect( - utils.rebaseURL("/foo/", "me/page.html")) - .toBe("/foo/me/page.html"); + utils.rebaseURL("/foo/", "me/page.html").indexOf("/foo/me/page.html") > 0) + .toBe(true); }); - it("Don't rebase with wrong base url", function() { + it("Rebase with relative base url", function() { expect( - utils.rebaseURL("example.com/foo/", "me/page.html")) - .toBe("me/page.html"); + utils.rebaseURL("example.com/foo/", "me/page.html").indexOf('example.com/foo/me/page.html') > 0) + .toBe(true); }); it("Doesn't rebase data: urls", function() {