Skip to content

Commit 905d477

Browse files
committed
fix attributes without a value not being parsed
1 parent 11f7456 commit 905d477

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ both are rendered the same way.
154154

155155
By default, this library renders nested custom HTML elements. So you don't need to worry about that.
156156

157+
## Disabling custom HTML elements
158+
159+
You have 2 ways how you can disable custom HTML elements:
160+
161+
### Disable all occurence of custom HTML elements
162+
163+
You can add the attributes
164+
165+
```php
166+
public bool $disabled = true;
167+
```
168+
169+
to your Custom HTML Element class.
170+
171+
### Disable only specific occurence of custom HTML elements
172+
173+
You can add the attribute `disabled`, then it will not be rendered.
174+
175+
```html
176+
<c-youtube src="RLdsCL4RDf8" disabled />
177+
```
178+
157179
## More examples?
158180

159181
See all the different [TagEngine Tests](https://github.com/LordSimal/custom-html-elements/blob/main/tests/TagEngine/)

src/TagEngine.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ 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];
140-
$value = $match[2];
140+
$value = $match[2] ?? true; // If no value, set it to true
141141

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

tests/TagEngine/CustomTagsTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ public function testMultipleTagsWithAttributeSelfClosing(): void
110110
*/
111111
public function testTagWithMultipleAttributes(): void
112112
{
113-
$element = '<c-button type="primary" text="Click me" url="/something/stupid" />';
113+
$element = '<c-button type="primary" text="Click me" url="/something/stupid" other />';
114114
$result = $this->tagEngine->parse($element);
115-
$expected = <<<HTML
116-
<a href="/something/stupid" class="c-button c-button--primary">Click me</a>
117-
HTML;
115+
$expected = '<a href="/something/stupid" class="c-button c-button--primary" other>Click me</a>';
118116
$this->assertSame($expected, $result);
119117
}
120118

tests/Tags/Button.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @property string $type
1010
* @property string $text
1111
* @property string $url
12+
* @property bool $other
1213
*/
1314
class Button extends CustomTag
1415
{
@@ -22,8 +23,6 @@ public function render(): string
2223
}
2324
$classes = implode(' ', $classes);
2425

25-
return <<< HTML
26-
<a href="$this->url" class="$classes">$this->text</a>
27-
HTML;
26+
return '<a href="' . $this->url . '" class="' . $classes . '"' . ($this->other ? ' other' : '') . '>' . $this->text . '</a>';
2827
}
2928
}

0 commit comments

Comments
 (0)