@@ -23,11 +23,6 @@ export const LabelUtils = {
23
23
// @type {Map<string, array> }
24
24
_mappedLabels : null ,
25
25
26
- // An array consisting of label elements whose correponding form field doesn't
27
- // have an id attribute.
28
- // @type {Array<[HTMLLabelElement, HTMLElement]> }
29
- _unmappedLabelControls : null ,
30
-
31
26
// A weak map consisting of label element and extracted strings pairs.
32
27
// @type {WeakMap<HTMLLabelElement, array> }
33
28
_labelStrings : null ,
@@ -178,41 +173,29 @@ export const LabelUtils = {
178
173
179
174
generateLabelMap ( doc ) {
180
175
this . _mappedLabels = new Map ( ) ;
181
- this . _unmappedLabelControls = [ ] ;
182
176
this . _labelStrings = new WeakMap ( ) ;
183
177
184
178
// A map of potential label -> control for labels that don't have an id or
185
179
// control associated with them. Labels that have ids or associated controls
186
180
// will be placed in _mappedLabels.
187
181
let potentialLabels = new Map ( ) ;
188
-
189
182
for ( let label of doc . querySelectorAll ( "label" ) ) {
190
- let id = label . htmlFor ;
191
- let control ;
192
- if ( ! id ) {
193
- control = label . control ;
194
- if ( ! control ) {
195
- // If the label has no control, look for the next input or select
196
- // element in the document and add that to the potentialLabels list.
197
- control = this . findAdjacentControl ( label , potentialLabels ) ;
198
- if ( control ) {
199
- potentialLabels . set ( control , label ) ;
200
- } else {
201
- continue ;
202
- }
203
- }
204
- id = control . id ;
205
- }
206
- if ( id ) {
207
- let labels = this . _mappedLabels . get ( id ) ;
183
+ let control = label . control ;
184
+ if ( control ) {
185
+ const controlId = lazy . FormAutofillUtils . getElementIdentifier ( control ) ;
186
+ let labels = this . _mappedLabels . get ( controlId ) ;
208
187
if ( labels ) {
209
188
labels . push ( label ) ;
210
189
} else {
211
- this . _mappedLabels . set ( id , [ label ] ) ;
190
+ this . _mappedLabels . set ( controlId , [ label ] ) ;
212
191
}
213
192
} else {
214
- // control must be non-empty here
215
- this . _unmappedLabelControls . push ( { label, control } ) ;
193
+ // If the label has no control, look for the next input or select
194
+ // element in the document and add that to the potentialLabels list.
195
+ control = this . findAdjacentControl ( label , potentialLabels ) ;
196
+ if ( control ) {
197
+ potentialLabels . set ( control , label ) ;
198
+ }
216
199
}
217
200
}
218
201
@@ -221,22 +204,16 @@ export const LabelUtils = {
221
204
// control that is nearby even when it has no for attribute or doesn't match an id.
222
205
if ( potentialLabels . size ) {
223
206
for ( let label of potentialLabels ) {
224
- if (
225
- ! this . _unmappedLabelControls . some ( e => e . control == label [ 0 ] ) &&
226
- ( ! label [ 1 ] . id || ! this . _mappedLabels . has ( label [ 1 ] . id ) )
227
- ) {
228
- this . _unmappedLabelControls . push ( {
229
- label : label [ 1 ] ,
230
- control : label [ 0 ] ,
231
- } ) ;
207
+ const elementId = lazy . FormAutofillUtils . getElementIdentifier ( label [ 0 ] ) ;
208
+ if ( ! this . _mappedLabels . has ( elementId ) ) {
209
+ this . _mappedLabels . set ( elementId , [ label [ 1 ] ] ) ;
232
210
}
233
211
}
234
212
}
235
213
} ,
236
214
237
215
clearLabelMap ( ) {
238
216
this . _mappedLabels = null ;
239
- this . _unmappedLabelControls = null ;
240
217
this . _labelStrings = null ;
241
218
} ,
242
219
@@ -245,12 +222,7 @@ export const LabelUtils = {
245
222
this . generateLabelMap ( element . ownerDocument ) ;
246
223
}
247
224
248
- let id = element . id ;
249
- if ( ! id ) {
250
- return this . _unmappedLabelControls
251
- . filter ( lc => lc . control == element )
252
- . map ( lc => lc . label ) ;
253
- }
225
+ let id = lazy . FormAutofillUtils . getElementIdentifier ( element ) ;
254
226
return this . _mappedLabels . get ( id ) || [ ] ;
255
227
} ,
256
228
} ;
0 commit comments