Skip to content

Commit

Permalink
Bug 1407759 - SELECT element supports some fieldNames only. (e.g. cc-…
Browse files Browse the repository at this point in the history
…exp*, country, address-level*) r=lchang,ralin

MozReview-Commit-ID: KtGO4TseJwH

--HG--
extra : rebase_source : 736c7dfa6bd9fd96a8e1cb0b890cb5a7cdea255b
  • Loading branch information
weilonge committed Oct 11, 2017
1 parent abfaaa7 commit 1827171
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 22 deletions.
86 changes: 64 additions & 22 deletions browser/extensions/formautofill/FormAutofillHeuristics.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,69 @@ this.FormAutofillHeuristics = {
return fieldScanner.trimmedFieldDetail;
},

_regExpTableHashValue(...signBits) {
return signBits.reduce((p, c, i) => p | !!c << i, 0);
},

_setRegExpListCache(regexps, b0, b1, b2) {
if (!this._regexpList) {
this._regexpList = [];
}
this._regexpList[this._regExpTableHashValue(b0, b1, b2)] = regexps;
},

_getRegExpListCache(b0, b1, b2) {
if (!this._regexpList) {
return null;
}
return this._regexpList[this._regExpTableHashValue(b0, b1, b2)];
},

_getRegExpList(isAutoCompleteOff, elementTagName) {
let isSelectElem = elementTagName == "SELECT";
let regExpListCache = this._getRegExpListCache(
isAutoCompleteOff,
FormAutofillUtils.isAutofillCreditCardsAvailable,
isSelectElem
);
if (regExpListCache) {
return regExpListCache;
}
const FIELDNAMES_IGNORING_AUTOCOMPLETE_OFF = [
"cc-name",
"cc-number",
"cc-exp-month",
"cc-exp-year",
"cc-exp",
];
let regexps = isAutoCompleteOff ? FIELDNAMES_IGNORING_AUTOCOMPLETE_OFF : Object.keys(this.RULES);

if (!FormAutofillUtils.isAutofillCreditCardsAvailable) {
regexps = regexps.filter(name => !FormAutofillUtils.isCreditCardField(name));
}

if (isSelectElem) {
const FIELDNAMES_FOR_SELECT_ELEMENT = [
"address-level1",
"address-level2",
"country",
"cc-exp-month",
"cc-exp-year",
"cc-exp",
];
regexps = regexps.filter(name => FIELDNAMES_FOR_SELECT_ELEMENT.includes(name));
}

this._setRegExpListCache(
regexps,
isAutoCompleteOff,
FormAutofillUtils.isAutofillCreditCardsAvailable,
isSelectElem
);

return regexps;
},

getInfo(element) {
if (!FormAutofillUtils.isFieldEligibleForAutofill(element)) {
return null;
Expand Down Expand Up @@ -598,28 +661,7 @@ this.FormAutofillHeuristics = {
};
}

const FIELDNAMES_IGNORING_AUTOCOMPLETE_OFF = [
"cc-name",
"cc-number",
"cc-exp-month",
"cc-exp-year",
"cc-exp",
];
let regexps = isAutoCompleteOff ? FIELDNAMES_IGNORING_AUTOCOMPLETE_OFF : Object.keys(this.RULES);

if (!FormAutofillUtils.isAutofillCreditCardsAvailable) {
if (isAutoCompleteOff) {
if (!this._regexpListOf_CcUnavailable_AcOff) {
this._regexpListOf_CcUnavailable_AcOff = regexps.filter(name => !FormAutofillUtils.isCreditCardField(name));
}
regexps = this._regexpListOf_CcUnavailable_AcOff;
} else {
if (!this._regexpListOf_CcUnavailable_AcOn) {
this._regexpListOf_CcUnavailable_AcOn = regexps.filter(name => !FormAutofillUtils.isCreditCardField(name));
}
regexps = this._regexpListOf_CcUnavailable_AcOn;
}
}
let regexps = this._getRegExpList(isAutoCompleteOff, element.tagName);
if (regexps.length == 0) {
return null;
}
Expand Down
50 changes: 50 additions & 0 deletions browser/extensions/formautofill/test/unit/test_getInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,53 @@ TESTCASES.forEach(testcase => {
LabelUtils.clearLabelMap();
});
});

add_task(async function test_regexp_list() {
do_print("Verify the fieldName support for select element.");
let SUPPORT_LIST = {
"email": null, // email
"tel-extension": null, // tel-extension
"phone": null, // tel
"organization": null, // organization
"street-address": null, // street-address
"address1": null, // address-line1
"address2": null, // address-line2
"address3": null, // address-line3
"city": "address-level2",
"region": "address-level1",
"postal-code": null, // postal-code
"country": "country",
"fullname": null, // name
"fname": null, // given-name
"mname": null, // additional-name
"lname": null, // family-name
"cardholder": null, // cc-name
"cc-number": null, // cc-number
"addmonth": "cc-exp-month",
"addyear": "cc-exp-year",
};
for (let label of Object.keys(SUPPORT_LIST)) {
let testcase = {
description: `A select element supports ${label} or not`,
document: `<select id="${label}"></select>`,
elementId: label,
expectedReturnValue: (SUPPORT_LIST[label] ? {
fieldName: SUPPORT_LIST[label],
section: "",
addressType: "",
contactType: "",
} : null),
};
do_print(testcase.description);
do_print(testcase.document);
let doc = MockDocument.createTestDocument(
"http://localhost:8080/test/", testcase.document);

let element = doc.getElementById(testcase.elementId);
let value = FormAutofillHeuristics.getInfo(element);

Assert.deepEqual(value, testcase.expectedReturnValue, label);
}
LabelUtils.clearLabelMap();
});

0 comments on commit 1827171

Please sign in to comment.