forms.js 3.4 KB

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