Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../includes/jquery.s...
File: jquery.serializejson.js
/*!
[0] Fix | Delete
SerializeJSON jQuery plugin.
[1] Fix | Delete
https://github.com/marioizquierdo/jquery.serializeJSON
[2] Fix | Delete
version 3.2.0 (Dec, 2020)
[3] Fix | Delete
[4] Fix | Delete
Copyright (c) 2012-2021 Mario Izquierdo
[5] Fix | Delete
Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
[6] Fix | Delete
and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
[7] Fix | Delete
*/
[8] Fix | Delete
(function (factory) {
[9] Fix | Delete
/* global define, require, module */
[10] Fix | Delete
if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module.
[11] Fix | Delete
define(["jquery"], factory);
[12] Fix | Delete
} else if (typeof exports === "object") { // Node/CommonJS
[13] Fix | Delete
var jQuery = require("jquery");
[14] Fix | Delete
module.exports = factory(jQuery);
[15] Fix | Delete
} else { // Browser globals (zepto supported)
[16] Fix | Delete
factory(window.jQuery || window.Zepto || window.$); // Zepto supported on browsers as well
[17] Fix | Delete
}
[18] Fix | Delete
[19] Fix | Delete
}(function ($) {
[20] Fix | Delete
"use strict";
[21] Fix | Delete
[22] Fix | Delete
var rCRLF = /\r?\n/g;
[23] Fix | Delete
var rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i;
[24] Fix | Delete
var rsubmittable = /^(?:input|select|textarea|keygen)/i;
[25] Fix | Delete
var rcheckableType = /^(?:checkbox|radio)$/i;
[26] Fix | Delete
[27] Fix | Delete
$.fn.serializeJSON = function (options) {
[28] Fix | Delete
var f = $.serializeJSON;
[29] Fix | Delete
var $form = this; // NOTE: the set of matched elements is most likely a form, but it could also be a group of inputs
[30] Fix | Delete
var opts = f.setupOpts(options); // validate options and apply defaults
[31] Fix | Delete
var typeFunctions = $.extend({}, opts.defaultTypes, opts.customTypes);
[32] Fix | Delete
[33] Fix | Delete
// Make a list with {name, value, el} for each input element
[34] Fix | Delete
var serializedArray = f.serializeArray($form, opts);
[35] Fix | Delete
[36] Fix | Delete
// Convert the serializedArray into a serializedObject with nested keys
[37] Fix | Delete
var serializedObject = {};
[38] Fix | Delete
$.each(serializedArray, function (_i, obj) {
[39] Fix | Delete
[40] Fix | Delete
var nameSansType = obj.name;
[41] Fix | Delete
var type = $(obj.el).attr("data-value-type");
[42] Fix | Delete
[43] Fix | Delete
if (!type && !opts.disableColonTypes) { // try getting the type from the input name
[44] Fix | Delete
var p = f.splitType(obj.name); // "foo:string" => ["foo", "string"]
[45] Fix | Delete
nameSansType = p[0];
[46] Fix | Delete
type = p[1];
[47] Fix | Delete
}
[48] Fix | Delete
if (type === "skip") {
[49] Fix | Delete
return; // ignore fields with type skip
[50] Fix | Delete
}
[51] Fix | Delete
if (!type) {
[52] Fix | Delete
type = opts.defaultType; // "string" by default
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
var typedValue = f.applyTypeFunc(obj.name, obj.value, type, obj.el, typeFunctions); // Parse type as string, number, etc.
[56] Fix | Delete
[57] Fix | Delete
if (!typedValue && f.shouldSkipFalsy(obj.name, nameSansType, type, obj.el, opts)) {
[58] Fix | Delete
return; // ignore falsy inputs if specified in the options
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
var keys = f.splitInputNameIntoKeysArray(nameSansType);
[62] Fix | Delete
f.deepSet(serializedObject, keys, typedValue, opts);
[63] Fix | Delete
});
[64] Fix | Delete
return serializedObject;
[65] Fix | Delete
};
[66] Fix | Delete
[67] Fix | Delete
// Use $.serializeJSON as namespace for the auxiliar functions
[68] Fix | Delete
// and to define defaults
[69] Fix | Delete
$.serializeJSON = {
[70] Fix | Delete
defaultOptions: {}, // reassign to override option defaults for all serializeJSON calls
[71] Fix | Delete
[72] Fix | Delete
defaultBaseOptions: { // do not modify, use defaultOptions instead
[73] Fix | Delete
checkboxUncheckedValue: undefined, // to include that value for unchecked checkboxes (instead of ignoring them)
[74] Fix | Delete
useIntKeysAsArrayIndex: false, // name="foo[2]" value="v" => {foo: [null, null, "v"]}, instead of {foo: ["2": "v"]}
[75] Fix | Delete
[76] Fix | Delete
skipFalsyValuesForTypes: [], // skip serialization of falsy values for listed value types
[77] Fix | Delete
skipFalsyValuesForFields: [], // skip serialization of falsy values for listed field names
[78] Fix | Delete
[79] Fix | Delete
disableColonTypes: false, // do not interpret ":type" suffix as a type
[80] Fix | Delete
customTypes: {}, // extends defaultTypes
[81] Fix | Delete
defaultTypes: {
[82] Fix | Delete
"string": function(str) { return String(str); },
[83] Fix | Delete
"number": function(str) { return Number(str); },
[84] Fix | Delete
"boolean": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1; },
[85] Fix | Delete
"null": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1 ? str : null; },
[86] Fix | Delete
"array": function(str) { return JSON.parse(str); },
[87] Fix | Delete
"object": function(str) { return JSON.parse(str); },
[88] Fix | Delete
"skip": null // skip is a special type used to ignore fields
[89] Fix | Delete
},
[90] Fix | Delete
defaultType: "string",
[91] Fix | Delete
},
[92] Fix | Delete
[93] Fix | Delete
// Validate and set defaults
[94] Fix | Delete
setupOpts: function(options) {
[95] Fix | Delete
if (options == null) options = {};
[96] Fix | Delete
var f = $.serializeJSON;
[97] Fix | Delete
[98] Fix | Delete
// Validate
[99] Fix | Delete
var validOpts = [
[100] Fix | Delete
"checkboxUncheckedValue",
[101] Fix | Delete
"useIntKeysAsArrayIndex",
[102] Fix | Delete
[103] Fix | Delete
"skipFalsyValuesForTypes",
[104] Fix | Delete
"skipFalsyValuesForFields",
[105] Fix | Delete
[106] Fix | Delete
"disableColonTypes",
[107] Fix | Delete
"customTypes",
[108] Fix | Delete
"defaultTypes",
[109] Fix | Delete
"defaultType"
[110] Fix | Delete
];
[111] Fix | Delete
for (var opt in options) {
[112] Fix | Delete
if (validOpts.indexOf(opt) === -1) {
[113] Fix | Delete
throw new Error("serializeJSON ERROR: invalid option '" + opt + "'. Please use one of " + validOpts.join(", "));
[114] Fix | Delete
}
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
// Helper to get options or defaults
[118] Fix | Delete
return $.extend({}, f.defaultBaseOptions, f.defaultOptions, options);
[119] Fix | Delete
},
[120] Fix | Delete
[121] Fix | Delete
// Just like jQuery's serializeArray method, returns an array of objects with name and value.
[122] Fix | Delete
// but also includes the dom element (el) and is handles unchecked checkboxes if the option or data attribute are provided.
[123] Fix | Delete
serializeArray: function($form, opts) {
[124] Fix | Delete
if (opts == null) { opts = {}; }
[125] Fix | Delete
var f = $.serializeJSON;
[126] Fix | Delete
[127] Fix | Delete
return $form.map(function() {
[128] Fix | Delete
var elements = $.prop(this, "elements"); // handle propHook "elements" to filter or add form elements
[129] Fix | Delete
return elements ? $.makeArray(elements) : this;
[130] Fix | Delete
[131] Fix | Delete
}).filter(function() {
[132] Fix | Delete
var $el = $(this);
[133] Fix | Delete
var type = this.type;
[134] Fix | Delete
[135] Fix | Delete
// Filter with the standard W3C rules for successful controls: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2
[136] Fix | Delete
return this.name && // must contain a name attribute
[137] Fix | Delete
!$el.is(":disabled") && // must not be disable (use .is(":disabled") so that fieldset[disabled] works)
[138] Fix | Delete
rsubmittable.test(this.nodeName) && !rsubmitterTypes.test(type) && // only serialize submittable fields (and not buttons)
[139] Fix | Delete
(this.checked || !rcheckableType.test(type) || f.getCheckboxUncheckedValue($el, opts) != null); // skip unchecked checkboxes (unless using opts)
[140] Fix | Delete
[141] Fix | Delete
}).map(function(_i, el) {
[142] Fix | Delete
var $el = $(this);
[143] Fix | Delete
var val = $el.val();
[144] Fix | Delete
var type = this.type; // "input", "select", "textarea", "checkbox", etc.
[145] Fix | Delete
[146] Fix | Delete
if (val == null) {
[147] Fix | Delete
return null;
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
if (rcheckableType.test(type) && !this.checked) {
[151] Fix | Delete
val = f.getCheckboxUncheckedValue($el, opts);
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
if (isArray(val)) {
[155] Fix | Delete
return $.map(val, function(val) {
[156] Fix | Delete
return { name: el.name, value: val.replace(rCRLF, "\r\n"), el: el };
[157] Fix | Delete
} );
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
return { name: el.name, value: val.replace(rCRLF, "\r\n"), el: el };
[161] Fix | Delete
[162] Fix | Delete
}).get();
[163] Fix | Delete
},
[164] Fix | Delete
[165] Fix | Delete
getCheckboxUncheckedValue: function($el, opts) {
[166] Fix | Delete
var val = $el.attr("data-unchecked-value");
[167] Fix | Delete
if (val == null) {
[168] Fix | Delete
val = opts.checkboxUncheckedValue;
[169] Fix | Delete
}
[170] Fix | Delete
return val;
[171] Fix | Delete
},
[172] Fix | Delete
[173] Fix | Delete
// Parse value with type function
[174] Fix | Delete
applyTypeFunc: function(name, valStr, type, el, typeFunctions) {
[175] Fix | Delete
var typeFunc = typeFunctions[type];
[176] Fix | Delete
if (!typeFunc) { // quick feedback to user if there is a typo or missconfiguration
[177] Fix | Delete
throw new Error("serializeJSON ERROR: Invalid type " + type + " found in input name '" + name + "', please use one of " + objectKeys(typeFunctions).join(", "));
[178] Fix | Delete
}
[179] Fix | Delete
return typeFunc(valStr, el);
[180] Fix | Delete
},
[181] Fix | Delete
[182] Fix | Delete
// Splits a field name into the name and the type. Examples:
[183] Fix | Delete
// "foo" => ["foo", ""]
[184] Fix | Delete
// "foo:boolean" => ["foo", "boolean"]
[185] Fix | Delete
// "foo[bar]:null" => ["foo[bar]", "null"]
[186] Fix | Delete
splitType : function(name) {
[187] Fix | Delete
var parts = name.split(":");
[188] Fix | Delete
if (parts.length > 1) {
[189] Fix | Delete
var t = parts.pop();
[190] Fix | Delete
return [parts.join(":"), t];
[191] Fix | Delete
} else {
[192] Fix | Delete
return [name, ""];
[193] Fix | Delete
}
[194] Fix | Delete
},
[195] Fix | Delete
[196] Fix | Delete
// Check if this input should be skipped when it has a falsy value,
[197] Fix | Delete
// depending on the options to skip values by name or type, and the data-skip-falsy attribute.
[198] Fix | Delete
shouldSkipFalsy: function(name, nameSansType, type, el, opts) {
[199] Fix | Delete
var skipFromDataAttr = $(el).attr("data-skip-falsy");
[200] Fix | Delete
if (skipFromDataAttr != null) {
[201] Fix | Delete
return skipFromDataAttr !== "false"; // any value is true, except the string "false"
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
var optForFields = opts.skipFalsyValuesForFields;
[205] Fix | Delete
if (optForFields && (optForFields.indexOf(nameSansType) !== -1 || optForFields.indexOf(name) !== -1)) {
[206] Fix | Delete
return true;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
var optForTypes = opts.skipFalsyValuesForTypes;
[210] Fix | Delete
if (optForTypes && optForTypes.indexOf(type) !== -1) {
[211] Fix | Delete
return true;
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
return false;
[215] Fix | Delete
},
[216] Fix | Delete
[217] Fix | Delete
// Split the input name in programatically readable keys.
[218] Fix | Delete
// Examples:
[219] Fix | Delete
// "foo" => ["foo"]
[220] Fix | Delete
// "[foo]" => ["foo"]
[221] Fix | Delete
// "foo[inn][bar]" => ["foo", "inn", "bar"]
[222] Fix | Delete
// "foo[inn[bar]]" => ["foo", "inn", "bar"]
[223] Fix | Delete
// "foo[inn][arr][0]" => ["foo", "inn", "arr", "0"]
[224] Fix | Delete
// "arr[][val]" => ["arr", "", "val"]
[225] Fix | Delete
splitInputNameIntoKeysArray: function(nameWithNoType) {
[226] Fix | Delete
var keys = nameWithNoType.split("["); // split string into array
[227] Fix | Delete
keys = $.map(keys, function (key) { return key.replace(/\]/g, ""); }); // remove closing brackets
[228] Fix | Delete
if (keys[0] === "") { keys.shift(); } // ensure no opening bracket ("[foo][inn]" should be same as "foo[inn]")
[229] Fix | Delete
return keys;
[230] Fix | Delete
},
[231] Fix | Delete
[232] Fix | Delete
// Set a value in an object or array, using multiple keys to set in a nested object or array.
[233] Fix | Delete
// This is the main function of the script, that allows serializeJSON to use nested keys.
[234] Fix | Delete
// Examples:
[235] Fix | Delete
//
[236] Fix | Delete
// deepSet(obj, ["foo"], v) // obj["foo"] = v
[237] Fix | Delete
// deepSet(obj, ["foo", "inn"], v) // obj["foo"]["inn"] = v // Create the inner obj["foo"] object, if needed
[238] Fix | Delete
// deepSet(obj, ["foo", "inn", "123"], v) // obj["foo"]["arr"]["123"] = v //
[239] Fix | Delete
//
[240] Fix | Delete
// deepSet(obj, ["0"], v) // obj["0"] = v
[241] Fix | Delete
// deepSet(arr, ["0"], v, {useIntKeysAsArrayIndex: true}) // arr[0] = v
[242] Fix | Delete
// deepSet(arr, [""], v) // arr.push(v)
[243] Fix | Delete
// deepSet(obj, ["arr", ""], v) // obj["arr"].push(v)
[244] Fix | Delete
//
[245] Fix | Delete
// arr = [];
[246] Fix | Delete
// deepSet(arr, ["", v] // arr => [v]
[247] Fix | Delete
// deepSet(arr, ["", "foo"], v) // arr => [v, {foo: v}]
[248] Fix | Delete
// deepSet(arr, ["", "bar"], v) // arr => [v, {foo: v, bar: v}]
[249] Fix | Delete
// deepSet(arr, ["", "bar"], v) // arr => [v, {foo: v, bar: v}, {bar: v}]
[250] Fix | Delete
//
[251] Fix | Delete
deepSet: function (o, keys, value, opts) {
[252] Fix | Delete
if (opts == null) { opts = {}; }
[253] Fix | Delete
var f = $.serializeJSON;
[254] Fix | Delete
if (isUndefined(o)) { throw new Error("ArgumentError: param 'o' expected to be an object or array, found undefined"); }
[255] Fix | Delete
if (!keys || keys.length === 0) { throw new Error("ArgumentError: param 'keys' expected to be an array with least one element"); }
[256] Fix | Delete
[257] Fix | Delete
var key = keys[0];
[258] Fix | Delete
[259] Fix | Delete
// Only one key, then it's not a deepSet, just assign the value in the object or add it to the array.
[260] Fix | Delete
if (keys.length === 1) {
[261] Fix | Delete
if (key === "") { // push values into an array (o must be an array)
[262] Fix | Delete
o.push(value);
[263] Fix | Delete
} else {
[264] Fix | Delete
o[key] = value; // keys can be object keys (strings) or array indexes (numbers)
[265] Fix | Delete
}
[266] Fix | Delete
return;
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
var nextKey = keys[1]; // nested key
[270] Fix | Delete
var tailKeys = keys.slice(1); // list of all other nested keys (nextKey is first)
[271] Fix | Delete
[272] Fix | Delete
if (key === "") { // push nested objects into an array (o must be an array)
[273] Fix | Delete
var lastIdx = o.length - 1;
[274] Fix | Delete
var lastVal = o[lastIdx];
[275] Fix | Delete
[276] Fix | Delete
// if the last value is an object or array, and the new key is not set yet
[277] Fix | Delete
if (isObject(lastVal) && isUndefined(f.deepGet(lastVal, tailKeys))) {
[278] Fix | Delete
key = lastIdx; // then set the new value as a new attribute of the same object
[279] Fix | Delete
} else {
[280] Fix | Delete
key = lastIdx + 1; // otherwise, add a new element in the array
[281] Fix | Delete
}
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
if (nextKey === "") { // "" is used to push values into the nested array "array[]"
[285] Fix | Delete
if (isUndefined(o[key]) || !isArray(o[key])) {
[286] Fix | Delete
o[key] = []; // define (or override) as array to push values
[287] Fix | Delete
}
[288] Fix | Delete
} else {
[289] Fix | Delete
if (opts.useIntKeysAsArrayIndex && isValidArrayIndex(nextKey)) { // if 1, 2, 3 ... then use an array, where nextKey is the index
[290] Fix | Delete
if (isUndefined(o[key]) || !isArray(o[key])) {
[291] Fix | Delete
o[key] = []; // define (or override) as array, to insert values using int keys as array indexes
[292] Fix | Delete
}
[293] Fix | Delete
} else { // nextKey is going to be the nested object's attribute
[294] Fix | Delete
if (isUndefined(o[key]) || !isObject(o[key])) {
[295] Fix | Delete
o[key] = {}; // define (or override) as object, to set nested properties
[296] Fix | Delete
}
[297] Fix | Delete
}
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
// Recursively set the inner object
[301] Fix | Delete
f.deepSet(o[key], tailKeys, value, opts);
[302] Fix | Delete
},
[303] Fix | Delete
[304] Fix | Delete
deepGet: function (o, keys) {
[305] Fix | Delete
var f = $.serializeJSON;
[306] Fix | Delete
if (isUndefined(o) || isUndefined(keys) || keys.length === 0 || (!isObject(o) && !isArray(o))) {
[307] Fix | Delete
return o;
[308] Fix | Delete
}
[309] Fix | Delete
var key = keys[0];
[310] Fix | Delete
if (key === "") { // "" means next array index (used by deepSet)
[311] Fix | Delete
return undefined;
[312] Fix | Delete
}
[313] Fix | Delete
if (keys.length === 1) {
[314] Fix | Delete
return o[key];
[315] Fix | Delete
}
[316] Fix | Delete
var tailKeys = keys.slice(1);
[317] Fix | Delete
return f.deepGet(o[key], tailKeys);
[318] Fix | Delete
}
[319] Fix | Delete
};
[320] Fix | Delete
[321] Fix | Delete
// polyfill Object.keys to get option keys in IE<9
[322] Fix | Delete
var objectKeys = function(obj) {
[323] Fix | Delete
if (Object.keys) {
[324] Fix | Delete
return Object.keys(obj);
[325] Fix | Delete
} else {
[326] Fix | Delete
var key, keys = [];
[327] Fix | Delete
for (key in obj) { keys.push(key); }
[328] Fix | Delete
return keys;
[329] Fix | Delete
}
[330] Fix | Delete
};
[331] Fix | Delete
[332] Fix | Delete
var isObject = function(obj) { return obj === Object(obj); }; // true for Objects and Arrays
[333] Fix | Delete
var isUndefined = function(obj) { return obj === void 0; }; // safe check for undefined values
[334] Fix | Delete
var isValidArrayIndex = function(val) { return /^[0-9]+$/.test(String(val)); }; // 1,2,3,4 ... are valid array indexes
[335] Fix | Delete
var isArray = Array.isArray || function(obj) { return Object.prototype.toString.call(obj) === "[object Array]"; };
[336] Fix | Delete
}));
[337] Fix | Delete
[338] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function