forms.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.serializeArray = exports.serialize = void 0;
  4. var utils_1 = require("../utils");
  5. /*
  6. * https://github.com/jquery/jquery/blob/2.1.3/src/manipulation/var/rcheckableType.js
  7. * https://github.com/jquery/jquery/blob/2.1.3/src/serialize.js
  8. */
  9. var submittableSelector = 'input,select,textarea,keygen';
  10. var r20 = /%20/g;
  11. var rCRLF = /\r?\n/g;
  12. /**
  13. * Encode a set of form elements as a string for submission.
  14. *
  15. * @category Forms
  16. * @returns The serialized form.
  17. * @see {@link https://api.jquery.com/serialize/}
  18. */
  19. function serialize() {
  20. // Convert form elements into name/value objects
  21. var arr = this.serializeArray();
  22. // Serialize each element into a key/value string
  23. var retArr = arr.map(function (data) {
  24. return encodeURIComponent(data.name) + "=" + encodeURIComponent(data.value);
  25. });
  26. // Return the resulting serialization
  27. return retArr.join('&').replace(r20, '+');
  28. }
  29. exports.serialize = serialize;
  30. /**
  31. * Encode a set of form elements as an array of names and values.
  32. *
  33. * @category Forms
  34. * @example
  35. *
  36. * ```js
  37. * $('<form><input name="foo" value="bar" /></form>').serializeArray();
  38. * //=> [ { name: 'foo', value: 'bar' } ]
  39. * ```
  40. *
  41. * @returns The serialized form.
  42. * @see {@link https://api.jquery.com/serializeArray/}
  43. */
  44. function serializeArray() {
  45. var _this = this;
  46. // Resolve all form elements from either forms or collections of form elements
  47. return this.map(function (_, elem) {
  48. var $elem = _this._make(elem);
  49. if (utils_1.isTag(elem) && elem.name === 'form') {
  50. return $elem.find(submittableSelector).toArray();
  51. }
  52. return $elem.filter(submittableSelector).toArray();
  53. })
  54. .filter(
  55. // Verify elements have a name (`attr.name`) and are not disabled (`:enabled`)
  56. '[name!=""]:enabled' +
  57. // And cannot be clicked (`[type=submit]`) or are used in `x-www-form-urlencoded` (`[type=file]`)
  58. ':not(:submit, :button, :image, :reset, :file)' +
  59. // And are either checked/don't have a checkable state
  60. ':matches([checked], :not(:checkbox, :radio))'
  61. // Convert each of the elements to its value(s)
  62. )
  63. .map(function (_, elem) {
  64. var _a;
  65. var $elem = _this._make(elem);
  66. var name = $elem.attr('name'); // We have filtered for elements with a name before.
  67. // If there is no value set (e.g. `undefined`, `null`), then default value to empty
  68. var value = (_a = $elem.val()) !== null && _a !== void 0 ? _a : '';
  69. // If we have an array of values (e.g. `<select multiple>`), return an array of key/value pairs
  70. if (Array.isArray(value)) {
  71. return value.map(function (val) {
  72. /*
  73. * We trim replace any line endings (e.g. `\r` or `\r\n` with `\r\n`) to guarantee consistency across platforms
  74. * These can occur inside of `<textarea>'s`
  75. */
  76. return ({ name: name, value: val.replace(rCRLF, '\r\n') });
  77. });
  78. }
  79. // Otherwise (e.g. `<input type="text">`, return only one key/value pair
  80. return { name: name, value: value.replace(rCRLF, '\r\n') };
  81. })
  82. .toArray();
  83. }
  84. exports.serializeArray = serializeArray;