forms.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { isTag } from 'domhandler';
  2. /*
  3. * https://github.com/jquery/jquery/blob/2.1.3/src/manipulation/var/rcheckableType.js
  4. * https://github.com/jquery/jquery/blob/2.1.3/src/serialize.js
  5. */
  6. const submittableSelector = 'input,select,textarea,keygen';
  7. const r20 = /%20/g;
  8. const rCRLF = /\r?\n/g;
  9. /**
  10. * Encode a set of form elements as a string for submission.
  11. *
  12. * @category Forms
  13. * @example
  14. *
  15. * ```js
  16. * $('<form><input name="foo" value="bar" /></form>').serialize();
  17. * //=> 'foo=bar'
  18. * ```
  19. *
  20. * @returns The serialized form.
  21. * @see {@link https://api.jquery.com/serialize/}
  22. */
  23. export function serialize() {
  24. // Convert form elements into name/value objects
  25. const arr = this.serializeArray();
  26. // Serialize each element into a key/value string
  27. const retArr = arr.map((data) => `${encodeURIComponent(data.name)}=${encodeURIComponent(data.value)}`);
  28. // Return the resulting serialization
  29. return retArr.join('&').replace(r20, '+');
  30. }
  31. /**
  32. * Encode a set of form elements as an array of names and values.
  33. *
  34. * @category Forms
  35. * @example
  36. *
  37. * ```js
  38. * $('<form><input name="foo" value="bar" /></form>').serializeArray();
  39. * //=> [ { name: 'foo', value: 'bar' } ]
  40. * ```
  41. *
  42. * @returns The serialized form.
  43. * @see {@link https://api.jquery.com/serializeArray/}
  44. */
  45. export function serializeArray() {
  46. // Resolve all form elements from either forms or collections of form elements
  47. return this.map((_, elem) => {
  48. const $elem = this._make(elem);
  49. if (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. .map((_, elem) => {
  62. var _a;
  63. const $elem = this._make(elem);
  64. const name = $elem.attr('name'); // We have filtered for elements with a name before.
  65. // If there is no value set (e.g. `undefined`, `null`), then default value to empty
  66. const value = (_a = $elem.val()) !== null && _a !== void 0 ? _a : '';
  67. // If we have an array of values (e.g. `<select multiple>`), return an array of key/value pairs
  68. if (Array.isArray(value)) {
  69. return value.map((val) =>
  70. /*
  71. * We trim replace any line endings (e.g. `\r` or `\r\n` with `\r\n`) to guarantee consistency across platforms
  72. * These can occur inside of `<textarea>'s`
  73. */
  74. ({ name, value: val.replace(rCRLF, '\r\n') }));
  75. }
  76. // Otherwise (e.g. `<input type="text">`, return only one key/value pair
  77. return { name, value: value.replace(rCRLF, '\r\n') };
  78. })
  79. .toArray();
  80. }
  81. //# sourceMappingURL=forms.js.map