Skip to content

Commit 64947e1

Browse files
committed
Issue #2215857 by michielnugter, Lendude, gmercer, tim.plunkett, cferthorney, marabak, olli, ericmulder1980, TwoD, sanduhrs, stella, dww, nod_: Behaviors get attached to removed forms
(cherry picked from commit 57f1f1a)
1 parent 34919d6 commit 64947e1

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

misc/ajax.es6.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@
774774
// when there is a form such that this.$form.ajaxSubmit() is used instead of
775775
// $.ajax(). When there is no form and $.ajax() is used, beforeSerialize()
776776
// isn't called, but don't rely on that: explicitly check this.$form.
777-
if (this.$form) {
777+
if (this.$form && document.contains(this.$form.get(0))) {
778778
const settings = this.settings || drupalSettings;
779779
Drupal.detachBehaviors(this.$form.get(0), settings, 'serialize');
780780
}
@@ -1020,7 +1020,7 @@
10201020
// attachBehaviors() called on the new content from processing the response
10211021
// commands is not sufficient, because behaviors from the entire form need
10221022
// to be reattached.
1023-
if (this.$form) {
1023+
if (this.$form && document.contains(this.$form.get(0))) {
10241024
const settings = this.settings || drupalSettings;
10251025
Drupal.attachBehaviors(this.$form.get(0), settings);
10261026
}
@@ -1088,8 +1088,9 @@
10881088
$(this.wrapper).show();
10891089
// Re-enable the element.
10901090
$(this.element).prop('disabled', false);
1091-
// Reattach behaviors, if they were detached in beforeSerialize().
1092-
if (this.$form) {
1091+
// Reattach behaviors, if they were detached in beforeSerialize(), and the
1092+
// form is still part of the document.
1093+
if (this.$form && document.contains(this.$form.get(0))) {
10931094
const settings = this.settings || drupalSettings;
10941095
Drupal.attachBehaviors(this.$form.get(0), settings);
10951096
}

misc/ajax.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
339339
};
340340

341341
Drupal.Ajax.prototype.beforeSerialize = function (element, options) {
342-
if (this.$form) {
342+
if (this.$form && document.contains(this.$form.get(0))) {
343343
var settings = this.settings || drupalSettings;
344344
Drupal.detachBehaviors(this.$form.get(0), settings, 'serialize');
345345
}
@@ -451,7 +451,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
451451
}
452452
}
453453

454-
if (this.$form) {
454+
if (this.$form && document.contains(this.$form.get(0))) {
455455
var settings = this.settings || drupalSettings;
456456
Drupal.attachBehaviors(this.$form.get(0), settings);
457457
}
@@ -493,7 +493,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
493493

494494
$(this.element).prop('disabled', false);
495495

496-
if (this.$form) {
496+
if (this.$form && document.contains(this.$form.get(0))) {
497497
var settings = this.settings || drupalSettings;
498498
Drupal.attachBehaviors(this.$form.get(0), settings);
499499
}

modules/views_ui/tests/src/FunctionalJavascript/FilterCriteriaTest.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,7 @@ public function testFilterCriteriaDialog() {
4242
$assert_session = $this->assertSession();
4343
$page = $this->getSession()->getPage();
4444

45-
// Use the 'And/Or Rearrange' link for fields to open a dialog.
46-
$dropbutton = $page->find('css', '.views-ui-display-tab-bucket.filter .dropbutton-toggle button');
47-
$dropbutton->click();
48-
$add_link = $page->findById('views-rearrange-filter');
49-
$this->assertTrue($add_link->isVisible(), 'And/Or Rearrange button found.');
50-
$add_link->click();
51-
$assert_session->assertWaitOnAjaxRequest();
45+
$this->openFilterDialog();
5246

5347
// Add a new filter group.
5448
$create_new_filter_group = $page->findById('views-add-group-link');
@@ -71,6 +65,38 @@ public function testFilterCriteriaDialog() {
7165
$this->drupalGet('admin/structure/views/view/who_s_online');
7266
$page = $this->getSession()->getPage();
7367
$this->assertNotNull($page->findLink('User: Last access (>= -15 minutes)'));
68+
69+
// Add group again to test drag-n-drop.
70+
$this->openFilterDialog();
71+
72+
$this->assertSession()->waitForLink('Create new filter group', 20000);
73+
$create_new_filter_group = $page->findLink('Create new filter group');
74+
$this->assertTrue($create_new_filter_group->isVisible(), 'Add group link found.');
75+
$create_new_filter_group->click();
76+
$assert_session->assertWaitOnAjaxRequest();
77+
78+
// Validate dragging works correctly and the new group will contain the new
79+
// filter.
80+
$dragged = $page->find('css', ".tabledrag-handle");
81+
$target = $page->find('css', '.filter-group-operator-row');
82+
$dragged->dragTo($target);
83+
84+
$remove_link = $page->findLink('Remove group');
85+
$this->assertFalse($remove_link->isVisible(), 'Remove group should be invisible after drag.');
86+
}
87+
88+
/**
89+
* Uses the 'And/Or Rearrange' link for filters to open a dialog.
90+
*/
91+
protected function openFilterDialog() {
92+
$assert_session = $this->assertSession();
93+
$page = $this->getSession()->getPage();
94+
$dropbutton = $page->find('css', '.views-ui-display-tab-bucket.filter .dropbutton-toggle button');
95+
$dropbutton->click();
96+
$add_link = $page->findById('views-rearrange-filter');
97+
$this->assertTrue($add_link->isVisible(), 'And/Or Rearrange button found.');
98+
$add_link->click();
99+
$assert_session->assertWaitOnAjaxRequest();
74100
}
75101

76102
}

tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,20 @@ public function testAjaxCommands() {
3737
$this->assertWaitPageContains('<div id="after_div">Something can be inserted after this</div>This will be placed after');
3838

3939
// Tests the 'alert' command.
40-
$test_alert_command = <<<JS
41-
window.alert = function() {
42-
document.body.innerHTML += '<div class="alert-command">Alert</div>';
43-
};
44-
JS;
45-
$session->executeScript($test_alert_command);
4640
$page->pressButton("AJAX 'Alert': Click to alert");
47-
$this->assertWaitPageContains('<div class="alert-command">Alert</div>');
41+
// Wait for the alert to appear.
42+
$page->waitFor(10, function () use ($session) {
43+
try {
44+
$session->getDriver()->getWebDriverSession()->getAlert_text();
45+
return TRUE;
46+
}
47+
catch (\Exception $e) {
48+
return FALSE;
49+
}
50+
});
51+
$alert_text = $this->getSession()->getDriver()->getWebDriverSession()->getAlert_text();
52+
$this->assertEquals('Alert', $alert_text);
53+
$this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
4854

4955
// Tests the 'append' command.
5056
$page->pressButton("AJAX 'Append': Click to append something");

0 commit comments

Comments
 (0)