Skip to content

Commit

Permalink
Latte: fixed rendering of empty <label> for CheckboxList/RadioList items
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 6, 2023
1 parent 888aba2 commit c1794e6
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 54 deletions.
10 changes: 5 additions & 5 deletions src/Bridges/FormsLatte/Nodes/FieldNNameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private function init(Tag $tag)
$elName = strtolower($el->name);

$tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global))'
. ($elName === 'label' ? '->getLabelPart(%node)' : '->getControlPart(%node)')
'echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)'
. ($elName === 'label' ? '->getLabelPart(%node))' : '->getControlPart(%node))')
. ($usedAttributes ? '->addAttributes(%dump)' : '')
. '->attributes() %3.line;',
$this->name,
Expand All @@ -77,20 +77,20 @@ private function init(Tag $tag)
if ($elName === 'label') {
if ($el->content instanceof NopNode) {
$el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo $ʟ_input->getLabelPart()->getHtml() %line;',
'echo $ʟ_elem->getHtml() %line;',
$this->position,
));
}
} elseif ($elName === 'button') {
if ($el->content instanceof NopNode) {
$el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo %escape($ʟ_input->getCaption()) %line;',
'echo %escape($ʟ_elem->value) %line;',
$this->position,
));
}
} elseif ($el->content) { // select, textarea
$el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo $ʟ_input->getControl()->getHtml() %line;',
'echo $ʟ_elem->getHtml() %line;',
$this->position,
));
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Forms.Latte3/expected/forms.button.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo '>
<button';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '>
description of button
</button>
<button';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '></button>
<button';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '>';
echo LR\Filters::escapeHtmlText($ʟ_input->getCaption()) /* line %d% */;
echo LR\Filters::escapeHtmlText($ʟ_elem->value) /* line %d% */;
echo '</button>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
Expand Down
4 changes: 4 additions & 0 deletions tests/Forms.Latte3/expected/forms.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@
<form action="" method="post" class="form-class">
<label for="frm-sex-m"> <input type="radio" name="sex" id="frm-sex-m" value="m"> male</label>
<label for="frm-sex-m" title=hello> <input type="radio" name="sex" id="frm-sex-m" value="m"> </label>
<label for="frm-sex-m" title=hello>male</label>
<label for="frm-sex-f"> <input type="radio" name="sex" id="frm-sex-f" value="f"> female</label>
<label for="frm-sex-f" title=hello> <input type="radio" name="sex" id="frm-sex-f" value="f"> </label>
<label for="frm-sex-f" title=hello>female</label>
<label></label>
<label>Sex:</label>
<label title="hello">Sex:</label>
Expand All @@ -77,8 +79,10 @@

<label for="frm-checklist-m"> <input type="checkbox" name="checklist[]" id="frm-checklist-m" value="m"> male</label>
<label for="frm-checklist-m"> <input type="checkbox" name="checklist[]" id="frm-checklist-m" value="m" title=hello> </label>
<label for="frm-checklist-m">male</label>
<label for="frm-checklist-f"> <input type="checkbox" name="checklist[]" id="frm-checklist-f" value="f"> female</label>
<label for="frm-checklist-f"> <input type="checkbox" name="checklist[]" id="frm-checklist-f" value="f" title=hello> </label>
<label for="frm-checklist-f">female</label>
<label></label>
<label>CheckboxList:</label>
<label title="hello">CheckboxList:</label>
Expand Down
92 changes: 51 additions & 41 deletions tests/Forms.Latte3/expected/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@
echo '
<LABEL';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global))->getLabelPart()->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global)->getLabelPart())->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title=hello>Name</LABEL>
<input value=val type class="hello"';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global))->getControlPart()->addAttributes(['value' => null, 'type' => null, 'class' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global)->getControlPart())->addAttributes(['value' => null, 'type' => null, 'class' => null])->attributes() /* line %d% */;
echo '>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item($form['username'], $this->global))->getLabelPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item($form['username'], $this->global)->getLabelPart())->attributes() /* line %d% */;
echo '></label>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item($form['username'], $this->global))->getLabelPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item($form['username'], $this->global)->getLabelPart())->attributes() /* line %d% */;
echo '>';
echo $ʟ_input->getLabelPart()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
<input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item($form['username'], $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item($form['username'], $this->global)->getControlPart())->attributes() /* line %d% */;
echo '>
';
Expand Down Expand Up @@ -86,29 +86,34 @@
echo $ʟ_label?->endTag() /* line %d% */;
echo '
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global))->getLabelPart($key)->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global)->getLabelPart($key))->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title=hello> <input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global))->getControlPart($key)->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global)->getControlPart($key))->attributes() /* line %d% */;
echo '> </label>
<label';
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global)->getLabelPart($key))->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title=hello>';
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
';

}

