Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: TASK: Cleanup Tokenizer fallthrough switch case and forbid > inside tagContent #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Parser/Tokenizer/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static function period(\Iterator $fragments): \Iterator

public static function symbol(\Iterator $fragments, ?Buffer $buffer = null): \Iterator
{
$buffer = $buffer ?? Buffer::empty();
$buffer ??= Buffer::empty();
$capture = true;

while ($capture && $fragments->valid()) {
Expand Down Expand Up @@ -324,7 +324,7 @@ public static function angle(\Iterator $fragments): \Iterator
*/
public static function tag(\Iterator $fragments, ?Buffer $buffer = null): \Iterator
{
$buffer = $buffer ?? Buffer::empty();
$buffer ??= Buffer::empty();
$isClosing = false;

while ($fragments->valid()) {
Expand Down Expand Up @@ -410,16 +410,19 @@ public static function tagContent(\Iterator $fragments): \Iterator
yield from $buffer->flush(TokenType::STRING);
yield from self::block($fragments);
break;
case $fragment->value === '>':
throw new \Exception(sprintf('@TODO: Illegal Character "%s"', $fragment->value));
case $fragment->value === '<':
$fragments->next();
if ($fragments->current()?->value === '/') {
if (!$fragments->valid()) {
throw new \Exception("@TODO: Unexpected end of input");
}
if ($fragments->current()->value === '/') {
yield from $buffer->flush(TokenType::STRING);
return Buffer::empty()->append($fragment);
} else if (!ctype_space($fragments->current()?->value)) {
yield from self::tag($fragments, Buffer::empty()->append($fragment));
} else {
$buffer->append($fragment);
}
yield from self::tag($fragments, Buffer::empty()->append($fragment));
break;
case ctype_space($fragment->value):
yield from $buffer->flush(TokenType::STRING);
yield from self::space($fragments);
Expand Down
48 changes: 48 additions & 0 deletions test/Unit/Parser/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace PackageFactory\ComponentEngine\Test\Unit\Parser\Tokenizer;

use PackageFactory\ComponentEngine\Parser\Source\Source;
use PackageFactory\ComponentEngine\Parser\Tokenizer\Tokenizer;

class TokenizerTest extends \PHPUnit\Framework\TestCase
{
public function examplesInvalidTagContentsSyntax(): \Iterator
{
yield "> inside text" => [
'<p>acd>def<p>',
'@TODO: Illegal Character ">"'
];

yield "> between tags" => [
'<p>><p>',
'@TODO: Illegal Character ">"'
];

/*
* @todo no exception is thrown on tokenizer level ...
*
* <p>abc<def</p>
* <p>< </p>
* <p><</p>
*/
}

/**
* @test
* @dataProvider examplesInvalidTagContentsSyntax
*/
public function invalidTagContents(string $sourceCode, string $expectedMessage): void
{
$tokenizer = Tokenizer::fromSource(
Source::fromString($sourceCode)
);

$this->expectException(\Exception::class);
$this->expectExceptionMessage($expectedMessage);

foreach ($tokenizer as $token) {
continue;
}
}
}