Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion src/Markup.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ class Markup implements \Countable, \JsonSerializable, \Stringable
{
private $content;
private ?string $charset;
private array $options;

public function __construct($content, $charset)
public function __construct($content, $charset, array $options = [])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should pass an array of strategies there, not an array of options.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I thought about that too, but was unsure whether there can be more options. Also, this would make the syntax kinda identical with functions and filters. But I'm totally fine with either way.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think the Markup value object need to be consistent in syntax with functions and filters. They have totally different purposes.

{
$this->content = (string) $content;
$this->charset = $charset;

$this->options = array_merge([
'is_safe' => null,
], $options);
}

public function __toString(): string
Expand All @@ -37,6 +42,11 @@ public function getCharset(): string
return $this->charset;
}

public function getSafe(): array|null
{
return $this->options['is_safe'] ?? null;
}

/**
* @return int
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/EscaperRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function addSafeClass(string $class, array $strategies)
*/
public function escape($string, string $strategy = 'html', ?string $charset = null, bool $autoescape = false)
{
if ($autoescape && $string instanceof Markup) {
if ($autoescape && $string instanceof Markup && (null === $string->getSafe() || \in_array($strategy, $string->getSafe(), true))) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should probably take into account the case of strategies that are subset of others (html_attr is safe for html_attr_relaxed and html, and html_attr_relaxed is safe for html)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I didn't know about that! Does that also apply to functions and filters? If my function [is_safe => 'html_attr'], does a foo.html.twig skip autoescaping? Could you point to any code to look at?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

return $string;
}

Expand Down