From 425d04edbdda91da41df5f7f8a49b9292f2f8817 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Fri, 10 Oct 2025 01:19:29 +0200 Subject: [PATCH 1/2] fix(pat-inject): Don't break when no target is found. Don't break when no target can be found, but improve the error message and spit out the CSS selector causing the problem. --- src/pat/inject/inject.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index f94035e97..5e5fde644 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -318,7 +318,7 @@ const inject = { ) { _confirm = this.elementIsDirty(cfg.$target); } else if (cfg.confirm === "class" && cfg.target && cfg.target !== "none") { - _confirm = cfg.$target.hasClass("is-dirty"); + _confirm = cfg.$target?.hasClass("is-dirty"); } if (_confirm) { should_confirm = true; @@ -411,8 +411,8 @@ const inject = { // Special case, we don't want to display any return value. return; } - const $form = cfg.$target.parents("form"); - if ($form.length !== 0 && cfg.$target.data("initial-value") === undefined) { + const $form = cfg.$target?.parents("form"); + if ($form && $form.length !== 0 && cfg.$target.data("initial-value") === undefined) { cfg.$target.data("initial-value", cfg.$target.html()); $form.on("reset", () => { cfg.$target.html(cfg.$target.data("initial-value")); @@ -455,7 +455,10 @@ 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) }); @@ -737,7 +740,7 @@ const inject = { // Add a loading class to the target. // Can be used for loading-spinners. if (cfg?.loadingClass && cfg?.target !== "none") { - cfg.$target.addClass(cfg.loadingClass); + cfg.$target?.addClass(cfg.loadingClass); } } From e21d2c67eca0b840999ea562aa083bebe1a19ffe Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Sun, 12 Oct 2025 03:03:40 +0200 Subject: [PATCH 2/2] fix(pat-inject): More guards to not break when no target is found. Should maybe exit early, but this is a non-breaking, safe method. --- src/pat/inject/inject.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index 5e5fde644..4eeaa2f02 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -314,10 +314,16 @@ const inject = { } else if ( cfg.confirm === "form-data" && cfg.target && - cfg.target !== "none" + cfg.target !== "none" && + cfg.$target ) { _confirm = this.elementIsDirty(cfg.$target); - } else if (cfg.confirm === "class" && cfg.target && cfg.target !== "none") { + } else if ( + cfg.confirm === "class" && + cfg.target && + cfg.target !== "none" && + cfg.$target + ) { _confirm = cfg.$target?.hasClass("is-dirty"); } if (_confirm) { @@ -407,11 +413,11 @@ const inject = { * Cancel button is pressed (this triggers reset event on the * form) you would expect to populate with initial placeholder */ - if (cfg.target === "none") { + if (cfg.target === "none" || !cfg.$target) { // Special case, we don't want to display any return value. return; } - const $form = cfg.$target?.parents("form"); + const $form = cfg.$target.parents("form"); if ($form && $form.length !== 0 && cfg.$target.data("initial-value") === undefined) { cfg.$target.data("initial-value", cfg.$target.html()); $form.on("reset", () => { @@ -559,7 +565,7 @@ const inject = { // 1) finding the scroll container // 2) getting the element to scroll to (if not "top") const scroll_target = ["top", "target"].includes(cfg.scroll) - ? cfg.$target[0] + ? cfg?.$target[0] : dom.querySelectorAllAndMe($injected[0], cfg.scroll); const scroll_container = dom.find_scroll_container( @@ -609,7 +615,7 @@ const inject = { for (const [idx1, cfg] of cfgs.entries()) { const perform_inject = () => { - if (cfg.target !== "none") { + if (cfg.target !== "none" && cfg.$target) { for (const target of cfg.$target) { this._performInjection( target, @@ -691,7 +697,7 @@ const inject = { if ("$created_target" in cfg) { cfg.$created_target.remove(); } - cfg.$target.removeClass(cfg.loadingClass); + cfg.$target?.removeClass(cfg.loadingClass); $el.removeClass(cfg.executingClass); } $el.off("pat-ajax-success.pat-inject"); @@ -739,7 +745,7 @@ const inject = { } // Add a loading class to the target. // Can be used for loading-spinners. - if (cfg?.loadingClass && cfg?.target !== "none") { + if (cfg?.loadingClass && cfg?.target !== "none" && cfg.$target) { cfg.$target?.addClass(cfg.loadingClass); } } @@ -1144,7 +1150,7 @@ $(document).on("patterns-injected.inject", async (ev, cfg, trigger, injected) => return; } if (cfg) { - cfg.$target.removeClass(cfg.loadingClass); + cfg?.$target.removeClass(cfg.loadingClass); // Remove the executing class, add the executed class to the element with pat.inject on it. $(trigger).removeClass(cfg.executingClass).addClass(cfg.executedClass); }