MCPcopy
hub / github.com/ampproject/amphtml / onDocumentFormSubmit_

Function onDocumentFormSubmit_

src/document-submit.js:36–158  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

34 * @param {!Event} e
35 */
36export function onDocumentFormSubmit_(e) {
37 if (e.defaultPrevented) {
38 return;
39 }
40
41 const form = dev().assertElement(e.target);
42 if (!form || form.tagName != 'FORM') {
43 return;
44 }
45
46 // amp-form extension will add novalidate to all forms to manually trigger
47 // validation. In that case `novalidate` doesn't have the same meaning.
48 const isAmpFormMarked = form.classList.contains('i-amphtml-form');
49 let shouldValidate;
50 if (isAmpFormMarked) {
51 shouldValidate = !form.hasAttribute('amp-novalidate');
52 } else {
53 shouldValidate = !form.hasAttribute('novalidate');
54 }
55
56 // Safari does not trigger validation check on submission, hence we
57 // trigger it manually. In other browsers this would never execute since
58 // the submit event wouldn't be fired if the form is invalid.
59 if (shouldValidate && form.checkValidity && !form.checkValidity()) {
60 e.preventDefault();
61 }
62
63 const inputs = form.elements;
64 for (let i = 0; i < inputs.length; i++) {
65 userAssert(
66 !inputs[i].name || inputs[i].name != SOURCE_ORIGIN_PARAM,
67 'Illegal input name, %s found: %s',
68 SOURCE_ORIGIN_PARAM,
69 inputs[i]
70 );
71 }
72
73 const action = form.getAttribute('action');
74 const actionXhr = form.getAttribute('action-xhr');
75 const method = (form.getAttribute('method') || 'GET').toUpperCase();
76
77 if (actionXhr) {
78 assertHttpsUrl(actionXhr, form, 'action-xhr');
79 userAssert(
80 !isProxyOrigin(actionXhr),
81 'form action-xhr should not be on AMP CDN: %s',
82 form
83 );
84 checkCorsUrl(actionXhr);
85 }
86 if (action) {
87 assertHttpsUrl(action, form, 'action');
88 userAssert(
89 !isProxyOrigin(action),
90 'form action should not be on AMP CDN: %s',
91 form
92 );
93 checkCorsUrl(action);

Callers 1

Calls 12

devFunction · 0.90
userAssertFunction · 0.90
assertHttpsUrlFunction · 0.90
isProxyOriginFunction · 0.90
checkCorsUrlFunction · 0.90
userFunction · 0.90
assertElementMethod · 0.80
getAttributeMethod · 0.80
containsMethod · 0.45
errorMethod · 0.45
setAttributeMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected