diff --git a/var/Typecho/Widget/Helper/Form/Element.php b/var/Typecho/Widget/Helper/Form/Element.php index f95d1b82..7ff5523a 100644 --- a/var/Typecho/Widget/Helper/Form/Element.php +++ b/var/Typecho/Widget/Helper/Form/Element.php @@ -217,7 +217,7 @@ abstract class Element extends Layout public function value($value): Element { $this->value = $value; - $this->inputValue($value ?? ''); + $this->inputValue($value); return $this; } diff --git a/var/Typecho/Widget/Helper/Form/Element/Checkbox.php b/var/Typecho/Widget/Helper/Form/Element/Checkbox.php index 3f89586e..a3ba93f9 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Checkbox.php +++ b/var/Typecho/Widget/Helper/Form/Element/Checkbox.php @@ -61,7 +61,7 @@ class Checkbox extends Element */ protected function inputValue($value) { - $values = is_null($value) ? [] : (is_array($value) ? $value : [$value]); + $values = isset($value) ? (is_array($value) ? $value : [$value]) : []; foreach ($this->options as $option) { $option->removeAttribute('checked'); diff --git a/var/Typecho/Widget/Helper/Form/Element/Hidden.php b/var/Typecho/Widget/Helper/Form/Element/Hidden.php index f551f4cd..cd05c7ae 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Hidden.php +++ b/var/Typecho/Widget/Helper/Form/Element/Hidden.php @@ -3,7 +3,6 @@ namespace Typecho\Widget\Helper\Form\Element; use Typecho\Widget\Helper\Form\Element; -use Typecho\Widget\Helper\Layout; if (!defined('__TYPECHO_ROOT_DIR__')) { exit; @@ -19,6 +18,8 @@ if (!defined('__TYPECHO_ROOT_DIR__')) { */ class Hidden extends Element { + use TextInputTrait; + /** * 自定义初始函数 * @@ -31,28 +32,19 @@ class Hidden extends Element } /** - * 初始化当前输入项 - * - * @access public - * @param string|null $name 表单元素名称 - * @param array|null $options 选择项 - * @return Layout|null + * @param string $value + * @return string */ - public function input(?string $name = null, ?array $options = null): ?Layout + protected function filterValue(string $value): string { - $input = new Layout('input', ['name' => $name, 'type' => 'hidden']); - $this->container($input); - $this->inputs[] = $input; - return $input; + return htmlspecialchars($value); } /** - * 设置表单项默认值 - * - * @param mixed $value 表单项默认值 + * @return string */ - protected function inputValue($value) + protected function getType(): string { - $this->input->setAttribute('value', htmlspecialchars($value)); + return 'hidden'; } } diff --git a/var/Typecho/Widget/Helper/Form/Element/Password.php b/var/Typecho/Widget/Helper/Form/Element/Password.php index 4ced2755..c07ca06c 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Password.php +++ b/var/Typecho/Widget/Helper/Form/Element/Password.php @@ -3,7 +3,6 @@ namespace Typecho\Widget\Helper\Form\Element; use Typecho\Widget\Helper\Form\Element; -use Typecho\Widget\Helper\Layout; if (!defined('__TYPECHO_ROOT_DIR__')) { exit; @@ -19,30 +18,22 @@ if (!defined('__TYPECHO_ROOT_DIR__')) { */ class Password extends Element { + use TextInputTrait; + /** - * 初始化当前输入项 - * - * @param string|null $name 表单元素名称 - * @param array|null $options 选择项 - * @return Layout|null + * @param string $value + * @return string */ - public function input(?string $name = null, ?array $options = null): ?Layout + protected function filterValue(string $value): string { - $input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId, - 'name' => $name, 'type' => 'password', 'class' => 'password']); - $this->label->setAttribute('for', $name . '-0-' . self::$uniqueId); - $this->container($input); - $this->inputs[] = $input; - return $input; + return htmlspecialchars($value); } /** - * 设置表单项默认值 - * - * @param mixed $value 表单项默认值 + * @return string */ - protected function inputValue($value) + protected function getType(): string { - $this->input->setAttribute('value', htmlspecialchars($value)); + return 'password'; } } diff --git a/var/Typecho/Widget/Helper/Form/Element/Radio.php b/var/Typecho/Widget/Helper/Form/Element/Radio.php index bcc8618c..f9cdf191 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Radio.php +++ b/var/Typecho/Widget/Helper/Form/Element/Radio.php @@ -65,7 +65,7 @@ class Radio extends Element $option->removeAttribute('checked'); } - if (isset($this->options[$value])) { + if (isset($value) && isset($this->options[$value])) { $this->value = $value; $this->options[$value]->setAttribute('checked', 'true'); $this->input = $this->options[$value]; diff --git a/var/Typecho/Widget/Helper/Form/Element/Select.php b/var/Typecho/Widget/Helper/Form/Element/Select.php index 2a57b860..c759e3e2 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Select.php +++ b/var/Typecho/Widget/Helper/Form/Element/Select.php @@ -60,7 +60,7 @@ class Select extends Element $option->removeAttribute('selected'); } - if (isset($this->options[$value])) { + if (isset($value) && isset($this->options[$value])) { $this->options[$value]->setAttribute('selected', 'true'); } } diff --git a/var/Typecho/Widget/Helper/Form/Element/Submit.php b/var/Typecho/Widget/Helper/Form/Element/Submit.php index b2f3acbb..f5538d8f 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Submit.php +++ b/var/Typecho/Widget/Helper/Form/Element/Submit.php @@ -43,6 +43,6 @@ class Submit extends Element */ protected function inputValue($value) { - $this->input->html($value); + $this->input->html($value ?? 'Submit'); } } diff --git a/var/Typecho/Widget/Helper/Form/Element/Text.php b/var/Typecho/Widget/Helper/Form/Element/Text.php index 8c97d302..3d33e59b 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Text.php +++ b/var/Typecho/Widget/Helper/Form/Element/Text.php @@ -3,7 +3,6 @@ namespace Typecho\Widget\Helper\Form\Element; use Typecho\Widget\Helper\Form\Element; -use Typecho\Widget\Helper\Layout; if (!defined('__TYPECHO_ROOT_DIR__')) { exit; @@ -19,35 +18,22 @@ if (!defined('__TYPECHO_ROOT_DIR__')) { */ class Text extends Element { - /** - * 初始化当前输入项 - * - * @param string|null $name 表单元素名称 - * @param array|null $options 选择项 - * @return Layout|null - */ - public function input(?string $name = null, ?array $options = null): ?Layout - { - $input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId, - 'name' => $name, 'type' => 'text', 'class' => 'text']); - $this->container($input); - $this->label->setAttribute('for', $name . '-0-' . self::$uniqueId); - $this->inputs[] = $input; + use TextInputTrait; - return $input; + /** + * @param string $value + * @return string + */ + protected function filterValue(string $value): string + { + return htmlspecialchars($value); } /** - * 设置表单项默认值 - * - * @param mixed $value 表单项默认值 + * @return string */ - protected function inputValue($value) + protected function getType(): string { - if (isset($value)) { - $this->input->setAttribute('value', htmlspecialchars($value)); - } else { - $this->input->removeAttribute('value'); - } + return 'text'; } } diff --git a/var/Typecho/Widget/Helper/Form/Element/TextInputTrait.php b/var/Typecho/Widget/Helper/Form/Element/TextInputTrait.php new file mode 100644 index 00000000..67b07705 --- /dev/null +++ b/var/Typecho/Widget/Helper/Form/Element/TextInputTrait.php @@ -0,0 +1,59 @@ + $name . '-0-' . self::$uniqueId, + 'name' => $name, + 'type' => $this->getType(), + 'class' => 'text' + ]); + + $this->container($input); + $this->inputs[] = $input; + + if (isset($this->label)) { + $this->label->setAttribute('for', $name . '-0-' . self::$uniqueId); + } + + return $input; + } + + /** + * 设置表单项默认值 + * + * @param mixed $value 表单项默认值 + */ + protected function inputValue($value) + { + if (isset($value)) { + $this->input->setAttribute('value', $this->filterValue($value)); + } else { + $this->input->removeAttribute('value'); + } + } + + /** + * @param string $value + * @return string + */ + abstract protected function filterValue(string $value): string; + + /** + * @return string + */ + abstract protected function getType(): string; +} diff --git a/var/Typecho/Widget/Helper/Form/Element/Textarea.php b/var/Typecho/Widget/Helper/Form/Element/Textarea.php index 581ab93f..b62e6d26 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Textarea.php +++ b/var/Typecho/Widget/Helper/Form/Element/Textarea.php @@ -43,6 +43,6 @@ class Textarea extends Element */ protected function inputValue($value) { - $this->input->html(htmlspecialchars($value)); + $this->input->html(htmlspecialchars($value ?? '')); } } diff --git a/var/Typecho/Widget/Helper/Form/Element/Url.php b/var/Typecho/Widget/Helper/Form/Element/Url.php index c8973b33..395d8b82 100644 --- a/var/Typecho/Widget/Helper/Form/Element/Url.php +++ b/var/Typecho/Widget/Helper/Form/Element/Url.php @@ -4,7 +4,6 @@ namespace Typecho\Widget\Helper\Form\Element; use Typecho\Common; use Typecho\Widget\Helper\Form\Element; -use Typecho\Widget\Helper\Layout; if (!defined('__TYPECHO_ROOT_DIR__')) { exit; @@ -20,35 +19,22 @@ if (!defined('__TYPECHO_ROOT_DIR__')) { */ class Url extends Element { - /** - * 初始化当前输入项 - * - * @param string|null $name 表单元素名称 - * @param array|null $options 选择项 - * @return Layout|null - */ - public function input(?string $name = null, ?array $options = null): ?Layout - { - $input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId, - 'name' => $name, 'type' => 'url', 'class' => 'text']); - $this->container($input); - $this->label->setAttribute('for', $name . '-0-' . self::$uniqueId); - $this->inputs[] = $input; + use TextInputTrait; - return $input; + /** + * @param string $value + * @return string + */ + protected function filterValue(string $value): string + { + return htmlspecialchars(Common::idnToUtf8($value)); } /** - * 设置表单项默认值 - * - * @param mixed $value 表单项默认值 + * @return string */ - protected function inputValue($value) + protected function getType(): string { - if (isset($value)) { - $this->input->setAttribute('value', htmlspecialchars(Common::idnToUtf8($value))); - } else { - $this->input->removeAttribute('value'); - } + return 'url'; } }