mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 07:13:32 +00:00
support regex in custom field names
This commit is contained in:
@@ -701,21 +701,42 @@ export default class AutofillService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private findMatchingFieldIndex(field: AutofillField, names: string[]): number {
|
private findMatchingFieldIndex(field: AutofillField, names: string[]): number {
|
||||||
let matchingIndex = -1;
|
for (let i = 0; i < names.length; i++) {
|
||||||
if (field.htmlID != null && field.htmlID !== '') {
|
if (this.fieldPropertyIsMatch(field, 'htmlID', names[i])) {
|
||||||
matchingIndex = names.indexOf(field.htmlID.toLowerCase());
|
return i;
|
||||||
}
|
}
|
||||||
if (matchingIndex < 0 && field.htmlName != null && field.htmlName !== '') {
|
if (this.fieldPropertyIsMatch(field, 'htmlName', names[i])) {
|
||||||
matchingIndex = names.indexOf(field.htmlName.toLowerCase());
|
return i;
|
||||||
}
|
}
|
||||||
if (matchingIndex < 0 && field['label-tag'] != null && field['label-tag'] !== '') {
|
if (this.fieldPropertyIsMatch(field, 'label-tag', names[i])) {
|
||||||
matchingIndex = names.indexOf(field['label-tag'].replace(/(?:\r\n|\r|\n)/g, '').trim().toLowerCase());
|
return i;
|
||||||
}
|
}
|
||||||
if (matchingIndex < 0 && field.placeholder != null && field.placeholder !== '') {
|
if (this.fieldPropertyIsMatch(field, 'placeholder', names[i])) {
|
||||||
matchingIndex = names.indexOf(field.placeholder.toLowerCase());
|
return i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return matchingIndex;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private fieldPropertyIsMatch(field: any, property: string, name: string): boolean {
|
||||||
|
let fieldVal = field[property] as string;
|
||||||
|
if (fieldVal == null || fieldVal === '') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldVal = fieldVal.replace(/(?:\r\n|\r|\n)/g, '');
|
||||||
|
if (name.startsWith('regex:')) {
|
||||||
|
try {
|
||||||
|
const regexParts = name.split(':', 2);
|
||||||
|
if (regexParts.length === 2) {
|
||||||
|
const regex = new RegExp(regexParts[1], 'i');
|
||||||
|
return regex.test(fieldVal);
|
||||||
|
}
|
||||||
|
} catch (e) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
return fieldVal.toLowerCase() === name;
|
||||||
}
|
}
|
||||||
|
|
||||||
private fieldIsFuzzyMatch(field: AutofillField, names: string[]): boolean {
|
private fieldIsFuzzyMatch(field: AutofillField, names: string[]): boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user