Skip to content

Commit

Permalink
Fixed the parsing of empty s-val: values
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Oct 25, 2021
1 parent cf1a93d commit 23b2292
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Release Notes for Sprig Core

## 1.1.5 - Unreleased
### Fixed
- Fixed the parsing of empty `s-val:` values to ensure the value is maintained ([#178](https://github.com/putyourlightson/craft-sprig/issues/178)).

## 1.1.4 - 2021-10-22
### Fixed
- Fixed an issue in which attributes with spaces before or after the `=` were not being correctly parsed ([#178](https://github.com/putyourlightson/craft-sprig/issues/178)).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
@@ -1,7 +1,7 @@
{
"name": "putyourlightson/craft-sprig-core",
"description": "A reactive Twig component framework for Craft.",
"version": "1.1.4",
"version": "1.1.5",
"type": "craft-module",
"license": "mit",
"require": {
Expand Down
6 changes: 6 additions & 0 deletions src/services/ComponentsService.php
Expand Up @@ -320,6 +320,12 @@ private function _parseAttribute(array &$attributes, string $key, $value)
if (strpos($name, 'val:') === 0) {
$name = StringHelper::toCamelCase(substr($name, 4));

/**
* If the value is `true` then convert it back to a blank string
* @see HtmlHelper::parseTagAttribute()
*/
$value = $value === true ? '' : $value;

$this->_mergeJsonAttributes($attributes, 'vals', [$name => $value]);
}
elseif ($name == 'replace') {
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/services/ComponentsTest.php
Expand Up @@ -154,6 +154,13 @@ public function testGetParsedTagAttributesVals()
$this->assertStringContainsString('data-hx-vals="{"xYZ":"a","limit":1}"', $html);
}

public function testGetParsedTagAttributesValsWithEmpty()
{
$html = '<div s-val:x-y-z="" s-vals=\'{"limit":1}\'></div>';
$html = Sprig::$core->components->parse($html);
$this->assertStringContainsString('data-hx-vals="{&quot;xYZ&quot;:&quot;&quot;,&quot;limit&quot;:1}"', $html);
}

public function testGetParsedTagAttributesValsWithEncoded()
{
$html = '<div s-val:x-y-z="a" s-vals=\'{&quot;limit&quot;:1}\'></div>';
Expand Down

0 comments on commit 23b2292

Please sign in to comment.