echo '<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global))->getLabelPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global)->getLabelPart())->attributes() /* line %d% */;
echo '></label>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global))->getLabelPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global)->getLabelPart())->attributes() /* line %d% */;
echo '>';
echo $ʟ_input->getLabelPart()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global))->getLabelPart()->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global)->getLabelPart())->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title="hello">';
echo $ʟ_input->getLabelPart()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
<input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item($form['sex'], $this->global))->getControlPart("{$key}")->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item($form['sex'], $this->global)->getControlPart("{$key}"))->attributes() /* line %d% */;
echo '>
Expand All @@ -120,24 +125,24 @@
echo $ʟ_label?->endTag() /* line %d% */;
echo '
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global))->getLabelPart('')->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global)->getLabelPart(''))->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title=hello> <input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global))->getControlPart('')->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global)->getControlPart(''))->attributes() /* line %d% */;
echo '> </label>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global))->getLabelPart()->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global)->getLabelPart())->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title=hello> <input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '> </label>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global))->getLabelPart('')->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global)->getLabelPart(''))->attributes() /* line %d% */;
echo '>';
echo $ʟ_input->getLabelPart()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global))->getLabelPart()->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checkbox', $this->global)->getLabelPart())->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title=hello>';
echo $ʟ_input->getLabelPart()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
Expand All @@ -152,26 +157,31 @@
echo $ʟ_label?->endTag() /* line %d% */;
echo '
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global))->getLabelPart($key)->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global)->getLabelPart($key))->attributes() /* line %d% */;
echo '> <input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global))->getControlPart($key)->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global)->getControlPart($key))->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title=hello> </label>
<label';
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global)->getLabelPart($key))->attributes() /* line %d% */;
echo '>';
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
';

}

echo '<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global))->getLabelPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global)->getLabelPart())->attributes() /* line %d% */;
echo '></label>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global))->getLabelPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global)->getLabelPart())->attributes() /* line %d% */;
echo '>';
echo $ʟ_input->getLabelPart()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global))->getLabelPart()->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('checklist', $this->global)->getLabelPart())->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title="hello">';
echo $ʟ_input->getLabelPart()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
Expand All @@ -183,7 +193,7 @@
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['id' => null, 'class' => null], false) /* line %d% */;
echo ' id="myForm" class="ajax">
<input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
Expand All @@ -201,7 +211,7 @@
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>
<input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global))->getControlPart()->addAttributes(['class' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global)->getControlPart())->addAttributes(['class' => null])->attributes() /* line %d% */;
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>
';
Expand All @@ -218,7 +228,7 @@
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo '>
<input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
Expand All @@ -228,25 +238,25 @@
echo '
<select';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('select', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('select', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '>';
echo $ʟ_input->getControl()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</select>
<textarea';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('area', $this->global))->getControlPart()->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('area', $this->global)->getControlPart())->addAttributes(['title' => null])->attributes() /* line %d% */;
echo ' title="';
echo LR\Filters::escapeHtmlAttr(10) /* line %d% */;
echo '">';
echo $ʟ_input->getControl()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</textarea>
<select';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('select', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('select', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '>';
echo $ʟ_input->getControl()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</select>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack)) /* line %d% */;
Expand All @@ -259,12 +269,12 @@
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global))->getLabelPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global)->getLabelPart())->attributes() /* line %d% */;
echo '>';
echo $ʟ_input->getLabelPart()->getHtml() /* line %d% */;
echo $ʟ_elem->getHtml() /* line %d% */;
echo '</label>
<input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global)->getControlPart())->attributes() /* line %d% */;
echo '>
';
array_pop($this->global->formsStack) /* line %d% */;
Expand Down
8 changes: 4 additions & 4 deletions tests/Forms.Latte3/n-name.input.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ $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%',
'%A%echo ($ʟ_elem = 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%',
'%A%echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'\'))->attributes() %A%',
$latte->compile('<input n:name="foo:">'),
);

Expand All @@ -35,11 +35,11 @@ Assert::exception(
);

Assert::match(
'%A%echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global))->getControlPart(\'x\')->attributes() %A%',
'%A%echo ($ʟ_elem = 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%',
'%A%echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item(\'foo\', $this->global)->getControlPart(\'x\'))->attributes() %A%',
$latte->compile('<input n:name=\'"foo":"x"\'>'),
);
2 changes: 2 additions & 0 deletions tests/Forms.Latte3/templates/forms.latte
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
{foreach $form[sex]->items as $key => $label}
{label sex:$key} {input sex:$key} {$label}{/label}
<label n:name="sex:$key" title=hello> <input n:name="sex:$key"> </label>
<label n:name="sex:$key" title=hello />
{/foreach}
<label n:name="sex"></label>
<label n:name="sex" />
Expand All @@ -49,6 +50,7 @@
{foreach $form[checklist]->items as $key => $label}
{label checklist:$key} {input checklist:$key} {$label}{/label}
<label n:name="checklist:$key"> <input n:name="checklist:$key" title=hello> </label>
<label n:name="checklist:$key" />
{/foreach}
<label n:name="checklist"></label>
<label n:name="checklist" />
Expand Down

0 comments on commit c1794e6

Please sign in to comment.