Skip to content

Commit 144240d

Browse files
committed
maint(pat inject): Move core.utils.rebaseURL to inject patterns.
core.utils.rebaseURL was only used by the inject pattern and is in this form not useful for broader use. If you need to rebase an relative or absolute URL against a base url, use: new URL(url, base_url)
1 parent cd16107 commit 144240d

File tree

4 files changed

+62
-60
lines changed

4 files changed

+62
-60
lines changed

src/core/utils.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@ function extend(obj) {
156156
}
157157
// END: Taken from Underscore.js until here.
158158

159-
function rebaseURL(base, url) {
160-
base = new URL(base, window.location).href; // If base is relative make it absolute.
161-
if (url.indexOf("://") !== -1 || url[0] === "/" || url.indexOf("data:") === 0) {
162-
return url;
163-
}
164-
return base.slice(0, base.lastIndexOf("/") + 1) + url;
165-
}
166-
167159
function findLabel(input) {
168160
var $label;
169161
for (
@@ -683,7 +675,6 @@ var utils = {
683675
escapeRegExp: escapeRegExp,
684676
isObject: isObject,
685677
extend: extend,
686-
rebaseURL: rebaseURL,
687678
findLabel: findLabel,
688679
elementInViewport: elementInViewport,
689680
removeWildcardClass: removeWildcardClass,

src/core/utils.test.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,6 @@ import $ from "jquery";
33
import { jest } from "@jest/globals";
44

55
describe("basic tests", function () {
6-
describe("rebaseURL", function () {
7-
it("Keep URL with scheme", function () {
8-
expect(
9-
utils.rebaseURL("http://example.com/foo/", "http://other.com/me")
10-
).toBe("http://other.com/me");
11-
});
12-
13-
it("Keep URL with absolute path", function () {
14-
expect(utils.rebaseURL("http://example.com/foo/", "/me")).toBe("/me");
15-
});
16-
17-
it("Rebase to base with filename", function () {
18-
expect(
19-
utils.rebaseURL("http://example.com/foo/index.html", "me/page.html")
20-
).toBe("http://example.com/foo/me/page.html");
21-
});
22-
23-
it("Rebase to base with directory path", function () {
24-
expect(utils.rebaseURL("http://example.com/foo/", "me/page.html")).toBe(
25-
"http://example.com/foo/me/page.html"
26-
);
27-
});
28-
29-
it("Rebase with absolute base url", function () {
30-
expect(
31-
utils.rebaseURL("/foo/", "me/page.html").indexOf("/foo/me/page.html") > 0
32-
).toBe(true);
33-
});
34-
35-
it("Rebase with relative base url", function () {
36-
expect(
37-
utils
38-
.rebaseURL("example.com/foo/", "me/page.html")
39-
.indexOf("example.com/foo/me/page.html") > 0
40-
).toBe(true);
41-
});
42-
43-
it("Doesn't rebase data: urls", function () {
44-
expect(
45-
utils.rebaseURL("http://example.com/foo/", "data:image-base64gibberish")
46-
).toBe("data:image-base64gibberish");
47-
});
48-
});
49-
506
describe("removeDuplicateObjects", function () {
517
it("removes removes duplicates inside an array of objects", function () {
528
var objs = [];

src/pat/inject/inject.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,14 @@ const inject = {
861861
"inject": ["url"],
862862
},
863863

864+
_rebaseURL(base, url) {
865+
if (url.indexOf("://") !== -1 || url[0] === "/" || url.indexOf("data:") === 0) {
866+
return url;
867+
}
868+
base = new URL(base, window.location).href; // If base is relative make it absolute.
869+
return base.slice(0, base.lastIndexOf("/") + 1) + url;
870+
},
871+
864872
_rebaseHTML(base, html) {
865873
if (html === "") {
866874
// Special case, source is none
@@ -889,7 +897,7 @@ const inject = {
889897
value.slice(0, 6) !== "teams:" &&
890898
value.slice(0, 11) !== "javascript:"
891899
) {
892-
value = utils.rebaseURL(base, value);
900+
value = this._rebaseURL(base, value);
893901
$el_.attr(attrName, value);
894902
}
895903
});
@@ -915,9 +923,9 @@ const inject = {
915923
}
916924
changed = true;
917925
if (Array.isArray(val)) {
918-
config[opt] = val.map((it) => utils.rebaseURL(base, it));
926+
config[opt] = val.map((it) => this._rebaseURL(base, it));
919927
} else {
920-
config[opt] = utils.rebaseURL(base, val);
928+
config[opt] = this._rebaseURL(base, val);
921929
}
922930
}
923931
}
@@ -945,9 +953,7 @@ const inject = {
945953
.trim();
946954
},
947955

948-
_parseRawHtml(html, url) {
949-
url = url || "";
950-
956+
_parseRawHtml(html, url = "") {
951957
// remove script tags and head and replace body by a div
952958
const title = html.match(/\<title\>(.*)\<\/title\>/);
953959
let clean_html = html

src/pat/inject/inject.test.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,55 @@ describe("pat-inject", function () {
253253
});
254254
});
255255

256+
describe("rebaseURL", function () {
257+
it("Keep URL with scheme", function () {
258+
expect(
259+
pattern._rebaseURL("http://example.com/foo/", "http://other.com/me")
260+
).toBe("http://other.com/me");
261+
});
262+
263+
it("Keep URL with absolute path", function () {
264+
expect(pattern._rebaseURL("http://example.com/foo/", "/me")).toBe("/me");
265+
});
266+
267+
it("Rebase to base with filename", function () {
268+
expect(
269+
pattern._rebaseURL("http://example.com/foo/index.html", "me/page.html")
270+
).toBe("http://example.com/foo/me/page.html");
271+
});
272+
273+
it("Rebase to base with directory path", function () {
274+
expect(pattern._rebaseURL("http://example.com/foo/", "me/page.html")).toBe(
275+
"http://example.com/foo/me/page.html"
276+
);
277+
});
278+
279+
it("Rebase with absolute base url", function () {
280+
expect(
281+
pattern
282+
._rebaseURL("/foo/", "me/page.html")
283+
.indexOf("/foo/me/page.html") > 0
284+
).toBe(true);
285+
});
286+
287+
it("Rebase with relative base url", function () {
288+
expect(
289+
pattern
290+
._rebaseURL("example.com/foo/", "me/page.html")
291+
.indexOf("example.com/foo/me/page.html") > 0
292+
).toBe(true);
293+
});
294+
295+
it("Doesn't rebase data: urls", function () {
296+
expect(
297+
pattern._rebaseURL(
298+
"http://example.com/foo/",
299+
"data:image-base64gibberish"
300+
)
301+
).toBe("data:image-base64gibberish");
302+
});
303+
});
304+
256305
describe("rebaseHTML", function () {
257306
it("Basic markup with DOCTYPE", function () {
258307
expect(
@@ -276,7 +325,7 @@ describe("pat-inject", function () {
276325
});
277326

278327
it("Element without link attribute", function () {
279-
var spy_rebaseURL = jest.spyOn(utils, "rebaseURL");
328+
var spy_rebaseURL = jest.spyOn(pattern, "_rebaseURL");
280329
expect(pattern._rebaseHTML("base", "<a>This is a test</a>")).toBe(
281330
"<a>This is a test</a>"
282331
);

0 commit comments

Comments
 (0)