11<?php
22
33declare (strict_types=1 );
4+
45namespace In2code \Powermail \ViewHelpers \String ;
56
67use In2code \Powermail \Domain \Model \Field ;
@@ -26,7 +27,7 @@ public function render(): string
2627 {
2728 $ field = $ this ->arguments ['field ' ];
2829
29- list ( $ autocompleteTokens , $ token , $ section , $ type , $ purpose)
30+ [ $ autocompleteTokens , $ token , $ section , $ type , $ purpose]
3031 = [
3132 '' ,
3233 $ field ->getAutocompleteToken (),
@@ -35,22 +36,26 @@ public function render(): string
3536 $ field ->getAutocompletePurpose (),
3637 ];
3738
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.
3940 if (empty ($ token ) || in_array ($ token , ['on ' , 'off ' ])) {
4041 return $ token ;
4142 }
4243
43- //optional section token must begin with the string 'section-'
44+ // Optional section token must begin with the string 'section-'
4445 if (!empty ($ section )) {
45- $ autocompleteTokens = 'section- ' . $ section . ' ' ;
46+ if ($ this ->tokenIsAllowedForSection ($ token )) {
47+ $ autocompleteTokens .= 'section- ' . $ section . ' ' ;
48+ }
4649 }
4750
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+ }
5156 }
5257
53- //optional purpose token is only allowed for certain autofill-field tokens
58+ // Optional purpose token is only allowed for certain autofill-field tokens
5459 if (!empty ($ purpose )) {
5560 if ($ this ->tokenIsAllowedForPurpose ($ token , $ purpose )) {
5661 $ autocompleteTokens .= $ purpose . ' ' ;
@@ -60,20 +65,45 @@ public function render(): string
6065 return $ autocompleteTokens . $ token ;
6166 }
6267
68+
6369 /**
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
6872 *
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+ /**
6985 * @param string $token
7086 * @param string $purpose
7187 *
7288 * @return bool
7389 */
7490 protected function tokenIsAllowedForPurpose (string $ token , string $ purpose ): bool
7591 {
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 );
78108 }
79109}
0 commit comments