Skip to content

Commit

Permalink
Merge pull request #1235 from Patternslib/fix-inject
Browse files Browse the repository at this point in the history
Fix inject
  • Loading branch information
thet authored Feb 3, 2025
2 parents b41b73d + 6c9ad2d commit 819311c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/pat/inject/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,13 @@ const inject = {
return;
}
let url = cfg.url;
const glue = url.indexOf("?") > -1 ? "&" : "?";
if (cfg.params) {
const glue = url.indexOf("?") > -1 ? "&" : "?";
url = `${url}${glue}${cfg.params}`;
}
history.pushState({ url: url }, "", url);
// Also inject title element if we have one
if ($title.length) {
if ($title?.length) {
const title_el = document.querySelector("title");
if (title_el) {
this._inject(trigger, $title, title_el, {
Expand Down
78 changes: 78 additions & 0 deletions src/pat/inject/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,84 @@ describe("pat-inject", function () {
expect(title).toBeTruthy();
expect(title.textContent.trim()).toBe("test"); // Old title
});

it("9.4.3 - Does not break, if no title is found in source", async function () {
document.head.innerHTML = `
<title>test</title>
`;
document.body.innerHTML = `
<a class="pat-inject"
href="test.html"
data-pat-inject="
source: body;
target: body;
history: record;
">link</a>
`;

answer(`
<html>
<body>
OK
</body>
</html>
`);

const inject = document.querySelector(".pat-inject");

pattern.init($(inject));
await utils.timeout(1); // wait a tick for async to settle.

inject.click();

await utils.timeout(1); // wait a tick for async to settle.

expect(document.body.textContent.trim()).toBe("OK");

// Title in head target is not modified.
const title = document.head.querySelector("title");
expect(title).toBeTruthy();
expect(title.textContent.trim()).toBe("test"); // Old title
});

it("9.4.4 - Does not break, if no title is found in target", async function () {
document.head.innerHTML = "";
document.body.innerHTML = `
<a class="pat-inject"
href="test.html"
data-pat-inject="
source: body;
target: body;
history: record;
">link</a>
`;

answer(`
<html>
<head>
<title>hello</title>
<body>
OK
</body>
</html>
`);

const inject = document.querySelector(".pat-inject");

pattern.init($(inject));
await utils.timeout(1); // wait a tick for async to settle.

inject.click();

await utils.timeout(1); // wait a tick for async to settle.

expect(document.body.textContent.trim()).toBe("OK");

// There is no title to be updated in target.
const title = document.head.querySelector("title");
expect(title).toBeFalsy();
});

});

describe("9.5 - support multiple source element matches.", function () {
Expand Down

0 comments on commit 819311c

Please sign in to comment.