Bug 1416664 - Move the duplication logic to _trimFieldDetails function. r=lchang,ralin draft
authorSean Lee <weilonge@gmail.com>
Mon, 27 Nov 2017 16:42:12 +0800
changeset 706060 08b4a04bfa04b4be9782f79ebf2e34ce1c276199
parent 706059 e203198cd62ee497c6d94f49340be45ae4760b79
child 706061 ddd6d996608f2223583cfc137640a09efe8d2002
push id91679
push userbmo:selee@mozilla.com
push dateFri, 01 Dec 2017 03:52:21 +0000
reviewerslchang, ralin
bugs1416664
milestone59.0a1
Bug 1416664 - Move the duplication logic to _trimFieldDetails function. r=lchang,ralin
browser/extensions/formautofill/FormAutofillHeuristics.jsm
--- a/browser/extensions/formautofill/FormAutofillHeuristics.jsm
+++ b/browser/extensions/formautofill/FormAutofillHeuristics.jsm
@@ -128,17 +128,16 @@ class FieldScanner {
       }
       if (seenTypes.has(fieldDetail.fieldName) &&
           previousType != fieldDetail.fieldName) {
         seenTypes.clear();
         sectionCount++;
       }
       previousType = fieldDetail.fieldName;
       seenTypes.add(fieldDetail.fieldName);
-      delete fieldDetail._duplicated;
       this._pushToSection(DEFAULT_SECTION_NAME + "-" + sectionCount, fieldDetail);
     }
   }
 
   /**
    * The result is an array contains the sections with its belonging field
    * details. If `this._sections` contains one section only with the default
    * section name (DEFAULT_SECTION_NAME), `this._classifySections` should be
@@ -163,18 +162,17 @@ class FieldScanner {
 
     return this._sections.map(section =>
       this._getFinalDetails(section.fieldDetails)
     );
   }
 
   /**
    * This function will prepare an autocomplete info object with getInfo
-   * function and push the detail to fieldDetails property. Any duplicated
-   * detail will be marked as _duplicated = true for the parser.
+   * function and push the detail to fieldDetails property.
    * Any field will be pushed into `this._sections` based on the section name
    * in `autocomplete` attribute.
    *
    * Any element without the related detail will be used for adding the detail
    * to the end of field details.
    */
   pushDetail() {
     let elementIndex = this.fieldDetails.length;
@@ -193,23 +191,16 @@ class FieldScanner {
       fieldName: info.fieldName,
       elementWeakRef: Cu.getWeakReference(element),
     };
 
     if (info._reason) {
       fieldInfo._reason = info._reason;
     }
 
-    // Store the association between the field metadata and the element.
-    if (this.findSameField(info) != -1) {
-      // A field with the same identifier already exists.
-      log.debug("Not collecting a field matching another with the same info:", info);
-      fieldInfo._duplicated = true;
-    }
-
     this.fieldDetails.push(fieldInfo);
     this._pushToSection(this._getSectionName(fieldInfo), fieldInfo);
   }
 
   _getSectionName(info) {
     let names = [];
     if (info.section) {
       names.push(info.section);
@@ -229,28 +220,22 @@ class FieldScanner {
    * @param {string} fieldName
    *        The new fieldName
    */
   updateFieldName(index, fieldName) {
     if (index >= this.fieldDetails.length) {
       throw new Error("Try to update the non-existing field detail.");
     }
     this.fieldDetails[index].fieldName = fieldName;
-
-    delete this.fieldDetails[index]._duplicated;
-    let indexSame = this.findSameField(this.fieldDetails[index]);
-    if (indexSame != index && indexSame != -1) {
-      this.fieldDetails[index]._duplicated = true;
-    }
   }
 
-  findSameField(info) {
-    return this.fieldDetails.findIndex(f => f.section == info.section &&
-                                       f.addressType == info.addressType &&
-                                       f.fieldName == info.fieldName);
+  _isSameField(field1, field2) {
+    return field1.section == field2.section &&
+           field1.addressType == field2.addressType &&
+           field1.fieldName == field2.fieldName;
   }
 
   /**
    * Provide the final field details without invalid field name, and the
    * duplicated fields will be removed as well. For the debugging purpose,
    * the final `fieldDetails` will include the duplicated fields if
    * `_allowDuplicates` is true.
    *
@@ -259,17 +244,26 @@ class FieldScanner {
    * @returns {Array<Object>}
    *          The array with the field details without invalid field name and
    *          duplicated fields.
    */
   _getFinalDetails(fieldDetails) {
     if (this._allowDuplicates) {
       return fieldDetails.filter(f => f.fieldName);
     }
-    return fieldDetails.filter(f => f.fieldName && !f._duplicated);
+
+    let dedupedFieldDetails = [];
+    for (let fieldDetail of fieldDetails) {
+      if (fieldDetail.fieldName && !dedupedFieldDetails.find(f => this._isSameField(fieldDetail, f))) {
+        dedupedFieldDetails.push(fieldDetail);
+      } else {
+        log.debug("Not collecting an invalid field or matching another with the same info:", fieldDetail);
+      }
+    }
+    return dedupedFieldDetails;
   }
 
   elementExisting(index) {
     return index < this._elements.length;
   }
 }
 
 this.LabelUtils = {