From e5de11d2dadf3651d4ac922f835978851b1dab80 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 19 Dec 2020 13:15:03 +0100 Subject: [PATCH] Add `TAutoCompleteFormField` --- .../field/TAutoCompleteFormField.class.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/TAutoCompleteFormField.class.php diff --git a/wcfsetup/install/files/lib/system/form/builder/field/TAutoCompleteFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/TAutoCompleteFormField.class.php new file mode 100644 index 0000000000..4494ed6a34 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/TAutoCompleteFormField.class.php @@ -0,0 +1,58 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field + * @since 5.4 + */ +trait TAutoCompleteFormField { + /** @var ?string */ + protected $autoComplete; + + /** + * Sets the `autocomplete` attribute of the form field. + * + * Multiple tokens can be separated by spaces and if `null` is given, the attribute is unset. + * + * @throws \InvalidArgumentException if an invalid `autocomplete` token is included in the attribute value + */ + public function autocomplete(?string $autoComplete): self { + if ($autoComplete !== null && $autoComplete !== 'on' && $autoComplete !== 'off') { + if (preg_match('~^(?:section-\w+ )?(?:(shipping|billing) )?(?P.+)$~', $autoComplete, $matches)) { + if (!in_array($matches['token'], $this->getValidAutoCompleteTokens())) { + throw new \InvalidArgumentException("Invalid autocomplete attribute '{$autoComplete}'."); + } + } + else { + throw new \InvalidArgumentException("Invalid autocomplete attribute '{$autoComplete}'."); + } + } + + $this->autoComplete = $autoComplete; + + return $this; + } + + /** + * Returns the `autocomplete` attribute of the form field. + * + * If `null` is returned, no `autocomplete` attribute will be set. + */ + public function getAutoComplete(): ?string { + return $this->autoComplete; + } + + /** + * Returns all valid `autocomplete` tokens. If an empty array is returned, all tokens are considered valid. + * + * @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#inappropriate-for-the-control + */ + protected function getValidAutoCompleteTokens(): array { + return []; + } +} -- 2.20.1