diff --git a/docs/markup.md b/docs/markup.md index 50bb89f..d61509c 100644 --- a/docs/markup.md +++ b/docs/markup.md @@ -214,6 +214,7 @@ setAttribute(string $name, ?string $value): self $markup->setAttribute('id', 'main-content'); $markup->setAttribute('data-user-id', '123'); $markup->setAttribute('role', 'navigation'); +$markup->setAttribute('required', ''); // Remove an attribute (null value) $markup->setAttribute('disabled', null); @@ -1220,4 +1221,3 @@ $list = new Markup( - [MarkupQueryBuilder](markup-query-builder.html) - Fluent query builder - [MarkupCollection](markup-collection.html) - Collection methods - [Getting Started](getting-started.html) - Introduction and first steps - diff --git a/src/Markup.php b/src/Markup.php index 809e380..840e4f4 100644 --- a/src/Markup.php +++ b/src/Markup.php @@ -664,7 +664,7 @@ public function classes( ?array $classes = null ) { * Sets or removes an HTML attribute on the wrapper element. * * If value is provided, sets the attribute. If value is null, removes the attribute. - * For boolean attributes (e.g., disabled, readonly), pass the attribute name as the value. + * For boolean attributes (e.g., disabled, readonly), pass an empty string as the value. * * @since 1.0.0 * @@ -1928,7 +1928,11 @@ private function wrapperOpenerTag(): string { if ( ! empty( $this->wrapperAttributes ) ) { $attributes = []; foreach ( $this->wrapperAttributes as $attribute => $value ) { - $attributes[] = $attribute . '="' . $value . '"'; + if ( '' === $value ) { + $attributes[] = $attribute; + } else { + $attributes[] = $attribute . '="' . htmlspecialchars( (string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ) . '"'; + } } $attributesStr = ' ' . implode( ' ', $attributes ); } else { @@ -2092,4 +2096,3 @@ private function renderSlot( MarkupSlot $slot ): string { } } -