-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #202 Fix double escaping in stimulus DTOs when using toArray() in…
… combination with form methods (jeroennoten) This PR was merged into the main branch. Discussion ---------- Fix double escaping in stimulus DTOs when using toArray() in combination with form methods This fixes a double-escaping bug when the `toArray()` methods are used in combination with the `form_` functions, as described in the README. `toArray()` should return an array with non-escaped attribute key-value pairs, because they will be escaped when printed. Therefore, I moved the escaping of the values to the `toString()` methods and I added some additional tests for the DTO classes to verify the change. This additionally fixes a missed escape for the `data-[controller]-[key]-class` attribute values. Commits ------- 0d40126 Fix double escaping in stimulus DTOs when using toArray() in combination with form methods
- Loading branch information
Showing
8 changed files
with
151 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony WebpackEncoreBundle package. | ||
* (c) Fabien Potencier <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\WebpackEncoreBundle\Tests\Dto; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\WebpackEncoreBundle\Dto\StimulusActionsDto; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
class StimulusActionsDtoTest extends TestCase | ||
{ | ||
/** | ||
* @var StimulusActionsDto | ||
*/ | ||
private $stimulusActionsDto; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->stimulusActionsDto = new StimulusActionsDto(new Environment(new ArrayLoader())); | ||
} | ||
|
||
public function testToStringEscapingAttributeValues(): void | ||
{ | ||
$this->stimulusActionsDto->addAction('foo', 'bar', 'baz', ['qux' => '"']); | ||
$attributesHtml = (string) $this->stimulusActionsDto; | ||
self::assertSame('data-action="baz->foo#bar" data-foo-qux-param="""', $attributesHtml); | ||
} | ||
|
||
public function testToArrayNoEscapingAttributeValues(): void | ||
{ | ||
$this->stimulusActionsDto->addAction('foo', 'bar', 'baz', ['qux' => '"']); | ||
$attributesArray = $this->stimulusActionsDto->toArray(); | ||
self::assertSame(['data-action' => 'baz->foo#bar', 'data-foo-qux-param' => '"'], $attributesArray); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony WebpackEncoreBundle package. | ||
* (c) Fabien Potencier <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\WebpackEncoreBundle\Tests\Dto; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\WebpackEncoreBundle\Dto\StimulusControllersDto; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
class StimulusControllersDtoTest extends TestCase | ||
{ | ||
/** | ||
* @var StimulusControllersDto | ||
*/ | ||
private $stimulusControllersDto; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->stimulusControllersDto = new StimulusControllersDto(new Environment(new ArrayLoader())); | ||
} | ||
|
||
public function testToStringEscapingAttributeValues(): void | ||
{ | ||
$this->stimulusControllersDto->addController('foo', ['bar' => '"'], ['baz' => '"']); | ||
$attributesHtml = (string) $this->stimulusControllersDto; | ||
self::assertSame( | ||
'data-controller="foo" '. | ||
'data-foo-bar-value=""" '. | ||
'data-foo-baz-class="""', | ||
$attributesHtml | ||
); | ||
} | ||
|
||
public function testToArrayNoEscapingAttributeValues(): void | ||
{ | ||
$this->stimulusControllersDto->addController('foo', ['bar' => '"'], ['baz' => '"']); | ||
$attributesArray = $this->stimulusControllersDto->toArray(); | ||
self::assertSame( | ||
[ | ||
'data-controller' => 'foo', | ||
'data-foo-bar-value' => '"', | ||
'data-foo-baz-class' => '"', | ||
], | ||
$attributesArray | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony WebpackEncoreBundle package. | ||
* (c) Fabien Potencier <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\WebpackEncoreBundle\Tests\Dto; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\WebpackEncoreBundle\Dto\StimulusTargetsDto; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
class StimulusTargetsDtoTest extends TestCase | ||
{ | ||
/** | ||
* @var StimulusTargetsDto | ||
*/ | ||
private $stimulusTargetsDto; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->stimulusTargetsDto = new StimulusTargetsDto(new Environment(new ArrayLoader())); | ||
} | ||
|
||
public function testToStringEscapingAttributeValues(): void | ||
{ | ||
$this->stimulusTargetsDto->addTarget('foo', '"'); | ||
$attributesHtml = (string) $this->stimulusTargetsDto; | ||
self::assertSame('data-foo-target="""', $attributesHtml); | ||
} | ||
|
||
public function testToArrayNoEscapingAttributeValues(): void | ||
{ | ||
$this->stimulusTargetsDto->addTarget('foo', '"'); | ||
$attributesArray = $this->stimulusTargetsDto->toArray(); | ||
self::assertSame(['data-foo-target' => '"'], $attributesArray); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters