Skip to content

Commit c4cb1f8

Browse files
committed
fix more normal HTML problems
1 parent 0a537ce commit c4cb1f8

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ $tagEngine = new \LordSimal\CustomHtmlElements\TagEngine([
163163
]);
164164
```
165165

166+
## More examples?
167+
168+
See the [TagEngineTest](https://github.com/LordSimal/custom-html-elements/blob/main/tests/TagEngineTest.php) for all kinds of different variants how to use the TagEngine
169+
166170
## Acknowledgements
167171

168172
This library is inspired by the following packages

Diff for: src/TagEngine.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function setDefaults(): void
6262
TagRegistry::register($class);
6363
}
6464
// Also catch all other HTML elements
65-
$searchRegex .= '|\b.*>\b';
65+
$searchRegex .= '|\b\w*[>\s]\b';
6666
}
6767
$this->searchReg = "<($searchRegex)";
6868
}
@@ -221,7 +221,7 @@ public function processTags(string $source): array
221221
$closer = "</$tagName>";
222222
$currentSource = substr($source, $eot); // HTML from Last occurrence till end or Last processed Tag
223223
$nextDOM = strpos($currentSource, '<', 1); // Start of Next DOM Tag
224-
$nextCloseTag = strpos($currentSource, '/' . '>'); // Close Bracket Loc
224+
$nextCloseTag = strpos($currentSource, '/>'); // Close Bracket Loc
225225

226226
if ($nextCloseTag !== false && ($nextCloseTag < $nextDOM || ($nextCloseTag && $nextDOM === false))) {
227227
// Closing DOM is before the next DOM element (indicates <tag /> format)

Diff for: tests/TagEngineTest.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use RecursiveIteratorIterator;
1111

1212
/**
13-
* @see TagEngine
13+
* @see \LordSimal\CustomHtmlElements\TagEngine
1414
*/
1515
class TagEngineTest extends TestCase
1616
{
@@ -294,7 +294,7 @@ public function testTagWithAttributeAndNormalHTML(): void
294294
*/
295295
public function testTagWithAttributeSelfClosingAndNormalHTML(): void
296296
{
297-
$element = '<c-youtube src="RLdsCL4RDf8" /><div>Test</div>';
297+
$element = '<c-youtube src="RLdsCL4RDf8" /><div>Test</div><input type="text" />';
298298
$tagEngine = new TagEngine([
299299
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
300300
]);
@@ -304,7 +304,7 @@ public function testTagWithAttributeSelfClosingAndNormalHTML(): void
304304
src="https://www.youtube.com/embed/RLdsCL4RDf8"
305305
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
306306
allowfullscreen>
307-
</iframe><div>Test</div>
307+
</iframe><div>Test</div><input type="text" />
308308
HTML;
309309
$this->assertSame($expected, $result);
310310
}
@@ -340,4 +340,28 @@ public function testNoCustomTag(): void
340340
$expected = '<div>This is a Test</div>';
341341
$this->assertSame($expected, $result);
342342
}
343+
344+
/**
345+
* Test output buffered
346+
*
347+
* @return void
348+
*/
349+
public function testOutputBuffered(): void
350+
{
351+
$element = '<c-github></c-github>';
352+
$tagEngine = new TagEngine([
353+
'tag_directories' => [
354+
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
355+
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
356+
],
357+
]);
358+
ob_start();
359+
echo $element;
360+
$result = $tagEngine->parse();
361+
$expected = <<<HTML
362+
This is a render from a plugin tag
363+
364+
HTML;
365+
$this->assertSame($expected, $result);
366+
}
343367
}

Diff for: tests/TagRegistryTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace LordSimal\CustomHtmlElements\Test;
5+
6+
use LordSimal\CustomHtmlElements\Error\TagNotFoundException;
7+
use LordSimal\CustomHtmlElements\TagRegistry;
8+
use LordSimal\CustomHtmlElements\Test\Tags\Button;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* @see \LordSimal\CustomHtmlElements\TagRegistry
13+
*/
14+
class TagRegistryTest extends TestCase
15+
{
16+
public function testRegisterAndGet(): void
17+
{
18+
TagRegistry::register(Button::class);
19+
$class = TagRegistry::getTag('c-button');
20+
$this->assertSame($class, Button::class);
21+
}
22+
23+
public function testRegisterAndGetAll(): void
24+
{
25+
TagRegistry::register(Button::class);
26+
$tags = TagRegistry::getTags();
27+
$this->assertSame($tags['c-button'], Button::class);
28+
}
29+
30+
public function testGetUnknownTag(): void
31+
{
32+
$this->expectException(TagNotFoundException::class);
33+
$this->expectExceptionMessage('Tag Unknown was not found.');
34+
TagRegistry::getTag('Unknown');
35+
}
36+
}

0 commit comments

Comments
 (0)