Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/Core/ViewHelper/TagBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 $isXHtml = true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If at all this flag should be named requireAttributeValues or similar to clearly indicate its purpose. See the other single-purpose flags above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed.


/**
* Constructor
Expand Down Expand Up @@ -168,6 +173,11 @@ public function ignoreEmptyAttributes(bool $ignoreEmptyAttributes): void
}
}

public function isXHtml(bool $isXHtml): void
{
$this->isXHtml = $isXHtml;
}

/**
* Adds an attribute to the $attributes-collection
*
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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->isXHtml) {
$output .= ' ' . $attributeName . '="' . $attributeName . '"';
} else {
$output .= ' ' . $attributeName;
}
} elseif ($attributeValue !== false) {
$output .= ' ' . $attributeName . '="' . $attributeValue . '"';
}
}
if ($this->hasContent() || $this->forceClosingTag) {
$output .= '>' . $this->content . '</' . $this->tagName . '>';
Expand Down
Loading