Skip to content

Fix form property assignment edge case #35073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion web_src/js/features/common-button.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import {assignElementProperty} from './common-button.ts';
import {assignElementProperty, type ElementWithAssignableProperties} from './common-button.ts';

test('assignElementProperty', () => {
const elForm = document.createElement('form');
assignElementProperty(elForm, 'action', '/test-link');
expect(elForm.action).contains('/test-link'); // the DOM always returns absolute URL
expect(elForm.getAttribute('action')).eq('/test-link');
assignElementProperty(elForm, 'text-content', 'dummy');
expect(elForm.textContent).toBe('dummy');

// mock a form with its property "action" overwritten by an input element
const elFormWithAction = new class implements ElementWithAssignableProperties {
action = document.createElement('input'); // now "form.action" is not string, but an input element
_attrs: Record<string, string> = {};
setAttribute(name: string, value: string) { this._attrs[name] = value }
getAttribute(name: string): string | null { return this._attrs[name] }
}();
assignElementProperty(elFormWithAction, 'action', '/bar');
expect(elFormWithAction.getAttribute('action')).eq('/bar');

const elInput = document.createElement('input');
expect(elInput.readOnly).toBe(false);
assignElementProperty(elInput, 'read-only', 'true');
Expand Down
22 changes: 15 additions & 7 deletions web_src/js/features/common-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,26 @@ function onHidePanelClick(el: HTMLElement, e: MouseEvent) {
throw new Error('no panel to hide'); // should never happen, otherwise there is a bug in code
}

export function assignElementProperty(el: any, name: string, val: string) {
name = camelize(name);
const old = el[name];
export type ElementWithAssignableProperties = {
getAttribute: (name: string) => string | null;
setAttribute: (name: string, value: string) => void;
} & Record<string, any>

export function assignElementProperty(el: ElementWithAssignableProperties, kebabName: string, val: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not Element or HTMLElement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it needs "any":

Image

Copy link
Member

@silverwind silverwind Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't keyof typeof Element work for the property access?

  const camelizedName: keyof typeof Element = camelize(kebabName);

Copy link
Member

@silverwind silverwind Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not, use (el as any)[camelizedName] = ... to assign. Better do dirty stuff within a function than to expose any to consumers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a clearer solution.

Feel free to edit this PR directly with your solution.

const camelizedName = camelize(kebabName);
const old = el[camelizedName];
if (typeof old === 'boolean') {
el[name] = val === 'true';
el[camelizedName] = val === 'true';
} else if (typeof old === 'number') {
el[name] = parseFloat(val);
el[camelizedName] = parseFloat(val);
} else if (typeof old === 'string') {
el[name] = val;
el[camelizedName] = val;
} else if (old?.nodeName) {
// "form" has an edge case: its "<input name=action>" element overwrites the "action" property, we can only set attribute
el.setAttribute(kebabName, val);
} else {
// in the future, we could introduce a better typing system like `data-modal-form.action:string="..."`
throw new Error(`cannot assign element property ${name} by value ${val}`);
throw new Error(`cannot assign element property "${camelizedName}" by value "${val}"`);
}
}

Expand Down