1/** 2 * @author Kyle Florence <kyle[dot]florence[at]gmail[dot]com> 3 * @website https://github.com/kflorence/jquery-deserialize/ 4 * @version 1.3.3 5 * 6 * Dual licensed under the MIT and GPLv2 licenses. 7 */ 8(function( jQuery, undefined ) { 9 10var push = Array.prototype.push, 11 rcheck = /^(?:radio|checkbox)$/i, 12 rplus = /\+/g, 13 rselect = /^(?:option|select-one|select-multiple)$/i, 14 rvalue = /^(?:button|color|date|datetime|datetime-local|email|hidden|month|number|password|range|reset|search|submit|tel|text|textarea|time|url|week)$/i; 15 16function getElements( elements ) { 17 return elements.map(function() { 18 return this.elements ? jQuery.makeArray( this.elements ) : this; 19 }).filter( ":input:not(:disabled)" ).get(); 20} 21 22function getElementsByName( elements ) { 23 var current, 24 elementsByName = {}; 25 26 jQuery.each( elements, function( i, element ) { 27 current = elementsByName[ element.name ]; 28 if ( current === undefined ) { 29 elementsByName[ element.name ] = []; 30 } 31 elementsByName[ element.name ].push( element ); 32 }); 33 34 return elementsByName; 35} 36 37jQuery.fn.deserialize = function( data, options ) { 38 var i, length, 39 elements = getElements( this ), 40 normalized = []; 41 42 if ( !data || !elements.length ) { 43 return this; 44 } 45 46 if ( jQuery.isArray( data ) ) { 47 normalized = data; 48 49 } else if ( jQuery.isPlainObject( data ) ) { 50 var key, value; 51 52 for ( key in data ) { 53 jQuery.isArray( value = data[ key ] ) ? 54 push.apply( normalized, jQuery.map( value, function( v ) { 55 return { name: key, value: v }; 56 })) : push.call( normalized, { name: key, value: value } ); 57 } 58 59 } else if ( typeof data === "string" ) { 60 var parts; 61 62 data = data.replace(/'/g, "'"); 63 data = data.replace(/amp;/g, '' ); 64 data = data.split( "&" ); 65 66 for ( i = 0, length = data.length; i < length; i++ ) { 67 parts = data[ i ].split( "=" ); 68 push.call( normalized, { 69 name: decodeURIComponent( parts[ 0 ].replace( rplus, "%20" ) ), 70 value: decodeURIComponent( parts[ 1 ].replace( rplus, "%20" ) ) 71 }); 72 } 73 } 74 75 if ( !( length = normalized.length ) ) { 76 return this; 77 } 78 79 var current, element, j, len, name, property, type, value, 80 elementsForName, k, nameIndex, optionsAndInputs, 81 change = jQuery.noop, 82 complete = jQuery.noop, 83 names = {}; 84 85 options = options || {}; 86 elements = getElementsByName( elements ); 87 88 // Backwards compatible with old arguments: data, callback 89 if ( jQuery.isFunction( options ) ) { 90 complete = options; 91 92 } else { 93 change = jQuery.isFunction( options.change ) ? options.change : change; 94 complete = jQuery.isFunction( options.complete ) ? options.complete : complete; 95 } 96 97 for ( i = 0; i < length; i++ ) { 98 current = normalized[ i ]; 99 100 name = current.name; 101 value = current.value; 102 103 elementsForName = elements[ name ]; 104 if ( !elementsForName || elementsForName.length === 0 ) { 105 continue; 106 } 107 108 // Keep track of parameters that are named the same for array handling. 109 if ( names[ name ] === undefined ) { 110 names[ name ] = 0; 111 } 112 nameIndex = names[ name ]++; 113 114 // Handle the simple case of inputs that take a simple value. 115 // 116 // Possible arrays are handled by fetching the element that corresponds 117 // to the index of the current name. 118 if ( elementsForName[ nameIndex ] ) { 119 element = elementsForName[ nameIndex ]; 120 type = ( element.type || element.nodeName ).toLowerCase(); 121 if ( rvalue.test( type ) ) { 122 change.call( element, ( element.value = value ) ); 123 124 // Skip further processing for this simple case. 125 continue; 126 } 127 } 128 129 // Handle more complex cases involving select menus, checkboxes, or radios. 130 for ( j = 0, len = elementsForName.length; j < len; j++) { 131 element = elementsForName[ j ]; 132 type = ( element.type || element.nodeName ).toLowerCase(); 133 property = null; 134 135 if ( rcheck.test( type ) ) { 136 property = "checked"; 137 138 } else if ( rselect.test( type ) ) { 139 property = "selected"; 140 141 } 142 143 if ( property ) { 144 // Flatten all of the inputs (radios & checkboxes) and options 145 // (under select menus), so all of them can be treated in a 146 // standard way. 147 optionsAndInputs = []; 148 if ( element.options ) { 149 for ( k = 0; k < element.options.length; k++ ) { 150 optionsAndInputs.push( element.options[ k ] ); 151 } 152 153 } else { 154 optionsAndInputs.push(element); 155 } 156 157 for ( k = 0; k < optionsAndInputs.length; k++ ) { 158 current = optionsAndInputs[ k ]; 159 if ( current.value == value ) { 160 change.call( current, ( current[ property ] = true ) && value ); 161 } 162 } 163 } 164 } 165 } 166 167 complete.call( this ); 168 169 return this; 170};
171 172})( jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.