Skip to content

Commit 3e3e651

Browse files
xificurkdg
authored andcommitted
FormMacros: fix edcompatibility between n:class macro and n:name form macro. (#159)
1 parent edb7b83 commit 3e3e651

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

src/Bridges/FormsLatte/FormMacros.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
165165
$tagName = strtolower($node->htmlNode->name);
166166
$node->empty = $tagName === 'input';
167167

168+
$definedHtmlAttributes = array_keys($node->htmlNode->attrs);
169+
if (isset($node->htmlNode->macroAttrs['class'])) {
170+
$definedHtmlAttributes[] = 'class';
171+
}
172+
168173
if ($tagName === 'form') {
169174
$node->openingCode = $writer->write(
170175
'<?php $form = $_form = $this->global->formsStack[] = '
@@ -174,17 +179,17 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
174179
);
175180
return $writer->write(
176181
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), %0.var, false)',
177-
array_fill_keys(array_keys($node->htmlNode->attrs), null)
182+
array_fill_keys($definedHtmlAttributes, null)
178183
);
179184
} else {
180185
$method = $tagName === 'label' ? 'getLabel' : 'getControl';
181186
return $writer->write(
182187
'$_input = ' . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '')
183188
. 'end($this->global->formsStack)[%0.word]; echo $_input->%1.raw'
184-
. ($node->htmlNode->attrs ? '->addAttributes(%2.var)' : '') . '->attributes()',
189+
. ($definedHtmlAttributes ? '->addAttributes(%2.var)' : '') . '->attributes()',
185190
$name,
186191
$method . 'Part(' . implode(', ', array_map([$writer, 'formatWord'], $words)) . ')',
187-
array_fill_keys(array_keys($node->htmlNode->attrs), null)
192+
array_fill_keys($definedHtmlAttributes, null)
188193
);
189194
}
190195
}

tests/Forms.Latte/FormMacros.forms.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class MyControl extends Nette\Forms\Controls\BaseControl
3030

3131

3232
$form = new Form;
33+
$form->getElementPrototype()->addClass('form-class');
3334
$form->addHidden('id');
3435
$form->addText('username', 'Username:'); // must have just one textfield to generate IE fix
36+
$form['username']->getControlPrototype()->addClass('control-class');
3537
$form->addRadioList('sex', 'Sex:', ['m' => 'male', 'f' => 'female']);
3638
$form->addSelect('select', null, ['m' => 'male', 'f' => 'female']);
3739
$form->addTextArea('area', null)->setValue('one<two');

tests/Forms.Latte/expected/FormMacros.forms.html

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<form action="" method="post" id="myForm" class="ajax">
1+
<form action="" method="post" class="ajax" id="myForm">
22

33
<input type="hidden" name="id" value="" title="Hello" size="10">
44

@@ -9,13 +9,13 @@
99
<input type="hidden" name="id" value="" title="Hello" size="10">
1010

1111
<label for="frm-username">Username:</label>
12-
<input type="text" name="username" size="10" id="frm-username" title="Hello">
12+
<input type="text" name="username" size="10" class="control-class" id="frm-username" title="Hello">
1313
error
1414
error
1515

1616
<br>
1717

18-
<label for="frm-username" title="hello"> <input type="text" name="username" size="10" id="frm-username" title="Hello"> </label>
18+
<label for="frm-username" title="hello"> <input type="text" name="username" size="10" class="control-class" id="frm-username" title="Hello"> </label>
1919
error
2020
<label for="frm-select"></label>
2121
<select name="select" size="10" id="frm-select" title="Hello"><option value="m">male</option><option value="f">female</option></select>
@@ -52,19 +52,19 @@
5252

5353
<label for="frm-username"></label>
5454
<label for="frm-username">Username:</label>
55-
<input type="text" name="username" id="frm-username">
55+
<input type="text" name="username" class="control-class" id="frm-username">
5656

5757
<label>My</label><input name=My>
5858
<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
5959
</form>
6060

6161

6262

63-
<form action="" method="post"><input type="hidden" name="id" value=""><!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
63+
<form action="" method="post" class="form-class"><input type="hidden" name="id" value=""><!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
6464
</form>
6565

6666

67-
<form action="" method="post">
67+
<form action="" method="post" class="form-class">
6868
<label for="frm-sex-m"> <input type="radio" name="sex" id="frm-sex-m" value="m"> male</label>
6969
<label title=hello for="frm-sex-m"> <input type="radio" name="sex" id="frm-sex-m" value="m"> </label>
7070
<label for="frm-sex-f"> <input type="radio" name="sex" id="frm-sex-f" value="f"> female</label>
@@ -91,13 +91,19 @@
9191

9292

9393
<form id="myForm" class="ajax" action="" method="post">
94-
<input type="text" name="username" id="frm-username">
94+
<input type="text" name="username" class="control-class" id="frm-username">
9595
<input type="hidden" name="id" value=""><!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
9696
</form>
9797

9898

99-
<FORM action="" method="post">
100-
<input type="text" name="username" id="frm-username">
99+
<form class="nclass" action="" method="post">
100+
<input type="text" name="username" id="frm-username" class="nclass">
101+
<input type="hidden" name="id" value=""><!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
102+
</form>
103+
104+
105+
<FORM action="" method="post" class="form-class">
106+
<input type="text" name="username" class="control-class" id="frm-username">
101107
<input type="hidden" name="id" value=""><!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
102108
</FORM>
103109

tests/Forms.Latte/expected/FormMacros.forms.phtml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@
202202
?>
203203

204204

205+
<?php
206+
$form = $_form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
207+
?><form<?php
208+
if ($_tmp = array_filter(['nclass'])) echo ' class="', LR\Filters::escapeHtmlAttr(implode(" ", array_unique($_tmp))), '"';
209+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), array (
210+
'class' => NULL,
211+
), false) ?>>
212+
<input<?php
213+
$_input = end($this->global->formsStack)["username"];
214+
echo $_input->getControlPart()->addAttributes(array (
215+
'class' => NULL,
216+
))->attributes();
217+
if ($_tmp = array_filter(['nclass'])) echo ' class="', LR\Filters::escapeHtmlAttr(implode(" ", array_unique($_tmp))), '"' ?>>
218+
<?php
219+
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false);
220+
?></form>
221+
222+
205223
<?php
206224
$form = $_form = $this->global->formsStack[] = is_object($this->global->uiControl['myForm']) ? $this->global->uiControl['myForm'] : $this->global->uiControl[$this->global->uiControl['myForm']];
207225
?><FORM<?php
@@ -220,7 +238,7 @@
220238
echo $_input->getControlPart()->attributes() ?>><?php echo $_input->getControl()->getHtml() ?></select>
221239

222240

223-
<textarea title="<?php echo LR\Filters::escapeHtmlAttr(10) /* line 71 */ ?>"<?php
241+
<textarea title="<?php echo LR\Filters::escapeHtmlAttr(10) /* line 76 */ ?>"<?php
224242
$_input = end($this->global->formsStack)["area"];
225243
echo $_input->getControlPart()->addAttributes(array (
226244
'title' => NULL,

tests/Forms.Latte/templates/forms.latte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
</form>
6161

6262

63+
<form n:name="myForm" n:class="nclass">
64+
<input n:name=username n:class="nclass">
65+
</form>
66+
67+
6368
<FORM n:name="$this->global->uiControl[myForm]">
6469
<input n:name=username>
6570
</FORM>

0 commit comments

Comments
 (0)