Skip to content
Merged
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
21 changes: 19 additions & 2 deletions src/BladeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,27 @@ public static function compileComments(string $input): string
/**
* Preprocess a component attribute string using Laravel's ComponentTagCompiler.
*
* Transforms {{ $attributes... }} into :attributes="...",
* @class(...) into :class="...", and @style(...) into :style="...".
* Runs all five of Laravel's preprocessing transforms:
* :$foo → :foo="$foo" (parseShortAttributeSyntax)
* {{ $attrs }} → :attributes="$attrs" (parseAttributeBag)
* @class(...) → :class="..." (parseComponentTagClassStatements)
* @style(...) → :style="..." (parseComponentTagStyleStatements)
* :attr= → bind:attr= (parseBindAttributes)
*/
public static function preprocessAttributeString(string $attributeString): string
{
$compiler = new ComponentTagCompiler(blade: app('blade.compiler'));

// Laravel expects a space at the start of the attribute string...
$attributeString = Str::start($attributeString, ' ');

return (function (string $str): string {
/** @var ComponentTagCompiler $this */
$str = $this->parseShortAttributeSyntax($str);
$str = $this->parseAttributeBag($str);
$str = $this->parseComponentTagClassStatements($str);
$str = $this->parseComponentTagStyleStatements($str);
$str = $this->parseBindAttributes($str);

return $str;
})->call($compiler, $attributeString);
Expand All @@ -238,6 +247,14 @@ public static function compileAttributeEchos(string $input): string
return Str::unwrap("'".$method->invoke($compiler, $input)."'", "''.", ".''");
}

/**
* Strip surrounding quotes from a string using ComponentTagCompiler.
*/
public static function stripQuotes(string $input): string
{
return (new ComponentTagCompiler(blade: app('blade.compiler')))->stripQuotes($input);
}

/**
* Register a callback to intercept view cache invalidation events.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected function compileComponentTag(ComponentNode $node, ComponentSource $sou
protected function compileDelegateComponentTag(ComponentNode $node): string
{
$attributesArray = Utils::parseAttributeStringToArray($node->attributeString);
$componentName = "'flux::' . " . $attributesArray['component']['value'];
$componentName = "'flux::' . " . $attributesArray['component']->value;

$output = '<' . '?php $__resolved = $__blaze->resolve(' . $componentName . '); ?>' . "\n";
$output .= '<' . '?php require_once $__blaze->compiledPath . \'/\' . $__resolved . \'.php\'; ?>' . "\n";
Expand Down
14 changes: 1 addition & 13 deletions src/Parser/Nodes/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Livewire\Blaze\Parser\Nodes;

use Illuminate\Support\Str;
use Livewire\Blaze\Support\AttributeParser;
use Livewire\Blaze\Support\Utils;
use Livewire\Blaze\Parser\Attribute;
Expand All @@ -26,18 +25,7 @@ public function __construct(
public array $attributes = [],
) {
if (empty($this->attributes) && ! empty($this->attributeString)) {
$attributes = Utils::parseAttributeStringToArray($this->attributeString);

foreach ($attributes as $key => $attribute) {
$this->attributes[$key] = new Attribute(
name: $attribute['name'],
value: $attribute['value'],
propName: $key,
dynamic: $attribute['isDynamic'] || str_contains($attribute['original'], '{{'),
prefix: Str::match('/^(::|\:\$|:)/', $attribute['original']),
quotes: $attribute['quotes'],
);
}
$this->attributes = Utils::parseAttributeStringToArray($this->attributeString);
}
}

Expand Down
13 changes: 1 addition & 12 deletions src/Parser/Nodes/SlotNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,7 @@ public function __construct(
public string $prefix = 'x-slot',
public bool $closeHasName = false,
) {
$attributes = Utils::parseAttributeStringToArray($this->attributeString);

foreach ($attributes as $key => $attribute) {
$this->attributes[$key] = new Attribute(
name: $attribute['name'],
value: $attribute['value'],
propName: $key,
dynamic: $attribute['isDynamic'] || str_contains($attribute['original'], '{{'),
prefix: \Illuminate\Support\Str::match('/^(:\$?)/', $attribute['original']),
quotes: $attribute['quotes'],
);
}
$this->attributes = Utils::parseAttributeStringToArray($this->attributeString);
}

/** {@inheritdoc} */
Expand Down
50 changes: 0 additions & 50 deletions src/Parser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,6 @@ protected function collectAttributes(): void
while (!$this->isAtEnd()) {
$char = $this->current();

// Skip over Blade echo expressions ({{ }} and {!! !!}) without
// updating quote state — quotes inside Blade tags are PHP-level
// and must not interfere with HTML attribute boundary detection.
if ($char === '{' && ($bladeExpr = $this->consumeBladeExpression())) {
$attrString .= $bladeExpr;

continue;
}

$prevChar = $this->position > 0 ? $this->content[$this->position - 1] : '';

if ($char === '"' && !$inSingleQuote && $prevChar !== '\\') {
Expand Down Expand Up @@ -405,47 +396,6 @@ protected function collectAttributes(): void
}
}

/**
* If the current position is at a Blade echo expression ({{ or {!!),
* consume the entire expression and return it. Returns null otherwise.
*/
protected function consumeBladeExpression(): ?string
{
if ($this->matchesAt('{!!')) {
return $this->consumeUntil('{!!', '!!}');
}

if ($this->matchesAt('{{')) {
return $this->consumeUntil('{{', '}}');
}

return null;
}

/**
* Consume content from the opening delimiter through the closing delimiter,
* advancing the position past the entire expression.
*/
protected function consumeUntil(string $open, string $close): string
{
$start = $this->position;

$this->advance(strlen($open));

while (!$this->isAtEnd()) {
if ($this->matchesAt($close)) {
$this->advance(strlen($close));

return substr($this->content, $start, $this->position - $start);
}

$this->advance();
}

// Unclosed expression — return what we have.
return substr($this->content, $start, $this->position - $start);
}

/**
* Try to match a slot opening tag at the current position.
*/
Expand Down
Loading