Skip to content

Commit be7d40e

Browse files
committed
escape tag content and props
1 parent 7b41c30 commit be7d40e

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/Tags/Script.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Script extends Tag
88
{
99
public string $tag = 'script';
1010

11+
protected bool $escape = false;
12+
1113
public function __construct(
1214
public ?string $type = null,
1315
public ?string $content = null,

src/Tags/Tag.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ abstract class Tag extends TagVoid
66
{
77
public ?string $content = null;
88

9+
protected bool $escape = true;
10+
911
public function toHtml(): string
1012
{
11-
return "<{$this->tag} {$this->toProperties()->join(' ')}>{$this->content}</{$this->tag}>";
13+
$content = $this->escape ? e($this->content, false) : $this->content;
14+
15+
return "<{$this->tag} {$this->toProperties()->join(' ')}>{$content}</{$this->tag}>";
1216
}
1317
}

src/Tags/TagVoid.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ abstract class TagVoid implements Htmlable
2222
*/
2323
public function toProperties(): Collection
2424
{
25+
if (! $this->properties) {
26+
return new Collection;
27+
}
28+
2529
return $this->properties
26-
?->map(fn (?string $value) => $value ? trim($value) : null)
27-
->map(fn (?string $value, string $property) => "{$property}=\"{$value}\"")
28-
?? new Collection;
30+
->map(function (string $value, string $property) {
31+
$value = e(trim($value));
32+
33+
return "{$property}=\"{$value}\"";
34+
});
2935
}
3036

3137
public function toHtml(): string

0 commit comments

Comments
 (0)