1
+ import { isValidAutocomplete } from "../../commons/text" ;
2
+
3
+ function checkIsElementValidAutocomplete ( node , options , virtualNode ) {
4
+ const autocomplete = virtualNode . attr ( 'autocomplete' ) ?. toLowerCase ( ) . trim ( ) ;
5
+ // if element has autocomplete attribute as off then it is not a violation
6
+ if ( autocomplete === "off" ) {
7
+ return true ;
8
+ }
9
+
10
+ // if it is on then we check whether name / id have valid autocomplete value or not
11
+ // same for the case if autocomplete is not present or has a non-standard value
12
+ if ( ! autocomplete || autocomplete === "on" || ! isValidAutocomplete ( autocomplete , options ) ) {
13
+ const name = virtualNode . attr ( 'name' ) ;
14
+ const id = virtualNode . attr ( 'id' ) ;
15
+ if ( ( name && isValidAutocomplete ( name , options ) ) || ( id && isValidAutocomplete ( id , options ) ) )
16
+ return true ;
17
+ return false ;
18
+ }
19
+
20
+ // if element autocomplete attribute is neither off nor on then we check if its a standard value
21
+ if ( isValidAutocomplete ( autocomplete , options ) ) {
22
+ return true ;
23
+ }
24
+
25
+ return false ;
26
+ }
27
+
28
+ function autocompleteA11yEvaluate ( node , options , virtualNode ) {
29
+ try {
30
+ const autocomplete = virtualNode . attr ( 'autocomplete' ) ;
31
+
32
+ // check if the autocomplete applicable element is inside form or exist freely
33
+ const closestForm = virtualNode . actualNode . closest ( "form" ) ;
34
+
35
+ //if it exists inside the form and autocomplete for form is off
36
+ if ( closestForm && closestForm . getAttribute ( 'autocomplete' ) ?. toLowerCase ( ) . trim ( ) === "off" ) {
37
+ // if autocomplete attribute is not present for element then its a pass in this scenario
38
+ // otherwise check all posibilities with the method
39
+ return autocomplete ? checkIsElementValidAutocomplete ( node , options , virtualNode ) : true ;
40
+ } else {
41
+ // The else case is if form is present and it has autocomplete as on or not set and
42
+ // the other case this handles is that element exists independently
43
+
44
+ // this method would check for all posibilities
45
+ return checkIsElementValidAutocomplete ( node , options , virtualNode ) ;
46
+ }
47
+ }
48
+ catch ( err ) {
49
+ ErrorHandler . addCheckError ( "autocomplete-attribute-valid-check" , err ) ;
50
+ return undefined ;
51
+ }
52
+ }
53
+
54
+ export default autocompleteA11yEvaluate ;
0 commit comments