Skip to content

Commit

Permalink
fix attributes with a dash inside them not being parsed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal committed Aug 11, 2024
1 parent 68b5796 commit 11f7456
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/TagEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,16 @@ protected function replaceComponent(array $matches): string
protected function parseAttributes(string $attributesString): array
{
// Regex to match attributes (both static and dynamic)
$pattern = '/(:?\w+)=["\']([^"\']+)["\']/';
$pattern = '/([:?\w-]+)=["\']([^"\']+)["\']/';
preg_match_all($pattern, $attributesString, $matches, PREG_SET_ORDER);

$attributes = [];
foreach ($matches as $match) {
$name = $match[1];
$value = $match[2];

$name = str_replace('-', '_', $name); // Replace hyphens with underscores so that it works with properties

// Check if it's a dynamic attribute (starts with ":")
if (str_starts_with($name, ':')) {
$varName = substr($name, 1); // remove the leading ":"
Expand Down
2 changes: 1 addition & 1 deletion tests/TagEngine/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testCacheWillBeFilledAndRead(): void
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe>
HTML;
$this->assertSame($expected, $result);
Expand Down
33 changes: 26 additions & 7 deletions tests/TagEngine/CustomTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,26 @@ public function testTagWithAttribute(): void
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe>
HTML;
$this->assertSame($expected, $result);
}

/**
* Test a tag with a longer attribute
*
* @return void
*/
public function testTagWithLongerAttribute(): void
{
$element = '<c-youtube src="RLdsCL4RDf8" data-test-something="test" />';
$result = $this->tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315" test
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
</iframe>
HTML;
$this->assertSame($expected, $result);
Expand All @@ -55,7 +74,7 @@ public function testTagWithAttributeSelfClosing(): void
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe>
HTML;
$this->assertSame($expected, $result);
Expand All @@ -74,11 +93,11 @@ public function testMultipleTagsWithAttributeSelfClosing(): void
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe> <iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe>
HTML;
$this->assertSame($expected, $result);
Expand Down Expand Up @@ -182,7 +201,7 @@ public function testTagWithAttributeAndNormalHTML(): void
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe><div>Test</div>
HTML;
$this->assertSame($expected, $result);
Expand All @@ -201,7 +220,7 @@ public function testTagWithAttributeSelfClosingAndNormalHTML(): void
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe><div>Test</div><input type="text"/>
HTML;
$this->assertSame($expected, $result);
Expand Down Expand Up @@ -254,7 +273,7 @@ public function testWithDivWrapped(): void
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe>
</div>
HTML;
Expand Down
5 changes: 3 additions & 2 deletions tests/Tags/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @property string $src
* @property string $data_test_something
*/
class Youtube extends CustomTag
{
Expand All @@ -15,10 +16,10 @@ class Youtube extends CustomTag
public function render(): string
{
return <<< HTML
<iframe width="560" height="315"
<iframe width="560" height="315" $this->data_test_something
src="https://www.youtube.com/embed/{$this->src}"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
allowfullscreen
</iframe>
HTML;
}
Expand Down

0 comments on commit 11f7456

Please sign in to comment.