1
1
<?php
2
2
3
3
declare (strict_types=1 );
4
+
4
5
namespace In2code \Powermail \ViewHelpers \String ;
5
6
6
7
use In2code \Powermail \Domain \Model \Field ;
@@ -26,7 +27,7 @@ public function render(): string
26
27
{
27
28
$ field = $ this ->arguments ['field ' ];
28
29
29
- list ( $ autocompleteTokens , $ token , $ section , $ type , $ purpose)
30
+ [ $ autocompleteTokens , $ token , $ section , $ type , $ purpose]
30
31
= [
31
32
'' ,
32
33
$ field ->getAutocompleteToken (),
@@ -35,22 +36,26 @@ public function render(): string
35
36
$ field ->getAutocompletePurpose (),
36
37
];
37
38
38
- //if token is empty or 'on'/'off' other tokens are not allowed
39
+ // If token is empty or 'on'/'off', other tokens are not allowed.
39
40
if (empty ($ token ) || in_array ($ token , ['on ' , 'off ' ])) {
40
41
return $ token ;
41
42
}
42
43
43
- //optional section token must begin with the string 'section-'
44
+ // Optional section token must begin with the string 'section-'
44
45
if (!empty ($ section )) {
45
- $ autocompleteTokens = 'section- ' . $ section . ' ' ;
46
+ if ($ this ->tokenIsAllowedForSection ($ token )) {
47
+ $ autocompleteTokens .= 'section- ' . $ section . ' ' ;
48
+ }
46
49
}
47
50
48
- //optional type token must be either shipping or billing
49
- if (!empty ($ type ) && in_array ($ type , ['shipping ' , 'billing ' ])) {
50
- $ autocompleteTokens .= $ type . ' ' ;
51
+ // Optional type token must be either 'shipping' or 'billing'
52
+ if (!empty ($ type )) {
53
+ if ($ this ->tokenIsAllowedForType ($ token , $ type )) {
54
+ $ autocompleteTokens .= $ type . ' ' ;
55
+ }
51
56
}
52
57
53
- //optional purpose token is only allowed for certain autofill-field tokens
58
+ // Optional purpose token is only allowed for certain autofill-field tokens
54
59
if (!empty ($ purpose )) {
55
60
if ($ this ->tokenIsAllowedForPurpose ($ token , $ purpose )) {
56
61
$ autocompleteTokens .= $ purpose . ' ' ;
@@ -60,20 +65,45 @@ public function render(): string
60
65
return $ autocompleteTokens . $ token ;
61
66
}
62
67
68
+
63
69
/**
64
- * hardcoded check:
65
- * purpose is only allowed for email, imp, tel and tel-*
66
- *
67
- * @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill-detail-tokens
70
+ * @param string $token
71
+ * @param string $type
68
72
*
73
+ * @return bool
74
+ */
75
+ protected function tokenIsAllowedForType (string $ token , string $ type ): bool
76
+ {
77
+ $ allowedTypes = ['shipping ' , 'billing ' ];
78
+ $ tokensNotSupportingType = ['nickname ' , 'sex ' , 'impp ' , 'url ' , 'organization-title ' , 'tel-country-code ' , 'tel-area-code ' , 'tel-national ' , 'tel-local ' , 'tel-local-prefix ' , 'tel-local-suffix ' , 'tel-extension ' , 'username ' , 'new-password ' , 'current-password ' , 'one-time-code ' , 'bday ' , 'bday-day ' , 'bday-month ' , 'bday-year ' , 'language ' , 'photo ' ];
79
+ return in_array ($ type , $ allowedTypes )
80
+ && !in_array ($ token , $ tokensNotSupportingType );
81
+ }
82
+
83
+
84
+ /**
69
85
* @param string $token
70
86
* @param string $purpose
71
87
*
72
88
* @return bool
73
89
*/
74
90
protected function tokenIsAllowedForPurpose (string $ token , string $ purpose ): bool
75
91
{
76
- return in_array ($ purpose , ['home ' , 'work ' , 'mobile ' , 'fax ' , 'pager ' ])
77
- && in_array ($ token , ['tel ' , 'tel-country-code ' , 'tel-national ' , 'tel-area-code ' , 'tel-local ' , 'tel-local-prefix ' , 'tel-local-suffix ' , 'tel-extension ' , 'email ' , 'impp ' ]);
92
+ $ allowedPurposes = ['home ' , 'work ' , 'mobile ' , 'fax ' , 'pager ' ];
93
+ $ tokensSupportingPurpose = ['tel ' , 'email ' , 'impp ' ];
94
+
95
+ return in_array ($ token , $ allowedPurposes , true )
96
+ && !in_array ($ token , $ tokensSupportingPurpose , true );
97
+ }
98
+
99
+ /**
100
+ * @param string $token
101
+ *
102
+ * @return bool
103
+ */
104
+ protected function tokenIsAllowedForSection (string $ token ): bool
105
+ {
106
+ $ tokensNotSupportingSection = ['nickname ' , 'sex ' , 'impp ' , 'url ' , 'organization-title ' , 'username ' , 'new-password ' , 'current-password ' , 'one-time-code ' , 'bday ' , 'bday-day ' , 'bday-month ' , 'bday-year ' , 'language ' , 'photo ' ];
107
+ return !in_array ($ token , $ tokensNotSupportingSection , true );
78
108
}
79
109
}
0 commit comments