forms.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { isTag } from '../utils.js';
  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. // Convert each of the elements to its value(s)
  62. )
  63. .map((_, elem) => {
  64. var _a;
  65. const $elem = this._make(elem);
  66. const 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. const 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((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. ({ name, value: val.replace(rCRLF, '\r\n') }));
  77. }
  78. // Otherwise (e.g. `<input type="text">`, return only one key/value pair
  79. return { name, value: value.replace(rCRLF, '\r\n') };
  80. })
  81. .toArray();
  82. }
  83. //# sourceMappingURL=forms.js.map