jquery-extend-3.4.0.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * For jQuery versions less than 3.4.0, this replaces the jQuery.extend
  3. * function with the one from jQuery 3.4.0, slightly modified (documented
  4. * below) to be compatible with older jQuery versions and browsers.
  5. *
  6. * This provides the Object.prototype pollution vulnerability fix to Drupal
  7. * installations running older jQuery versions, including the versions shipped
  8. * with Drupal core and https://www.drupal.org/project/jquery_update.
  9. *
  10. * @see https://github.com/jquery/jquery/pull/4333
  11. */
  12. (function (jQuery) {
  13. // Do not override jQuery.extend() if the jQuery version is already >=3.4.0.
  14. var versionParts = jQuery.fn.jquery.split('.');
  15. var majorVersion = parseInt(versionParts[0]);
  16. var minorVersion = parseInt(versionParts[1]);
  17. var patchVersion = parseInt(versionParts[2]);
  18. var isPreReleaseVersion = (patchVersion.toString() !== versionParts[2]);
  19. if (
  20. (majorVersion > 3) ||
  21. (majorVersion === 3 && minorVersion > 4) ||
  22. (majorVersion === 3 && minorVersion === 4 && patchVersion > 0) ||
  23. (majorVersion === 3 && minorVersion === 4 && patchVersion === 0 && !isPreReleaseVersion)
  24. ) {
  25. return;
  26. }
  27. /**
  28. * This is almost verbatim copied from jQuery 3.4.0.
  29. *
  30. * Only two minor changes have been made:
  31. * - The call to isFunction() is changed to jQuery.isFunction().
  32. * - The two calls to Array.isArray() is changed to jQuery.isArray().
  33. *
  34. * The above two changes ensure compatibility with all older jQuery versions
  35. * (1.4.4 - 3.3.1) and older browser versions (e.g., IE8).
  36. */
  37. jQuery.extend = jQuery.fn.extend = function() {
  38. var options, name, src, copy, copyIsArray, clone,
  39. target = arguments[ 0 ] || {},
  40. i = 1,
  41. length = arguments.length,
  42. deep = false;
  43. // Handle a deep copy situation
  44. if ( typeof target === "boolean" ) {
  45. deep = target;
  46. // Skip the boolean and the target
  47. target = arguments[ i ] || {};
  48. i++;
  49. }
  50. // Handle case when target is a string or something (possible in deep copy)
  51. if ( typeof target !== "object" && !jQuery.isFunction( target ) ) {
  52. target = {};
  53. }
  54. // Extend jQuery itself if only one argument is passed
  55. if ( i === length ) {
  56. target = this;
  57. i--;
  58. }
  59. for ( ; i < length; i++ ) {
  60. // Only deal with non-null/undefined values
  61. if ( ( options = arguments[ i ] ) != null ) {
  62. // Extend the base object
  63. for ( name in options ) {
  64. copy = options[ name ];
  65. // Prevent Object.prototype pollution
  66. // Prevent never-ending loop
  67. if ( name === "__proto__" || target === copy ) {
  68. continue;
  69. }
  70. // Recurse if we're merging plain objects or arrays
  71. if ( deep && copy && ( jQuery.isPlainObject( copy ) ||
  72. ( copyIsArray = jQuery.isArray( copy ) ) ) ) {
  73. src = target[ name ];
  74. // Ensure proper type for the source value
  75. if ( copyIsArray && !jQuery.isArray( src ) ) {
  76. clone = [];
  77. } else if ( !copyIsArray && !jQuery.isPlainObject( src ) ) {
  78. clone = {};
  79. } else {
  80. clone = src;
  81. }
  82. copyIsArray = false;
  83. // Never move original objects, clone them
  84. target[ name ] = jQuery.extend( deep, clone, copy );
  85. // Don't bring in undefined values
  86. } else if ( copy !== undefined ) {
  87. target[ name ] = copy;
  88. }
  89. }
  90. }
  91. }
  92. // Return the modified object
  93. return target;
  94. };
  95. })(jQuery);