Skip to content

Commit 65eec42

Browse files
authored
Use FormDatasubmitter parameter (#29028)
1 parent 454fc41 commit 65eec42

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

‎packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ function coerceFormActionProp(
4545
}
4646
}
4747

48-
functioncreateFormDataWithSubmitter(
49-
form: HTMLFormElement,
50-
submitter: HTMLInputElement|HTMLButtonElement,
51-
){
52-
// The submitter's value should be included in the FormData.
53-
// It should be in the document order in the form.
54-
// Since the FormData constructor invokes the formdata event it also
55-
// needs to be available before that happens so after construction it's too
56-
// late. We use a temporary fake node for the duration of this event.
57-
// TODO: FormData takes a second argument that it's the submitter but this
58-
// is fairly new so not all browsers support it yet. Switch to that technique
59-
// when available.
60-
consttemp=submitter.ownerDocument.createElement('input');
61-
temp.name=submitter.name;
62-
temp.value=submitter.value;
63-
if(form.id){
64-
temp.setAttribute('form',form.id);
65-
}
66-
(submitter.parentNode: any).insertBefore(temp,submitter);
67-
constformData=newFormData(form);
68-
(temp.parentNode: any).removeChild(temp);
69-
returnformData;
70-
}
71-
7248
/**
7349
* This plugin invokes action functions on forms, inputs and buttons if
7450
* the form doesn't prevent default.
@@ -129,9 +105,7 @@ function extractEvents(
129105
if(didCurrentEventScheduleTransition()){
130106
// We're going to set the pending form status, but because the submission
131107
// was prevented, we should not fire the action function.
132-
constformData=submitter
133-
? createFormDataWithSubmitter(form,submitter)
134-
: newFormData(form);
108+
constformData=newFormData(form,submitter);
135109
constpendingState: FormStatus={
136110
pending: true,
137111
data: formData,
@@ -160,9 +134,7 @@ function extractEvents(
160134
event.preventDefault();
161135

162136
// Dispatch the action and set a pending form status.
163-
constformData=submitter
164-
? createFormDataWithSubmitter(form,submitter)
165-
: newFormData(form);
137+
constformData=newFormData(form,submitter);
166138
constpendingState: FormStatus={
167139
pending: true,
168140
data: formData,

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,7 @@ export function listenToFormSubmissionsForReplaying() {
634634
event.preventDefault();
635635

636636
// Take a snapshot of the FormData at the time of the event.
637-
letformData;
638-
if(formDataSubmitter){
639-
// The submitter's value should be included in the FormData.
640-
// It should be in the document order in the form.
641-
// Since the FormData constructor invokes the formdata event it also
642-
// needs to be available before that happens so after construction it's too
643-
// late. We use a temporary fake node for the duration of this event.
644-
// TODO: FormData takes a second argument that it's the submitter but this
645-
// is fairly new so not all browsers support it yet. Switch to that technique
646-
// when available.
647-
consttemp=document.createElement('input');
648-
temp.name=formDataSubmitter.name;
649-
temp.value=formDataSubmitter.value;
650-
formDataSubmitter.parentNode.insertBefore(temp,formDataSubmitter);
651-
formData=newFormData(form);
652-
temp.parentNode.removeChild(temp);
653-
}else{
654-
formData=newFormData(form);
655-
}
637+
constformData=newFormData(form,formDataSubmitter);
656638

657639
// Queue for replaying later. This field could potentially be shared with multiple
658640
// Reacts on the same page since each one will preventDefault for the next one.

‎packages/react-dom/src/__tests__/ReactDOMForm-test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
1414
// Our current version of JSDOM doesn't implement the event dispatching
1515
// so we polyfill it.
1616
constNativeFormData=global.FormData;
17-
constFormDataPolyfill=functionFormData(form){
18-
constformData=newNativeFormData(form);
17+
constFormDataPolyfill=functionFormData(form,submitter){
18+
constformData=newNativeFormData(form,submitter);
1919
constformDataEvent=newEvent('formdata',{
2020
bubbles: true,
2121
cancelable: false,
@@ -489,11 +489,16 @@ describe('ReactDOMForm', () => {
489489
constinputRef=React.createRef();
490490
constbuttonRef=React.createRef();
491491
constoutsideButtonRef=React.createRef();
492+
constimageButtonRef=React.createRef();
492493
letbutton;
494+
letbuttonX;
495+
letbuttonY;
493496
lettitle;
494497

495498
functionaction(formData){
496499
button=formData.get('button');
500+
buttonX=formData.get('button.x');
501+
buttonY=formData.get('button.y');
497502
title=formData.get('title');
498503
}
499504

@@ -508,6 +513,12 @@ describe('ReactDOMForm', () => {
508513
<buttonname="button"value="edit"ref={buttonRef}>
509514
Edit
510515
</button>
516+
<input
517+
type="image"
518+
name="button"
519+
href="/some/image.png"
520+
ref={imageButtonRef}
521+
/>
511522
</form>
512523
<formid="form"action={action}>
513524
<inputtype="text"name="title"defaultValue="hello"/>
@@ -546,9 +557,12 @@ describe('ReactDOMForm', () => {
546557
expect(button).toBe('outside');
547558
expect(title).toBe('hello');
548559

549-
// Ensure that the type field got correctly restored
550-
expect(inputRef.current.getAttribute('type')).toBe('submit');
551-
expect(buttonRef.current.getAttribute('type')).toBe(null);
560+
awaitsubmit(imageButtonRef.current);
561+
562+
expect(button).toBe(null);
563+
expect(buttonX).toBe('0');
564+
expect(buttonY).toBe('0');
565+
expect(title).toBe('hello');
552566
});
553567

554568
it('excludes the submitter name when the submitter is a function action',async()=>{

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ describe('ReactFlightDOMForm', () => {
121121
constmethod=(submitter&&submitter.formMethod)||form.method;
122122
constencType=(submitter&&submitter.formEnctype)||form.enctype;
123123
if(method==='post'&&encType==='multipart/form-data'){
124-
letformData;
125-
if(submitter){
126-
consttemp=document.createElement('input');
127-
temp.name=submitter.name;
128-
temp.value=submitter.value;
129-
submitter.parentNode.insertBefore(temp,submitter);
130-
formData=newFormData(form);
131-
temp.parentNode.removeChild(temp);
132-
}else{
133-
formData=newFormData(form);
134-
}
124+
constformData=newFormData(form,submitter);
135125
returnPOST(formData);
136126
}
137127
thrownewError('Navigate to: '+action);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Use `FormData` `submitter` parameter (#29028) · react/react@65eec42 · GitHub
Skip to content

Commit 65eec42

Browse files
authored
Use FormDatasubmitter parameter (#29028)
1 parent 454fc41 commit 65eec42

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

‎packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ function coerceFormActionProp(
4545
}
4646
}
4747

48-
functioncreateFormDataWithSubmitter(
49-
form: HTMLFormElement,
50-
submitter: HTMLInputElement|HTMLButtonElement,
51-
){
52-
// The submitter's value should be included in the FormData.
53-
// It should be in the document order in the form.
54-
// Since the FormData constructor invokes the formdata event it also
55-
// needs to be available before that happens so after construction it's too
56-
// late. We use a temporary fake node for the duration of this event.
57-
// TODO: FormData takes a second argument that it's the submitter but this
58-
// is fairly new so not all browsers support it yet. Switch to that technique
59-
// when available.
60-
consttemp=submitter.ownerDocument.createElement('input');
61-
temp.name=submitter.name;
62-
temp.value=submitter.value;
63-
if(form.id){
64-
temp.setAttribute('form',form.id);
65-
}
66-
(submitter.parentNode: any).insertBefore(temp,submitter);
67-
constformData=newFormData(form);
68-
(temp.parentNode: any).removeChild(temp);
69-
returnformData;
70-
}
71-
7248
/**
7349
* This plugin invokes action functions on forms, inputs and buttons if
7450
* the form doesn't prevent default.
@@ -129,9 +105,7 @@ function extractEvents(
129105
if(didCurrentEventScheduleTransition()){
130106
// We're going to set the pending form status, but because the submission
131107
// was prevented, we should not fire the action function.
132-
constformData=submitter
133-
? createFormDataWithSubmitter(form,submitter)
134-
: newFormData(form);
108+
constformData=newFormData(form,submitter);
135109
constpendingState: FormStatus={
136110
pending: true,
137111
data: formData,
@@ -160,9 +134,7 @@ function extractEvents(
160134
event.preventDefault();
161135

162136
// Dispatch the action and set a pending form status.
163-
constformData=submitter
164-
? createFormDataWithSubmitter(form,submitter)
165-
: newFormData(form);
137+
constformData=newFormData(form,submitter);
166138
constpendingState: FormStatus={
167139
pending: true,
168140
data: formData,

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,7 @@ export function listenToFormSubmissionsForReplaying() {
634634
event.preventDefault();
635635

636636
// Take a snapshot of the FormData at the time of the event.
637-
letformData;
638-
if(formDataSubmitter){
639-
// The submitter's value should be included in the FormData.
640-
// It should be in the document order in the form.
641-
// Since the FormData constructor invokes the formdata event it also
642-
// needs to be available before that happens so after construction it's too
643-
// late. We use a temporary fake node for the duration of this event.
644-
// TODO: FormData takes a second argument that it's the submitter but this
645-
// is fairly new so not all browsers support it yet. Switch to that technique
646-
// when available.
647-
consttemp=document.createElement('input');
648-
temp.name=formDataSubmitter.name;
649-
temp.value=formDataSubmitter.value;
650-
formDataSubmitter.parentNode.insertBefore(temp,formDataSubmitter);
651-
formData=newFormData(form);
652-
temp.parentNode.removeChild(temp);
653-
}else{
654-
formData=newFormData(form);
655-
}
637+
constformData=newFormData(form,formDataSubmitter);
656638

657639
// Queue for replaying later. This field could potentially be shared with multiple
658640
// Reacts on the same page since each one will preventDefault for the next one.

‎packages/react-dom/src/__tests__/ReactDOMForm-test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
1414
// Our current version of JSDOM doesn't implement the event dispatching
1515
// so we polyfill it.
1616
constNativeFormData=global.FormData;
17-
constFormDataPolyfill=functionFormData(form){
18-
constformData=newNativeFormData(form);
17+
constFormDataPolyfill=functionFormData(form,submitter){
18+
constformData=newNativeFormData(form,submitter);
1919
constformDataEvent=newEvent('formdata',{
2020
bubbles: true,
2121
cancelable: false,
@@ -489,11 +489,16 @@ describe('ReactDOMForm', () => {
489489
constinputRef=React.createRef();
490490
constbuttonRef=React.createRef();
491491
constoutsideButtonRef=React.createRef();
492+
constimageButtonRef=React.createRef();
492493
letbutton;
494+
letbuttonX;
495+
letbuttonY;
493496
lettitle;
494497

495498
functionaction(formData){
496499
button=formData.get('button');
500+
buttonX=formData.get('button.x');
501+
buttonY=formData.get('button.y');
497502
title=formData.get('title');
498503
}
499504

@@ -508,6 +513,12 @@ describe('ReactDOMForm', () => {
508513
<buttonname="button"value="edit"ref={buttonRef}>
509514
Edit
510515
</button>
516+
<input
517+
type="image"
518+
name="button"
519+
href="/some/image.png"
520+
ref={imageButtonRef}
521+
/>
511522
</form>
512523
<formid="form"action={action}>
513524
<inputtype="text"name="title"defaultValue="hello"/>
@@ -546,9 +557,12 @@ describe('ReactDOMForm', () => {
546557
expect(button).toBe('outside');
547558
expect(title).toBe('hello');
548559

549-
// Ensure that the type field got correctly restored
550-
expect(inputRef.current.getAttribute('type')).toBe('submit');
551-
expect(buttonRef.current.getAttribute('type')).toBe(null);
560+
awaitsubmit(imageButtonRef.current);
561+
562+
expect(button).toBe(null);
563+
expect(buttonX).toBe('0');
564+
expect(buttonY).toBe('0');
565+
expect(title).toBe('hello');
552566
});
553567

554568
it('excludes the submitter name when the submitter is a function action',async()=>{

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ describe('ReactFlightDOMForm', () => {
121121
constmethod=(submitter&&submitter.formMethod)||form.method;
122122
constencType=(submitter&&submitter.formEnctype)||form.enctype;
123123
if(method==='post'&&encType==='multipart/form-data'){
124-
letformData;
125-
if(submitter){
126-
consttemp=document.createElement('input');
127-
temp.name=submitter.name;
128-
temp.value=submitter.value;
129-
submitter.parentNode.insertBefore(temp,submitter);
130-
formData=newFormData(form);
131-
temp.parentNode.removeChild(temp);
132-
}else{
133-
formData=newFormData(form);
134-
}
124+
constformData=newFormData(form,submitter);
135125
returnPOST(formData);
136126
}
137127
thrownewError('Navigate to: '+action);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Use `FormData` `submitter` parameter (#29028) · react/react@65eec42 · GitHub
Skip to content

Commit 65eec42

Browse files
authored
Use FormDatasubmitter parameter (#29028)
1 parent 454fc41 commit 65eec42

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

‎packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ function coerceFormActionProp(
4545
}
4646
}
4747

48-
functioncreateFormDataWithSubmitter(
49-
form: HTMLFormElement,
50-
submitter: HTMLInputElement|HTMLButtonElement,
51-
){
52-
// The submitter's value should be included in the FormData.
53-
// It should be in the document order in the form.
54-
// Since the FormData constructor invokes the formdata event it also
55-
// needs to be available before that happens so after construction it's too
56-
// late. We use a temporary fake node for the duration of this event.
57-
// TODO: FormData takes a second argument that it's the submitter but this
58-
// is fairly new so not all browsers support it yet. Switch to that technique
59-
// when available.
60-
consttemp=submitter.ownerDocument.createElement('input');
61-
temp.name=submitter.name;
62-
temp.value=submitter.value;
63-
if(form.id){
64-
temp.setAttribute('form',form.id);
65-
}
66-
(submitter.parentNode: any).insertBefore(temp,submitter);
67-
constformData=newFormData(form);
68-
(temp.parentNode: any).removeChild(temp);
69-
returnformData;
70-
}
71-
7248
/**
7349
* This plugin invokes action functions on forms, inputs and buttons if
7450
* the form doesn't prevent default.
@@ -129,9 +105,7 @@ function extractEvents(
129105
if(didCurrentEventScheduleTransition()){
130106
// We're going to set the pending form status, but because the submission
131107
// was prevented, we should not fire the action function.
132-
constformData=submitter
133-
? createFormDataWithSubmitter(form,submitter)
134-
: newFormData(form);
108+
constformData=newFormData(form,submitter);
135109
constpendingState: FormStatus={
136110
pending: true,
137111
data: formData,
@@ -160,9 +134,7 @@ function extractEvents(
160134
event.preventDefault();
161135

162136
// Dispatch the action and set a pending form status.
163-
constformData=submitter
164-
? createFormDataWithSubmitter(form,submitter)
165-
: newFormData(form);
137+
constformData=newFormData(form,submitter);
166138
constpendingState: FormStatus={
167139
pending: true,
168140
data: formData,

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,7 @@ export function listenToFormSubmissionsForReplaying() {
634634
event.preventDefault();
635635

636636
// Take a snapshot of the FormData at the time of the event.
637-
letformData;
638-
if(formDataSubmitter){
639-
// The submitter's value should be included in the FormData.
640-
// It should be in the document order in the form.
641-
// Since the FormData constructor invokes the formdata event it also
642-
// needs to be available before that happens so after construction it's too
643-
// late. We use a temporary fake node for the duration of this event.
644-
// TODO: FormData takes a second argument that it's the submitter but this
645-
// is fairly new so not all browsers support it yet. Switch to that technique
646-
// when available.
647-
consttemp=document.createElement('input');
648-
temp.name=formDataSubmitter.name;
649-
temp.value=formDataSubmitter.value;
650-
formDataSubmitter.parentNode.insertBefore(temp,formDataSubmitter);
651-
formData=newFormData(form);
652-
temp.parentNode.removeChild(temp);
653-
}else{
654-
formData=newFormData(form);
655-
}
637+
constformData=newFormData(form,formDataSubmitter);
656638

657639
// Queue for replaying later. This field could potentially be shared with multiple
658640
// Reacts on the same page since each one will preventDefault for the next one.

‎packages/react-dom/src/__tests__/ReactDOMForm-test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
1414
// Our current version of JSDOM doesn't implement the event dispatching
1515
// so we polyfill it.
1616
constNativeFormData=global.FormData;
17-
constFormDataPolyfill=functionFormData(form){
18-
constformData=newNativeFormData(form);
17+
constFormDataPolyfill=functionFormData(form,submitter){
18+
constformData=newNativeFormData(form,submitter);
1919
constformDataEvent=newEvent('formdata',{
2020
bubbles: true,
2121
cancelable: false,
@@ -489,11 +489,16 @@ describe('ReactDOMForm', () => {
489489
constinputRef=React.createRef();
490490
constbuttonRef=React.createRef();
491491
constoutsideButtonRef=React.createRef();
492+
constimageButtonRef=React.createRef();
492493
letbutton;
494+
letbuttonX;
495+
letbuttonY;
493496
lettitle;
494497

495498
functionaction(formData){
496499
button=formData.get('button');
500+
buttonX=formData.get('button.x');
501+
buttonY=formData.get('button.y');
497502
title=formData.get('title');
498503
}
499504

@@ -508,6 +513,12 @@ describe('ReactDOMForm', () => {
508513
<buttonname="button"value="edit"ref={buttonRef}>
509514
Edit
510515
</button>
516+
<input
517+
type="image"
518+
name="button"
519+
href="/some/image.png"
520+
ref={imageButtonRef}
521+
/>
511522
</form>
512523
<formid="form"action={action}>
513524
<inputtype="text"name="title"defaultValue="hello"/>
@@ -546,9 +557,12 @@ describe('ReactDOMForm', () => {
546557
expect(button).toBe('outside');
547558
expect(title).toBe('hello');
548559

549-
// Ensure that the type field got correctly restored
550-
expect(inputRef.current.getAttribute('type')).toBe('submit');
551-
expect(buttonRef.current.getAttribute('type')).toBe(null);
560+
awaitsubmit(imageButtonRef.current);
561+
562+
expect(button).toBe(null);
563+
expect(buttonX).toBe('0');
564+
expect(buttonY).toBe('0');
565+
expect(title).toBe('hello');
552566
});
553567

554568
it('excludes the submitter name when the submitter is a function action',async()=>{

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ describe('ReactFlightDOMForm', () => {
121121
constmethod=(submitter&&submitter.formMethod)||form.method;
122122
constencType=(submitter&&submitter.formEnctype)||form.enctype;
123123
if(method==='post'&&encType==='multipart/form-data'){
124-
letformData;
125-
if(submitter){
126-
consttemp=document.createElement('input');
127-
temp.name=submitter.name;
128-
temp.value=submitter.value;
129-
submitter.parentNode.insertBefore(temp,submitter);
130-
formData=newFormData(form);
131-
temp.parentNode.removeChild(temp);
132-
}else{
133-
formData=newFormData(form);
134-
}
124+
constformData=newFormData(form,submitter);
135125
returnPOST(formData);
136126
}
137127
thrownewError('Navigate to: '+action);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Use `FormData` `submitter` parameter (#29028) · react/react@65eec42 · GitHub
Skip to content

Commit 65eec42

Browse files
authored
Use FormDatasubmitter parameter (#29028)
1 parent 454fc41 commit 65eec42

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

‎packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ function coerceFormActionProp(
4545
}
4646
}
4747

48-
functioncreateFormDataWithSubmitter(
49-
form: HTMLFormElement,
50-
submitter: HTMLInputElement|HTMLButtonElement,
51-
){
52-
// The submitter's value should be included in the FormData.
53-
// It should be in the document order in the form.
54-
// Since the FormData constructor invokes the formdata event it also
55-
// needs to be available before that happens so after construction it's too
56-
// late. We use a temporary fake node for the duration of this event.
57-
// TODO: FormData takes a second argument that it's the submitter but this
58-
// is fairly new so not all browsers support it yet. Switch to that technique
59-
// when available.
60-
consttemp=submitter.ownerDocument.createElement('input');
61-
temp.name=submitter.name;
62-
temp.value=submitter.value;
63-
if(form.id){
64-
temp.setAttribute('form',form.id);
65-
}
66-
(submitter.parentNode: any).insertBefore(temp,submitter);
67-
constformData=newFormData(form);
68-
(temp.parentNode: any).removeChild(temp);
69-
returnformData;
70-
}
71-
7248
/**
7349
* This plugin invokes action functions on forms, inputs and buttons if
7450
* the form doesn't prevent default.
@@ -129,9 +105,7 @@ function extractEvents(
129105
if(didCurrentEventScheduleTransition()){
130106
// We're going to set the pending form status, but because the submission
131107
// was prevented, we should not fire the action function.
132-
constformData=submitter
133-
? createFormDataWithSubmitter(form,submitter)
134-
: newFormData(form);
108+
constformData=newFormData(form,submitter);
135109
constpendingState: FormStatus={
136110
pending: true,
137111
data: formData,
@@ -160,9 +134,7 @@ function extractEvents(
160134
event.preventDefault();
161135

162136
// Dispatch the action and set a pending form status.
163-
constformData=submitter
164-
? createFormDataWithSubmitter(form,submitter)
165-
: newFormData(form);
137+
constformData=newFormData(form,submitter);
166138
constpendingState: FormStatus={
167139
pending: true,
168140
data: formData,

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,7 @@ export function listenToFormSubmissionsForReplaying() {
634634
event.preventDefault();
635635

636636
// Take a snapshot of the FormData at the time of the event.
637-
letformData;
638-
if(formDataSubmitter){
639-
// The submitter's value should be included in the FormData.
640-
// It should be in the document order in the form.
641-
// Since the FormData constructor invokes the formdata event it also
642-
// needs to be available before that happens so after construction it's too
643-
// late. We use a temporary fake node for the duration of this event.
644-
// TODO: FormData takes a second argument that it's the submitter but this
645-
// is fairly new so not all browsers support it yet. Switch to that technique
646-
// when available.
647-
consttemp=document.createElement('input');
648-
temp.name=formDataSubmitter.name;
649-
temp.value=formDataSubmitter.value;
650-
formDataSubmitter.parentNode.insertBefore(temp,formDataSubmitter);
651-
formData=newFormData(form);
652-
temp.parentNode.removeChild(temp);
653-
}else{
654-
formData=newFormData(form);
655-
}
637+
constformData=newFormData(form,formDataSubmitter);
656638

657639
// Queue for replaying later. This field could potentially be shared with multiple
658640
// Reacts on the same page since each one will preventDefault for the next one.

‎packages/react-dom/src/__tests__/ReactDOMForm-test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
1414
// Our current version of JSDOM doesn't implement the event dispatching
1515
// so we polyfill it.
1616
constNativeFormData=global.FormData;
17-
constFormDataPolyfill=functionFormData(form){
18-
constformData=newNativeFormData(form);
17+
constFormDataPolyfill=functionFormData(form,submitter){
18+
constformData=newNativeFormData(form,submitter);
1919
constformDataEvent=newEvent('formdata',{
2020
bubbles: true,
2121
cancelable: false,
@@ -489,11 +489,16 @@ describe('ReactDOMForm', () => {
489489
constinputRef=React.createRef();
490490
constbuttonRef=React.createRef();
491491
constoutsideButtonRef=React.createRef();
492+
constimageButtonRef=React.createRef();
492493
letbutton;
494+
letbuttonX;
495+
letbuttonY;
493496
lettitle;
494497

495498
functionaction(formData){
496499
button=formData.get('button');
500+
buttonX=formData.get('button.x');
501+
buttonY=formData.get('button.y');
497502
title=formData.get('title');
498503
}
499504

@@ -508,6 +513,12 @@ describe('ReactDOMForm', () => {
508513
<buttonname="button"value="edit"ref={buttonRef}>
509514
Edit
510515
</button>
516+
<input
517+
type="image"
518+
name="button"
519+
href="/some/image.png"
520+
ref={imageButtonRef}
521+
/>
511522
</form>
512523
<formid="form"action={action}>
513524
<inputtype="text"name="title"defaultValue="hello"/>
@@ -546,9 +557,12 @@ describe('ReactDOMForm', () => {
546557
expect(button).toBe('outside');
547558
expect(title).toBe('hello');
548559

549-
// Ensure that the type field got correctly restored
550-
expect(inputRef.current.getAttribute('type')).toBe('submit');
551-
expect(buttonRef.current.getAttribute('type')).toBe(null);
560+
awaitsubmit(imageButtonRef.current);
561+
562+
expect(button).toBe(null);
563+
expect(buttonX).toBe('0');
564+
expect(buttonY).toBe('0');
565+
expect(title).toBe('hello');
552566
});
553567

554568
it('excludes the submitter name when the submitter is a function action',async()=>{

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ describe('ReactFlightDOMForm', () => {
121121
constmethod=(submitter&&submitter.formMethod)||form.method;
122122
constencType=(submitter&&submitter.formEnctype)||form.enctype;
123123
if(method==='post'&&encType==='multipart/form-data'){
124-
letformData;
125-
if(submitter){
126-
consttemp=document.createElement('input');
127-
temp.name=submitter.name;
128-
temp.value=submitter.value;
129-
submitter.parentNode.insertBefore(temp,submitter);
130-
formData=newFormData(form);
131-
temp.parentNode.removeChild(temp);
132-
}else{
133-
formData=newFormData(form);
134-
}
124+
constformData=newFormData(form,submitter);
135125
returnPOST(formData);
136126
}
137127
thrownewError('Navigate to: '+action);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Use `FormData` `submitter` parameter (#29028) · react/react@65eec42 · GitHub
Skip to content

Commit 65eec42

Browse files
authored
Use FormDatasubmitter parameter (#29028)
1 parent 454fc41 commit 65eec42

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

‎packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ function coerceFormActionProp(
4545
}
4646
}
4747

48-
functioncreateFormDataWithSubmitter(
49-
form: HTMLFormElement,
50-
submitter: HTMLInputElement|HTMLButtonElement,
51-
){
52-
// The submitter's value should be included in the FormData.
53-
// It should be in the document order in the form.
54-
// Since the FormData constructor invokes the formdata event it also
55-
// needs to be available before that happens so after construction it's too
56-
// late. We use a temporary fake node for the duration of this event.
57-
// TODO: FormData takes a second argument that it's the submitter but this
58-
// is fairly new so not all browsers support it yet. Switch to that technique
59-
// when available.
60-
consttemp=submitter.ownerDocument.createElement('input');
61-
temp.name=submitter.name;
62-
temp.value=submitter.value;
63-
if(form.id){
64-
temp.setAttribute('form',form.id);
65-
}
66-
(submitter.parentNode: any).insertBefore(temp,submitter);
67-
constformData=newFormData(form);
68-
(temp.parentNode: any).removeChild(temp);
69-
returnformData;
70-
}
71-
7248
/**
7349
* This plugin invokes action functions on forms, inputs and buttons if
7450
* the form doesn't prevent default.
@@ -129,9 +105,7 @@ function extractEvents(
129105
if(didCurrentEventScheduleTransition()){
130106
// We're going to set the pending form status, but because the submission
131107
// was prevented, we should not fire the action function.
132-
constformData=submitter
133-
? createFormDataWithSubmitter(form,submitter)
134-
: newFormData(form);
108+
constformData=newFormData(form,submitter);
135109
constpendingState: FormStatus={
136110
pending: true,
137111
data: formData,
@@ -160,9 +134,7 @@ function extractEvents(
160134
event.preventDefault();
161135

162136
// Dispatch the action and set a pending form status.
163-
constformData=submitter
164-
? createFormDataWithSubmitter(form,submitter)
165-
: newFormData(form);
137+
constformData=newFormData(form,submitter);
166138
constpendingState: FormStatus={
167139
pending: true,
168140
data: formData,

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,7 @@ export function listenToFormSubmissionsForReplaying() {
634634
event.preventDefault();
635635

636636
// Take a snapshot of the FormData at the time of the event.
637-
letformData;
638-
if(formDataSubmitter){
639-
// The submitter's value should be included in the FormData.
640-
// It should be in the document order in the form.
641-
// Since the FormData constructor invokes the formdata event it also
642-
// needs to be available before that happens so after construction it's too
643-
// late. We use a temporary fake node for the duration of this event.
644-
// TODO: FormData takes a second argument that it's the submitter but this
645-
// is fairly new so not all browsers support it yet. Switch to that technique
646-
// when available.
647-
consttemp=document.createElement('input');
648-
temp.name=formDataSubmitter.name;
649-
temp.value=formDataSubmitter.value;
650-
formDataSubmitter.parentNode.insertBefore(temp,formDataSubmitter);
651-
formData=newFormData(form);
652-
temp.parentNode.removeChild(temp);
653-
}else{
654-
formData=newFormData(form);
655-
}
637+
constformData=newFormData(form,formDataSubmitter);
656638

657639
// Queue for replaying later. This field could potentially be shared with multiple
658640
// Reacts on the same page since each one will preventDefault for the next one.

‎packages/react-dom/src/__tests__/ReactDOMForm-test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
1414
// Our current version of JSDOM doesn't implement the event dispatching
1515
// so we polyfill it.
1616
constNativeFormData=global.FormData;
17-
constFormDataPolyfill=functionFormData(form){
18-
constformData=newNativeFormData(form);
17+
constFormDataPolyfill=functionFormData(form,submitter){
18+
constformData=newNativeFormData(form,submitter);
1919
constformDataEvent=newEvent('formdata',{
2020
bubbles: true,
2121
cancelable: false,
@@ -489,11 +489,16 @@ describe('ReactDOMForm', () => {
489489
constinputRef=React.createRef();
490490
constbuttonRef=React.createRef();
491491
constoutsideButtonRef=React.createRef();
492+
constimageButtonRef=React.createRef();
492493
letbutton;
494+
letbuttonX;
495+
letbuttonY;
493496
lettitle;
494497

495498
functionaction(formData){
496499
button=formData.get('button');
500+
buttonX=formData.get('button.x');
501+
buttonY=formData.get('button.y');
497502
title=formData.get('title');
498503
}
499504

@@ -508,6 +513,12 @@ describe('ReactDOMForm', () => {
508513
<buttonname="button"value="edit"ref={buttonRef}>
509514
Edit
510515
</button>
516+
<input
517+
type="image"
518+
name="button"
519+
href="/some/image.png"
520+
ref={imageButtonRef}
521+
/>
511522
</form>
512523
<formid="form"action={action}>
513524
<inputtype="text"name="title"defaultValue="hello"/>
@@ -546,9 +557,12 @@ describe('ReactDOMForm', () => {
546557
expect(button).toBe('outside');
547558
expect(title).toBe('hello');
548559

549-
// Ensure that the type field got correctly restored
550-
expect(inputRef.current.getAttribute('type')).toBe('submit');
551-
expect(buttonRef.current.getAttribute('type')).toBe(null);
560+
awaitsubmit(imageButtonRef.current);
561+
562+
expect(button).toBe(null);
563+
expect(buttonX).toBe('0');
564+
expect(buttonY).toBe('0');
565+
expect(title).toBe('hello');
552566
});
553567

554568
it('excludes the submitter name when the submitter is a function action',async()=>{

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ describe('ReactFlightDOMForm', () => {
121121
constmethod=(submitter&&submitter.formMethod)||form.method;
122122
constencType=(submitter&&submitter.formEnctype)||form.enctype;
123123
if(method==='post'&&encType==='multipart/form-data'){
124-
letformData;
125-
if(submitter){
126-
consttemp=document.createElement('input');
127-
temp.name=submitter.name;
128-
temp.value=submitter.value;
129-
submitter.parentNode.insertBefore(temp,submitter);
130-
formData=newFormData(form);
131-
temp.parentNode.removeChild(temp);
132-
}else{
133-
formData=newFormData(form);
134-
}
124+
constformData=newFormData(form,submitter);
135125
returnPOST(formData);
136126
}
137127
thrownewError('Navigate to: '+action);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Use `FormData` `submitter` parameter (#29028) · react/react@65eec42 · GitHub
Skip to content

Commit 65eec42

Browse files
authored
Use FormDatasubmitter parameter (#29028)
1 parent 454fc41 commit 65eec42

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

‎packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ function coerceFormActionProp(
4545
}
4646
}
4747

48-
functioncreateFormDataWithSubmitter(
49-
form: HTMLFormElement,
50-
submitter: HTMLInputElement|HTMLButtonElement,
51-
){
52-
// The submitter's value should be included in the FormData.
53-
// It should be in the document order in the form.
54-
// Since the FormData constructor invokes the formdata event it also
55-
// needs to be available before that happens so after construction it's too
56-
// late. We use a temporary fake node for the duration of this event.
57-
// TODO: FormData takes a second argument that it's the submitter but this
58-
// is fairly new so not all browsers support it yet. Switch to that technique
59-
// when available.
60-
consttemp=submitter.ownerDocument.createElement('input');
61-
temp.name=submitter.name;
62-
temp.value=submitter.value;
63-
if(form.id){
64-
temp.setAttribute('form',form.id);
65-
}
66-
(submitter.parentNode: any).insertBefore(temp,submitter);
67-
constformData=newFormData(form);
68-
(temp.parentNode: any).removeChild(temp);
69-
returnformData;
70-
}
71-
7248
/**
7349
* This plugin invokes action functions on forms, inputs and buttons if
7450
* the form doesn't prevent default.
@@ -129,9 +105,7 @@ function extractEvents(
129105
if(didCurrentEventScheduleTransition()){
130106
// We're going to set the pending form status, but because the submission
131107
// was prevented, we should not fire the action function.
132-
constformData=submitter
133-
? createFormDataWithSubmitter(form,submitter)
134-
: newFormData(form);
108+
constformData=newFormData(form,submitter);
135109
constpendingState: FormStatus={
136110
pending: true,
137111
data: formData,
@@ -160,9 +134,7 @@ function extractEvents(
160134
event.preventDefault();
161135

162136
// Dispatch the action and set a pending form status.
163-
constformData=submitter
164-
? createFormDataWithSubmitter(form,submitter)
165-
: newFormData(form);
137+
constformData=newFormData(form,submitter);
166138
constpendingState: FormStatus={
167139
pending: true,
168140
data: formData,

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,7 @@ export function listenToFormSubmissionsForReplaying() {
634634
event.preventDefault();
635635

636636
// Take a snapshot of the FormData at the time of the event.
637-
letformData;
638-
if(formDataSubmitter){
639-
// The submitter's value should be included in the FormData.
640-
// It should be in the document order in the form.
641-
// Since the FormData constructor invokes the formdata event it also
642-
// needs to be available before that happens so after construction it's too
643-
// late. We use a temporary fake node for the duration of this event.
644-
// TODO: FormData takes a second argument that it's the submitter but this
645-
// is fairly new so not all browsers support it yet. Switch to that technique
646-
// when available.
647-
consttemp=document.createElement('input');
648-
temp.name=formDataSubmitter.name;
649-
temp.value=formDataSubmitter.value;
650-
formDataSubmitter.parentNode.insertBefore(temp,formDataSubmitter);
651-
formData=newFormData(form);
652-
temp.parentNode.removeChild(temp);
653-
}else{
654-
formData=newFormData(form);
655-
}
637+
constformData=newFormData(form,formDataSubmitter);
656638

657639
// Queue for replaying later. This field could potentially be shared with multiple
658640
// Reacts on the same page since each one will preventDefault for the next one.

‎packages/react-dom/src/__tests__/ReactDOMForm-test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
1414
// Our current version of JSDOM doesn't implement the event dispatching
1515
// so we polyfill it.
1616
constNativeFormData=global.FormData;
17-
constFormDataPolyfill=functionFormData(form){
18-
constformData=newNativeFormData(form);
17+
constFormDataPolyfill=functionFormData(form,submitter){
18+
constformData=newNativeFormData(form,submitter);
1919
constformDataEvent=newEvent('formdata',{
2020
bubbles: true,
2121
cancelable: false,
@@ -489,11 +489,16 @@ describe('ReactDOMForm', () => {
489489
constinputRef=React.createRef();
490490
constbuttonRef=React.createRef();
491491
constoutsideButtonRef=React.createRef();
492+
constimageButtonRef=React.createRef();
492493
letbutton;
494+
letbuttonX;
495+
letbuttonY;
493496
lettitle;
494497

495498
functionaction(formData){
496499
button=formData.get('button');
500+
buttonX=formData.get('button.x');
501+
buttonY=formData.get('button.y');
497502
title=formData.get('title');
498503
}
499504

@@ -508,6 +513,12 @@ describe('ReactDOMForm', () => {
508513
<buttonname="button"value="edit"ref={buttonRef}>
509514
Edit
510515
</button>
516+
<input
517+
type="image"
518+
name="button"
519+
href="/some/image.png"
520+
ref={imageButtonRef}
521+
/>
511522
</form>
512523
<formid="form"action={action}>
513524
<inputtype="text"name="title"defaultValue="hello"/>
@@ -546,9 +557,12 @@ describe('ReactDOMForm', () => {
546557
expect(button).toBe('outside');
547558
expect(title).toBe('hello');
548559

549-
// Ensure that the type field got correctly restored
550-
expect(inputRef.current.getAttribute('type')).toBe('submit');
551-
expect(buttonRef.current.getAttribute('type')).toBe(null);
560+
awaitsubmit(imageButtonRef.current);
561+
562+
expect(button).toBe(null);
563+
expect(buttonX).toBe('0');
564+
expect(buttonY).toBe('0');
565+
expect(title).toBe('hello');
552566
});
553567

554568
it('excludes the submitter name when the submitter is a function action',async()=>{

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ describe('ReactFlightDOMForm', () => {
121121
constmethod=(submitter&&submitter.formMethod)||form.method;
122122
constencType=(submitter&&submitter.formEnctype)||form.enctype;
123123
if(method==='post'&&encType==='multipart/form-data'){
124-
letformData;
125-
if(submitter){
126-
consttemp=document.createElement('input');
127-
temp.name=submitter.name;
128-
temp.value=submitter.value;
129-
submitter.parentNode.insertBefore(temp,submitter);
130-
formData=newFormData(form);
131-
temp.parentNode.removeChild(temp);
132-
}else{
133-
formData=newFormData(form);
134-
}
124+
constformData=newFormData(form,submitter);
135125
returnPOST(formData);
136126
}
137127
thrownewError('Navigate to: '+action);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Use `FormData` `submitter` parameter (#29028) · react/react@65eec42 · GitHub
Skip to content

Commit 65eec42

Browse files
authored
Use FormDatasubmitter parameter (#29028)
1 parent 454fc41 commit 65eec42

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

‎packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ function coerceFormActionProp(
4545
}
4646
}
4747

48-
functioncreateFormDataWithSubmitter(
49-
form: HTMLFormElement,
50-
submitter: HTMLInputElement|HTMLButtonElement,
51-
){
52-
// The submitter's value should be included in the FormData.
53-
// It should be in the document order in the form.
54-
// Since the FormData constructor invokes the formdata event it also
55-
// needs to be available before that happens so after construction it's too
56-
// late. We use a temporary fake node for the duration of this event.
57-
// TODO: FormData takes a second argument that it's the submitter but this
58-
// is fairly new so not all browsers support it yet. Switch to that technique
59-
// when available.
60-
consttemp=submitter.ownerDocument.createElement('input');
61-
temp.name=submitter.name;
62-
temp.value=submitter.value;
63-
if(form.id){
64-
temp.setAttribute('form',form.id);
65-
}
66-
(submitter.parentNode: any).insertBefore(temp,submitter);
67-
constformData=newFormData(form);
68-
(temp.parentNode: any).removeChild(temp);
69-
returnformData;
70-
}
71-
7248
/**
7349
* This plugin invokes action functions on forms, inputs and buttons if
7450
* the form doesn't prevent default.
@@ -129,9 +105,7 @@ function extractEvents(
129105
if(didCurrentEventScheduleTransition()){
130106
// We're going to set the pending form status, but because the submission
131107
// was prevented, we should not fire the action function.
132-
constformData=submitter
133-
? createFormDataWithSubmitter(form,submitter)
134-
: newFormData(form);
108+
constformData=newFormData(form,submitter);
135109
constpendingState: FormStatus={
136110
pending: true,
137111
data: formData,
@@ -160,9 +134,7 @@ function extractEvents(
160134
event.preventDefault();
161135

162136
// Dispatch the action and set a pending form status.
163-
constformData=submitter
164-
? createFormDataWithSubmitter(form,submitter)
165-
: newFormData(form);
137+
constformData=newFormData(form,submitter);
166138
constpendingState: FormStatus={
167139
pending: true,
168140
data: formData,

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,7 @@ export function listenToFormSubmissionsForReplaying() {
634634
event.preventDefault();
635635

636636
// Take a snapshot of the FormData at the time of the event.
637-
letformData;
638-
if(formDataSubmitter){
639-
// The submitter's value should be included in the FormData.
640-
// It should be in the document order in the form.
641-
// Since the FormData constructor invokes the formdata event it also
642-
// needs to be available before that happens so after construction it's too
643-
// late. We use a temporary fake node for the duration of this event.
644-
// TODO: FormData takes a second argument that it's the submitter but this
645-
// is fairly new so not all browsers support it yet. Switch to that technique
646-
// when available.
647-
consttemp=document.createElement('input');
648-
temp.name=formDataSubmitter.name;
649-
temp.value=formDataSubmitter.value;
650-
formDataSubmitter.parentNode.insertBefore(temp,formDataSubmitter);
651-
formData=newFormData(form);
652-
temp.parentNode.removeChild(temp);
653-
}else{
654-
formData=newFormData(form);
655-
}
637+
constformData=newFormData(form,formDataSubmitter);
656638

657639
// Queue for replaying later. This field could potentially be shared with multiple
658640
// Reacts on the same page since each one will preventDefault for the next one.

‎packages/react-dom/src/__tests__/ReactDOMForm-test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
1414
// Our current version of JSDOM doesn't implement the event dispatching
1515
// so we polyfill it.
1616
constNativeFormData=global.FormData;
17-
constFormDataPolyfill=functionFormData(form){
18-
constformData=newNativeFormData(form);
17+
constFormDataPolyfill=functionFormData(form,submitter){
18+
constformData=newNativeFormData(form,submitter);
1919
constformDataEvent=newEvent('formdata',{
2020
bubbles: true,
2121
cancelable: false,
@@ -489,11 +489,16 @@ describe('ReactDOMForm', () => {
489489
constinputRef=React.createRef();
490490
constbuttonRef=React.createRef();
491491
constoutsideButtonRef=React.createRef();
492+
constimageButtonRef=React.createRef();
492493
letbutton;
494+
letbuttonX;
495+
letbuttonY;
493496
lettitle;
494497

495498
functionaction(formData){
496499
button=formData.get('button');
500+
buttonX=formData.get('button.x');
501+
buttonY=formData.get('button.y');
497502
title=formData.get('title');
498503
}
499504

@@ -508,6 +513,12 @@ describe('ReactDOMForm', () => {
508513
<buttonname="button"value="edit"ref={buttonRef}>
509514
Edit
510515
</button>
516+
<input
517+
type="image"
518+
name="button"
519+
href="/some/image.png"
520+
ref={imageButtonRef}
521+
/>
511522
</form>
512523
<formid="form"action={action}>
513524
<inputtype="text"name="title"defaultValue="hello"/>
@@ -546,9 +557,12 @@ describe('ReactDOMForm', () => {
546557
expect(button).toBe('outside');
547558
expect(title).toBe('hello');
548559

549-
// Ensure that the type field got correctly restored
550-
expect(inputRef.current.getAttribute('type')).toBe('submit');
551-
expect(buttonRef.current.getAttribute('type')).toBe(null);
560+
awaitsubmit(imageButtonRef.current);
561+
562+
expect(button).toBe(null);
563+
expect(buttonX).toBe('0');
564+
expect(buttonY).toBe('0');
565+
expect(title).toBe('hello');
552566
});
553567

554568
it('excludes the submitter name when the submitter is a function action',async()=>{

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ describe('ReactFlightDOMForm', () => {
121121
constmethod=(submitter&&submitter.formMethod)||form.method;
122122
constencType=(submitter&&submitter.formEnctype)||form.enctype;
123123
if(method==='post'&&encType==='multipart/form-data'){
124-
letformData;
125-
if(submitter){
126-
consttemp=document.createElement('input');
127-
temp.name=submitter.name;
128-
temp.value=submitter.value;
129-
submitter.parentNode.insertBefore(temp,submitter);
130-
formData=newFormData(form);
131-
temp.parentNode.removeChild(temp);
132-
}else{
133-
formData=newFormData(form);
134-
}
124+
constformData=newFormData(form,submitter);
135125
returnPOST(formData);
136126
}
137127
thrownewError('Navigate to: '+action);

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Use `FormData` `submitter` parameter (#29028) · react/react@65eec42 · GitHub
Skip to content

Commit 65eec42

Browse files
authored
Use FormDatasubmitter parameter (#29028)
1 parent 454fc41 commit 65eec42

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

‎packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ function coerceFormActionProp(
4545
}
4646
}
4747

48-
functioncreateFormDataWithSubmitter(
49-
form: HTMLFormElement,
50-
submitter: HTMLInputElement|HTMLButtonElement,
51-
){
52-
// The submitter's value should be included in the FormData.
53-
// It should be in the document order in the form.
54-
// Since the FormData constructor invokes the formdata event it also
55-
// needs to be available before that happens so after construction it's too
56-
// late. We use a temporary fake node for the duration of this event.
57-
// TODO: FormData takes a second argument that it's the submitter but this
58-
// is fairly new so not all browsers support it yet. Switch to that technique
59-
// when available.
60-
consttemp=submitter.ownerDocument.createElement('input');
61-
temp.name=submitter.name;
62-
temp.value=submitter.value;
63-
if(form.id){
64-
temp.setAttribute('form',form.id);
65-
}
66-
(submitter.parentNode: any).insertBefore(temp,submitter);
67-
constformData=newFormData(form);
68-
(temp.parentNode: any).removeChild(temp);
69-
returnformData;
70-
}
71-
7248
/**
7349
* This plugin invokes action functions on forms, inputs and buttons if
7450
* the form doesn't prevent default.
@@ -129,9 +105,7 @@ function extractEvents(
129105
if(didCurrentEventScheduleTransition()){
130106
// We're going to set the pending form status, but because the submission
131107
// was prevented, we should not fire the action function.
132-
constformData=submitter
133-
? createFormDataWithSubmitter(form,submitter)
134-
: newFormData(form);
108+
constformData=newFormData(form,submitter);
135109
constpendingState: FormStatus={
136110
pending: true,
137111
data: formData,
@@ -160,9 +134,7 @@ function extractEvents(
160134
event.preventDefault();
161135

162136
// Dispatch the action and set a pending form status.
163-
constformData=submitter
164-
? createFormDataWithSubmitter(form,submitter)
165-
: newFormData(form);
137+
constformData=newFormData(form,submitter);
166138
constpendingState: FormStatus={
167139
pending: true,
168140
data: formData,

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,25 +634,7 @@ export function listenToFormSubmissionsForReplaying() {
634634
event.preventDefault();
635635

636636
// Take a snapshot of the FormData at the time of the event.
637-
letformData;
638-
if(formDataSubmitter){
639-
// The submitter's value should be included in the FormData.
640-
// It should be in the document order in the form.
641-
// Since the FormData constructor invokes the formdata event it also
642-
// needs to be available before that happens so after construction it's too
643-
// late. We use a temporary fake node for the duration of this event.
644-
// TODO: FormData takes a second argument that it's the submitter but this
645-
// is fairly new so not all browsers support it yet. Switch to that technique
646-
// when available.
647-
consttemp=document.createElement('input');
648-
temp.name=formDataSubmitter.name;
649-
temp.value=formDataSubmitter.value;
650-
formDataSubmitter.parentNode.insertBefore(temp,formDataSubmitter);
651-
formData=newFormData(form);
652-
temp.parentNode.removeChild(temp);
653-
}else{
654-
formData=newFormData(form);
655-
}
637+
constformData=newFormData(form,formDataSubmitter);
656638

657639
// Queue for replaying later. This field could potentially be shared with multiple
658640
// Reacts on the same page since each one will preventDefault for the next one.

‎packages/react-dom/src/__tests__/ReactDOMForm-test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
1414
// Our current version of JSDOM doesn't implement the event dispatching
1515
// so we polyfill it.
1616
constNativeFormData=global.FormData;
17-
constFormDataPolyfill=functionFormData(form){
18-
constformData=newNativeFormData(form);
17+
constFormDataPolyfill=functionFormData(form,submitter){
18+
constformData=newNativeFormData(form,submitter);
1919
constformDataEvent=newEvent('formdata',{
2020
bubbles: true,
2121
cancelable: false,
@@ -489,11 +489,16 @@ describe('ReactDOMForm', () => {
489489
constinputRef=React.createRef();
490490
constbuttonRef=React.createRef();
491491
constoutsideButtonRef=React.createRef();
492+
constimageButtonRef=React.createRef();
492493
letbutton;
494+
letbuttonX;
495+
letbuttonY;
493496
lettitle;
494497

495498
functionaction(formData){
496499
button=formData.get('button');
500+
buttonX=formData.get('button.x');
501+
buttonY=formData.get('button.y');
497502
title=formData.get('title');
498503
}
499504

@@ -508,6 +513,12 @@ describe('ReactDOMForm', () => {
508513
<buttonname="button"value="edit"ref={buttonRef}>
509514
Edit
510515
</button>
516+
<input
517+
type="image"
518+
name="button"
519+
href="/some/image.png"
520+
ref={imageButtonRef}
521+
/>
511522
</form>
512523
<formid="form"action={action}>
513524
<inputtype="text"name="title"defaultValue="hello"/>
@@ -546,9 +557,12 @@ describe('ReactDOMForm', () => {
546557
expect(button).toBe('outside');
547558
expect(title).toBe('hello');
548559

549-
// Ensure that the type field got correctly restored
550-
expect(inputRef.current.getAttribute('type')).toBe('submit');
551-
expect(buttonRef.current.getAttribute('type')).toBe(null);
560+
awaitsubmit(imageButtonRef.current);
561+
562+
expect(button).toBe(null);
563+
expect(buttonX).toBe('0');
564+
expect(buttonY).toBe('0');
565+
expect(title).toBe('hello');
552566
});
553567

554568
it('excludes the submitter name when the submitter is a function action',async()=>{

‎packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMForm-test.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ describe('ReactFlightDOMForm', () => {
121121
constmethod=(submitter&&submitter.formMethod)||form.method;
122122
constencType=(submitter&&submitter.formEnctype)||form.enctype;
123123
if(method==='post'&&encType==='multipart/form-data'){
124-
letformData;
125-
if(submitter){
126-
consttemp=document.createElement('input');
127-
temp.name=submitter.name;
128-
temp.value=submitter.value;
129-
submitter.parentNode.insertBefore(temp,submitter);
130-
formData=newFormData(form);
131-
temp.parentNode.removeChild(temp);
132-
}else{
133-
formData=newFormData(form);
134-
}
124+
constformData=newFormData(form,submitter);
135125
returnPOST(formData);
136126
}
137127
thrownewError('Navigate to: '+action);

0 commit comments

Comments
 (0)