Skip to content

Commit 0b35817

Browse files
committed
feat(pat-base-url): Add new pattern to update data-base-url on the body tag when the URL changes.
1 parent 4844a2a commit 0b35817

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

src/pat/base-url/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# pat-base-url
2+
3+
Update the `data-base-url` attribute on the body tag when a `navigate` event is
4+
thrown and thus the browser's URL has changed.

src/pat/base-url/base-url.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Set the base URL based on the current location, listening on navigation changes.
2+
import { BasePattern } from "@patternslib/patternslib/src/core/basepattern";
3+
import registry from "@patternslib/patternslib/src/core/registry";
4+
import events from "@patternslib/patternslib/src/core/events";
5+
6+
class Pattern extends BasePattern {
7+
static name = "pat-base-url";
8+
static trigger = "body";
9+
10+
init() {
11+
events.add_event_listener(
12+
window.navigation,
13+
"navigate",
14+
"thet-base-url--set",
15+
this.set_base_url.bind(this)
16+
);
17+
}
18+
19+
set_base_url() {
20+
let url = window.location.href;
21+
22+
// Split the following words from the URL as we want to get the
23+
// contents absolute URL.
24+
const split_words = [
25+
// NOTE: order matters.
26+
"/@@", // also catches @@folder_contents and @@edit
27+
"/++", // traversal urls.
28+
"/folder_contents",
29+
"/edit",
30+
"/view",
31+
"#",
32+
"?",
33+
];
34+
35+
// Split all split words out of url
36+
url = split_words.reduce((url_, split_) => url_.split(split_)[0], url);
37+
// Remove the trailing slash
38+
if (url[url.length -1] === "/") {
39+
url = url.substring(0, url.length - 1)
40+
}
41+
42+
// Set the contents absolute URL on `data-base-url`.
43+
document.body.dataset.baseUrl = url;
44+
}
45+
}
46+
47+
registry.register(Pattern);
48+
export default Pattern;
49+
export { Pattern };

src/pat/base-url/base-url.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import "@patternslib/patternslib/src/core/polyfills";
2+
import utils from "@patternslib/patternslib/src/core/utils";
3+
import Pattern from "./base-url";
4+
5+
describe("pat-base-url tests", () => {
6+
afterEach(() => {
7+
document.body.innerHTML = "";
8+
});
9+
10+
it("is initialized correctly", async () => {
11+
document.body.dataset.baseUrl = "http://localhost/not/okay"
12+
13+
new Pattern(document.body);
14+
await utils.timeout(1); // wait a tick for async to settle.
15+
16+
expect(document.body.dataset.baseUrl).toBe("http://localhost/not/okay");
17+
history.pushState(null, "", "/okay/okay");
18+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay/okay");
19+
});
20+
21+
it("Cleans the URL from views and traversal URLs.", async () => {
22+
new Pattern(document.body);
23+
await utils.timeout(1); // wait a tick for async to settle.
24+
25+
history.pushState(null, "", "/okay/@@okay");
26+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay");
27+
28+
history.pushState(null, "", "/okay/++okay");
29+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay");
30+
31+
history.pushState(null, "", "/okay/folder_contents");
32+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay");
33+
34+
history.pushState(null, "", "/okay/edit");
35+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay");
36+
37+
history.pushState(null, "", "/okay/view");
38+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay");
39+
40+
history.pushState(null, "", "/okay/#okay");
41+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay");
42+
43+
history.pushState(null, "", "/okay/?okay");
44+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay");
45+
46+
history.pushState(null, "", "/okay/?okay#okay");
47+
expect(document.body.dataset.baseUrl).toBe("http://localhost/okay");
48+
});
49+
});

src/patterns.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import "@patternslib/patternslib/src/pat/depends/depends";
1919
// Import all used patterns for the bundle to be generated
2020
import "./pat/autotoc/autotoc";
2121
import "./pat/backdrop/backdrop";
22+
import "./pat/base-url/base-url";
2223
import "./pat/contentloader/contentloader";
2324
import "./pat/contentbrowser/contentbrowser";
2425
import "./pat/cookietrigger/cookietrigger";

0 commit comments

Comments
 (0)