Skip to content

Commit c823018

Browse files
committed
tests: void element with xhtml and html5
refactor: make it void element to valid schema HTML
1 parent 596fa54 commit c823018

File tree

6 files changed

+147
-26
lines changed

6 files changed

+147
-26
lines changed

system/Common.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ function csrf_hash(): string
281281
*/
282282
function csrf_field(?string $id = null): string
283283
{
284-
return '<input type="hidden"' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '" />';
284+
return '<input type="hidden"' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
285285
}
286286
}
287287

@@ -291,7 +291,7 @@ function csrf_field(?string $id = null): string
291291
*/
292292
function csrf_meta(?string $id = null): string
293293
{
294-
return '<meta' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '" />';
294+
return '<meta' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '"' . _solidus() . '>';
295295
}
296296
}
297297

@@ -852,6 +852,22 @@ function redirect(?string $route = null): RedirectResponse
852852
}
853853
}
854854

855+
if (! function_exists('_solidus')) {
856+
/**
857+
* Generates the solidus character (`/`) depending on the HTML5 compatibility flag in `Config\DocTypes`
858+
*
859+
* @internal
860+
*/
861+
function _solidus(): string
862+
{
863+
if (config('DocTypes')->html5 ?? false) {
864+
return '';
865+
}
866+
867+
return ' /';
868+
}
869+
}
870+
855871
if (! function_exists('remove_invisible_characters')) {
856872
/**
857873
* Remove Invisible Characters

system/Helpers/form_helper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function form_input($data = '', string $value = '', $extra = '', string $type =
148148
'value' => $value,
149149
];
150150

151-
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . " />\n";
151+
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . _solidus() . ">\n";
152152
}
153153
}
154154

@@ -194,7 +194,7 @@ function form_upload($data = '', string $value = '', $extra = ''): string
194194

195195
$data['type'] = 'file';
196196

197-
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . " />\n";
197+
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . _solidus() . ">\n";
198198
}
199199
}
200200

@@ -365,7 +365,7 @@ function form_checkbox($data = '', string $value = '', bool $checked = false, $e
365365
$defaults['checked'] = 'checked';
366366
}
367367

368-
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . " />\n";
368+
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . _solidus() . ">\n";
369369
}
370370
}
371371

system/Helpers/html_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string
128128
unset($attributes['alt'], $attributes['src']);
129129
}
130130

131-
return $img . stringify_attributes($attributes) . ' />';
131+
return $img . stringify_attributes($attributes) . _solidus() . '>';
132132
}
133133
}
134134

tests/system/CommonFunctionsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ public function testResponse()
163163
$this->assertInstanceOf(Response::class, $response);
164164
}
165165

166+
public function testSolidusElement()
167+
{
168+
$this->assertSame('', _solidus());
169+
}
170+
171+
public function testSolidusElementXHTML()
172+
{
173+
$doctypes = config('DocTypes');
174+
$doctypes->html5 = false;
175+
176+
$this->assertSame(' /', _solidus());
177+
}
178+
166179
public function testView()
167180
{
168181
$data = [

tests/system/Helpers/FormHelperTest.php

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testFormOpenBasic()
5656
$Name = csrf_token();
5757
$expected = <<<EOH
5858
<form action="http://example.com/index.php/foo/bar" name="form" id="form" method="POST" accept-charset="utf-8">
59-
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;" />
59+
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;">
6060
6161
EOH;
6262
} else {
@@ -101,7 +101,7 @@ public function testFormOpenWithoutAction()
101101
$Name = csrf_token();
102102
$expected = <<<EOH
103103
<form action="http://example.com/index.php" name="form" id="form" method="POST" accept-charset="utf-8">
104-
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;" />
104+
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;">
105105
106106
EOH;
107107
} else {
@@ -128,7 +128,7 @@ public function testFormOpenWithoutMethod()
128128
$Name = csrf_token();
129129
$expected = <<<EOH
130130
<form action="http://example.com/index.php/foo/bar" name="form" id="form" method="post" accept-charset="utf-8">
131-
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;" />
131+
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;">
132132
133133
EOH;
134134
} else {
@@ -155,15 +155,15 @@ public function testFormOpenWithHidden()
155155
$Name = csrf_token();
156156
$expected = <<<EOH
157157
<form action="http://example.com/index.php/foo/bar" name="form" id="form" method="POST" accept-charset="utf-8">
158-
<input type="hidden" name="foo" value="bar" />
159-
<input type="hidden" name="{$Name}" value="{$Value}" />
158+
<input type="hidden" name="foo" value="bar">
159+
<input type="hidden" name="{$Name}" value="{$Value}">
160160
161161
EOH;
162162
} else {
163163
$expected = <<<'EOH'
164164
<form action="http://example.com/index.php/foo/bar" name="form" id="form" method="POST" accept-charset="utf-8">
165165
166-
<input type="hidden" name="foo" value="bar" />
166+
<input type="hidden" name="foo" value="bar">
167167

168168
EOH;
169169
}
@@ -189,7 +189,7 @@ public function testFormOpenMultipart()
189189
$Name = csrf_token();
190190
$expected = <<<EOH
191191
<form action="http://example.com/index.php/foo/bar" name="form" id="form" method="POST" enctype="multipart/form-data" accept-charset="utf-8">
192-
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;" />
192+
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;">
193193
194194
EOH;
195195
} else {
@@ -214,7 +214,7 @@ public function testFormHidden()
214214
{
215215
$expected = <<<EOH
216216
217-
<input type="hidden" name="username" value="johndoe" />\n
217+
<input type="hidden" name="username" value="johndoe">\n
218218
EOH;
219219
$this->assertSame($expected, form_hidden('username', 'johndoe'));
220220
}
@@ -226,7 +226,7 @@ public function testFormHiddenArrayInput()
226226
];
227227
$expected = <<<'EOH'
228228
229-
<input type="hidden" name="foo" value="bar" />
229+
<input type="hidden" name="foo" value="bar">
230230

231231
EOH;
232232
$this->assertSame($expected, form_hidden($data, null));
@@ -239,14 +239,34 @@ public function testFormHiddenArrayValues()
239239
];
240240
$expected = <<<'EOH'
241241
242-
<input type="hidden" name="name[foo]" value="bar" />
242+
<input type="hidden" name="name[foo]" value="bar">
243243

244244
EOH;
245245
$this->assertSame($expected, form_hidden('name', $data));
246246
}
247247

248248
public function testFormInput()
249249
{
250+
$expected = <<<EOH
251+
<input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%">\n
252+
EOH;
253+
$data = [
254+
'name' => 'username',
255+
'id' => 'username',
256+
'value' => 'johndoe',
257+
'maxlength' => '100',
258+
'size' => '50',
259+
'style' => 'width:50%',
260+
];
261+
$this->assertSame($expected, form_input($data));
262+
}
263+
264+
public function testFormInputXHTML()
265+
{
266+
$doctypes = config('DocTypes');
267+
$default = $doctypes->html5;
268+
$doctypes->html5 = false;
269+
250270
$expected = <<<EOH
251271
<input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%" />\n
252272
EOH;
@@ -259,12 +279,15 @@ public function testFormInput()
259279
'style' => 'width:50%',
260280
];
261281
$this->assertSame($expected, form_input($data));
282+
283+
// Reset
284+
$doctypes->html5 = $default;
262285
}
263286

264287
public function testFormInputWithExtra()
265288
{
266289
$expected = <<<EOH
267-
<input type="email" name="identity" value="" id="identity" class="form-control form-control-lg" />\n
290+
<input type="email" name="identity" value="" id="identity" class="form-control form-control-lg">\n
268291
EOH;
269292
$data = [
270293
'id' => 'identity',
@@ -280,17 +303,32 @@ public function testFormInputWithExtra()
280303
public function testFormPassword()
281304
{
282305
$expected = <<<EOH
283-
<input type="password" name="password" value="" />\n
306+
<input type="password" name="password" value="">\n
284307
EOH;
285308
$this->assertSame($expected, form_password('password'));
286309
}
287310

288311
public function testFormUpload()
289312
{
313+
$expected = <<<EOH
314+
<input type="file" name="attachment">\n
315+
EOH;
316+
$this->assertSame($expected, form_upload('attachment'));
317+
}
318+
319+
public function testFormUploadXHTML()
320+
{
321+
$doctypes = config('DocTypes');
322+
$default = $doctypes->html5;
323+
$doctypes->html5 = false;
324+
290325
$expected = <<<EOH
291326
<input type="file" name="attachment" />\n
292327
EOH;
293328
$this->assertSame($expected, form_upload('attachment'));
329+
330+
// Reset
331+
$doctypes->html5 = $default;
294332
}
295333

296334
public function testFormTextarea()
@@ -611,10 +649,25 @@ public function testFormFieldsetClose()
611649

612650
public function testFormCheckbox()
613651
{
652+
$expected = <<<EOH
653+
<input type="checkbox" name="newsletter" value="accept" checked="checked">\n
654+
EOH;
655+
$this->assertSame($expected, form_checkbox('newsletter', 'accept', true));
656+
}
657+
658+
public function testFormCheckboxXHTML()
659+
{
660+
$doctypes = config('DocTypes');
661+
$default = $doctypes->html5;
662+
$doctypes->html5 = false;
663+
614664
$expected = <<<EOH
615665
<input type="checkbox" name="newsletter" value="accept" checked="checked" />\n
616666
EOH;
617667
$this->assertSame($expected, form_checkbox('newsletter', 'accept', true));
668+
669+
// Reset
670+
$doctypes->html5 = $default;
618671
}
619672

620673
public function testFormCheckboxArrayData()
@@ -625,7 +678,7 @@ public function testFormCheckboxArrayData()
625678
'checked' => true,
626679
];
627680
$expected = <<<'EOH'
628-
<input type="checkbox" name="foo" value="bar" checked="checked" />
681+
<input type="checkbox" name="foo" value="bar" checked="checked">
629682

630683
EOH;
631684
$this->assertSame($expected, form_checkbox($data));
@@ -639,7 +692,7 @@ public function testFormCheckboxArrayDataWithCheckedFalse()
639692
'checked' => false,
640693
];
641694
$expected = <<<'EOH'
642-
<input type="checkbox" name="foo" value="bar" />
695+
<input type="checkbox" name="foo" value="bar">
643696

644697
EOH;
645698
$this->assertSame($expected, form_checkbox($data));
@@ -648,15 +701,15 @@ public function testFormCheckboxArrayDataWithCheckedFalse()
648701
public function testFormRadio()
649702
{
650703
$expected = <<<EOH
651-
<input type="radio" name="newsletter" value="accept" checked="checked" />\n
704+
<input type="radio" name="newsletter" value="accept" checked="checked">\n
652705
EOH;
653706
$this->assertSame($expected, form_radio('newsletter', 'accept', true));
654707
}
655708

656709
public function testFormSubmit()
657710
{
658711
$expected = <<<EOH
659-
<input type="submit" name="mysubmit" value="Submit Post!" />\n
712+
<input type="submit" name="mysubmit" value="Submit Post!">\n
660713
EOH;
661714
$this->assertSame($expected, form_submit('mysubmit', 'Submit Post!'));
662715
}
@@ -683,7 +736,7 @@ public function testFormLabelWithAttributes()
683736
public function testFormReset()
684737
{
685738
$expected = <<<EOH
686-
<input type="reset" name="myreset" value="Reset" />\n
739+
<input type="reset" name="myreset" value="Reset">\n
687740
EOH;
688741
$this->assertSame($expected, form_reset('myreset', 'Reset'));
689742
}
@@ -724,7 +777,7 @@ public function testFormDatalist()
724777
'bar1',
725778
];
726779
$expected = <<<'EOH'
727-
<input type="text" name="foo" value="bar" list="foo_list" />
780+
<input type="text" name="foo" value="bar" list="foo_list">
728781
729782
<datalist id='foo_list'><option value='foo1'>
730783
<option value='bar1'>

0 commit comments

Comments
 (0)