Skip to content

Commit

Permalink
refactor: simply value storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dbajpeyi committed Dec 6, 2024
1 parent 1098e1b commit 89b52a8
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 142 deletions.
50 changes: 25 additions & 25 deletions dist/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions dist/autofill.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions src/DeviceInterface/InterfacePrototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,23 +799,17 @@ class InterfacePrototype {
postSubmit(values, form) {
if (!form.form) return;
if (!form.hasValues(values)) return;
const checks = [form.shouldPromptToStoreData && !form.submitHandlerExecuted, this.passwordGenerator.generated];

const isUsernameOnly = Object.keys(values?.credentials ?? {}).length === 1 && values?.credentials?.username;
const checks = [form.shouldPromptToStoreData && !form.submitHandlerExecuted, this.passwordGenerator.generated, isUsernameOnly];
if (checks.some(Boolean)) {
const formData = appendGeneratedKey(values, {
password: this.passwordGenerator.password,
username: this.emailProtection.lastGenerated,
});

// If credentials has only username field, and no password field, then trigger is a partialSave
const isUsernameOnly =
Boolean(formData.credentials?.username) && !formData.credentials?.password && form.inputs.credentials.size === 1;
// Is an email or phone number present in the form, but no other credentials
const isEmailOrPhoneOnly =
Boolean(formData.identities?.emailAddress) !== Boolean(formData.identities?.phone) &&
form.inputs.credentials.size === 0 &&
form.inputs.identities.size === 1;
const trigger = isUsernameOnly || isEmailOrPhoneOnly ? 'partialSave' : 'formSubmission';
const trigger = isUsernameOnly ? 'partialSave' : 'formSubmission';
this.storeFormData(formData, trigger);
}
}
Expand Down
17 changes: 2 additions & 15 deletions src/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -895,24 +895,11 @@ class Form {

// After autofill we check if form values match the data provided…
const formValues = this.getValuesReadyForStorage();
const hasNoCredentialsData = !formValues.credentials?.username && !formValues.credentials?.password;
const hasOnlyEmail =
formValues.identities && Object.keys(formValues.identities ?? {}).length === 1 && formValues.identities?.emailAddress;

const hasOnlyOneCredentialOrEmail =
Boolean(formValues.credentials?.username) !== Boolean(formValues.credentials?.password) ||
(hasOnlyEmail && hasNoCredentialsData);
const areAllFormValuesKnown = Object.keys(formValues[dataType] || {}).every(
(subtype) => formValues[dataType][subtype] === data[subtype],
);

// If we only have a single credential field - then we want to prompt a partial save with username,
// So that in multi step forms (like reset-password), we can identify which username was picked, or complete a password save.
if (hasOnlyOneCredentialOrEmail) {
this.shouldPromptToStoreData = true;
this.shouldAutoSubmit = this.device.globalConfig.isMobileApp;
} else if (areAllFormValuesKnown) {
// …if it's a normal form with more than one field and if we know all the values do not prompt to store data
if (areAllFormValuesKnown) {
// …if we know all the values do not prompt to store
this.shouldPromptToStoreData = false;
// reset this to its initial value
this.shouldAutoSubmit = this.device.globalConfig.isMobileApp;
Expand Down
6 changes: 1 addition & 5 deletions src/Form/Form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,7 @@ describe('Test the form class reading values correctly', () => {
</form>`,
expHasValues: true,
expValues: {
identities: {
emailAddress: '[email protected]',
firstName: 'Peppa',
lastName: 'Pig',
},
identities: undefined,
creditCards: {
cardName: 'Peppa Pig',
cardSecurityCode: '123',
Expand Down
22 changes: 9 additions & 13 deletions src/Form/formatters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { matchInPlaceholderAndLabels, checkPlaceholderAndLabels } from './matching.js';
import { COUNTRY_CODES_TO_NAMES, COUNTRY_NAMES_TO_CODES } from './countryNames.js';
import { hasUsernameLikeIdentity } from '../autofill-utils.js';

// Matches strings like mm/yy, mm-yyyy, mm-aa, 12 / 2024
const DATE_SEPARATOR_REGEX = /\b((.)\2{1,3}|\d+)(?<separator>\s?[/\s.\-_—–]\s?)((.)\5{1,3}|\d+)\b/i;
Expand Down Expand Up @@ -163,13 +164,8 @@ const getMMAndYYYYFromString = (expiration) => {
* @param {InternalDataStorageObject} credentials
* @return {boolean}
*/
const shouldStoreIdentities = ({ identities }) => {
return (
Boolean((identities.firstName || identities.fullName) && identities.addressStreet && identities.addressCity) ||
Boolean(identities.emailAddress) ||
Boolean(identities.phone)
);
};
const shouldStoreIdentities = ({ identities }) =>
Boolean((identities.firstName || identities.fullName) && identities.addressStreet && identities.addressCity);

/**
* @param {InternalDataStorageObject} credentials
Expand Down Expand Up @@ -207,12 +203,12 @@ const prepareFormValuesForStorage = (formValues) => {
}

/** Fixes for credentials */
if (credentials.username || credentials.password) {
// If we don't have a username to match a password, let's see if the email is available
if (credentials.password && !credentials.username && identities.emailAddress) {
credentials.username = identities.emailAddress;
}
} else {
if (!credentials.username && hasUsernameLikeIdentity(identities)) {
// @ts-ignore - We know that username is not a useful value here
credentials.username = identities.emailAddress ?? identities.phone;
}

if (Object.keys(credentials ?? {}).length === 0) {
credentials = undefined;
}

Expand Down
10 changes: 10 additions & 0 deletions src/autofill-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,15 @@ function queryElementsWithShadow(element, selector, forceScanShadowTree = false)
return [...elements];
}

/**
*
* @param {InternalIdentityObject} identities
* @returns {boolean}
*/
function hasUsernameLikeIdentity(identities) {
return Object.keys(identities ?? {}).length === 1 && Boolean(identities?.emailAddress || identities.phone);
}

export {
notifyWebApp,
sendAndWaitForAnswer,
Expand Down Expand Up @@ -659,4 +668,5 @@ export {
findElementsInShadowTree,
queryElementsWithShadow,
getFormControlElements,
hasUsernameLikeIdentity,
};
Loading

0 comments on commit 89b52a8

Please sign in to comment.