diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index d7592635e..9bbb46039 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -351,6 +351,10 @@ const inject = { return false; } cfg.$target = this.createTarget(cfg.target); + if (!cfg.$target) { + // Target could not be created. Abort. + return false; + } cfg.$created_target = cfg.$target; } return true; @@ -456,7 +460,7 @@ const inject = { * appended to the body. */ if (selector.slice(0, 1) !== "#") { - log.error("only id supported for non-existing target"); + log.error("only id supported for non-existing target. selector: ", selector); return null; } const $target = $("
").attr({ id: selector.slice(1) }); diff --git a/src/pat/inject/inject.test.js b/src/pat/inject/inject.test.js index 0d4475747..ba81b53c1 100644 --- a/src/pat/inject/inject.test.js +++ b/src/pat/inject/inject.test.js @@ -1769,6 +1769,29 @@ describe("pat-inject", function () { const modal = document.querySelector("#pat-modal"); expect(modal).toBeFalsy(); }); + + it("9.3.3 - Abort if a non-existing target cannot be created", async function () { + // Only id selectors can be created. For other selectors the + // injection is aborted before any request is made. + document.body.innerHTML = ` + link + `; + + 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(spy_ajax).not.toHaveBeenCalled(); + expect(inject.classList.contains("executing")).toBe(false); + expect(document.querySelector(".not-existing")).toBe(null); + }); }); describe("9.4 - injecton of the title element.", function () {