Skip to content

Commit

Permalink
latte: fixed parsing of {input} & {label} arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 27, 2022
1 parent 0a68eae commit 9399a8d
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Bridges/FormsLatte/Nodes/InputNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static function create(Tag $tag): static

$node = new static;
$node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false);
if ($tag->parser->stream->tryConsume(':') && !$tag->parser->stream->is(',')) {
$node->part = $tag->parser->isEnd()
if ($tag->parser->stream->tryConsume(':')) {
$node->part = $tag->parser->isEnd() || $tag->parser->stream->is(',')
? new StringNode('')
: $tag->parser->parseUnquotedStringOrExpression();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/FormsLatte/Nodes/LabelNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static function create(Tag $tag): \Generator

$node = new static;
$node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false);
if ($tag->parser->stream->tryConsume(':') && !$tag->parser->stream->is(',')) {
$node->part = $tag->parser->isEnd()
if ($tag->parser->stream->tryConsume(':')) {
$node->part = $tag->parser->isEnd() || $tag->parser->stream->is(',')
? new StringNode('')
: $tag->parser->parseUnquotedStringOrExpression();
}
Expand Down
45 changes: 45 additions & 0 deletions tests/Forms.Latte3/n-name.input.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/** @phpVersion 8.0 */

declare(strict_types=1);

use Nette\Bridges\FormsLatte\FormsExtension;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addExtension(new FormsExtension);

Assert::match(
'%A%echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global))->getControlPart()->attributes() %A%',
$latte->compile('<input n:name="foo">'),
);

Assert::match(
'%A%echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global))->getControlPart(\'\')->attributes() %A%',
$latte->compile('<input n:name="foo:">'),
);

Assert::exception(
fn() => $latte->compile('<input n:name="foo: class => foo">'),
Latte\CompileException::class,
"Unexpected '=>foo', expecting end of attribute in n:name (on line 1 at column 27)",
);

Assert::match(
'%A%echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global))->getControlPart(\'x\')->attributes() %A%',
$latte->compile('<input n:name="foo:x">'),
);

Assert::match(
'%A%echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global))->getControlPart(\'x\')->attributes() %A%',
$latte->compile('<input n:name=\'"foo":"x"\'>'),
);
75 changes: 75 additions & 0 deletions tests/Forms.Latte3/{input}.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/** @phpVersion 8.0 */

declare(strict_types=1);

use Nette\Bridges\FormsLatte\FormsExtension;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addExtension(new FormsExtension);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControl() %A%',
$latte->compile('{input foo}'),
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControl()->addAttributes([\'class\' => \'foo\']) %A%',
$latte->compile('{input foo class => foo}'),
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControl()->addAttributes([\'class\' => \'foo\']) %A%',
$latte->compile('{input foo, class => foo}'),
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'\') %A%',
$latte->compile('{input foo:}'),
);

Assert::exception(
fn() => $latte->compile('{input foo: class => foo}'),
Latte\CompileException::class,
"Unexpected '=>foo', expecting end of tag in {input} (on line 1 at column 19)",
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'\') %A%',
$latte->compile('{input foo:,}'),
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'\')->addAttributes([\'class\' => \'foo\']) %A%',
$latte->compile('{input foo:, class => foo}'),
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'x\') %A%',
$latte->compile('{input foo:x}'),
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'x\')->addAttributes([\'class\' => \'foo\']) %A%',
$latte->compile('{input foo:x, class => foo}'),
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'x\') %A%',
$latte->compile('{input "foo":"x"}'),
);

Assert::match(
'%A%echo Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'x\') %A%',
$latte->compile('{input "foo" : "x"}'),
);
75 changes: 75 additions & 0 deletions tests/Forms.Latte3/{label}.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/** @phpVersion 8.0 */

declare(strict_types=1);

use Nette\Bridges\FormsLatte\FormsExtension;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addExtension(new FormsExtension);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabel()) %A%',
$latte->compile('{label foo /}'),
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabel())?->addAttributes([\'class\' => \'foo\']) %A%',
$latte->compile('{label foo class => foo /}'),
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabel())?->addAttributes([\'class\' => \'foo\']) %A%',
$latte->compile('{label foo, class => foo /}'),
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabelPart(\'\')) %A%',
$latte->compile('{label foo: /}'),
);

Assert::exception(
fn() => $latte->compile('{label foo: class => foo /}'),
Latte\CompileException::class,
"Unexpected '=>foo', expecting end of tag in {label} (on line 1 at column 19)",
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabelPart(\'\')) %A%',
$latte->compile('{label foo:, /}'),
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabelPart(\'\'))?->addAttributes([\'class\' => \'foo\']) %A%',
$latte->compile('{label foo:, class => foo /}'),
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabelPart(\'x\')) %A%',
$latte->compile('{label foo:x /}'),
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabelPart(\'x\'))?->addAttributes([\'class\' => \'foo\']) %A%',
$latte->compile('{label foo:x, class => foo /}'),
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabelPart(\'x\')) %A%',
$latte->compile('{label "foo":"x" /}'),
);

Assert::match(
'%A%echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getLabelPart(\'x\')) %A%',
$latte->compile('{label "foo" : "x" /}'),
);

0 comments on commit 9399a8d

Please sign in to comment.