🌐 AI搜索 & 代理 主页
Skip to content

Commit c2d6847

Browse files
committed
Fix #12989. Remove ajax "global" event behavior.
1 parent b382af6 commit c2d6847

File tree

2 files changed

+43
-143
lines changed

2 files changed

+43
-143
lines changed

src/event.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -198,33 +198,30 @@ jQuery.event = {
198198
},
199199

200200
trigger: function( event, data, elem, onlyHandlers ) {
201-
// Don't do events on text and comment nodes
202-
if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) {
203-
return;
204-
}
205201

206-
// Event object or event type
207-
var cache, i, cur, old, ontype, special, handle, eventPath, bubbleType,
202+
var i, cur, old, ontype, special, handle, eventPath, bubbleType,
208203
type = event.type || event,
209204
namespaces = event.namespace ? event.namespace.split(".") : [];
210205

206+
elem = elem || document;
207+
208+
// Don't do events on text and comment nodes
209+
if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
210+
return;
211+
}
212+
211213
// focus/blur morphs to focusin/out; ensure we're not firing them right now
212214
if ( rfocusMorph.test( type + jQuery.event.triggered ) ) {
213215
return;
214216
}
215217

216-
if ( type.indexOf( "." ) >= 0 ) {
218+
if ( type.indexOf(".") >= 0 ) {
217219
// Namespaced trigger; create a regexp to match event type in handle()
218220
namespaces = type.split(".");
219221
type = namespaces.shift();
220222
namespaces.sort();
221223
}
222224

223-
if ( !elem && !jQuery.event.global[ type ] ) {
224-
// No jQuery handlers for this event type, and it can't have inline handlers
225-
return;
226-
}
227-
228225
// Caller can pass in an Event, Object, or just an event type string
229226
event = typeof event === "object" ?
230227
// jQuery.Event object
@@ -236,22 +233,9 @@ jQuery.event = {
236233

237234
event.type = type;
238235
event.isTrigger = true;
239-
event.namespace = namespaces.join( "." );
236+
event.namespace = namespaces.join(".");
240237
event.namespace_re = event.namespace? new RegExp("(^|\\.)" + namespaces.join("\\.(?:.*\\.|)") + "(\\.|$)") : null;
241-
ontype = type.indexOf( ":" ) < 0 ? "on" + type : "";
242-
243-
// Handle a global trigger
244-
if ( !elem ) {
245-
246-
// TODO: Stop taunting the data cache; remove global events and always attach to document
247-
cache = jQuery.cache;
248-
for ( i in cache ) {
249-
if ( cache[ i ].events && cache[ i ].events[ type ] ) {
250-
jQuery.event.trigger( event, data, cache[ i ].handle.elem, true );
251-
}
252-
}
253-
return;
254-
}
238+
ontype = type.indexOf(":") < 0 ? "on" + type : "";
255239

256240
// Clean up the event in case it is being reused
257241
event.result = undefined;

test/unit/ajax.js

Lines changed: 32 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ module( "ajax", {
77
return callback;
88
};
99
},
10-
teardown: moduleTeardown
10+
teardown: function() {
11+
jQuery( document ).off( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" );
12+
moduleTeardown();
13+
}
1114
});
1215

1316
(function() {
@@ -16,24 +19,16 @@ module( "ajax", {
1619
return;
1720
}
1821

22+
function addGlobalEvents() {
23+
jQuery( document ).on( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess", function( e ) {
24+
ok( true, e.type );
25+
});
26+
}
27+
1928
//----------- jQuery.ajax()
2029

2130
ajaxTest( "jQuery.ajax() - success callbacks", 8, {
22-
setup: function() {
23-
jQuery("#foo").ajaxStart(function() {
24-
ok( true, "ajaxStart" );
25-
}).ajaxStop(function() {
26-
ok( true, "ajaxStop" );
27-
}).ajaxSend(function() {
28-
ok( true, "ajaxSend" );
29-
}).ajaxComplete(function() {
30-
ok( true, "ajaxComplete" );
31-
}).ajaxError(function() {
32-
ok( false, "ajaxError" );
33-
}).ajaxSuccess(function() {
34-
ok( true, "ajaxSuccess" );
35-
});
36-
},
31+
setup: addGlobalEvents,
3732
url: url("data/name.html"),
3833
beforeSend: function() {
3934
ok( true, "beforeSend" );
@@ -47,21 +42,7 @@ module( "ajax", {
4742
});
4843

4944
ajaxTest( "jQuery.ajax() - success callbacks - (url, options) syntax", 8, {
50-
setup: function() {
51-
jQuery("#foo").ajaxStart(function() {
52-
ok( true, "ajaxStart" );
53-
}).ajaxStop(function() {
54-
ok( true, "ajaxStop" );
55-
}).ajaxSend(function() {
56-
ok( true, "ajaxSend" );
57-
}).ajaxComplete(function() {
58-
ok( true, "ajaxComplete" );
59-
}).ajaxError(function() {
60-
ok( false, "ajaxError" );
61-
}).ajaxSuccess(function() {
62-
ok( true, "ajaxSuccess" );
63-
});
64-
},
45+
setup: addGlobalEvents,
6546
create: function( options ) {
6647
return jQuery.ajax( url("data/name.html"), options );
6748
},
@@ -77,21 +58,7 @@ module( "ajax", {
7758
});
7859

7960
ajaxTest( "jQuery.ajax() - success callbacks (late binding)", 8, {
80-
setup: function() {
81-
jQuery("#foo").ajaxStart(function() {
82-
ok( true, "ajaxStart" );
83-
}).ajaxStop(function() {
84-
ok( true, "ajaxStop" );
85-
}).ajaxSend(function() {
86-
ok( true, "ajaxSend" );
87-
}).ajaxComplete(function() {
88-
ok( true, "ajaxComplete" );
89-
}).ajaxError(function() {
90-
ok( false, "ajaxError" );
91-
}).ajaxSuccess(function() {
92-
ok( true, "ajaxSuccess" );
93-
});
94-
},
61+
setup: addGlobalEvents,
9562
url: url("data/name.html"),
9663
beforeSend: function() {
9764
ok( true, "beforeSend" );
@@ -109,21 +76,7 @@ module( "ajax", {
10976
});
11077

11178
ajaxTest( "jQuery.ajax() - success callbacks (oncomplete binding)", 8, {
112-
setup: function() {
113-
jQuery("#foo").ajaxStart(function() {
114-
ok( true, "ajaxStart" );
115-
}).ajaxStop(function() {
116-
ok( true, "ajaxStop" );
117-
}).ajaxSend(function() {
118-
ok( true, "ajaxSend" );
119-
}).ajaxComplete(function() {
120-
ok( true, "ajaxComplete" );
121-
}).ajaxError(function() {
122-
ok( false, "ajaxError" );
123-
}).ajaxSuccess(function() {
124-
ok( true, "ajaxSuccess" );
125-
});
126-
},
79+
setup: addGlobalEvents,
12780
url: url("data/name.html"),
12881
beforeSend: function() {
12982
ok( true, "beforeSend" );
@@ -141,21 +94,7 @@ module( "ajax", {
14194
});
14295

14396
ajaxTest( "jQuery.ajax() - error callbacks", 8, {
144-
setup: function() {
145-
jQuery("#foo").ajaxStart(function() {
146-
ok( true, "ajaxStart" );
147-
}).ajaxStop(function() {
148-
ok( true, "ajaxStop" );
149-
}).ajaxSend(function() {
150-
ok( true, "ajaxSend" );
151-
}).ajaxComplete(function() {
152-
ok( true, "ajaxComplete" );
153-
}).ajaxError(function() {
154-
ok( true, "ajaxError" );
155-
}).ajaxSuccess(function() {
156-
ok( false, "ajaxSuccess" );
157-
});
158-
},
97+
setup: addGlobalEvents,
15998
url: url("data/name.php?wait=5"),
16099
beforeSend: function() {
161100
ok( true, "beforeSend" );
@@ -237,7 +176,7 @@ module( "ajax", {
237176

238177
ajaxTest( "jQuery.ajax() - headers", 4, {
239178
setup: function() {
240-
jQuery("#foo").ajaxSend(function( evt, xhr ) {
179+
jQuery( document ).ajaxSend(function( evt, xhr ) {
241180
xhr.setRequestHeader( "ajax-send", "test" );
242181
});
243182
},
@@ -394,18 +333,8 @@ module( "ajax", {
394333
];
395334
});
396335

397-
ajaxTest( "jQuery.ajax() - abort", 8, {
398-
setup: function() {
399-
jQuery("#foo").ajaxStart(function() {
400-
ok( true, "ajaxStart" );
401-
}).ajaxStop(function() {
402-
ok( true, "ajaxStop" );
403-
}).ajaxSend(function() {
404-
ok( true, "ajaxSend" );
405-
}).ajaxComplete(function() {
406-
ok( true, "ajaxComplete" );
407-
});
408-
},
336+
ajaxTest( "jQuery.ajax() - abort", 9, {
337+
setup: addGlobalEvents,
409338
url: url("data/name.php?wait=5"),
410339
beforeSend: function() {
411340
ok( true, "beforeSend" );
@@ -660,14 +589,14 @@ module( "ajax", {
660589
var success = function() {
661590
successCount++;
662591
};
663-
jQuery("#foo").ajaxError(function( e, xml, s, ex ) {
592+
jQuery( document ).on( "ajaxError.passthru", function( e, xml, s, ex ) {
664593
errorCount++;
665594
errorEx += ": " + xml.status;
666595
});
667-
jQuery("#foo").one( "ajaxStop", function() {
596+
jQuery( document ).one( "ajaxStop", function() {
668597
equal( successCount, 5, "Check all ajax calls successful" );
669598
equal( errorCount, 0, "Check no ajax errors (status" + errorEx + ")" );
670-
jQuery("#foo").unbind("ajaxError");
599+
jQuery( document ).off("ajaxError.passthru");
671600
start();
672601
});
673602

@@ -1592,7 +1521,7 @@ module( "ajax", {
15921521
pass = function() {
15931522
ok( passed++ < 2, "Error callback executed" );
15941523
if ( passed == 2 ) {
1595-
jQuery("#qunit-fixture").unbind("ajaxError");
1524+
jQuery( document ).off("ajaxError.setupTest");
15961525
start();
15971526
}
15981527
},
@@ -1601,7 +1530,7 @@ module( "ajax", {
16011530
start();
16021531
};
16031532

1604-
jQuery("#qunit-fixture").ajaxError( pass );
1533+
jQuery( document ).on( "ajaxError.setupTest", pass );
16051534

16061535
jQuery.ajaxSetup({
16071536
timeout: 1000
@@ -1767,20 +1696,8 @@ module( "ajax", {
17671696
});
17681697

17691698
asyncTest( "jQuery.fn.load() - 404 error callbacks", 6, function() {
1770-
jQuery("#foo").ajaxStart(function() {
1771-
ok( true, "ajaxStart" );
1772-
}).ajaxStop(function() {
1773-
ok( true, "ajaxStop" );
1774-
start();
1775-
}).ajaxSend(function() {
1776-
ok( true, "ajaxSend" );
1777-
}).ajaxComplete(function() {
1778-
ok( true, "ajaxComplete" );
1779-
}).ajaxError(function() {
1780-
ok( true, "ajaxError" );
1781-
}).ajaxSuccess(function() {
1782-
ok( false, "ajaxSuccess" );
1783-
});
1699+
addGlobalEvents();
1700+
jQuery( document ).ajaxStop( start );
17841701
jQuery("<div/>").load( "data/404.html", function() {
17851702
ok( true, "complete" );
17861703
});
@@ -1921,9 +1838,9 @@ module( "ajax", {
19211838
jQuery.ajaxSetup({
19221839
dataType: "json"
19231840
});
1924-
jQuery("#first").ajaxComplete(function( e, xml, s ) {
1841+
jQuery( document ).ajaxComplete(function( e, xml, s ) {
19251842
strictEqual( s.dataType, "html", "Verify the load() dataType was html" );
1926-
jQuery("#first").unbind("ajaxComplete");
1843+
jQuery( document ).unbind("ajaxComplete");
19271844
start();
19281845
});
19291846
jQuery("#first").load("data/test3.html");
@@ -1938,12 +1855,11 @@ module( "ajax", {
19381855
"foo": "bar"
19391856
}
19401857
});
1941-
jQuery("#foo")
1942-
.load( "data/echoQuery.php", data )
1943-
.ajaxComplete(function( event, jqXHR, options ) {
1944-
ok( ~options.data.indexOf("foo=bar"), "Data from ajaxSettings was used" );
1945-
start();
1946-
});
1858+
jQuery("#foo").load( "data/echoQuery.php", data );
1859+
jQuery( document ).ajaxComplete(function( event, jqXHR, options ) {
1860+
ok( ~options.data.indexOf("foo=bar"), "Data from ajaxSettings was used" );
1861+
start();
1862+
});
19471863
});
19481864

19491865
//----------- jQuery.post()

0 commit comments

Comments
 (0)