diff --git a/src/Core/ViewHelper/TagBuilder.php b/src/Core/ViewHelper/TagBuilder.php index 41c8c24e1..c896f4bdc 100644 --- a/src/Core/ViewHelper/TagBuilder.php +++ b/src/Core/ViewHelper/TagBuilder.php @@ -42,6 +42,11 @@ class TagBuilder protected bool $forceClosingTag = false; protected bool $ignoreEmptyAttributes = false; + /** + * I'd prefer to set the default value to FALSE. + * Maybe we can leave it to AbstractTagBasedViewHelper in TYPO3 + */ + protected bool $requireAttributeValues = true; /** * Constructor @@ -168,6 +173,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 +223,12 @@ 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) { + // Remove the attribute when it's NULL instead of FALSE, and keep the boolean values as they are + if ($attributeValue === null) { $this->removeAttribute($attributeName); return; } - if ($attributeValue === true) { - $attributeValue = $attributeName; - } - if ($attributeValue instanceof \BackedEnum) { $attributeValue = (string)$attributeValue->value; } elseif ($attributeValue instanceof \UnitEnum) { @@ -232,7 +238,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 +296,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 . '>';