25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

65 satır
2.5 KiB

3 yıl önce
  1. // https://github.com/jquery/jquery/blob/2.1.3/src/manipulation/var/rcheckableType.js
  2. // https://github.com/jquery/jquery/blob/2.1.3/src/serialize.js
  3. var submittableSelector = 'input,select,textarea,keygen',
  4. r20 = /%20/g,
  5. rCRLF = /\r?\n/g,
  6. _ = {
  7. map: require('lodash.map')
  8. };
  9. exports.serialize = function() {
  10. // Convert form elements into name/value objects
  11. var arr = this.serializeArray();
  12. // Serialize each element into a key/value string
  13. var retArr = _.map(arr, function(data) {
  14. return encodeURIComponent(data.name) + '=' + encodeURIComponent(data.value);
  15. });
  16. // Return the resulting serialization
  17. return retArr.join('&').replace(r20, '+');
  18. };
  19. exports.serializeArray = function() {
  20. // Resolve all form elements from either forms or collections of form elements
  21. var Cheerio = this.constructor;
  22. return this.map(function() {
  23. var elem = this;
  24. var $elem = Cheerio(elem);
  25. if (elem.name === 'form') {
  26. return $elem.find(submittableSelector).toArray();
  27. } else {
  28. return $elem.filter(submittableSelector).toArray();
  29. }
  30. }).filter(
  31. // Verify elements have a name (`attr.name`) and are not disabled (`:disabled`)
  32. '[name!=""]:not(:disabled)'
  33. // and cannot be clicked (`[type=submit]`) or are used in `x-www-form-urlencoded` (`[type=file]`)
  34. + ':not(:submit, :button, :image, :reset, :file)'
  35. // and are either checked/don't have a checkable state
  36. + ':matches([checked], :not(:checkbox, :radio))'
  37. // Convert each of the elements to its value(s)
  38. ).map(function(i, elem) {
  39. var $elem = Cheerio(elem);
  40. var name = $elem.attr('name');
  41. var val = $elem.val();
  42. // If there is no value set (e.g. `undefined`, `null`), then return nothing
  43. if (val == null) {
  44. return null;
  45. } else {
  46. // If we have an array of values (e.g. `<select multiple>`), return an array of key/value pairs
  47. if (Array.isArray(val)) {
  48. return _.map(val, function(val) {
  49. // We trim replace any line endings (e.g. `\r` or `\r\n` with `\r\n`) to guarantee consistency across platforms
  50. // These can occur inside of `<textarea>'s`
  51. return {name: name, value: val.replace( rCRLF, '\r\n' )};
  52. });
  53. // Otherwise (e.g. `<input type="text">`, return only one key/value pair
  54. } else {
  55. return {name: name, value: val.replace( rCRLF, '\r\n' )};
  56. }
  57. }
  58. // Convert our result to an array
  59. }).get();
  60. };