Skip to content

Commit

Permalink
keep track of original attr name
Browse files Browse the repository at this point in the history
  • Loading branch information
withinboredom committed Nov 18, 2023
1 parent b4d3c68 commit ede308b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
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 @@ private function resetState(): void
$this->selfClosing = false;
$this->attributeValue = '';
$this->attributeName = '';
$this->originalAttributeName = '';
$this->nameBuffer = '';
$this->isClosing = false;
}
Expand Down Expand Up @@ -545,7 +546,8 @@ private function renderTagName(Document $document): Document
private function renderBeforeAttributeName(Document $document): Document
{
$oops = function (string $c, Closure $next) use ($document) {
$this->attributeName = $c;
$this->attributeName = strtolower($c);
$this->originalAttributeName = $c;
$this->attributeValue = '';
return $next($document);
};
Expand All @@ -560,6 +562,7 @@ private function renderBeforeAttributeName(Document $document): Document
return $result;
}
$this->attributeName = '';
$this->originalAttributeName = '';
$this->attributeValue = '';
return $document->reconsume($this->renderAttributeName(...));
}
Expand Down Expand Up @@ -1130,6 +1133,8 @@ private function renderBogusDocType(Document $document): Document
return $this->renderBogusDocType($document);
}

private string $originalAttributeName = '';

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

Expand All @@ -1153,7 +1159,7 @@ private function renderBeforeAttributeValue(Document $document): Document
'"' => $this->renderAttributeValueDoubleQuoted($document),
"'" => $this->renderAttributeValueSingleQuoted($document),
'>' => (function () use ($document) {
$this->setAttribute($this->attributeName);
$this->setAttribute($this->originalAttributeName);
return $document;
})(),
default => $this->renderAttributeValueUnquoted($document),
Expand Down Expand Up @@ -1207,7 +1213,7 @@ private function processAttributes(Document $document): Document
$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 renderAttributeValueUnquoted(Document $document): Document

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 @@ private function renderAfterAttributeName(Document $document): Document
}
$this->attributeName = '';
$this->attributeValue = '';
$this->originalAttributeName = '';
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

0 comments on commit ede308b

Please sign in to comment.