Skip to content

Commit

Permalink
FormMacros: added ; after some statement, {form} and {input} print li…
Browse files Browse the repository at this point in the history
…ne numbers
  • Loading branch information
dg committed May 18, 2016
1 parent d306e45 commit fd381fb
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 73 deletions.
45 changes: 24 additions & 21 deletions src/Bridges/FormsLatte/FormMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Nette;
use Latte;
use Latte\Helpers;
use Latte\MacroNode;
use Latte\PhpWriter;
use Latte\CompileException;
Expand Down Expand Up @@ -49,21 +50,22 @@ public static function install(Latte\Compiler $compiler)
public function macroForm(MacroNode $node, PhpWriter $writer)
{
if ($node->modifiers) {
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
}
if ($node->prefix) {
throw new CompileException('Did you mean <form n:name=...> ?');
}
$name = $node->tokenizer->fetchWord();
if ($name === FALSE) {
throw new CompileException("Missing form name in {{$node->name}}.");
throw new CompileException('Missing form name in ' . Helpers::formatNotation($node));
}
$node->replaced = true;
$node->tokenizer->reset();
return $writer->write(
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = '
"/* line $node->startLine */\n"
. 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = '
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
. '$this->global->uiControl[%node.word], %node.array)'
. '$this->global->uiControl[%node.word], %node.array);'
);
}

Expand All @@ -74,17 +76,17 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
public function macroFormContainer(MacroNode $node, PhpWriter $writer)
{
if ($node->modifiers) {
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
}
$name = $node->tokenizer->fetchWord();
if ($name === FALSE) {
throw new CompileException("Missing name in {{$node->name}}.");
throw new CompileException('Missing name in ' . Helpers::formatNotation($node));
}
$node->tokenizer->reset();
return $writer->write(
'$this->global->formsStack[] = $formContainer = $_form = '
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
. 'end($this->global->formsStack)[%node.word]'
. 'end($this->global->formsStack)[%node.word];'
);
}

Expand All @@ -95,11 +97,11 @@ public function macroFormContainer(MacroNode $node, PhpWriter $writer)
public function macroLabel(MacroNode $node, PhpWriter $writer)
{
if ($node->modifiers) {
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
}
$words = $node->tokenizer->fetchWords();
if (!$words) {
throw new CompileException("Missing name in {{$node->name}}.");
throw new CompileException('Missing name in ' . Helpers::formatNotation($node));
}
$node->replaced = true;
$name = array_shift($words);
Expand Down Expand Up @@ -131,18 +133,19 @@ public function macroLabelEnd(MacroNode $node, PhpWriter $writer)
public function macroInput(MacroNode $node, PhpWriter $writer)
{
if ($node->modifiers) {
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
}
$words = $node->tokenizer->fetchWords();
if (!$words) {
throw new CompileException("Missing name in {{$node->name}}.");
throw new CompileException('Missing name in ' . Helpers::formatNotation($node));
}
$node->replaced = true;
$name = array_shift($words);
return $writer->write(
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo $_input' : 'echo end($this->global->formsStack)[%0.word]')
. '->%1.raw'
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : ''),
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : '')
. " /* line $node->startLine */",
$name,
$words ? 'getControlPart(' . implode(', ', array_map([$writer, 'formatWord'], $words)) . ')' : 'getControl()'
);
Expand All @@ -156,11 +159,11 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
{
$words = $node->tokenizer->fetchWords();
if (!$words) {
throw new CompileException("Missing name in n:{$node->name}.");
throw new CompileException('Missing name in ' . Helpers::formatNotation($node));
}
$name = array_shift($words);
$tagName = strtolower($node->htmlNode->name);
$node->isEmpty = $tagName === 'input';
$node->empty = $tagName === 'input';

if ($tagName === 'form') {
return $writer->write(
Expand Down Expand Up @@ -198,13 +201,13 @@ public function macroNameEnd(MacroNode $node, PhpWriter $writer)
{
$tagName = strtolower($node->htmlNode->name);
if ($tagName === 'form') {
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE) ?>';
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE); ?>';
} elseif ($tagName === 'label') {
if ($node->htmlNode->isEmpty) {
if ($node->htmlNode->empty) {
$node->innerContent = "<?php echo \$_input->getLabelPart()->getHtml() ?>";
}
} elseif ($tagName === 'button') {
if ($node->htmlNode->isEmpty) {
if ($node->htmlNode->empty) {
$node->innerContent = '<?php echo htmlspecialchars($_input->caption) ?>';
}
} else { // select, textarea
Expand All @@ -219,16 +222,16 @@ public function macroNameEnd(MacroNode $node, PhpWriter $writer)
public function macroInputError(MacroNode $node, PhpWriter $writer)
{
if ($node->modifiers) {
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
}
$name = $node->tokenizer->fetchWord();
$node->replaced = true;
if (!$name) {
return $writer->write('echo %escape($_input->getError())');
return $writer->write('echo %escape($_input->getError());');
} elseif ($name[0] === '$') {
return $writer->write('$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo %escape($_input->getError())', $name);
return $writer->write('$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo %escape($_input->getError());', $name);
} else {
return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError())', $name);
return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError());', $name);
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/Forms.Latte/FormMacros.error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Assert::exception(function () use ($latte) {

Assert::exception(function () use ($latte) {
$latte->compile('<form n:name></form>');
}, Latte\CompileException::class, 'Missing name in n:name.');
}, Latte\CompileException::class, 'Missing name in n:name');

Assert::exception(function () use ($latte) {
$latte->compile('<form n:inner-name></form>');
Expand All @@ -31,20 +31,20 @@ Assert::exception(function () use ($latte) {

Assert::exception(function () use ($latte) {
$latte->compile('<html>{form /}');
}, Latte\CompileException::class, 'Missing form name in {form}.');
}, Latte\CompileException::class, 'Missing form name in {form}');

Assert::exception(function () use ($latte) {
$latte->compile('<html>{formContainer /}');
}, Latte\CompileException::class, 'Missing name in {formContainer}.');
}, Latte\CompileException::class, 'Missing name in {formContainer}');


Assert::exception(function () use ($latte) {
$latte->compile('<html>{label /}');
}, Latte\CompileException::class, 'Missing name in {label}.');
}, Latte\CompileException::class, 'Missing name in {label}');

Assert::exception(function () use ($latte) {
$latte->compile('<html>{input /}');
}, Latte\CompileException::class, 'Missing name in {input}.');
}, Latte\CompileException::class, 'Missing name in {input}');

Assert::exception(function () use ($latte) {
$latte->compile('<html>{name /}');
Expand Down
4 changes: 3 additions & 1 deletion tests/Forms.Latte/expected/FormMacros.button.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class Template%a% extends Latte\Template
<button<?php
$_input = end($this->global->formsStack)["send"];
echo $_input->getControlPart()->attributes() ?>><?php echo htmlspecialchars($_input->caption) ?></button>
<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE) ?></form>
<?php
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE);
?></form>
<?php
}

