Bug 1428292 - Determine tel-extension in telephone parser only. r=lchang draft
authorSean Lee <selee@mozilla.com>
Mon, 08 Jan 2018 16:15:51 +0800
changeset 720018 5b20053d9d73a04b8af2f0053a2c1cdcc8c4808c
parent 719928 f5b4481c9fd50becb35cef02b599198b766fb1bb
child 745962 b583b36e5b37786f1bb4b797664ca8943353b278
push id95432
push userbmo:selee@mozilla.com
push dateSat, 13 Jan 2018 12:13:15 +0000
reviewerslchang
bugs1428292
milestone59.0a1
Bug 1428292 - Determine tel-extension in telephone parser only. r=lchang MozReview-Commit-ID: KRArH8kxuu0
browser/extensions/formautofill/FormAutofillHeuristics.jsm
browser/extensions/formautofill/content/heuristicsRegexp.js
browser/extensions/formautofill/test/unit/heuristics/third_party/test_CDW.js
browser/extensions/formautofill/test/unit/heuristics/third_party/test_Walmart.js
--- a/browser/extensions/formautofill/FormAutofillHeuristics.jsm
+++ b/browser/extensions/formautofill/FormAutofillHeuristics.jsm
@@ -177,28 +177,25 @@ class FieldScanner {
    */
   pushDetail() {
     let elementIndex = this.fieldDetails.length;
     if (elementIndex >= this._elements.length) {
       throw new Error("Try to push the non-existing element info.");
     }
     let element = this._elements[elementIndex];
     let info = FormAutofillHeuristics.getInfo(element);
-    if (!info) {
-      info = {};
-    }
     let fieldInfo = {
-      section: info.section,
-      addressType: info.addressType,
-      contactType: info.contactType,
-      fieldName: info.fieldName,
+      section: info ? info.section : "",
+      addressType: info ? info.addressType : "",
+      contactType: info ? info.contactType : "",
+      fieldName: info ? info.fieldName : "",
       elementWeakRef: Cu.getWeakReference(element),
     };
 
-    if (info._reason) {
+    if (info && info._reason) {
       fieldInfo._reason = info._reason;
     }
 
     this.fieldDetails.push(fieldInfo);
     this._pushToSection(this._getSectionName(fieldInfo), fieldInfo);
   }
 
   _getSectionName(info) {
@@ -534,19 +531,29 @@ this.FormAutofillHeuristics = {
       }
     }
 
     if (fieldScanner.parsingFinished) {
       return parsedField;
     }
 
     let nextField = fieldScanner.getFieldDetailByIndex(fieldScanner.parsingIndex);
-    if (nextField && nextField.fieldName == "tel-extension") {
-      fieldScanner.parsingIndex++;
-      parsedField = true;
+    if (nextField && nextField._reason != "autocomplete" && fieldScanner.parsingIndex > 0) {
+      const regExpTelExtension = new RegExp(
+        "\\bext|ext\\b|extension" +
+        "|ramal", // pt-BR, pt-PT
+        "iu");
+      const previousField = fieldScanner.getFieldDetailByIndex(fieldScanner.parsingIndex - 1);
+      const previousFieldType = FormAutofillUtils.getCategoryFromFieldName(previousField.fieldName);
+      if (previousField && previousFieldType == "tel" &&
+        this._matchRegexp(nextField.elementWeakRef.get(), regExpTelExtension)) {
+        fieldScanner.updateFieldName(fieldScanner.parsingIndex, "tel-extension");
+        fieldScanner.parsingIndex++;
+        parsedField = true;
+      }
     }
 
     return parsedField;
   },
 
   /**
    * Try to find the correct address-line[1-3] sequence and correct their field
    * names.
--- a/browser/extensions/formautofill/content/heuristicsRegexp.js
+++ b/browser/extensions/formautofill/content/heuristicsRegexp.js
@@ -24,21 +24,16 @@ var HeuristicsRegExp = {
       "|Электронной.?Почты" + // ru
       "|邮件|邮箱" + // zh-CN
       "|電郵地址" + // zh-TW
       "|(?:이메일|전자.?우편|[Ee]-?mail)(.?주소)?", // ko-KR
       "iu"
     ),
 
     // ==== Telephone ====
-    "tel-extension": new RegExp(
-      "\\bext|ext\\b|extension" +
-      "|ramal", // pt-BR, pt-PT
-      "iu"
-    ),
     "tel": new RegExp(
       "phone|mobile|contact.?number" +
       "|telefonnummer" + // de-DE
       "|telefono|teléfono" + // es
       "|telfixe" + // fr-FR
       "|電話" + // ja-JP
       "|telefone|telemovel" + // pt-BR, pt-PT
       "|телефон" + // ru
--- a/browser/extensions/formautofill/test/unit/heuristics/third_party/test_CDW.js
+++ b/browser/extensions/formautofill/test/unit/heuristics/third_party/test_CDW.js
@@ -10,52 +10,40 @@ runHeuristicsTest([
         {"section": "", "addressType": "", "contactType": "", "fieldName": "given-name"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "family-name"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "organization"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "address-line1"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "address-line2"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "address-level2"}, // city
         {"section": "", "addressType": "", "contactType": "", "fieldName": "address-level1"}, // state
         {"section": "", "addressType": "", "contactType": "", "fieldName": "postal-code"},
-
-        // FIXME: bug 1392932 - misdetect ZIP ext string
-        {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-extension"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "email"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "tel"},
-      ], [
-        // The below "tel-extension" is correct and removed due to the
-        // duplicated field above.
         {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-extension"},
       ]],
       [],
     ],
   }, {
     fixturePath: "Checkout_BillingPaymentInfo.html",
     expectedResult: [
       [[
         {"section": "", "addressType": "", "contactType": "", "fieldName": "given-name"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "family-name"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "organization"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "address-line1"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "address-line2"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "address-level2"}, // city
         {"section": "", "addressType": "", "contactType": "", "fieldName": "address-level1"}, // state
         {"section": "", "addressType": "", "contactType": "", "fieldName": "postal-code"},
-
-        // FIXME: bug 1392932 - misdetect ZIP ext string
-        {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-extension"},
       ], [
 //      {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-type"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-number"}, // ac-off
         {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-exp-month"},
         {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-exp-year"},
-
 //      {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-csc"},
-      ], [
-        {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-extension"},
       ]],
       [],
     ],
   }, {
     fixturePath: "Checkout_Logon.html",
     expectedResult: [
       [],
       [],
--- a/browser/extensions/formautofill/test/unit/heuristics/third_party/test_Walmart.js
+++ b/browser/extensions/formautofill/test/unit/heuristics/third_party/test_Walmart.js
@@ -31,22 +31,16 @@ runHeuristicsTest([
           {"section": "section-payment", "addressType": "", "contactType": "", "fieldName": "given-name"},
           {"section": "section-payment", "addressType": "", "contactType": "", "fieldName": "family-name"},
           {"section": "section-payment", "addressType": "", "contactType": "", "fieldName": "tel"},
         ], [
           {"section": "section-payment", "addressType": "", "contactType": "", "fieldName": "cc-number"},
           {"section": "section-payment", "addressType": "", "contactType": "", "fieldName": "cc-exp-month"},
           {"section": "section-payment", "addressType": "", "contactType": "", "fieldName": "cc-exp-year"},
 //        {"section": "section-payment", "addressType": "", "contactType": "", "fieldName": "cc-csc"},
-        ], [
-          // FIXME bug 1392932 - the following field shouldn't be recognized as
-          // "tel-extension".
-          // The wrong prediction is caused by the name attr "brwsrAutofillText"
-          // which matches the regexp "ext\\b".
-          {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-extension"},
         ],
       ],
     ],
   }, {
     fixturePath: "Shipping.html",
     expectedResult: [
       [[
         {"section": "", "addressType": "", "contactType": "", "fieldName": "postal-code"},