Skip to content

Commit 11f7456

Browse files
committed
fix attributes with a dash inside them not being parsed correctly
1 parent 68b5796 commit 11f7456

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

src/TagEngine.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,16 @@ protected function replaceComponent(array $matches): string
131131
protected function parseAttributes(string $attributesString): array
132132
{
133133
// Regex to match attributes (both static and dynamic)
134-
$pattern = '/(:?\w+)=["\']([^"\']+)["\']/';
134+
$pattern = '/([:?\w-]+)=["\']([^"\']+)["\']/';
135135
preg_match_all($pattern, $attributesString, $matches, PREG_SET_ORDER);
136136

137137
$attributes = [];
138138
foreach ($matches as $match) {
139139
$name = $match[1];
140140
$value = $match[2];
141141

142+
$name = str_replace('-', '_', $name); // Replace hyphens with underscores so that it works with properties
143+
142144
// Check if it's a dynamic attribute (starts with ":")
143145
if (str_starts_with($name, ':')) {
144146
$varName = substr($name, 1); // remove the leading ":"

tests/TagEngine/CacheTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testCacheWillBeFilledAndRead(): void
6363
<iframe width="560" height="315"
6464
src="https://www.youtube.com/embed/RLdsCL4RDf8"
6565
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
66-
allowfullscreen>
66+
allowfullscreen
6767
</iframe>
6868
HTML;
6969
$this->assertSame($expected, $result);

tests/TagEngine/CustomTagsTest.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,26 @@ public function testTagWithAttribute(): void
3636
<iframe width="560" height="315"
3737
src="https://www.youtube.com/embed/RLdsCL4RDf8"
3838
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
39-
allowfullscreen>
39+
allowfullscreen
40+
</iframe>
41+
HTML;
42+
$this->assertSame($expected, $result);
43+
}
44+
45+
/**
46+
* Test a tag with a longer attribute
47+
*
48+
* @return void
49+
*/
50+
public function testTagWithLongerAttribute(): void
51+
{
52+
$element = '<c-youtube src="RLdsCL4RDf8" data-test-something="test" />';
53+
$result = $this->tagEngine->parse($element);
54+
$expected = <<<HTML
55+
<iframe width="560" height="315" test
56+
src="https://www.youtube.com/embed/RLdsCL4RDf8"
57+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
58+
allowfullscreen
4059
</iframe>
4160
HTML;
4261
$this->assertSame($expected, $result);
@@ -55,7 +74,7 @@ public function testTagWithAttributeSelfClosing(): void
5574
<iframe width="560" height="315"
5675
src="https://www.youtube.com/embed/RLdsCL4RDf8"
5776
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
58-
allowfullscreen>
77+
allowfullscreen
5978
</iframe>
6079
HTML;
6180
$this->assertSame($expected, $result);
@@ -74,11 +93,11 @@ public function testMultipleTagsWithAttributeSelfClosing(): void
7493
<iframe width="560" height="315"
7594
src="https://www.youtube.com/embed/RLdsCL4RDf8"
7695
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
77-
allowfullscreen>
96+
allowfullscreen
7897
</iframe> <iframe width="560" height="315"
7998
src="https://www.youtube.com/embed/RLdsCL4RDf8"
8099
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
81-
allowfullscreen>
100+
allowfullscreen
82101
</iframe>
83102
HTML;
84103
$this->assertSame($expected, $result);
@@ -182,7 +201,7 @@ public function testTagWithAttributeAndNormalHTML(): void
182201
<iframe width="560" height="315"
183202
src="https://www.youtube.com/embed/RLdsCL4RDf8"
184203
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
185-
allowfullscreen>
204+
allowfullscreen
186205
</iframe><div>Test</div>
187206
HTML;
188207
$this->assertSame($expected, $result);
@@ -201,7 +220,7 @@ public function testTagWithAttributeSelfClosingAndNormalHTML(): void
201220
<iframe width="560" height="315"
202221
src="https://www.youtube.com/embed/RLdsCL4RDf8"
203222
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
204-
allowfullscreen>
223+
allowfullscreen
205224
</iframe><div>Test</div><input type="text"/>
206225
HTML;
207226
$this->assertSame($expected, $result);
@@ -254,7 +273,7 @@ public function testWithDivWrapped(): void
254273
<iframe width="560" height="315"
255274
src="https://www.youtube.com/embed/RLdsCL4RDf8"
256275
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
257-
allowfullscreen>
276+
allowfullscreen
258277
</iframe>
259278
</div>
260279
HTML;

tests/Tags/Youtube.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* @property string $src
10+
* @property string $data_test_something
1011
*/
1112
class Youtube extends CustomTag
1213
{
@@ -15,10 +16,10 @@ class Youtube extends CustomTag
1516
public function render(): string
1617
{
1718
return <<< HTML
18-
<iframe width="560" height="315"
19+
<iframe width="560" height="315" $this->data_test_something
1920
src="https://www.youtube.com/embed/{$this->src}"
2021
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
21-
allowfullscreen>
22+
allowfullscreen
2223
</iframe>
2324
HTML;
2425
}

0 commit comments

Comments
 (0)