Bug 1411990 - (WIP) Add consecutive cc-exp-* check. draft
authorRay Lin <ralin@mozilla.com>
Fri, 10 Nov 2017 01:28:43 +0800
changeset 695695 bc75858996d073022d8edaeb259cd2a274fa7951
parent 695600 2bdf6eed0f64a51dfe099e089852533595f1a896
child 739674 624d04d3262e69b58873cf47b8d2a5a9c7ef3df7
push id88503
push userbmo:ralin@mozilla.com
push dateThu, 09 Nov 2017 17:31:27 +0000
bugs1411990
milestone58.0a1
Bug 1411990 - (WIP) Add consecutive cc-exp-* check. This WIP is able to fix the aliexpress.com. Let's make the code cleaner, and maybe add a test case. MozReview-Commit-ID: 5P2nSSJd2Dl
browser/extensions/formautofill/FormAutofillHeuristics.jsm
--- a/browser/extensions/formautofill/FormAutofillHeuristics.jsm
+++ b/browser/extensions/formautofill/FormAutofillHeuristics.jsm
@@ -508,16 +508,32 @@ this.FormAutofillHeuristics = {
         fieldScanner.updateFieldName(fieldScanner.parsingIndex, "cc-exp-year");
         fieldScanner.parsingIndex++;
 
         return true;
       }
     }
     fieldScanner.parsingIndex = savedIndex;
 
+    if (this._matchRegex(element, "cc-exp-month")) {
+      fieldScanner.updateFieldName(fieldScanner.parsingIndex, "cc-exp-month");
+      fieldScanner.parsingIndex++;
+      if (!fieldScanner.parsingFinished) {
+        const nextDetail = fieldScanner.getFieldDetailByIndex(fieldScanner.parsingIndex);
+        const nextElement = nextDetail.elementWeakRef.get();
+        if (this._matchRegex(nextElement, "cc-exp-year")) {
+          fieldScanner.updateFieldName(fieldScanner.parsingIndex, "cc-exp-year");
+          fieldScanner.parsingIndex++;
+
+          return true;
+        }
+      }
+    }
+    fieldScanner.parsingIndex = savedIndex;
+
     // If no possible regular expiration fields are detected in current parsing window
     // fallback to "cc-exp" as there's no such case that cc-exp-month or cc-exp-year
     // presents alone.
     fieldScanner.updateFieldName(fieldScanner.parsingIndex, "cc-exp");
     fieldScanner.parsingIndex++;
 
     return true;
   },
@@ -660,51 +676,60 @@ this.FormAutofillHeuristics = {
       };
     }
 
     let regexps = this._getRegExpList(isAutoCompleteOff, element.tagName);
     if (regexps.length == 0) {
       return null;
     }
 
+    for (let regexp of regexps) {
+      const info = this._matchRegex(element, regexp);
+      if (info) {
+        return info; 
+      }
+    }
+
+    return null;
+  },
+
+  _matchRegex(element, regexp) {
     let labelStrings;
     let getElementStrings = {};
     getElementStrings[Symbol.iterator] = function* () {
       yield element.id;
       yield element.name;
       if (!labelStrings) {
         labelStrings = [];
         let labels = LabelUtils.findLabelElements(element);
         for (let label of labels) {
           labelStrings.push(...LabelUtils.extractLabelStrings(label));
         }
       }
       yield *labelStrings;
     };
 
-    for (let regexp of regexps) {
-      for (let string of getElementStrings) {
-        // The original regexp "(?<!united )state|county|region|province" for
-        // "address-line1" wants to exclude any "united state" string, so the
-        // following code is to remove all "united state" string before applying
-        // "addess-level1" regexp.
-        //
-        // Since "united state" string matches to the regexp of address-line2&3,
-        // the two regexps should be excluded here.
-        if (["address-level1", "address-line2", "address-line3"].includes(regexp)) {
-          string = string.toLowerCase().split("united state").join("");
-        }
-        if (this.RULES[regexp].test(string)) {
-          return {
-            fieldName: regexp,
-            section: "",
-            addressType: "",
-            contactType: "",
-          };
-        }
+    for (let string of getElementStrings) {
+      // The original regexp "(?<!united )state|county|region|province" for
+      // "address-line1" wants to exclude any "united state" string, so the
+      // following code is to remove all "united state" string before applying
+      // "addess-level1" regexp.
+      //
+      // Since "united state" string matches to the regexp of address-line2&3,
+      // the two regexps should be excluded here.
+      if (["address-level1", "address-line2", "address-line3"].includes(regexp)) {
+        string = string.toLowerCase().split("united state").join("");
+      }
+      if (this.RULES[regexp].test(string)) {
+        return {
+          fieldName: regexp,
+          section: "",
+          addressType: "",
+          contactType: "",
+        };
       }
     }
 
     return null;
   },
 
 /**
  * Phone field grammars - first matched grammar will be parsed. Grammars are