Skip to content

Commit 3718361

Browse files
committed
fix(pat-inject): Fix error when no title is found.
1 parent b41b73d commit 3718361

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/pat/inject/inject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ const inject = {
504504
}
505505
history.pushState({ url: url }, "", url);
506506
// Also inject title element if we have one
507-
if ($title.length) {
507+
if ($title?.length) {
508508
const title_el = document.querySelector("title");
509509
if (title_el) {
510510
this._inject(trigger, $title, title_el, {

src/pat/inject/inject.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,84 @@ describe("pat-inject", function () {
17391739
expect(title).toBeTruthy();
17401740
expect(title.textContent.trim()).toBe("test"); // Old title
17411741
});
1742+
1743+
it("9.4.3 - Does not break, if no title is found in source", async function () {
1744+
document.head.innerHTML = `
1745+
<title>test</title>
1746+
`;
1747+
document.body.innerHTML = `
1748+
<a class="pat-inject"
1749+
href="test.html"
1750+
data-pat-inject="
1751+
source: body;
1752+
target: body;
1753+
history: record;
1754+
">link</a>
1755+
`;
1756+
1757+
answer(`
1758+
<html>
1759+
<body>
1760+
OK
1761+
</body>
1762+
</html>
1763+
`);
1764+
1765+
const inject = document.querySelector(".pat-inject");
1766+
1767+
pattern.init($(inject));
1768+
await utils.timeout(1); // wait a tick for async to settle.
1769+
1770+
inject.click();
1771+
1772+
await utils.timeout(1); // wait a tick for async to settle.
1773+
1774+
expect(document.body.textContent.trim()).toBe("OK");
1775+
1776+
// Title in head target is not modified.
1777+
const title = document.head.querySelector("title");
1778+
expect(title).toBeTruthy();
1779+
expect(title.textContent.trim()).toBe("test"); // Old title
1780+
});
1781+
1782+
it("9.4.4 - Does not break, if no title is found in target", async function () {
1783+
document.head.innerHTML = "";
1784+
document.body.innerHTML = `
1785+
<a class="pat-inject"
1786+
href="test.html"
1787+
data-pat-inject="
1788+
source: body;
1789+
target: body;
1790+
history: record;
1791+
">link</a>
1792+
`;
1793+
1794+
answer(`
1795+
<html>
1796+
<head>
1797+
<title>hello</title>
1798+
<body>
1799+
OK
1800+
</body>
1801+
</html>
1802+
`);
1803+
1804+
const inject = document.querySelector(".pat-inject");
1805+
1806+
pattern.init($(inject));
1807+
await utils.timeout(1); // wait a tick for async to settle.
1808+
1809+
inject.click();
1810+
1811+
await utils.timeout(1); // wait a tick for async to settle.
1812+
1813+
expect(document.body.textContent.trim()).toBe("OK");
1814+
1815+
// There is no title to be updated in target.
1816+
const title = document.head.querySelector("title");
1817+
expect(title).toBeFalsy();
1818+
});
1819+
17421820
});
17431821

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

0 commit comments

Comments
 (0)