Skip to content

Commit ab1ae4f

Browse files
committed
Update token rules to correctly reflect the specs
1 parent 94e585d commit ab1ae4f

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

Classes/ViewHelpers/String/AutocompleteViewHelper.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
namespace In2code\Powermail\ViewHelpers\String;
56

67
use 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
}

Configuration/TCA/tx_powermail_domain_model_field.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@
10481048
'displayCond' => [
10491049
'AND' => [
10501050
'FIELD:type:IN:input,textarea',
1051-
'FIELD:autocomplete_token:!IN:on,off',
1051+
'FIELD:autocomplete_token:!IN:on,off,nickname,sex,impp,url,organization-title,username,new-password,current-password,one-time-code,bday,bday-day,bday-month,bday-year,language,photo',
10521052
'FIELD:autocomplete_token:REQ:true',
10531053
],
10541054
],
@@ -1067,7 +1067,7 @@
10671067
'displayCond' => [
10681068
'AND' => [
10691069
'FIELD:type:IN:input,textarea',
1070-
'FIELD:autocomplete_token:!IN:on,off',
1070+
'FIELD:autocomplete_token:!IN:on,off,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',
10711071
'FIELD:autocomplete_token:REQ:true',
10721072
],
10731073
],
@@ -1090,7 +1090,7 @@
10901090
'displayCond' => [
10911091
'AND' => [
10921092
'FIELD:type:IN:input,textarea',
1093-
'FIELD:autocomplete_token:IN:email,impp,tel,tel-country-code,tel-national,tel-area-code,tel-local,tel-local-prefix,tel-local-suffix,tel-extension',
1093+
'FIELD:autocomplete_token:IN:email,impp,tel',
10941094
],
10951095
],
10961096
],

0 commit comments

Comments
 (0)