Skip to content

Commit

Permalink
prevent already escaped double quotes from being escaped again, causi…
Browse files Browse the repository at this point in the history
…ng a syntax error
  • Loading branch information
thewunder committed Mar 21, 2024
1 parent caadeba commit 5b16741
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Util/StringLiteralFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function formatValueForRHS($value): string
{
if (is_string($value)) {
if (!static::isVariable($value)) {
$value = str_replace('"', '\"', $value);
$value = preg_replace(['/([^\\\])"/', '/^"/'], ['$1\"', '\"'], $value);
if (strpos($value, "\n") !== false) {
$value = '"""' . $value . '"""';
} else {
Expand Down
3 changes: 3 additions & 0 deletions tests/StringLiteralFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function testFormatForClassRHSValue()
$formattedString = StringLiteralFormatter::formatValueForRHS("\"quotedString\"");
$this->assertEquals('"\"quotedString\""', $formattedString);

$formattedString = StringLiteralFormatter::formatValueForRHS('<span class=\\"quotedStringEscaped\\" id="unescaped"></span>');
$this->assertEquals('"<span class=\\"quotedStringEscaped\\" id=\\"unescaped\\"></span>"', $formattedString);

$formattedString = StringLiteralFormatter::formatValueForRHS('\'singleQuotes\'');
$this->assertEquals('"\'singleQuotes\'"', $formattedString);

Expand Down

0 comments on commit 5b16741

Please sign in to comment.