Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand Down
7 changes: 2 additions & 5 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 12 additions & 16 deletions src/pat/inject/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -828,22 +829,17 @@ define([
"$1src=\"\" data-pat-inject-rebase-$2="
).trim()).wrapAll("<div>").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() {
Expand Down
10 changes: 5 additions & 5 deletions tests/specs/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down