From 8ac73be31189779ca3c8d9cb152a8b9641c3a84f Mon Sep 17 00:00:00 2001 From: nod_ <nod_@598310.no-reply.drupal.org> Date: Mon, 22 Apr 2024 23:05:04 +0200 Subject: [PATCH] Issue #3238868 by ahsannazir, smustgrave: Refactor some uses of the jQuery parents function to use vanillaJS --- core/misc/ajax.js | 23 ++++++++++--------- core/modules/editor/js/editor.js | 2 +- .../modules/settings_tray/js/settings_tray.js | 10 ++++---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/core/misc/ajax.js b/core/misc/ajax.js index c476f204914c..fe1f8a1845cf 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -1377,17 +1377,18 @@ $newContent[effect.showEffect](effect.showSpeed); } - // Attach all JavaScript behaviors to the new content, if it was - // successfully added to the page, this if statement allows - // `#ajax['wrapper']` to be optional. - if ($newContent.parents('html').length) { - // Attach behaviors to all element nodes. - $newContent.each((index, element) => { - if (element.nodeType === Node.ELEMENT_NODE) { - Drupal.attachBehaviors(element, settings); - } - }); - } + // Attach behaviors to all element nodes. + $newContent.each((index, element) => { + if ( + element.nodeType === Node.ELEMENT_NODE && + // Attach all JavaScript behaviors to the new content, if it was + // successfully added to the page, this condition allows + // `#ajax['wrapper']` to be optional. + document.documentElement.contains(element) + ) { + Drupal.attachBehaviors(element, settings); + } + }); }, /** diff --git a/core/modules/editor/js/editor.js b/core/modules/editor/js/editor.js index b0900e99b614..da98baef3405 100644 --- a/core/modules/editor/js/editor.js +++ b/core/modules/editor/js/editor.js @@ -241,7 +241,7 @@ $this.on('change.editorAttach', { field }, onTextFormatChange); } // Detach any editor when the containing form is submitted. - $this.parents('form').on('submit', (event) => { + $(field.form).on('submit', (event) => { // Do not detach if the event was canceled. if (event.isDefaultPrevented()) { return; diff --git a/core/modules/settings_tray/js/settings_tray.js b/core/modules/settings_tray/js/settings_tray.js index 69e4d85deee6..70e5df5a40a9 100644 --- a/core/modules/settings_tray/js/settings_tray.js +++ b/core/modules/settings_tray/js/settings_tray.js @@ -160,15 +160,15 @@ * set active editable ID. */ .forEach((instance) => { + const closestSettingsTray = instance.element.closest( + '.settings-tray-editable', + ); // Check to make sure existing dialogOptions aren't overridden. if (!instance.options.data.hasOwnProperty('dialogOptions')) { instance.options.data.dialogOptions = {}; } - instance.options.data.dialogOptions.settingsTrayActiveEditableId = $( - instance.element, - ) - .parents('.settings-tray-editable') - .attr('id'); + instance.options.data.dialogOptions.settingsTrayActiveEditableId = + closestSettingsTray.id; instance.progress = { type: 'fullscreen' }; }); } -- GitLab