Skip to content

Commit

Permalink
fix normal HTMl not working
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal committed Oct 14, 2023
1 parent 108ba58 commit b9bfcfc
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/CustomTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public function __construct(
$this->placeholder = '------@@%' . $instance . '-' . $index . '%@@------';
if (str_ends_with($this->block, '/>')) {
$this->isSelfClosing = true;
//$this->tagclose = "\/>";
}
$tag = static::$tag;
$this->tagSearch = "/<($tag)\s*([^$this->tagclose]*)/";
Expand Down
2 changes: 1 addition & 1 deletion src/Error/TagNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace LordSimal\CustomHtmlElements;
namespace LordSimal\CustomHtmlElements\Error;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/SimpleTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class SimpleTag extends CustomTag
*/
public function render(): string
{
return $this->content;
return $this->block;
}
}
27 changes: 16 additions & 11 deletions src/TagEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace LordSimal\CustomHtmlElements;

use LordSimal\CustomHtmlElements\Error\TagNotFoundException;
use Spatie\StructureDiscoverer\Discover;

class TagEngine
Expand Down Expand Up @@ -62,6 +63,8 @@ public function setDefaults(): void
$searchRegex .= '\b' . $class::$tag . '\b';
TagRegistry::register($class);
}
// Also catch all other HTML elements
$searchRegex .= '|\b.*>\b';
}
$this->searchReg = "<($searchRegex)";
}
Expand Down Expand Up @@ -125,7 +128,7 @@ private function renderTag(CustomTag|array $tag): bool|string
$tag_data = call_user_func_array([$this->options['custom_cache_tag_class'], 'getCache'], [$tag]);
} else {
$cache_file = $this->options['cache_directory'] . md5(serialize($tag));
if (is_file($cache_file) === true) {
if (is_file($cache_file)) {
$tag_data = file_get_contents($cache_file);
}
}
Expand All @@ -148,7 +151,7 @@ private function renderTag(CustomTag|array $tag): bool|string
if ($this->options['custom_cache_tag_class'] !== false) {
call_user_func_array([$this->options['custom_cache_tag_class'], 'cache'], [$tag, $tag_data]);
} else {
file_put_contents($this->options['cache_directory'] . md5(serialize($tag)), $tag_data, LOCK_EX);
file_put_contents($this->options['cache_directory'] . md5(serialize($tag)), $tag_data);
}
}
}
Expand All @@ -165,7 +168,8 @@ private function renderTag(CustomTag|array $tag): bool|string
private function renderTags(array $tags): mixed
{
if ($tags) {
foreach ($tags as $key => &$tag) {
$resultHtml = '';
foreach ($tags as $tag) {
// Loop through Tags
if ($tag->attributes->delayed ?? false) {
continue;
Expand Down Expand Up @@ -200,9 +204,10 @@ private function renderTags(array $tags): mixed
}
$tag->parsedcontent = $body;
$tag->parsed = true;
$resultHtml .= $body;
}

return $tags[$key]->parsedcontent;
return $resultHtml;
}

return false;
Expand All @@ -229,7 +234,6 @@ private function getLastTag(string $subject): bool|array
*
* @param string $source The source to search for custom tags in.
* @return array An array of found tags.
* @throws \LordSimal\CustomHtmlElements\TagNotFoundException
*/
public function processTags(string $source): array
{
Expand All @@ -249,7 +253,7 @@ public function processTags(string $source): array
$tags[] = $tag;
break;
} else { // Tag found (start from last find)
$tagName = str_replace('<', '', $eot[0]);
$tagName = str_replace(['<','>'], '', $eot[0]);
$eot = $eot[1];
$closer = "</$tagName>";
$currentSource = substr($source, $eot); // HTML from Last occurrence till end or Last processed Tag
Expand All @@ -266,18 +270,19 @@ public function processTags(string $source): array
}

$tag_source = substr($currentSource, 0, $TagClose);
$Class = TagRegistry::getTag($tagName);
if (!class_exists($Class)) {
try {
$class = TagRegistry::getTag($tagName);
$tag = new $class($tag_source, self::$instance, count($tags));
} catch (TagNotFoundException) {
$tag = new SimpleTag($tag_source, self::$instance, count($tags));
} else {
$tag = new $Class($tag_source, self::$instance, count($tags));
}

$tags[] = $tag;

$source = substr($source, 0, $eot) . $tag->placeholder . substr($source, $eot + $TagClose); // Update Source for next request
}
}

return $tags;
return array_reverse($tags);
}
}
4 changes: 3 additions & 1 deletion src/TagRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace LordSimal\CustomHtmlElements;

use LordSimal\CustomHtmlElements\Error\TagNotFoundException;

class TagRegistry
{
protected static array $listOfTags = [];
Expand All @@ -27,7 +29,7 @@ public static function getTags(): array
/**
* @param string $tag
* @return string
* @throws \LordSimal\CustomHtmlElements\TagNotFoundException
* @throws \LordSimal\CustomHtmlElements\Error\TagNotFoundException
*/
public static function getTag(string $tag): string
{
Expand Down
96 changes: 96 additions & 0 deletions tests/TagEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,41 @@

namespace LordSimal\CustomHtmlElements\Test;

use FilesystemIterator;
use LordSimal\CustomHtmlElements\TagEngine;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
* @see TagEngine
*/
class TagEngineTest extends TestCase
{
protected const CACHE_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;

protected function setUp(): void
{
if (!file_exists(self::CACHE_DIR)) {
mkdir(self::CACHE_DIR);
}
}

protected function tearDown(): void
{
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(self::CACHE_DIR, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}

rmdir(self::CACHE_DIR);
}

/**
* Test a tab with a simple attribute
*
Expand Down Expand Up @@ -157,6 +184,75 @@ public function testNestedContentRendersWithConfig(): void
$expected = <<<HTML
This is a render from a plugin tag
HTML;
$this->assertSame($expected, $result);
}

/**
* Test that sub-tags will trigger cache correctly
*
* @return void
*/
public function testNestedContentRendersWithCache(): void
{
$element = '<c-nested />';
$tagEngine = new TagEngine([
'tag_directories' => [
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
'sniff_for_nested_tags' => true,
'cache_tags' => true,
'cache_directory' => self::CACHE_DIR,
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
This is a render from a plugin tag
HTML;
$this->assertSame($expected, $result);
}

/**
* Test tag variant and normal HTML
*
* @return void
*/
public function testTagWithAttributeAndNormalHTML(): void
{
$element = '<c-youtube src="RLdsCL4RDf8"></c-youtube><div>Test</div>';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe><div>Test</div>
HTML;
$this->assertSame($expected, $result);
}

/**
* Test self-closing tag variant and normal HTML
*
* @return void
*/
public function testTagWithAttributeSelfClosingAndNormalHTML(): void
{
$element = '<c-youtube src="RLdsCL4RDf8" /><div>Test</div>';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe><div>Test</div>
HTML;
$this->assertSame($expected, $result);
}
Expand Down

0 comments on commit b9bfcfc

Please sign in to comment.