diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index 309bf971c..9d4a5d6db 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -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, { diff --git a/src/pat/inject/inject.test.js b/src/pat/inject/inject.test.js index e18e56f31..028085599 100644 --- a/src/pat/inject/inject.test.js +++ b/src/pat/inject/inject.test.js @@ -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 = ` + test + `; + document.body.innerHTML = ` + link + `; + + answer(` + + + OK + + + `); + + 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 = ` + link + `; + + answer(` + + + hello + + OK + + + `); + + 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 () {