diff --git a/src/Core/ViewHelper/TagBuilder.php b/src/Core/ViewHelper/TagBuilder.php index 41c8c24e1..f239bb572 100644 --- a/src/Core/ViewHelper/TagBuilder.php +++ b/src/Core/ViewHelper/TagBuilder.php @@ -42,6 +42,10 @@ class TagBuilder protected bool $forceClosingTag = false; protected bool $ignoreEmptyAttributes = false; + /** + * Specifies whether a value is required for boolean attributes + */ + protected bool $requireAttributeValues = true; /** * Constructor @@ -168,6 +172,11 @@ public function ignoreEmptyAttributes(bool $ignoreEmptyAttributes): void } } + public function requireAttributeValues(bool $requireAttributeValues): void + { + $this->requireAttributeValues = $requireAttributeValues; + } + /** * Adds an attribute to the $attributes-collection * @@ -213,16 +222,6 @@ public function addAttribute(string $attributeName, $attributeValue, bool $escap $this->addAttribute($attributeName . '-' . $name, $value, $escapeSpecialCharacters); } } else { - // This should probably also check for null, but we can't do that for now because of backwards compatibility - if ($attributeValue === false) { - $this->removeAttribute($attributeName); - return; - } - - if ($attributeValue === true) { - $attributeValue = $attributeName; - } - if ($attributeValue instanceof \BackedEnum) { $attributeValue = (string)$attributeValue->value; } elseif ($attributeValue instanceof \UnitEnum) { @@ -232,7 +231,8 @@ public function addAttribute(string $attributeName, $attributeValue, bool $escap if (trim((string)$attributeValue) === '' && $this->ignoreEmptyAttributes) { return; } - if ($escapeSpecialCharacters) { + // Escape non-boolean values + if (!is_bool($attributeValue) && $escapeSpecialCharacters) { $attributeValue = htmlspecialchars((string)$attributeValue); } $this->attributes[$attributeName] = $attributeValue; @@ -289,7 +289,15 @@ public function render(): string } $output = '<' . $this->tagName; foreach ($this->attributes as $attributeName => $attributeValue) { - $output .= ' ' . $attributeName . '="' . $attributeValue . '"'; + if ($attributeValue === true) { + if ($this->requireAttributeValues) { + $output .= ' ' . $attributeName . '="' . $attributeName . '"'; + } else { + $output .= ' ' . $attributeName; + } + } elseif ($attributeValue !== false) { + $output .= ' ' . $attributeName . '="' . $attributeValue . '"'; + } } if ($this->hasContent() || $this->forceClosingTag) { $output .= '>' . $this->content . 'tagName . '>';