Skip to content

Commit fcd87d4

Browse files
committed
maint(pat-inject): Test automatic title insertion in history record mode.
1 parent 9030ba5 commit fcd87d4

File tree

2 files changed

+105
-11
lines changed

2 files changed

+105
-11
lines changed

src/pat/inject/inject.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ const inject = {
454454
return $target;
455455
},
456456

457-
_performInjection(target, $el, $source, cfg, trigger, title) {
457+
_performInjection(target, $el, $source, cfg, trigger, $title) {
458458
/* Called after the XHR has succeeded and we have a new $source
459459
* element to inject.
460460
*/
@@ -485,13 +485,13 @@ const inject = {
485485
// Now the injection actually happens.
486486
if (this._inject(trigger, source_nodes, target, cfg)) {
487487
// Update history
488-
this._update_history(cfg, trigger, title);
488+
this._update_history(cfg, trigger, $title);
489489
// Post-injection
490490
this._afterInjection($el, cfg.$created_target || $(source_nodes), cfg);
491491
}
492492
},
493493

494-
_update_history(cfg, trigger, title) {
494+
_update_history(cfg, trigger, $title) {
495495
// History support. if subform is submitted, append form params
496496
if (cfg.history !== "record" || !history?.pushState) {
497497
return;
@@ -503,10 +503,10 @@ const inject = {
503503
}
504504
history.pushState({ url: url }, "", url);
505505
// Also inject title element if we have one
506-
if (title) {
506+
if ($title.length) {
507507
const title_el = document.querySelector("title");
508508
if (title_el) {
509-
this._inject(trigger, [title], title_el, {
509+
this._inject(trigger, $title, title_el, {
510510
action: "element",
511511
});
512512
}
@@ -585,14 +585,14 @@ const inject = {
585585
]);
586586
/* pick the title source for dedicated handling later
587587
Title - if present - is always appended at the end. */
588-
let title;
588+
let $title;
589589
if (
590590
sources$ &&
591591
sources$[sources$.length - 1] &&
592592
sources$[sources$.length - 1][0] &&
593593
sources$[sources$.length - 1][0].nodeName === "TITLE"
594594
) {
595-
title = sources$[sources$.length - 1];
595+
$title = sources$[sources$.length - 1];
596596
}
597597
cfgs.forEach((cfg, idx1) => {
598598
const perform_inject = () => {
@@ -604,7 +604,7 @@ const inject = {
604604
sources$[idx1],
605605
cfg,
606606
ev.target,
607-
title
607+
$title
608608
);
609609
}
610610
}
@@ -967,8 +967,11 @@ const inject = {
967967
let clean_html = html
968968
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
969969
.replace(/<head\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/head>/gi, "")
970+
.replace(/<html([^>]*?)>/gi, "")
971+
.replace(/<\/html([^>]*?)>/gi, "")
970972
.replace(/<body([^>]*?)>/gi, '<div id="__original_body">')
971973
.replace(/<\/body([^>]*?)>/gi, "</div>");
974+
972975
if (title && title.length == 2) {
973976
clean_html = title[0] + clean_html;
974977
}
@@ -1109,7 +1112,8 @@ const inject = {
11091112
sources(cfgs, data) {
11101113
const sources = cfgs.map((cfg) => cfg.source);
11111114
sources.push("title");
1112-
return this._sourcesFromHtml(data, cfgs[0].url, sources);
1115+
const result = this._sourcesFromHtml(data, cfgs[0].url, sources);
1116+
return result;
11131117
},
11141118
},
11151119
},

src/pat/inject/inject.test.js

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,9 +1557,7 @@ describe("pat-inject", function () {
15571557

15581558
expect(catched).toBe(true);
15591559
expect(pattern.execute).toHaveBeenCalled();
1560-
15611560
});
1562-
15631561
});
15641562
});
15651563
});
@@ -1652,6 +1650,98 @@ describe("pat-inject", function () {
16521650
});
16531651
});
16541652

1653+
describe("9.4 - injecton of the title element.", function () {
1654+
let spy_ajax;
1655+
1656+
beforeEach(function () {
1657+
spy_ajax = jest.spyOn($, "ajax").mockImplementation(() => deferred);
1658+
});
1659+
1660+
afterEach(function () {
1661+
spy_ajax.mockRestore();
1662+
});
1663+
1664+
it("9.4.1 - Injects a title element with history:record", async function () {
1665+
document.head.innerHTML = `
1666+
<title>test</title>
1667+
`;
1668+
document.body.innerHTML = `
1669+
<a class="pat-inject"
1670+
href="test.html"
1671+
data-pat-inject="
1672+
source: body;
1673+
target: body;
1674+
history: record;
1675+
">link</a>
1676+
`;
1677+
1678+
answer(`
1679+
<html>
1680+
<head>
1681+
<title>hello</title>
1682+
</head>
1683+
<body>
1684+
OK
1685+
</body>
1686+
</html>
1687+
`);
1688+
1689+
const inject = document.querySelector(".pat-inject");
1690+
1691+
pattern.init($(inject));
1692+
await utils.timeout(1); // wait a tick for async to settle.
1693+
1694+
inject.click();
1695+
1696+
await utils.timeout(1); // wait a tick for async to settle.
1697+
1698+
expect(document.body.textContent.trim()).toBe("OK");
1699+
1700+
const title = document.head.querySelector("title");
1701+
expect(title).toBeTruthy();
1702+
expect(title.textContent.trim()).toBe("hello");
1703+
});
1704+
1705+
it("9.4.2 - Does not inject a title element without history:record", async function () {
1706+
document.head.innerHTML = `
1707+
<title>test</title>
1708+
`;
1709+
document.body.innerHTML = `
1710+
<a class="pat-inject"
1711+
href="test.html"
1712+
data-pat-inject="
1713+
source: body;
1714+
target: body;
1715+
">link</a>
1716+
`;
1717+
1718+
answer(`
1719+
<html>
1720+
<head>
1721+
<title>hello</title>
1722+
</head>
1723+
<body>
1724+
OK
1725+
</body>
1726+
</html>
1727+
`);
1728+
1729+
const inject = document.querySelector(".pat-inject");
1730+
1731+
pattern.init($(inject));
1732+
await utils.timeout(1); // wait a tick for async to settle.
1733+
1734+
inject.click();
1735+
1736+
await utils.timeout(1); // wait a tick for async to settle.
1737+
1738+
expect(document.body.textContent.trim()).toBe("OK");
1739+
1740+
const title = document.head.querySelector("title");
1741+
expect(title).toBeTruthy();
1742+
expect(title.textContent.trim()).toBe("test"); // Old title
1743+
});
1744+
});
16551745
});
16561746

16571747
describe("10 - Error handling", () => {

0 commit comments

Comments
 (0)