Skip to content

Commit e21d2c6

Browse files
committed
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.
1 parent 425d04e commit e21d2c6

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/pat/inject/inject.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,16 @@ const inject = {
314314
} else if (
315315
cfg.confirm === "form-data" &&
316316
cfg.target &&
317-
cfg.target !== "none"
317+
cfg.target !== "none" &&
318+
cfg.$target
318319
) {
319320
_confirm = this.elementIsDirty(cfg.$target);
320-
} else if (cfg.confirm === "class" && cfg.target && cfg.target !== "none") {
321+
} else if (
322+
cfg.confirm === "class" &&
323+
cfg.target &&
324+
cfg.target !== "none" &&
325+
cfg.$target
326+
) {
321327
_confirm = cfg.$target?.hasClass("is-dirty");
322328
}
323329
if (_confirm) {
@@ -407,11 +413,11 @@ const inject = {
407413
* Cancel button is pressed (this triggers reset event on the
408414
* form) you would expect to populate with initial placeholder
409415
*/
410-
if (cfg.target === "none") {
416+
if (cfg.target === "none" || !cfg.$target) {
411417
// Special case, we don't want to display any return value.
412418
return;
413419
}
414-
const $form = cfg.$target?.parents("form");
420+
const $form = cfg.$target.parents("form");
415421
if ($form && $form.length !== 0 && cfg.$target.data("initial-value") === undefined) {
416422
cfg.$target.data("initial-value", cfg.$target.html());
417423
$form.on("reset", () => {
@@ -559,7 +565,7 @@ const inject = {
559565
// 1) finding the scroll container
560566
// 2) getting the element to scroll to (if not "top")
561567
const scroll_target = ["top", "target"].includes(cfg.scroll)
562-
? cfg.$target[0]
568+
? cfg?.$target[0]
563569
: dom.querySelectorAllAndMe($injected[0], cfg.scroll);
564570

565571
const scroll_container = dom.find_scroll_container(
@@ -609,7 +615,7 @@ const inject = {
609615

610616
for (const [idx1, cfg] of cfgs.entries()) {
611617
const perform_inject = () => {
612-
if (cfg.target !== "none") {
618+
if (cfg.target !== "none" && cfg.$target) {
613619
for (const target of cfg.$target) {
614620
this._performInjection(
615621
target,
@@ -691,7 +697,7 @@ const inject = {
691697
if ("$created_target" in cfg) {
692698
cfg.$created_target.remove();
693699
}
694-
cfg.$target.removeClass(cfg.loadingClass);
700+
cfg.$target?.removeClass(cfg.loadingClass);
695701
$el.removeClass(cfg.executingClass);
696702
}
697703
$el.off("pat-ajax-success.pat-inject");
@@ -739,7 +745,7 @@ const inject = {
739745
}
740746
// Add a loading class to the target.
741747
// Can be used for loading-spinners.
742-
if (cfg?.loadingClass && cfg?.target !== "none") {
748+
if (cfg?.loadingClass && cfg?.target !== "none" && cfg.$target) {
743749
cfg.$target?.addClass(cfg.loadingClass);
744750
}
745751
}
@@ -1144,7 +1150,7 @@ $(document).on("patterns-injected.inject", async (ev, cfg, trigger, injected) =>
11441150
return;
11451151
}
11461152
if (cfg) {
1147-
cfg.$target.removeClass(cfg.loadingClass);
1153+
cfg?.$target.removeClass(cfg.loadingClass);
11481154
// Remove the executing class, add the executed class to the element with pat.inject on it.
11491155
$(trigger).removeClass(cfg.executingClass).addClass(cfg.executedClass);
11501156
}

0 commit comments

Comments
 (0)