From fdbcdfd0430a04862649df20e3de2ff014768715 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 2 Aug 2022 17:46:57 +0200 Subject: [PATCH 1/2] fix(utils): explicitly exclude file type from text inputs --- app/javascript/shared/utils.test.ts | 84 ++++++++++++++++++++++++++++- app/javascript/shared/utils.ts | 4 +- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/app/javascript/shared/utils.test.ts b/app/javascript/shared/utils.test.ts index 5fa6c250e..4aaa569d5 100644 --- a/app/javascript/shared/utils.test.ts +++ b/app/javascript/shared/utils.test.ts @@ -1,6 +1,13 @@ import { suite, test, expect } from 'vitest'; -import { show, hide, toggle } from './utils'; +import { + show, + hide, + toggle, + isSelectElement, + isTextInputElement, + isCheckboxOrRadioInputElement +} from './utils'; suite('@utils', () => { test('show', () => { @@ -26,4 +33,79 @@ suite('@utils', () => { toggle(input); expect(input.classList.contains('hidden')).toBeFalsy(); }); + + test('isSelectElement', () => { + const select = document.createElement('select'); + const input = document.createElement('input'); + const textarea = document.createElement('textarea'); + + expect(isSelectElement(select)).toBeTruthy(); + expect(isSelectElement(input)).toBeFalsy(); + expect(isSelectElement(textarea)).toBeFalsy(); + + input.type = 'text'; + expect(isSelectElement(input)).toBeFalsy(); + + input.type = 'email'; + expect(isSelectElement(input)).toBeFalsy(); + + input.type = 'checkbox'; + expect(isSelectElement(input)).toBeFalsy(); + + input.type = 'radio'; + expect(isSelectElement(input)).toBeFalsy(); + + input.type = 'file'; + expect(isSelectElement(input)).toBeFalsy(); + }); + + test('isTextInputElement', () => { + const select = document.createElement('select'); + const input = document.createElement('input'); + const textarea = document.createElement('textarea'); + + expect(isTextInputElement(select)).toBeFalsy(); + expect(isTextInputElement(input)).toBeTruthy(); + expect(isTextInputElement(textarea)).toBeTruthy(); + + input.type = 'text'; + expect(isTextInputElement(input)).toBeTruthy(); + + input.type = 'email'; + expect(isTextInputElement(input)).toBeTruthy(); + + input.type = 'checkbox'; + expect(isTextInputElement(input)).toBeFalsy(); + + input.type = 'radio'; + expect(isTextInputElement(input)).toBeFalsy(); + + input.type = 'file'; + expect(isTextInputElement(input)).toBeFalsy(); + }); + + test('isCheckboxOrRadioInputElement', () => { + const select = document.createElement('select'); + const input = document.createElement('input'); + const textarea = document.createElement('textarea'); + + expect(isCheckboxOrRadioInputElement(select)).toBeFalsy(); + expect(isCheckboxOrRadioInputElement(input)).toBeFalsy(); + expect(isCheckboxOrRadioInputElement(textarea)).toBeFalsy(); + + input.type = 'text'; + expect(isCheckboxOrRadioInputElement(input)).toBeFalsy(); + + input.type = 'email'; + expect(isCheckboxOrRadioInputElement(input)).toBeFalsy(); + + input.type = 'checkbox'; + expect(isCheckboxOrRadioInputElement(input)).toBeTruthy(); + + input.type = 'radio'; + expect(isCheckboxOrRadioInputElement(input)).toBeTruthy(); + + input.type = 'file'; + expect(isCheckboxOrRadioInputElement(input)).toBeFalsy(); + }); }); diff --git a/app/javascript/shared/utils.ts b/app/javascript/shared/utils.ts index 4b8b0dc66..108280557 100644 --- a/app/javascript/shared/utils.ts +++ b/app/javascript/shared/utils.ts @@ -297,7 +297,7 @@ export function isTextInputElement( ): element is HTMLInputElement { return ( ['INPUT', 'TEXTAREA'].includes(element.tagName) && - element.type != 'checkbox' && - element.type != 'radio' + typeof element.type == 'string' && + !['checkbox', 'radio', 'file'].includes(element.type) ); } From cf315b7246e24134e659cb913f7a853155febb87 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 2 Aug 2022 17:47:26 +0200 Subject: [PATCH 2/2] fix(autosave): on check condition requests do not send file inputs --- app/javascript/controllers/check_conditions_controller.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/javascript/controllers/check_conditions_controller.ts b/app/javascript/controllers/check_conditions_controller.ts index d961dcc02..f99e99947 100644 --- a/app/javascript/controllers/check_conditions_controller.ts +++ b/app/javascript/controllers/check_conditions_controller.ts @@ -70,7 +70,14 @@ export class CheckConditionsController extends ApplicationController { return Promise.resolve(); } + const fileInputs = form.querySelectorAll('input[type="file"]'); + for (const input of fileInputs) { + input.setAttribute('disabled', 'disabled'); + } const formData = new FormData(form); + for (const input of fileInputs) { + input.removeAttribute('disabled'); + } formData.set('check_conditions', 'true'); return httpRequest(form.action, {