From d348e41ff5e4a1c97cfddb525dd76a03c983d17a Mon Sep 17 00:00:00 2001 From: Ben Croker Date: Thu, 21 Oct 2021 13:05:00 +0200 Subject: [PATCH] Fixed double encoding in nested components --- CHANGELOG.md | 4 ++++ composer.json | 2 +- src/helpers/HtmlHelper.php | 21 +++++++++++++++++++++ src/services/ComponentsService.php | 10 +++++----- tests/unit/services/ComponentsTest.php | 16 ++++++++-------- 5 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 src/helpers/HtmlHelper.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1866048..d639157 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Sprig Core +## 1.1.3 - 2021-10-21 +### Fixed +- Fixed a bug in which attributes could be double encoded in nested components ([#176](https://github.com/putyourlightson/craft-sprig/issues/176), [#178](https://github.com/putyourlightson/craft-sprig/issues/178)). + ## 1.1.2 - 2021-10-20 ### Fixed - Fixed a bug in which using `s-action` could throw an exception when parsed ([#177](https://github.com/putyourlightson/craft-sprig/issues/177)). diff --git a/composer.json b/composer.json index 562b1a1..e66b45c 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "putyourlightson/craft-sprig-core", "description": "A reactive Twig component framework for Craft.", - "version": "1.1.2", + "version": "1.1.3", "type": "craft-module", "license": "mit", "require": { diff --git a/src/helpers/HtmlHelper.php b/src/helpers/HtmlHelper.php new file mode 100644 index 0000000..8f21038 --- /dev/null +++ b/src/helpers/HtmlHelper.php @@ -0,0 +1,21 @@ +charset : 'UTF-8', $doubleEncode); + } +} diff --git a/src/services/ComponentsService.php b/src/services/ComponentsService.php index 208b28f..a094429 100644 --- a/src/services/ComponentsService.php +++ b/src/services/ComponentsService.php @@ -8,7 +8,6 @@ use Craft; use craft\base\Component as BaseComponent; use craft\base\ElementInterface; -use craft\helpers\Html; use craft\helpers\Json; use craft\helpers\StringHelper; use craft\helpers\Template; @@ -18,6 +17,7 @@ use putyourlightson\sprig\base\Component; use putyourlightson\sprig\errors\InvalidVariableException; use putyourlightson\sprig\events\ComponentEvent; +use putyourlightson\sprig\helpers\HtmlHelper; use putyourlightson\sprig\Sprig; use putyourlightson\sprig\plugin\components\SprigPlayground; use Twig\Markup; @@ -149,7 +149,7 @@ public function create(string $value, array $variables = [], array $attributes = $this->_parseAttributes($attributes); - $event->output = Html::tag('div', $content, $attributes); + $event->output = HtmlHelper::tag('div', $content, $attributes); if ($this->hasEventHandlers(self::EVENT_AFTER_CREATE_COMPONENT)) { $this->trigger(self::EVENT_AFTER_CREATE_COMPONENT, $event); @@ -233,16 +233,16 @@ private function _getParseableTags(string $content): array private function _getParsedTag(string $tag) { try { - $attributes = Html::parseTagAttributes($tag); + $attributes = HtmlHelper::parseTagAttributes($tag); } catch (InvalidArgumentException $exception) { return null; } - $name = substr($tag, 1, strpos($tag, ' ')); + $name = substr($tag, 1, strpos($tag, ' ') - 1); $this->_parseAttributes($attributes); - return '<' . $name . Html::renderTagAttributes($attributes) . '>'; + return HtmlHelper::beginTag($name, $attributes); } /** diff --git a/tests/unit/services/ComponentsTest.php b/tests/unit/services/ComponentsTest.php index d41d057..9dc6515 100644 --- a/tests/unit/services/ComponentsTest.php +++ b/tests/unit/services/ComponentsTest.php @@ -134,23 +134,23 @@ public function testGetParsedTagAttributesWithData() public function testGetParsedTagAttributesVals() { - $html = '
'; + $html = '
'; $html = Sprig::$core->components->parse($html); $this->assertStringContainsString('data-hx-vals="{"xYZ":"a","limit":1}"', $html); } - public function testGetParsedTagAttributesValsWithBrackets() + public function testGetParsedTagAttributesValsWithEncoded() { - $html = '
'; + $html = '
'; $html = Sprig::$core->components->parse($html); - $this->assertStringContainsString('data-hx-vals="{"fields[xYZ]":"a"}"', $html); + $this->assertStringContainsString('data-hx-vals="{"xYZ":"a","limit":1}"', $html); } - public function testGetParsedTagAttributesValsWithEncoded() + public function testGetParsedTagAttributesValsWithBrackets() { - $html = '
'; + $html = '
'; $html = Sprig::$core->components->parse($html); - $this->assertStringContainsString('data-hx-vals="{"limit":1}"', $html); + $this->assertStringContainsString('data-hx-vals="{"fields[xYZ]":"a"}"', $html); } public function testGetParsedTagAttributesValsEncodedAndSanitized() @@ -183,7 +183,7 @@ public function testGetParsedTagAttributesDuplicateIds() public function testGetParsedTagAttributesComment() { - $html = ''; + $html = ''; $result = Sprig::$core->components->parse($html); $this->assertEquals($html, $result); }