forms.js 3.2 KB

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