Skip to content

Commit

Permalink
Merge pull request #39 from Stillat/additional-attribute-parsing
Browse files Browse the repository at this point in the history
Correct echo attribute parsing; add test coverage
  • Loading branch information
JohnathonKoster authored Jan 24, 2025
2 parents 299ab17 + 10d6f35 commit cc0c377
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions .phpunit.cache/test-results

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/Parser/ComponentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ private function containsAttributesVar(string $content): bool
return Str::contains(mb_strtolower($content), '$attributes');
}

public static $break = false;

private function parseParameters(): array
{
$containsValue = false;
Expand All @@ -211,17 +213,18 @@ private function parseParameters(): array
$startIndex = $this->currentIndex;
}

if ($this->cur == self::C_LeftCurlyBracket && $this->next == self::C_LeftCurlyBracket && $this->fetchAtRelative(3, 1) == self::C_LeftCurlyBracket) {
if ($this->cur == self::C_LeftCurlyBracket && $this->next == self::C_LeftCurlyBracket && $this->fetchAtRelative($this->currentIndex + 2, 1) == self::C_LeftCurlyBracket) {
$this->seekToEndOfTripleEcho();
$parameters[] = $this->makeEchoParameter(ParameterType::AttributeTripleEcho, $startIndex);

continue;
} if ($this->cur == self::C_LeftCurlyBracket && $this->next == self::C_ExclamationMark && $this->fetchAtRelative(3, 1) == self::C_ExclamationMark) {
} if ($this->cur == self::C_LeftCurlyBracket && $this->next == self::C_ExclamationMark && $this->fetchAtRelative($this->currentIndex + 2, 1) == self::C_ExclamationMark) {
$this->seekToEndOfRawEcho();
$parameters[] = $this->makeEchoParameter(ParameterType::AttributeRawEcho, $startIndex);

continue;
} elseif ($this->cur == self::C_LeftCurlyBracket && $this->next == self::C_LeftCurlyBracket) {
self::$break = true;
$this->seekEndOfEcho();
$parameters[] = $this->makeEchoParameter(ParameterType::AttributeEcho, $startIndex);

Expand Down
33 changes: 33 additions & 0 deletions tests/Parser/TagComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,36 @@
expect($nodes[1])->toBeInstanceOf(LiteralNode::class);
expect($nodes[2])->toBeInstanceOf(ComponentNode::class);
});

test('echo params are parsed', function () {
$template = <<<'EOT'
<x-alert
data-one={{ $something }}
data-two={{{ $something }}}
data-three={!! $something !!}
/>
EOT;
$nodes = $this->parser()->onlyParseComponents()->parse($template);

expect($nodes)->toHaveCount(1);
expect($nodes[0])->toBeInstanceOf(ComponentNode::class);

/** @var ComponentNode $component */
$component = $nodes[0];
expect($component->parameters)->toHaveCount(3);

/** @var ParameterNode $param1 */
$param1 = $component->parameters[0];
expect($param1->content)->toBe('data-one={{ $something }}');
expect($param1->type)->toBe(ParameterType::UnknownEcho);

/** @var ParameterNode $param2 */
$param2 = $component->parameters[1];
expect($param2->content)->toBe('data-two={{{ $something }}}');
expect($param2->type)->toBe(ParameterType::UnknownTripleEcho);

/** @var ParameterNode $param3 */
$param3 = $component->parameters[2];
expect($param3->content)->toBe('data-three={!! $something !!}');
expect($param3->type)->toBe(ParameterType::UnknownRawEcho);
});

0 comments on commit cc0c377

Please sign in to comment.