فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(a11y): associate only unambiguous modal field labels
davoodmehraban89 committed
Oct 31, 2026 at 00:16 UTC
ff24b64093dc5eba95edd03b87ae0a3d7d9ea37e
1 file changed
+25
src/core/Modal.js
+25
@@ -59,6 +59,30 @@ class ModalManager {
59
}
60
}
61
62
+ // Pair an existing visual label with its field only when the association is
63
+ // unambiguous. Multi-field groups and already-labelled controls are untouched.
64
+ _associateFormLabels(card) {
65
+ for (const group of card.querySelectorAll('.form-group')) {
66
+ const fields = [...group.querySelectorAll('input, select, textarea')]
67
+ .filter(field => field.closest('.form-group') === group && field.type !== 'hidden');
68
+ if (fields.length !== 1) continue;
69
+ const field = fields[0];
70
+ if (field.hasAttribute('aria-label') || field.hasAttribute('aria-labelledby') || field.labels?.length) continue;
71
+ const labels = [...group.querySelectorAll('label.form-label')]
72
+ .filter(label => label.closest('.form-group') === group &&
73
+ !label.hasAttribute('for') && !label.querySelector('input, select, textarea'));
74
+ if (labels.length !== 1 || !labels[0].textContent.trim()) continue;
75
+ if (field.id) {
76
+ if (card.querySelectorAll('[id]').length &&
77
+ [...card.querySelectorAll('[id]')].filter(el => el.id === field.id).length !== 1) continue;
78
+ if (document.getElementById(field.id)) continue;
79
+ } else {
80
+ field.id = 'finora-modal-field-' + (++this.sequence);
81
+ }
82
+ labels[0].setAttribute('for', field.id);
83
+ }
84
+ }
85
+
86
_create(options) {
87
const { title, body, footer, size = 'md', onClose, closeOnBackdrop = true } = options;
88
const backdrop = document.createElement('div');
@@ -103,6 +127,7 @@ class ModalManager {
127
}
128
129
card.querySelector('.modal-close').onclick = () => this.close();
130
+ this._associateFormLabels(card);
131
backdrop.appendChild(card);
132
return { backdrop, card, onClose, previousFocus: document.activeElement };
133
}