Expand Down
39 changes: 21 additions & 18 deletions tests/Forms.Latte/expected/FormMacros.formContainer.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,85 @@ class Template%a% extends Latte\Template
function render()
{
%A%
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) ?>
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []);
?>

<table>
<tr>
<th><?php if ($_label = end($this->global->formsStack)["input1"]->getLabel()) echo $_label ?></th>
<td><?php echo end($this->global->formsStack)["input1"]->getControl() ?></td>
<td><?php echo end($this->global->formsStack)["input1"]->getControl() /* line 5 */ ?></td>
</tr>
<?php $this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["cont1"] ?>
<?php
$this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["cont1"];
?>
<tr>
<th><?php if ($_label = end($this->global->formsStack)["input2"]->getLabel()) echo $_label ?></th>
<td><?php echo end($this->global->formsStack)["input2"]->getControl() ?></td>
<td><?php echo end($this->global->formsStack)["input2"]->getControl() /* line 10 */ ?></td>
</tr>
<tr>
<th><?php if ($_label = end($this->global->formsStack)["input3"]->getLabel()) echo $_label ?></th>
<td><?php echo end($this->global->formsStack)["input3"]->getControl() ?></td>
<td><?php echo end($this->global->formsStack)["input3"]->getControl() /* line 14 */ ?></td>
</tr>
<tr>
<th>Checkboxes</th>
<td>
<?php $this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["cont2"] ?> <ol>
<?php
$this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["cont2"];
?> <ol>
<?php
$iterations = 0;
if (isset($this->params['name'])) trigger_error('Variable $name overwritten in foreach.');
if (isset($this->params['field'])) trigger_error('Variable $field overwritten in foreach.');
foreach ($formContainer->controls AS $name => $field) {
?> <li><?php
$_input = is_object($field) ? $field : end($this->global->formsStack)[$field];
echo $_input->getControl() ?></li>
echo $_input->getControl() /* line 20 */ ?></li>
<?php
$iterations++;
}
?> </ol>
?> </ol>
<?php
array_pop($this->global->formsStack);
$formContainer = $_form = end($this->global->formsStack) ?>
</td>
</tr>
<tr>
<th><?php if ($_label = end($this->global->formsStack)["input7"]->getLabel()) echo $_label ?></th>
<td><?php echo end($this->global->formsStack)["input7"]->getControl() ?></td>
<td><?php echo end($this->global->formsStack)["input7"]->getControl() /* line 26 */ ?></td>
</tr>
<?php
array_pop($this->global->formsStack);
$formContainer = $_form = end($this->global->formsStack);
$this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["items"] ?>
$this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["items"];
?>
<tr>
<th>Items</th>
<td>
<?php
$items = array(1, 2, 3);
$iterations = 0;
if (isset($this->params['item'])) trigger_error('Variable $item overwritten in foreach.');
foreach ($items as $item) {
if (!isset($formContainer[$item])) continue;
$this->global->formsStack[] = $formContainer = $_form = is_object($item) ? $item : end($this->global->formsStack)[$item] ?> <?php
echo end($this->global->formsStack)["input"]->getControl() ?>
$this->global->formsStack[] = $formContainer = $_form = is_object($item) ? $item : end($this->global->formsStack)[$item];
?> <?php echo end($this->global->formsStack)["input"]->getControl() /* line 37 */ ?>

<?php
array_pop($this->global->formsStack);
$formContainer = $_form = end($this->global->formsStack);
$iterations++;
}
?>
?>
</td>
</tr>
<?php
array_pop($this->global->formsStack);
$formContainer = $_form = end($this->global->formsStack) ?>
<tr>
<th><?php if ($_label = end($this->global->formsStack)["input8"]->getLabel()) echo $_label ?></th>
<td><?php echo end($this->global->formsStack)["input8"]->getControl() ?></td>
<td><?php echo end($this->global->formsStack)["input8"]->getControl() /* line 45 */ ?></td>
</tr>
</table>
<?php
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
?>
?>

<?php
}
Expand Down
Loading

0 comments on commit fd381fb

Please sign in to comment.