Skip to content

Commit

Permalink
escape tag content and props
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Nov 24, 2024
1 parent 7b41c30 commit be7d40e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Tags/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Script extends Tag
{
public string $tag = 'script';

protected bool $escape = false;

public function __construct(
public ?string $type = null,
public ?string $content = null,
Expand Down
6 changes: 5 additions & 1 deletion src/Tags/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ abstract class Tag extends TagVoid
{
public ?string $content = null;

protected bool $escape = true;

public function toHtml(): string
{
return "<{$this->tag} {$this->toProperties()->join(' ')}>{$this->content}</{$this->tag}>";
$content = $this->escape ? e($this->content, false) : $this->content;

return "<{$this->tag} {$this->toProperties()->join(' ')}>{$content}</{$this->tag}>";
}
}
12 changes: 9 additions & 3 deletions src/Tags/TagVoid.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ abstract class TagVoid implements Htmlable
*/
public function toProperties(): Collection
{
if (! $this->properties) {
return new Collection;
}

return $this->properties
?->map(fn (?string $value) => $value ? trim($value) : null)
->map(fn (?string $value, string $property) => "{$property}=\"{$value}\"")
?? new Collection;
->map(function (string $value, string $property) {
$value = e(trim($value));

return "{$property}=\"{$value}\"";
});
}

public function toHtml(): string
Expand Down

0 comments on commit be7d40e

Please sign in to comment.