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

keep track of original attr name #52

Merged
merged 1 commit into from
Nov 18, 2023
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
17 changes: 12 additions & 5 deletions src/Template/Parser/StreamingCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
$this->selfClosing = false;
$this->attributeValue = '';
$this->attributeName = '';
$this->originalAttributeName = '';
$this->nameBuffer = '';
$this->isClosing = false;
}
Expand Down Expand Up @@ -545,7 +546,8 @@
private function renderBeforeAttributeName(Document $document): Document
{
$oops = function (string $c, Closure $next) use ($document) {
$this->attributeName = $c;
$this->attributeName = strtolower($c);
$this->originalAttributeName = $c;

Check warning on line 550 in src/Template/Parser/StreamingCompiler.php

View check run for this annotation

Codecov / codecov/patch

src/Template/Parser/StreamingCompiler.php#L549-L550

Added lines #L549 - L550 were not covered by tests
$this->attributeValue = '';
return $next($document);
};
Expand All @@ -560,6 +562,7 @@
return $result;
}
$this->attributeName = '';
$this->originalAttributeName = '';
$this->attributeValue = '';
return $document->reconsume($this->renderAttributeName(...));
}
Expand Down Expand Up @@ -1130,6 +1133,8 @@
return $this->renderBogusDocType($document);
}

private string $originalAttributeName = '';

private function renderAttributeName(Document $document): Document
{
//$selectionStart = $document->mark();
Expand All @@ -1143,6 +1148,7 @@
return $result;
}
$this->attributeName .= strtolower($char);
$this->originalAttributeName .= $char;
goto renderAttributeName;
}

Expand All @@ -1153,7 +1159,7 @@
'"' => $this->renderAttributeValueDoubleQuoted($document),
"'" => $this->renderAttributeValueSingleQuoted($document),
'>' => (function () use ($document) {
$this->setAttribute($this->attributeName);
$this->setAttribute($this->originalAttributeName);

Check warning on line 1162 in src/Template/Parser/StreamingCompiler.php

View check run for this annotation

Codecov / codecov/patch

src/Template/Parser/StreamingCompiler.php#L1162

Added line #L1162 was not covered by tests
return $document;
})(),
default => $this->renderAttributeValueUnquoted($document),
Expand Down Expand Up @@ -1207,7 +1213,7 @@
$value = $this->escaper->escapeHtmlAttr($originalValue);
// escaper doesn't escape single quotes, so we do that here.
$value = str_replace("'", ''', $value);
$this->setAttribute($this->attributeName, $originalValue);
$this->setAttribute($this->originalAttributeName, $originalValue);
if ($value !== $this->attributeValue) {
// we need to update the rendered html too...
$here = $document->mark();
Expand Down Expand Up @@ -1263,8 +1269,8 @@

private function renderAfterAttributeName(Document $document): Document
{
if (!empty($this->attributeName) && !array_key_exists($this->attributeName, $this->attributes)) {
$this->setAttribute($this->attributeName);
if (!empty($this->attributeName) && !array_key_exists($this->originalAttributeName, $this->attributes)) {
$this->setAttribute($this->originalAttributeName);
}
$result = match ($document->consume()) {
"\t", "\n", "\f", " " => $this->renderAfterAttributeName($document),
Expand All @@ -1278,6 +1284,7 @@
}
$this->attributeName = '';
$this->attributeValue = '';
$this->originalAttributeName = '';

Check warning on line 1287 in src/Template/Parser/StreamingCompiler.php

View check run for this annotation

Codecov / codecov/patch

src/Template/Parser/StreamingCompiler.php#L1287

Added line #L1287 was not covered by tests
return $document->reconsume($this->renderAttributeName(...));
}

Expand Down
14 changes: 7 additions & 7 deletions tests/StreamingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,24 +265,24 @@ public function render()
it('passes variables correctly', function () {
$container = containerWithComponents([
'echo' => new class {
public function render(string $test = ''): string
public function render(string $Test = ''): string
{
return $test;
return $Test;
}
},
'child' => new class {
public function render(string $test = ''): string
public function render(string $Test = ''): string
{
return "<div>{{$test}}<children/></div>";
return "<div>{{$Test}}<children/></div>";
}
}
]);

$streamer = $container->get(StreamingCompiler::class);
$document = <<<HTML
<echo test="{some text}" />
<child test="{outer text}">
<echo test="{inner text}" />
<echo Test="{some text}" />
<child Test="{outer text}">
<echo Test="{inner text}" />
</child>
HTML;
$result = $streamer->compile($document);
Expand Down
Loading