Skip to content

Commit cc0c377

Browse files
Merge pull request #39 from Stillat/additional-attribute-parsing
Correct echo attribute parsing; add test coverage
2 parents 299ab17 + 10d6f35 commit cc0c377

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

.phpunit.cache/test-results

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/Parser/ComponentParser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ private function containsAttributesVar(string $content): bool
194194
return Str::contains(mb_strtolower($content), '$attributes');
195195
}
196196

197+
public static $break = false;
198+
197199
private function parseParameters(): array
198200
{
199201
$containsValue = false;
@@ -211,17 +213,18 @@ private function parseParameters(): array
211213
$startIndex = $this->currentIndex;
212214
}
213215

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

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

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

tests/Parser/TagComponentsTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,36 @@
423423
expect($nodes[1])->toBeInstanceOf(LiteralNode::class);
424424
expect($nodes[2])->toBeInstanceOf(ComponentNode::class);
425425
});
426+
427+
test('echo params are parsed', function () {
428+
$template = <<<'EOT'
429+
<x-alert
430+
data-one={{ $something }}
431+
data-two={{{ $something }}}
432+
data-three={!! $something !!}
433+
/>
434+
EOT;
435+
$nodes = $this->parser()->onlyParseComponents()->parse($template);
436+
437+
expect($nodes)->toHaveCount(1);
438+
expect($nodes[0])->toBeInstanceOf(ComponentNode::class);
439+
440+
/** @var ComponentNode $component */
441+
$component = $nodes[0];
442+
expect($component->parameters)->toHaveCount(3);
443+
444+
/** @var ParameterNode $param1 */
445+
$param1 = $component->parameters[0];
446+
expect($param1->content)->toBe('data-one={{ $something }}');
447+
expect($param1->type)->toBe(ParameterType::UnknownEcho);
448+
449+
/** @var ParameterNode $param2 */
450+
$param2 = $component->parameters[1];
451+
expect($param2->content)->toBe('data-two={{{ $something }}}');
452+
expect($param2->type)->toBe(ParameterType::UnknownTripleEcho);
453+
454+
/** @var ParameterNode $param3 */
455+
$param3 = $component->parameters[2];
456+
expect($param3->content)->toBe('data-three={!! $something !!}');
457+
expect($param3->type)->toBe(ParameterType::UnknownRawEcho);
458+
});

0 commit comments

Comments
 (0)