Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | define( [ |
| 2 | "./core", |
| 3 | "./var/document", |
| 4 | "./var/isFunction", |
| 5 | "./var/rnothtmlwhite", |
| 6 | "./ajax/var/location", |
| 7 | "./ajax/var/nonce", |
| 8 | "./ajax/var/rquery", |
| 9 | |
| 10 | "./core/init", |
Renovate bot | f591dcf | 2023-12-30 14:13:54 +0000 | [diff] [blame] | 11 | "./core/parseXML", |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 12 | "./event/trigger", |
| 13 | "./deferred", |
| 14 | "./serialize" // jQuery.param |
| 15 | ], function( jQuery, document, isFunction, rnothtmlwhite, location, nonce, rquery ) { |
| 16 | |
| 17 | "use strict"; |
| 18 | |
| 19 | var |
| 20 | r20 = /%20/g, |
| 21 | rhash = /#.*$/, |
| 22 | rantiCache = /([?&])_=[^&]*/, |
| 23 | rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg, |
| 24 | |
| 25 | // #7653, #8125, #8152: local protocol detection |
| 26 | rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/, |
| 27 | rnoContent = /^(?:GET|HEAD)$/, |
| 28 | rprotocol = /^\/\//, |
| 29 | |
| 30 | /* Prefilters |
| 31 | * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example) |
| 32 | * 2) These are called: |
| 33 | * - BEFORE asking for a transport |
| 34 | * - AFTER param serialization (s.data is a string if s.processData is true) |
| 35 | * 3) key is the dataType |
| 36 | * 4) the catchall symbol "*" can be used |
| 37 | * 5) execution will start with transport dataType and THEN continue down to "*" if needed |
| 38 | */ |
| 39 | prefilters = {}, |
| 40 | |
| 41 | /* Transports bindings |
| 42 | * 1) key is the dataType |
| 43 | * 2) the catchall symbol "*" can be used |
| 44 | * 3) selection will start with transport dataType and THEN go to "*" if needed |
| 45 | */ |
| 46 | transports = {}, |
| 47 | |
| 48 | // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression |
| 49 | allTypes = "*/".concat( "*" ), |
| 50 | |
| 51 | // Anchor tag for parsing the document origin |
| 52 | originAnchor = document.createElement( "a" ); |
| 53 | originAnchor.href = location.href; |
| 54 | |
| 55 | // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport |
| 56 | function addToPrefiltersOrTransports( structure ) { |
| 57 | |
| 58 | // dataTypeExpression is optional and defaults to "*" |
| 59 | return function( dataTypeExpression, func ) { |
| 60 | |
| 61 | if ( typeof dataTypeExpression !== "string" ) { |
| 62 | func = dataTypeExpression; |
| 63 | dataTypeExpression = "*"; |
| 64 | } |
| 65 | |
| 66 | var dataType, |
| 67 | i = 0, |
| 68 | dataTypes = dataTypeExpression.toLowerCase().match( rnothtmlwhite ) || []; |
| 69 | |
| 70 | if ( isFunction( func ) ) { |
| 71 | |
| 72 | // For each dataType in the dataTypeExpression |
| 73 | while ( ( dataType = dataTypes[ i++ ] ) ) { |
| 74 | |
| 75 | // Prepend if requested |
| 76 | if ( dataType[ 0 ] === "+" ) { |
| 77 | dataType = dataType.slice( 1 ) || "*"; |
| 78 | ( structure[ dataType ] = structure[ dataType ] || [] ).unshift( func ); |
| 79 | |
| 80 | // Otherwise append |
| 81 | } else { |
| 82 | ( structure[ dataType ] = structure[ dataType ] || [] ).push( func ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | // Base inspection function for prefilters and transports |
| 90 | function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) { |
| 91 | |
| 92 | var inspected = {}, |
| 93 | seekingTransport = ( structure === transports ); |
| 94 | |
| 95 | function inspect( dataType ) { |
| 96 | var selected; |
| 97 | inspected[ dataType ] = true; |
| 98 | jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) { |
| 99 | var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR ); |
| 100 | if ( typeof dataTypeOrTransport === "string" && |
| 101 | !seekingTransport && !inspected[ dataTypeOrTransport ] ) { |
| 102 | |
| 103 | options.dataTypes.unshift( dataTypeOrTransport ); |
| 104 | inspect( dataTypeOrTransport ); |
| 105 | return false; |
| 106 | } else if ( seekingTransport ) { |
| 107 | return !( selected = dataTypeOrTransport ); |
| 108 | } |
| 109 | } ); |
| 110 | return selected; |
| 111 | } |
| 112 | |
| 113 | return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" ); |
| 114 | } |
| 115 | |
| 116 | // A special extend for ajax options |
| 117 | // that takes "flat" options (not to be deep extended) |
| 118 | // Fixes #9887 |
| 119 | function ajaxExtend( target, src ) { |
| 120 | var key, deep, |
| 121 | flatOptions = jQuery.ajaxSettings.flatOptions || {}; |
| 122 | |
| 123 | for ( key in src ) { |
| 124 | if ( src[ key ] !== undefined ) { |
| 125 | ( flatOptions[ key ] ? target : ( deep || ( deep = {} ) ) )[ key ] = src[ key ]; |
| 126 | } |
| 127 | } |
| 128 | if ( deep ) { |
| 129 | jQuery.extend( true, target, deep ); |
| 130 | } |
| 131 | |
| 132 | return target; |
| 133 | } |
| 134 | |
| 135 | /* Handles responses to an ajax request: |
| 136 | * - finds the right dataType (mediates between content-type and expected dataType) |
| 137 | * - returns the corresponding response |
| 138 | */ |
| 139 | function ajaxHandleResponses( s, jqXHR, responses ) { |
| 140 | |
| 141 | var ct, type, finalDataType, firstDataType, |
| 142 | contents = s.contents, |
| 143 | dataTypes = s.dataTypes; |
| 144 | |
| 145 | // Remove auto dataType and get content-type in the process |
| 146 | while ( dataTypes[ 0 ] === "*" ) { |
| 147 | dataTypes.shift(); |
| 148 | if ( ct === undefined ) { |
| 149 | ct = s.mimeType || jqXHR.getResponseHeader( "Content-Type" ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Check if we're dealing with a known content-type |
| 154 | if ( ct ) { |
| 155 | for ( type in contents ) { |
| 156 | if ( contents[ type ] && contents[ type ].test( ct ) ) { |
| 157 | dataTypes.unshift( type ); |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Check to see if we have a response for the expected dataType |
| 164 | if ( dataTypes[ 0 ] in responses ) { |
| 165 | finalDataType = dataTypes[ 0 ]; |
| 166 | } else { |
| 167 | |
| 168 | // Try convertible dataTypes |
| 169 | for ( type in responses ) { |
| 170 | if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[ 0 ] ] ) { |
| 171 | finalDataType = type; |
| 172 | break; |
| 173 | } |
| 174 | if ( !firstDataType ) { |
| 175 | firstDataType = type; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Or just use first one |
| 180 | finalDataType = finalDataType || firstDataType; |
| 181 | } |
| 182 | |
| 183 | // If we found a dataType |
| 184 | // We add the dataType to the list if needed |
| 185 | // and return the corresponding response |
| 186 | if ( finalDataType ) { |
| 187 | if ( finalDataType !== dataTypes[ 0 ] ) { |
| 188 | dataTypes.unshift( finalDataType ); |
| 189 | } |
| 190 | return responses[ finalDataType ]; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /* Chain conversions given the request and the original response |
| 195 | * Also sets the responseXXX fields on the jqXHR instance |
| 196 | */ |
| 197 | function ajaxConvert( s, response, jqXHR, isSuccess ) { |
| 198 | var conv2, current, conv, tmp, prev, |
| 199 | converters = {}, |
| 200 | |
| 201 | // Work with a copy of dataTypes in case we need to modify it for conversion |
| 202 | dataTypes = s.dataTypes.slice(); |
| 203 | |
| 204 | // Create converters map with lowercased keys |
| 205 | if ( dataTypes[ 1 ] ) { |
| 206 | for ( conv in s.converters ) { |
| 207 | converters[ conv.toLowerCase() ] = s.converters[ conv ]; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | current = dataTypes.shift(); |
| 212 | |
| 213 | // Convert to each sequential dataType |
| 214 | while ( current ) { |
| 215 | |
| 216 | if ( s.responseFields[ current ] ) { |
| 217 | jqXHR[ s.responseFields[ current ] ] = response; |
| 218 | } |
| 219 | |
| 220 | // Apply the dataFilter if provided |
| 221 | if ( !prev && isSuccess && s.dataFilter ) { |
| 222 | response = s.dataFilter( response, s.dataType ); |
| 223 | } |
| 224 | |
| 225 | prev = current; |
| 226 | current = dataTypes.shift(); |
| 227 | |
| 228 | if ( current ) { |
| 229 | |
| 230 | // There's only work to do if current dataType is non-auto |
| 231 | if ( current === "*" ) { |
| 232 | |
| 233 | current = prev; |
| 234 | |
| 235 | // Convert response if prev dataType is non-auto and differs from current |
| 236 | } else if ( prev !== "*" && prev !== current ) { |
| 237 | |
| 238 | // Seek a direct converter |
| 239 | conv = converters[ prev + " " + current ] || converters[ "* " + current ]; |
| 240 | |
| 241 | // If none found, seek a pair |
| 242 | if ( !conv ) { |
| 243 | for ( conv2 in converters ) { |
| 244 | |
| 245 | // If conv2 outputs current |
| 246 | tmp = conv2.split( " " ); |
| 247 | if ( tmp[ 1 ] === current ) { |
| 248 | |
| 249 | // If prev can be converted to accepted input |
| 250 | conv = converters[ prev + " " + tmp[ 0 ] ] || |
| 251 | converters[ "* " + tmp[ 0 ] ]; |
| 252 | if ( conv ) { |
| 253 | |
| 254 | // Condense equivalence converters |
| 255 | if ( conv === true ) { |
| 256 | conv = converters[ conv2 ]; |
| 257 | |
| 258 | // Otherwise, insert the intermediate dataType |
| 259 | } else if ( converters[ conv2 ] !== true ) { |
| 260 | current = tmp[ 0 ]; |
| 261 | dataTypes.unshift( tmp[ 1 ] ); |
| 262 | } |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // Apply converter (if not an equivalence) |
| 270 | if ( conv !== true ) { |
| 271 | |
| 272 | // Unless errors are allowed to bubble, catch and return them |
| 273 | if ( conv && s.throws ) { |
| 274 | response = conv( response ); |
| 275 | } else { |
| 276 | try { |
| 277 | response = conv( response ); |
| 278 | } catch ( e ) { |
| 279 | return { |
| 280 | state: "parsererror", |
| 281 | error: conv ? e : "No conversion from " + prev + " to " + current |
| 282 | }; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return { state: "success", data: response }; |
| 291 | } |
| 292 | |
| 293 | jQuery.extend( { |
| 294 | |
| 295 | // Counter for holding the number of active queries |
| 296 | active: 0, |
| 297 | |
| 298 | // Last-Modified header cache for next request |
| 299 | lastModified: {}, |
| 300 | etag: {}, |
| 301 | |
| 302 | ajaxSettings: { |
| 303 | url: location.href, |
| 304 | type: "GET", |
| 305 | isLocal: rlocalProtocol.test( location.protocol ), |
| 306 | global: true, |
| 307 | processData: true, |
| 308 | async: true, |
| 309 | contentType: "application/x-www-form-urlencoded; charset=UTF-8", |
| 310 | |
| 311 | /* |
| 312 | timeout: 0, |
| 313 | data: null, |
| 314 | dataType: null, |
| 315 | username: null, |
| 316 | password: null, |
| 317 | cache: null, |
| 318 | throws: false, |
| 319 | traditional: false, |
| 320 | headers: {}, |
| 321 | */ |
| 322 | |
| 323 | accepts: { |
| 324 | "*": allTypes, |
| 325 | text: "text/plain", |
| 326 | html: "text/html", |
| 327 | xml: "application/xml, text/xml", |
| 328 | json: "application/json, text/javascript" |
| 329 | }, |
| 330 | |
| 331 | contents: { |
| 332 | xml: /\bxml\b/, |
| 333 | html: /\bhtml/, |
| 334 | json: /\bjson\b/ |
| 335 | }, |
| 336 | |
| 337 | responseFields: { |
| 338 | xml: "responseXML", |
| 339 | text: "responseText", |
| 340 | json: "responseJSON" |
| 341 | }, |
| 342 | |
| 343 | // Data converters |
| 344 | // Keys separate source (or catchall "*") and destination types with a single space |
| 345 | converters: { |
| 346 | |
| 347 | // Convert anything to text |
| 348 | "* text": String, |
| 349 | |
| 350 | // Text to html (true = no transformation) |
| 351 | "text html": true, |
| 352 | |
| 353 | // Evaluate text as a json expression |
| 354 | "text json": JSON.parse, |
| 355 | |
| 356 | // Parse text as xml |
| 357 | "text xml": jQuery.parseXML |
| 358 | }, |
| 359 | |
| 360 | // For options that shouldn't be deep extended: |
| 361 | // you can add your own custom options here if |
| 362 | // and when you create one that shouldn't be |
| 363 | // deep extended (see ajaxExtend) |
| 364 | flatOptions: { |
| 365 | url: true, |
| 366 | context: true |
| 367 | } |
| 368 | }, |
| 369 | |
| 370 | // Creates a full fledged settings object into target |
| 371 | // with both ajaxSettings and settings fields. |
| 372 | // If target is omitted, writes into ajaxSettings. |
| 373 | ajaxSetup: function( target, settings ) { |
| 374 | return settings ? |
| 375 | |
| 376 | // Building a settings object |
| 377 | ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) : |
| 378 | |
| 379 | // Extending ajaxSettings |
| 380 | ajaxExtend( jQuery.ajaxSettings, target ); |
| 381 | }, |
| 382 | |
| 383 | ajaxPrefilter: addToPrefiltersOrTransports( prefilters ), |
| 384 | ajaxTransport: addToPrefiltersOrTransports( transports ), |
| 385 | |
| 386 | // Main method |
| 387 | ajax: function( url, options ) { |
| 388 | |
| 389 | // If url is an object, simulate pre-1.5 signature |
| 390 | if ( typeof url === "object" ) { |
| 391 | options = url; |
| 392 | url = undefined; |
| 393 | } |
| 394 | |
| 395 | // Force options to be an object |
| 396 | options = options || {}; |
| 397 | |
| 398 | var transport, |
| 399 | |
| 400 | // URL without anti-cache param |
| 401 | cacheURL, |
| 402 | |
| 403 | // Response headers |
| 404 | responseHeadersString, |
| 405 | responseHeaders, |
| 406 | |
| 407 | // timeout handle |
| 408 | timeoutTimer, |
| 409 | |
| 410 | // Url cleanup var |
| 411 | urlAnchor, |
| 412 | |
| 413 | // Request state (becomes false upon send and true upon completion) |
| 414 | completed, |
| 415 | |
| 416 | // To know if global events are to be dispatched |
| 417 | fireGlobals, |
| 418 | |
| 419 | // Loop variable |
| 420 | i, |
| 421 | |
| 422 | // uncached part of the url |
| 423 | uncached, |
| 424 | |
| 425 | // Create the final options object |
| 426 | s = jQuery.ajaxSetup( {}, options ), |
| 427 | |
| 428 | // Callbacks context |
| 429 | callbackContext = s.context || s, |
| 430 | |
| 431 | // Context for global events is callbackContext if it is a DOM node or jQuery collection |
| 432 | globalEventContext = s.context && |
| 433 | ( callbackContext.nodeType || callbackContext.jquery ) ? |
| 434 | jQuery( callbackContext ) : |
| 435 | jQuery.event, |
| 436 | |
| 437 | // Deferreds |
| 438 | deferred = jQuery.Deferred(), |
| 439 | completeDeferred = jQuery.Callbacks( "once memory" ), |
| 440 | |
| 441 | // Status-dependent callbacks |
| 442 | statusCode = s.statusCode || {}, |
| 443 | |
| 444 | // Headers (they are sent all at once) |
| 445 | requestHeaders = {}, |
| 446 | requestHeadersNames = {}, |
| 447 | |
| 448 | // Default abort message |
| 449 | strAbort = "canceled", |
| 450 | |
| 451 | // Fake xhr |
| 452 | jqXHR = { |
| 453 | readyState: 0, |
| 454 | |
| 455 | // Builds headers hashtable if needed |
| 456 | getResponseHeader: function( key ) { |
| 457 | var match; |
| 458 | if ( completed ) { |
| 459 | if ( !responseHeaders ) { |
| 460 | responseHeaders = {}; |
| 461 | while ( ( match = rheaders.exec( responseHeadersString ) ) ) { |
| 462 | responseHeaders[ match[ 1 ].toLowerCase() + " " ] = |
| 463 | ( responseHeaders[ match[ 1 ].toLowerCase() + " " ] || [] ) |
| 464 | .concat( match[ 2 ] ); |
| 465 | } |
| 466 | } |
| 467 | match = responseHeaders[ key.toLowerCase() + " " ]; |
| 468 | } |
| 469 | return match == null ? null : match.join( ", " ); |
| 470 | }, |
| 471 | |
| 472 | // Raw string |
| 473 | getAllResponseHeaders: function() { |
| 474 | return completed ? responseHeadersString : null; |
| 475 | }, |
| 476 | |
| 477 | // Caches the header |
| 478 | setRequestHeader: function( name, value ) { |
| 479 | if ( completed == null ) { |
| 480 | name = requestHeadersNames[ name.toLowerCase() ] = |
| 481 | requestHeadersNames[ name.toLowerCase() ] || name; |
| 482 | requestHeaders[ name ] = value; |
| 483 | } |
| 484 | return this; |
| 485 | }, |
| 486 | |
| 487 | // Overrides response content-type header |
| 488 | overrideMimeType: function( type ) { |
| 489 | if ( completed == null ) { |
| 490 | s.mimeType = type; |
| 491 | } |
| 492 | return this; |
| 493 | }, |
| 494 | |
| 495 | // Status-dependent callbacks |
| 496 | statusCode: function( map ) { |
| 497 | var code; |
| 498 | if ( map ) { |
| 499 | if ( completed ) { |
| 500 | |
| 501 | // Execute the appropriate callbacks |
| 502 | jqXHR.always( map[ jqXHR.status ] ); |
| 503 | } else { |
| 504 | |
| 505 | // Lazy-add the new callbacks in a way that preserves old ones |
| 506 | for ( code in map ) { |
| 507 | statusCode[ code ] = [ statusCode[ code ], map[ code ] ]; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | return this; |
| 512 | }, |
| 513 | |
| 514 | // Cancel the request |
| 515 | abort: function( statusText ) { |
| 516 | var finalText = statusText || strAbort; |
| 517 | if ( transport ) { |
| 518 | transport.abort( finalText ); |
| 519 | } |
| 520 | done( 0, finalText ); |
| 521 | return this; |
| 522 | } |
| 523 | }; |
| 524 | |
| 525 | // Attach deferreds |
| 526 | deferred.promise( jqXHR ); |
| 527 | |
| 528 | // Add protocol if not provided (prefilters might expect it) |
| 529 | // Handle falsy url in the settings object (#10093: consistency with old signature) |
| 530 | // We also use the url parameter if available |
| 531 | s.url = ( ( url || s.url || location.href ) + "" ) |
| 532 | .replace( rprotocol, location.protocol + "//" ); |
| 533 | |
| 534 | // Alias method option to type as per ticket #12004 |
| 535 | s.type = options.method || options.type || s.method || s.type; |
| 536 | |
| 537 | // Extract dataTypes list |
| 538 | s.dataTypes = ( s.dataType || "*" ).toLowerCase().match( rnothtmlwhite ) || [ "" ]; |
| 539 | |
| 540 | // A cross-domain request is in order when the origin doesn't match the current origin. |
| 541 | if ( s.crossDomain == null ) { |
| 542 | urlAnchor = document.createElement( "a" ); |
| 543 | |
| 544 | // Support: IE <=8 - 11, Edge 12 - 15 |
| 545 | // IE throws exception on accessing the href property if url is malformed, |
| 546 | // e.g. http://example.com:80x/ |
| 547 | try { |
| 548 | urlAnchor.href = s.url; |
| 549 | |
| 550 | // Support: IE <=8 - 11 only |
| 551 | // Anchor's host property isn't correctly set when s.url is relative |
| 552 | urlAnchor.href = urlAnchor.href; |
| 553 | s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !== |
| 554 | urlAnchor.protocol + "//" + urlAnchor.host; |
| 555 | } catch ( e ) { |
| 556 | |
| 557 | // If there is an error parsing the URL, assume it is crossDomain, |
| 558 | // it can be rejected by the transport if it is invalid |
| 559 | s.crossDomain = true; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | // Convert data if not already a string |
| 564 | if ( s.data && s.processData && typeof s.data !== "string" ) { |
| 565 | s.data = jQuery.param( s.data, s.traditional ); |
| 566 | } |
| 567 | |
| 568 | // Apply prefilters |
| 569 | inspectPrefiltersOrTransports( prefilters, s, options, jqXHR ); |
| 570 | |
| 571 | // If request was aborted inside a prefilter, stop there |
| 572 | if ( completed ) { |
| 573 | return jqXHR; |
| 574 | } |
| 575 | |
| 576 | // We can fire global events as of now if asked to |
| 577 | // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118) |
| 578 | fireGlobals = jQuery.event && s.global; |
| 579 | |
| 580 | // Watch for a new set of requests |
| 581 | if ( fireGlobals && jQuery.active++ === 0 ) { |
| 582 | jQuery.event.trigger( "ajaxStart" ); |
| 583 | } |
| 584 | |
| 585 | // Uppercase the type |
| 586 | s.type = s.type.toUpperCase(); |
| 587 | |
| 588 | // Determine if request has content |
| 589 | s.hasContent = !rnoContent.test( s.type ); |
| 590 | |
| 591 | // Save the URL in case we're toying with the If-Modified-Since |
| 592 | // and/or If-None-Match header later on |
| 593 | // Remove hash to simplify url manipulation |
| 594 | cacheURL = s.url.replace( rhash, "" ); |
| 595 | |
| 596 | // More options handling for requests with no content |
| 597 | if ( !s.hasContent ) { |
| 598 | |
| 599 | // Remember the hash so we can put it back |
| 600 | uncached = s.url.slice( cacheURL.length ); |
| 601 | |
| 602 | // If data is available and should be processed, append data to url |
| 603 | if ( s.data && ( s.processData || typeof s.data === "string" ) ) { |
| 604 | cacheURL += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data; |
| 605 | |
| 606 | // #9682: remove data so that it's not used in an eventual retry |
| 607 | delete s.data; |
| 608 | } |
| 609 | |
| 610 | // Add or update anti-cache param if needed |
| 611 | if ( s.cache === false ) { |
| 612 | cacheURL = cacheURL.replace( rantiCache, "$1" ); |
Renovate bot | f591dcf | 2023-12-30 14:13:54 +0000 | [diff] [blame] | 613 | uncached = ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + ( nonce.guid++ ) + |
| 614 | uncached; |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // Put hash and anti-cache on the URL that will be requested (gh-1732) |
| 618 | s.url = cacheURL + uncached; |
| 619 | |
| 620 | // Change '%20' to '+' if this is encoded form body content (gh-2658) |
| 621 | } else if ( s.data && s.processData && |
| 622 | ( s.contentType || "" ).indexOf( "application/x-www-form-urlencoded" ) === 0 ) { |
| 623 | s.data = s.data.replace( r20, "+" ); |
| 624 | } |
| 625 | |
| 626 | // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. |
| 627 | if ( s.ifModified ) { |
| 628 | if ( jQuery.lastModified[ cacheURL ] ) { |
| 629 | jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] ); |
| 630 | } |
| 631 | if ( jQuery.etag[ cacheURL ] ) { |
| 632 | jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] ); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | // Set the correct header, if data is being sent |
| 637 | if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) { |
| 638 | jqXHR.setRequestHeader( "Content-Type", s.contentType ); |
| 639 | } |
| 640 | |
| 641 | // Set the Accepts header for the server, depending on the dataType |
| 642 | jqXHR.setRequestHeader( |
| 643 | "Accept", |
| 644 | s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[ 0 ] ] ? |
| 645 | s.accepts[ s.dataTypes[ 0 ] ] + |
| 646 | ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) : |
| 647 | s.accepts[ "*" ] |
| 648 | ); |
| 649 | |
| 650 | // Check for headers option |
| 651 | for ( i in s.headers ) { |
| 652 | jqXHR.setRequestHeader( i, s.headers[ i ] ); |
| 653 | } |
| 654 | |
| 655 | // Allow custom headers/mimetypes and early abort |
| 656 | if ( s.beforeSend && |
| 657 | ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || completed ) ) { |
| 658 | |
| 659 | // Abort if not done already and return |
| 660 | return jqXHR.abort(); |
| 661 | } |
| 662 | |
| 663 | // Aborting is no longer a cancellation |
| 664 | strAbort = "abort"; |
| 665 | |
| 666 | // Install callbacks on deferreds |
| 667 | completeDeferred.add( s.complete ); |
| 668 | jqXHR.done( s.success ); |
| 669 | jqXHR.fail( s.error ); |
| 670 | |
| 671 | // Get transport |
| 672 | transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR ); |
| 673 | |
| 674 | // If no transport, we auto-abort |
| 675 | if ( !transport ) { |
| 676 | done( -1, "No Transport" ); |
| 677 | } else { |
| 678 | jqXHR.readyState = 1; |
| 679 | |
| 680 | // Send global event |
| 681 | if ( fireGlobals ) { |
| 682 | globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] ); |
| 683 | } |
| 684 | |
| 685 | // If request was aborted inside ajaxSend, stop there |
| 686 | if ( completed ) { |
| 687 | return jqXHR; |
| 688 | } |
| 689 | |
| 690 | // Timeout |
| 691 | if ( s.async && s.timeout > 0 ) { |
| 692 | timeoutTimer = window.setTimeout( function() { |
| 693 | jqXHR.abort( "timeout" ); |
| 694 | }, s.timeout ); |
| 695 | } |
| 696 | |
| 697 | try { |
| 698 | completed = false; |
| 699 | transport.send( requestHeaders, done ); |
| 700 | } catch ( e ) { |
| 701 | |
| 702 | // Rethrow post-completion exceptions |
| 703 | if ( completed ) { |
| 704 | throw e; |
| 705 | } |
| 706 | |
| 707 | // Propagate others as results |
| 708 | done( -1, e ); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // Callback for when everything is done |
| 713 | function done( status, nativeStatusText, responses, headers ) { |
| 714 | var isSuccess, success, error, response, modified, |
| 715 | statusText = nativeStatusText; |
| 716 | |
| 717 | // Ignore repeat invocations |
| 718 | if ( completed ) { |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | completed = true; |
| 723 | |
| 724 | // Clear timeout if it exists |
| 725 | if ( timeoutTimer ) { |
| 726 | window.clearTimeout( timeoutTimer ); |
| 727 | } |
| 728 | |
| 729 | // Dereference transport for early garbage collection |
| 730 | // (no matter how long the jqXHR object will be used) |
| 731 | transport = undefined; |
| 732 | |
| 733 | // Cache response headers |
| 734 | responseHeadersString = headers || ""; |
| 735 | |
| 736 | // Set readyState |
| 737 | jqXHR.readyState = status > 0 ? 4 : 0; |
| 738 | |
| 739 | // Determine if successful |
| 740 | isSuccess = status >= 200 && status < 300 || status === 304; |
| 741 | |
| 742 | // Get response data |
| 743 | if ( responses ) { |
| 744 | response = ajaxHandleResponses( s, jqXHR, responses ); |
| 745 | } |
| 746 | |
Renovate bot | f591dcf | 2023-12-30 14:13:54 +0000 | [diff] [blame] | 747 | // Use a noop converter for missing script |
| 748 | if ( !isSuccess && jQuery.inArray( "script", s.dataTypes ) > -1 ) { |
| 749 | s.converters[ "text script" ] = function() {}; |
| 750 | } |
| 751 | |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 752 | // Convert no matter what (that way responseXXX fields are always set) |
| 753 | response = ajaxConvert( s, response, jqXHR, isSuccess ); |
| 754 | |
| 755 | // If successful, handle type chaining |
| 756 | if ( isSuccess ) { |
| 757 | |
| 758 | // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. |
| 759 | if ( s.ifModified ) { |
| 760 | modified = jqXHR.getResponseHeader( "Last-Modified" ); |
| 761 | if ( modified ) { |
| 762 | jQuery.lastModified[ cacheURL ] = modified; |
| 763 | } |
| 764 | modified = jqXHR.getResponseHeader( "etag" ); |
| 765 | if ( modified ) { |
| 766 | jQuery.etag[ cacheURL ] = modified; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // if no content |
| 771 | if ( status === 204 || s.type === "HEAD" ) { |
| 772 | statusText = "nocontent"; |
| 773 | |
| 774 | // if not modified |
| 775 | } else if ( status === 304 ) { |
| 776 | statusText = "notmodified"; |
| 777 | |
| 778 | // If we have data, let's convert it |
| 779 | } else { |
| 780 | statusText = response.state; |
| 781 | success = response.data; |
| 782 | error = response.error; |
| 783 | isSuccess = !error; |
| 784 | } |
| 785 | } else { |
| 786 | |
| 787 | // Extract error from statusText and normalize for non-aborts |
| 788 | error = statusText; |
| 789 | if ( status || !statusText ) { |
| 790 | statusText = "error"; |
| 791 | if ( status < 0 ) { |
| 792 | status = 0; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | // Set data for the fake xhr object |
| 798 | jqXHR.status = status; |
| 799 | jqXHR.statusText = ( nativeStatusText || statusText ) + ""; |
| 800 | |
| 801 | // Success/Error |
| 802 | if ( isSuccess ) { |
| 803 | deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] ); |
| 804 | } else { |
| 805 | deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] ); |
| 806 | } |
| 807 | |
| 808 | // Status-dependent callbacks |
| 809 | jqXHR.statusCode( statusCode ); |
| 810 | statusCode = undefined; |
| 811 | |
| 812 | if ( fireGlobals ) { |
| 813 | globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError", |
| 814 | [ jqXHR, s, isSuccess ? success : error ] ); |
| 815 | } |
| 816 | |
| 817 | // Complete |
| 818 | completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] ); |
| 819 | |
| 820 | if ( fireGlobals ) { |
| 821 | globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] ); |
| 822 | |
| 823 | // Handle the global AJAX counter |
| 824 | if ( !( --jQuery.active ) ) { |
| 825 | jQuery.event.trigger( "ajaxStop" ); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return jqXHR; |
| 831 | }, |
| 832 | |
| 833 | getJSON: function( url, data, callback ) { |
| 834 | return jQuery.get( url, data, callback, "json" ); |
| 835 | }, |
| 836 | |
| 837 | getScript: function( url, callback ) { |
| 838 | return jQuery.get( url, undefined, callback, "script" ); |
| 839 | } |
| 840 | } ); |
| 841 | |
Renovate bot | f591dcf | 2023-12-30 14:13:54 +0000 | [diff] [blame] | 842 | jQuery.each( [ "get", "post" ], function( _i, method ) { |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 843 | jQuery[ method ] = function( url, data, callback, type ) { |
| 844 | |
| 845 | // Shift arguments if data argument was omitted |
| 846 | if ( isFunction( data ) ) { |
| 847 | type = type || callback; |
| 848 | callback = data; |
| 849 | data = undefined; |
| 850 | } |
| 851 | |
| 852 | // The url can be an options object (which then must have .url) |
| 853 | return jQuery.ajax( jQuery.extend( { |
| 854 | url: url, |
| 855 | type: method, |
| 856 | dataType: type, |
| 857 | data: data, |
| 858 | success: callback |
| 859 | }, jQuery.isPlainObject( url ) && url ) ); |
| 860 | }; |
| 861 | } ); |
| 862 | |
Renovate bot | f591dcf | 2023-12-30 14:13:54 +0000 | [diff] [blame] | 863 | jQuery.ajaxPrefilter( function( s ) { |
| 864 | var i; |
| 865 | for ( i in s.headers ) { |
| 866 | if ( i.toLowerCase() === "content-type" ) { |
| 867 | s.contentType = s.headers[ i ] || ""; |
| 868 | } |
| 869 | } |
| 870 | } ); |
| 871 | |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 872 | return jQuery; |
| 873 | } ); |