Skip to content

escaping data:image when not required #1443

@8ctopus

Description

@8ctopus

I've noticed that the following CSS gets escaped when it doesn't need to:

$input = <<<CSS
a {
  background-image: url("data:image/svg+xml,%3Csvg width='24' height='24' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M14.3145 2 3V11.9987H17.5687L18 8.20761H14.3145L14.32 6.31012C14.32 5.32134 14.4207 4.79153 15.9426 4.79153H17.977V1H14.7223C10.8129 1 9.43687 2.83909 9.43687 5.93187V8.20804H7V11.9991H9.43687V23H14.3145Z' fill='black'/%3E%3C/svg%3E%0A");
}

CSS;

$document = (new \Sabberworm\CSS\Parser($input))
    ->parse();

$format = OutputFormat::createPretty();

echo $document->render($format);

result:

a {
        background-image: url("data:image/svg+xml,%3Csvg width=\'24\' height=\'24\' viewBox=\'0 0 24 24\' fill
=\'none\' xmlns=\'http://www.w3.org/2000/svg\'%3E%3Cpath fill-rule=\'evenodd\' clip-rule=\'evenodd\' d=\'M14.3
145 2 3V11.9987H17.5687L18 8.20761H14.3145L14.32 6.31012C14.32 5.32134 14.4207 4.79153 15.9426 4.79153H17.977V
1H14.7223C10.8129 1 9.43687 2.83909 9.43687 5.93187V8.20804H7V11.9991H9.43687V23H14.3145Z\' fill=\'black\'/%3E
%3C/svg%3E%0A");
}

This happens in the following code:

public function render(OutputFormat $outputFormat): string
{
$string = \addslashes($this->string);
$string = \str_replace("\n", '\\A', $string);
return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType();
}

Would it be possible to check the quoting type and only escape when actually required?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions