diff --git a/src/Util/HTML.php b/src/Util/HTML.php index 76a51cc52b..1c0f773703 100644 --- a/src/Util/HTML.php +++ b/src/Util/HTML.php @@ -40,37 +40,27 @@ abstract class HTML /** * Creates an HTML tag without attributes - * - * @param string $tag - * @param null|mixed $attrs - * @param array|string $content - * @param bool $empty_tag - * - * @return array */ - public static function tag(string $tag, $attrs = null, $content = null, bool $empty_tag = false): array + public static function tag(string $tag, mixed $attrs = null, mixed $content = null, array $options = []): array { - return self::attr_tag($tag, $attrs ?? '', $content ?? '', $empty_tag); + return self::attr_tag($tag, $attrs ?? '', $content ?? '', $options); } /** * Create tag, possibly with attributes and indentation - * - * @param string $tag - * @param array|string $attrs - element attributes - * @param array|string $content - what goes inside the tag - * @param bool $empty_tag - * - * @return array */ - private static function attr_tag(string $tag, $attrs, $content = '', bool $empty_tag = false): array + private static function attr_tag(string $tag, mixed $attrs, mixed $content = '', array $options = []): array { - $html = '<' . $tag . (is_string($attrs) ? ($attrs ? ' ' : '') . $attrs : self::attr($attrs)); - if ($empty_tag) { + $html = '<' . $tag . (is_string($attrs) ? ($attrs ? ' ' : '') . $attrs : self::attr($attrs, $options)); + if ($options['empty'] ?? false) { $html .= '/>'; } else { - $inner = Formatting::indent($content); - $html .= ">\n" . ($inner == '' ? '' : $inner . "\n") . ""; + if ($options['indent'] ?? true) { + $inner = Formatting::indent($content); + $html .= ">\n" . ($inner == '' ? '' : $inner . "\n") . ""; + } else { + $html .= ">{$content}"; + } } return explode("\n", $html); } @@ -104,7 +94,7 @@ abstract class HTML /** * @param array|string $html The input to convert to HTML - * @param array $options = [] ['allowed_tags' => string[], 'forbidden_attributes' => string[], 'raw' => bool] + * @param array $options = [] ['allowed_tags' => string[], 'forbidden_attributes' => string[], 'raw' => bool, 'indent' => bool] * * @return string */ @@ -130,7 +120,7 @@ abstract class HTML throw new \InvalidArgumentException("HTML::html: Tag {$tag} is not allowed"); } if (!empty($inner)) { - $inner = "\n" . Formatting::indent($inner, $indent) . "\n"; + $inner = ($options['indent'] ?? true) ? ("\n" . Formatting::indent($inner, $indent) . "\n") : $inner; } $out .= "<{$tag}{$attrs}>{$inner}"; } else { diff --git a/tests/Util/HTMLTest.php b/tests/Util/HTMLTest.php index 30e81cf76f..0df4916097 100644 --- a/tests/Util/HTMLTest.php +++ b/tests/Util/HTMLTest.php @@ -35,7 +35,7 @@ class HTMLTest extends WebTestCase static::assertSame("\n

\n

\n
\n", HTML::html(['a' => ['attrs' => ['href' => 'test'], 'p' => '']])); static::assertSame("\n

\n foo\n

\n
\n
\n", HTML::html(['a' => ['p' => 'foo', 'br' => 'empty']])); static::assertThrows(\InvalidArgumentException::class, fn () => HTML::html(1)); - static::assertSame("\n foo\n", implode("\n", HTML::tag('a', ['href' => 'test'], content: 'foo', empty_tag: false))); - static::assertSame('
', implode("\n", HTML::tag('br', attrs: null, content: null, empty_tag: true))); + static::assertSame("\n foo\n", implode("\n", HTML::tag('a', ['href' => 'test'], content: 'foo', options: ['empty' => false]))); + static::assertSame('
', implode("\n", HTML::tag('br', attrs: null, content: null, options: ['empty' => true]))); } }