From bd536b5c6b670ae2893e2c2bdc92d70d860b5d0d Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Thu, 26 Nov 2026 01:53:41 +0100 Subject: [PATCH] fix(pat-inject): Abort injection if a non-existing target cannot be created. Non-existing targets are only created for id selectors. For other selectors `createTarget` returned `null`, but `ensureTarget` still reported success, which led to a TypeError later on when accessing `cfg.$target`. Now the configuration verification fails and the injection is aborted early, before any request is made. Also log the selector when a target cannot be created. Assisted-by: Claude Opus 5.5 --- src/pat/inject/inject.js | 6 +++++- src/pat/inject/inject.test.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) 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 () {