Bug 1401411 - All name related fields should be counted as 1 field only when creating the records. r=lchang

MozReview-Commit-ID: 468B9tlFH3p

--HG--
extra : rebase_source : 87b01de50367e5fa432e1178e632b070ae1753a8
This commit is contained in:
Sean Lee 2017-09-25 17:14:48 +08:00
parent 4658732fb4
commit 06f93d18d1
2 changed files with 142 additions and 27 deletions

View File

@ -516,6 +516,26 @@ FormAutofillHandler.prototype = {
fieldDetail.state = nextState;
},
_isAddressRecordCreatable(record) {
let hasName = 0;
let length = 0;
for (let key of Object.keys(record)) {
if (!record[key]) {
continue;
}
if (FormAutofillUtils.getCategoryFromFieldName(key) == "name") {
hasName = 1;
continue;
}
length++;
}
return (length + hasName) >= FormAutofillUtils.AUTOFILL_FIELDS_THRESHOLD;
},
_isCreditCardRecordCreatable(record) {
return record["cc-number"] && FormAutofillUtils.isCCNumber(record["cc-number"]);
},
/**
* Return the records that is converted from address/creditCard fieldDetails and
* only valid form records are included.
@ -581,17 +601,14 @@ FormAutofillHandler.prototype = {
this._normalizeAddress(data.address);
if (data.address &&
Object.values(data.address.record).filter(v => v).length <
FormAutofillUtils.AUTOFILL_FIELDS_THRESHOLD) {
if (data.address && !this._isAddressRecordCreatable(data.address.record)) {
log.debug("No address record saving since there are only",
Object.keys(data.address.record).length,
"usable fields");
delete data.address;
}
if (data.creditCard && (!data.creditCard.record["cc-number"] ||
!FormAutofillUtils.isCCNumber(data.creditCard.record["cc-number"]))) {
if (data.creditCard && !this._isCreditCardRecordCreatable(data.creditCard.record)) {
log.debug("No credit card record saving since card number is invalid");
delete data.creditCard;
}

View File

@ -26,19 +26,19 @@ const TESTCASES = [
description: "\"country\" using @autocomplete shouldn't be identified aggressively",
document: `<form>
<input id="given-name" autocomplete="given-name">
<input id="family-name" autocomplete="family-name">
<input id="organization" autocomplete="organization">
<input id="country" autocomplete="country">
</form>`,
formValue: {
"given-name": "John",
"family-name": "Doe",
country: "United States",
"organization": "Mozilla",
"country": "United States",
},
expectedRecord: {
address: {
"given-name": "John",
"family-name": "Doe",
country: "United States",
"organization": "Mozilla",
"country": "United States",
},
},
},
@ -46,19 +46,19 @@ const TESTCASES = [
description: "\"country\" using heuristics should be identified aggressively",
document: `<form>
<input id="given-name" autocomplete="given-name">
<input id="family-name" autocomplete="family-name">
<input id="organization" autocomplete="organization">
<input id="country" name="country">
</form>`,
formValue: {
"given-name": "John",
"family-name": "Doe",
country: "United States",
"organization": "Mozilla",
"country": "United States",
},
expectedRecord: {
address: {
"given-name": "John",
"family-name": "Doe",
country: "US",
"organization": "Mozilla",
"country": "US",
},
},
},
@ -66,20 +66,20 @@ const TESTCASES = [
description: "\"tel\" related fields should be concatenated",
document: `<form>
<input id="given-name" autocomplete="given-name">
<input id="family-name" autocomplete="family-name">
<input id="organization" autocomplete="organization">
<input id="tel-country-code" autocomplete="tel-country-code">
<input id="tel-national" autocomplete="tel-national">
</form>`,
formValue: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
"tel-country-code": "+1",
"tel-national": "1234567890",
},
expectedRecord: {
address: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
"tel": "+11234567890",
},
},
@ -88,21 +88,21 @@ const TESTCASES = [
description: "\"tel\" should be removed if it's too short",
document: `<form>
<input id="given-name" autocomplete="given-name">
<input id="family-name" autocomplete="family-name">
<input id="organization" autocomplete="organization">
<input id="country" autocomplete="country">
<input id="tel" autocomplete="tel-national">
</form>`,
formValue: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
"country": "United States",
"tel": "1234",
},
expectedRecord: {
address: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
"country": "United States",
"tel": "",
},
},
@ -111,21 +111,21 @@ const TESTCASES = [
description: "\"tel\" should be removed if it's too long",
document: `<form>
<input id="given-name" autocomplete="given-name">
<input id="family-name" autocomplete="family-name">
<input id="organization" autocomplete="organization">
<input id="country" autocomplete="country">
<input id="tel" autocomplete="tel-national">
</form>`,
formValue: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
"country": "United States",
"tel": "1234567890123456",
},
expectedRecord: {
address: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
"country": "United States",
"tel": "",
},
},
@ -134,25 +134,123 @@ const TESTCASES = [
description: "\"tel\" should be removed if it contains invalid characters",
document: `<form>
<input id="given-name" autocomplete="given-name">
<input id="family-name" autocomplete="family-name">
<input id="organization" autocomplete="organization">
<input id="country" autocomplete="country">
<input id="tel" autocomplete="tel-national">
</form>`,
formValue: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
"country": "United States",
"tel": "12345###!!!",
},
expectedRecord: {
address: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
"country": "United States",
"tel": "",
},
},
},
{
description: "All name related fields should be counted as 1 field only.",
document: `<form>
<input id="given-name" autocomplete="given-name">
<input id="family-name" autocomplete="family-name">
<input id="organization" autocomplete="organization">
</form>`,
formValue: {
"given-name": "John",
"family-name": "Doe",
"organization": "Mozilla",
},
expectedRecord: {
address: undefined,
},
},
{
description: "All telephone related fields should be counted as 1 field only.",
document: `<form>
<input id="tel-country-code" autocomplete="tel-country-code">
<input id="tel-area-code" autocomplete="tel-area-code">
<input id="tel-local" autocomplete="tel-local">
<input id="organization" autocomplete="organization">
</form>`,
formValue: {
"tel-country-code": "+1",
"tel-area-code": "123",
"tel-local": "4567890",
"organization": "Mozilla",
},
expectedRecord: {
address: undefined,
},
},
{
description: "A credit card form with the value of cc-number, cc-exp, and cc-name.",
document: `<form>
<input id="cc-number" autocomplete="cc-number">
<input id="cc-name" autocomplete="cc-name">
<input id="cc-exp" autocomplete="cc-exp">
</form>`,
formValue: {
"cc-number": "4444000022220000",
"cc-name": "Foo Bar",
"cc-exp": "2022-06",
},
expectedRecord: {
creditCard: {
"cc-number": "4444000022220000",
"cc-name": "Foo Bar",
"cc-exp": "2022-06",
},
},
},
{
description: "A credit card form with cc-number value only.",
document: `<form>
<input id="cc-number" autocomplete="cc-number">
</form>`,
formValue: {
"cc-number": "4444000022220000",
},
expectedRecord: {
creditCard: {
"cc-number": "4444000022220000",
},
},
},
{
description: "A credit card form must have cc-number value.",
document: `<form>
<input id="cc-number" autocomplete="cc-number">
<input id="cc-name" autocomplete="cc-name">
<input id="cc-exp" autocomplete="cc-exp">
</form>`,
formValue: {
"cc-number": "",
"cc-name": "Foo Bar",
"cc-exp": "2022-06",
},
expectedRecord: {
creditCard: undefined,
},
},
{
description: "A credit card form must have cc-number field.",
document: `<form>
<input id="cc-name" autocomplete="cc-name">
<input id="cc-exp" autocomplete="cc-exp">
</form>`,
formValue: {
"cc-name": "Foo Bar",
"cc-exp": "2022-06",
},
expectedRecord: {
creditCard: undefined,
},
},
];
for (let testcase of TESTCASES) {