Skip to content

Commit b1eb09e

Browse files
authored
Merge pull request #6899 from kenjis/fix-xhtml
fix: HTML output
2 parents 977c97b + cc7d1c1 commit b1eb09e

File tree

10 files changed

+49
-33
lines changed

10 files changed

+49
-33
lines changed

app/Views/welcome_message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title>Welcome to CodeIgniter 4!</title>
66
<meta name="description" content="The small framework with powerful features">
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8-
<link rel="shortcut icon" type="image/png" href="/favicon.ico"/>
8+
<link rel="shortcut icon" type="image/png" href="/favicon.ico">
99

1010
<!-- STYLES -->
1111

system/Debug/Toolbar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
408408
. 'data-time="' . $time . '" '
409409
. 'src="' . site_url() . '?debugbar"></script>'
410410
. '<script ' . csp_script_nonce() . ' id="debugbar_dynamic_script"></script>'
411-
. '<style type="text/css" ' . csp_style_nonce() . ' id="debugbar_dynamic_style"></style>'
411+
. '<style ' . csp_style_nonce() . ' id="debugbar_dynamic_style"></style>'
412412
. $kintScript
413413
. PHP_EOL;
414414

system/Debug/Toolbar/Views/toolbar.tpl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @var \CodeIgniter\View\Parser $parser
1919
*/
2020
?>
21-
<style type="text/css">
21+
<style>
2222
<?= preg_replace('#[\r\n\t ]+#', ' ', file_get_contents(__DIR__ . '/toolbar.css')) ?>
2323
</style>
2424

@@ -264,7 +264,7 @@
264264
<?= $parser->setData($config)->render('_config.tpl') ?>
265265
</div>
266266
</div>
267-
<style type="text/css">
267+
<style>
268268
<?php foreach ($styles as $name => $style): ?>
269269
<?= sprintf(".%s { %s }\n", $name, $style) ?>
270270
<?php endforeach ?>

system/Email/Email.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ private function printDebuggerRaw(): string
21472147
*/
21482148
protected function setErrorMessage($msg)
21492149
{
2150-
$this->debugMessage[] = $msg . '<br />';
2150+
$this->debugMessage[] = $msg . '<br>';
21512151
$this->debugMessageRaw[] = $msg;
21522152
}
21532153

system/Typography/Typography.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Typography
2424
public $blockElements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
2525

2626
/**
27-
* Elements that should not have <p> and <br /> tags within them.
27+
* Elements that should not have <p> and <br> tags within them.
2828
*
2929
* @var string
3030
*/
@@ -63,7 +63,7 @@ class Typography
6363
*
6464
* This function converts text, making it typographically correct:
6565
* - Converts double spaces into paragraphs.
66-
* - Converts single line breaks into <br /> tags
66+
* - Converts single line breaks into <br> tags
6767
* - Converts single and double quotes into correctly facing curly quote entities.
6868
* - Converts three dots into ellipsis.
6969
* - Converts double dashes into em-dashes.
@@ -160,7 +160,7 @@ public function autoTypography(string $str, bool $reduceLinebreaks = false): str
160160
$chunks[$i] .= "\n";
161161
}
162162

163-
// Convert Newlines into <p> and <br /> tags
163+
// Convert Newlines into <p> and <br> tags
164164
$str .= $this->formatNewLines($chunks[$i]);
165165
}
166166

@@ -275,7 +275,7 @@ public function formatCharacters(string $str): string
275275
/**
276276
* Format Newlines
277277
*
278-
* Converts newline characters into either <p> tags or <br />
278+
* Converts newline characters into either <p> tags or <br>
279279
*/
280280
protected function formatNewLines(string $str): string
281281
{
@@ -286,8 +286,9 @@ protected function formatNewLines(string $str): string
286286
// Convert two consecutive newlines to paragraphs
287287
$str = str_replace("\n\n", "</p>\n\n<p>", $str);
288288

289-
// Convert single spaces to <br /> tags
290-
$str = preg_replace("/([^\n])(\n)([^\n])/", '\\1<br />\\2\\3', $str);
289+
// Convert single spaces to <br> tags
290+
$br = '<br' . _solidus() . '>';
291+
$str = preg_replace("/([^\n])(\n)([^\n])/", '\\1' . $br . '\\2\\3', $str);
291292

292293
// Wrap the whole enchilada in enclosing paragraphs
293294
if ($str !== "\n") {
@@ -323,7 +324,8 @@ public function nl2brExceptPre(string $str): string
323324
$newstr = '';
324325

325326
for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++) {
326-
$newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i];
327+
$xhtml = ! (config('DocTypes')->html5 ?? false);
328+
$newstr .= (($i % 2) === 0) ? nl2br($ex[$i], $xhtml) : $ex[$i];
327329

328330
if ($ct - 1 !== $i) {
329331
$newstr .= 'pre>';

system/View/Filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static function local_currency($value, string $currency, ?string $locale
182182

183183
/**
184184
* Returns a string with all instances of newline character (\n)
185-
* converted to an HTML <br/> tag.
185+
* converted to an HTML <br> tag.
186186
*/
187187
public static function nl2br(string $value): string
188188
{

tests/system/Helpers/URLHelper/MiscUrlTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ public function autolinkUrls()
498498
'This is my [email protected] test',
499499
],
500500
'test03' => [
501-
'<br />www.google.com',
502-
'<br /><a href="http://www.google.com">www.google.com</a>',
501+
'<br>www.google.com',
502+
'<br><a href="http://www.google.com">www.google.com</a>',
503503
],
504504
'test04' => [
505505
'Download CodeIgniter at www.codeigniter.com. Period test.',
@@ -547,8 +547,8 @@ public function autolinkEmails()
547547
"This is my <script>var l=new Array();l[0] = '>';l[1] = 'a';l[2] = '/';l[3] = '<';l[4] = '|109';l[5] = '|111';l[6] = '|99';l[7] = '|46';l[8] = '|114';l[9] = '|101';l[10] = '|116';l[11] = '|105';l[12] = '|110';l[13] = '|103';l[14] = '|105';l[15] = '|101';l[16] = '|100';l[17] = '|111';l[18] = '|99';l[19] = '|64';l[20] = '|121';l[21] = '|108';l[22] = '|112';l[23] = '|101';l[24] = '|114';l[25] = '|111';l[26] = '|110';l[27] = '>';l[28] = '\"';l[29] = '|109';l[30] = '|111';l[31] = '|99';l[32] = '|46';l[33] = '|114';l[34] = '|101';l[35] = '|116';l[36] = '|105';l[37] = '|110';l[38] = '|103';l[39] = '|105';l[40] = '|101';l[41] = '|100';l[42] = '|111';l[43] = '|99';l[44] = '|64';l[45] = '|121';l[46] = '|108';l[47] = '|112';l[48] = '|101';l[49] = '|114';l[50] = '|111';l[51] = '|110';l[52] = ':';l[53] = 'o';l[54] = 't';l[55] = 'l';l[56] = 'i';l[57] = 'a';l[58] = 'm';l[59] = '\"';l[60] = '=';l[61] = 'f';l[62] = 'e';l[63] = 'r';l[64] = 'h';l[65] = ' ';l[66] = 'a';l[67] = '<';for (var i = l.length-1; i >= 0; i=i-1) {if (l[i].substring(0, 1) === '|') document.write(\"&#\"+unescape(l[i].substring(1))+\";\");else document.write(unescape(l[i]));}</script> test",
548548
],
549549
'test03' => [
550-
'<br />www.google.com',
551-
'<br />www.google.com',
550+
'<br>www.google.com',
551+
'<br>www.google.com',
552552
],
553553
'test04' => [
554554
'Download CodeIgniter at www.codeigniter.com. Period test.',
@@ -596,8 +596,8 @@ public function autolinkBoth()
596596
"This is my <script>var l=new Array();l[0] = '>';l[1] = 'a';l[2] = '/';l[3] = '<';l[4] = '|109';l[5] = '|111';l[6] = '|99';l[7] = '|46';l[8] = '|114';l[9] = '|101';l[10] = '|116';l[11] = '|105';l[12] = '|110';l[13] = '|103';l[14] = '|105';l[15] = '|101';l[16] = '|100';l[17] = '|111';l[18] = '|99';l[19] = '|64';l[20] = '|121';l[21] = '|108';l[22] = '|112';l[23] = '|101';l[24] = '|114';l[25] = '|111';l[26] = '|110';l[27] = '>';l[28] = '\"';l[29] = '|109';l[30] = '|111';l[31] = '|99';l[32] = '|46';l[33] = '|114';l[34] = '|101';l[35] = '|116';l[36] = '|105';l[37] = '|110';l[38] = '|103';l[39] = '|105';l[40] = '|101';l[41] = '|100';l[42] = '|111';l[43] = '|99';l[44] = '|64';l[45] = '|121';l[46] = '|108';l[47] = '|112';l[48] = '|101';l[49] = '|114';l[50] = '|111';l[51] = '|110';l[52] = ':';l[53] = 'o';l[54] = 't';l[55] = 'l';l[56] = 'i';l[57] = 'a';l[58] = 'm';l[59] = '\"';l[60] = '=';l[61] = 'f';l[62] = 'e';l[63] = 'r';l[64] = 'h';l[65] = ' ';l[66] = 'a';l[67] = '<';for (var i = l.length-1; i >= 0; i=i-1) {if (l[i].substring(0, 1) === '|') document.write(\"&#\"+unescape(l[i].substring(1))+\";\");else document.write(unescape(l[i]));}</script> test",
597597
],
598598
'test03' => [
599-
'<br />www.google.com',
600-
'<br /><a href="http://www.google.com">www.google.com</a>',
599+
'<br>www.google.com',
600+
'<br><a href="http://www.google.com">www.google.com</a>',
601601
],
602602
'test04' => [
603603
'Download CodeIgniter at www.codeigniter.com. Period test.',
@@ -645,8 +645,8 @@ public function autolinkPopup()
645645
'This is my [email protected] test',
646646
],
647647
'test03' => [
648-
'<br />www.google.com',
649-
'<br /><a href="http://www.google.com" target="_blank">www.google.com</a>',
648+
'<br>www.google.com',
649+
'<br><a href="http://www.google.com" target="_blank">www.google.com</a>',
650650
],
651651
'test04' => [
652652
'Download CodeIgniter at www.codeigniter.com. Period test.',

tests/system/Typography/TypographyTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public function testAutoTypographyLineBreaks()
6464
"\n\n" => "\n\n<p>&nbsp;</p>",
6565
"\n\n\n" => "\n\n<p>&nbsp;</p>",
6666
"Line One\n" => "<p>Line One</p>\n\n",
67-
"Line One\nLine Two" => "<p>Line One<br />\nLine Two</p>",
67+
"Line One\nLine Two" => "<p>Line One<br>\nLine Two</p>",
6868
"Line One\r\n" => "<p>Line One</p>\n\n",
69-
"Line One\r\nLine Two" => "<p>Line One<br />\nLine Two</p>",
69+
"Line One\r\nLine Two" => "<p>Line One<br>\nLine Two</p>",
7070
"Line One\r" => "<p>Line One</p>\n\n",
71-
"Line One\rLine Two" => "<p>Line One<br />\nLine Two</p>",
71+
"Line One\rLine Two" => "<p>Line One<br>\nLine Two</p>",
7272
"Line One\n\nLine Two\n\n\nLine Three" => "<p>Line One</p>\n\n<p>Line Two</p>\n\n<p>Line Three</p>",
7373
];
7474

@@ -84,12 +84,12 @@ public function testAutoTypographyReduceLineBreaks()
8484
"\n\n" => "\n\n",
8585
"\n\n\n" => "\n\n\n\n",
8686
"Line One\n" => "<p>Line One</p>\n\n",
87-
"Line One\nLine Two" => "<p>Line One<br />\nLine Two</p>",
87+
"Line One\nLine Two" => "<p>Line One<br>\nLine Two</p>",
8888
"Line One\r\n" => "<p>Line One</p>\n\n",
89-
"Line One\r\nLine Two" => "<p>Line One<br />\nLine Two</p>",
89+
"Line One\r\nLine Two" => "<p>Line One<br>\nLine Two</p>",
9090
"Line One\r" => "<p>Line One</p>\n\n",
91-
"Line One\rLine Two" => "<p>Line One<br />\nLine Two</p>",
92-
"Line One\n\nLine Two\n\n\nLine Three" => "<p>Line One</p>\n\n<p>Line Two</p>\n\n<p><br />\nLine Three</p>",
91+
"Line One\rLine Two" => "<p>Line One<br>\nLine Two</p>",
92+
"Line One\n\nLine Two\n\n\nLine Three" => "<p>Line One</p>\n\n<p>Line Two</p>\n\n<p><br>\nLine Three</p>",
9393
];
9494

9595
foreach ($strs as $str => $expect) {
@@ -141,9 +141,9 @@ public function testAutoTypographySpecialCharacters()
141141
public function testNewlinesToHTMLLineBreaksExceptWithinPRE()
142142
{
143143
$strs = [
144-
"Line One\nLine Two" => "Line One<br />\nLine Two",
144+
"Line One\nLine Two" => "Line One<br>\nLine Two",
145145
"<pre>Line One\nLine Two</pre>" => "<pre>Line One\nLine Two</pre>",
146-
"<div>Line One\nLine Two</div>" => "<div>Line One<br />\nLine Two</div>",
146+
"<div>Line One\nLine Two</div>" => "<div>Line One<br>\nLine Two</div>",
147147
];
148148

149149
foreach ($strs as $str => $expect) {

tests/system/View/ParserFilterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function testNL2BR()
250250
$template = '{ value1|nl2br }';
251251

252252
$parser->setData($data);
253-
$this->assertSame("first<br />\nsecond", $parser->renderString($template));
253+
$this->assertSame("first<br>\nsecond", $parser->renderString($template));
254254
}
255255

256256
public function testNumberFormat()

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ The following methods of the :doc:`Time <../libraries/time>` class had bugs that
7474
Others
7575
------
7676

77-
- **Helper:** Now ``form_helper``, ``html_helper`` and common functions that render HTML elements have been changed to make void HTML elements (e.g. ``<input>``) compatible with HTML5 by default.
7877
- **Helper:** :php:func:`script_tag()` and :php:func:`safe_mailto()` no longer output ``type="text/javascript"`` in ``<script>`` tag.
7978
- **CLI:** The ``spark`` file has been changed due to a change in the processing of Spark commands.
8079
- **CLI:** ``CITestStreamFilter::$buffer = ''`` no longer causes the filter to be registered to listen for streams. Now there
@@ -248,14 +247,29 @@ Libraries
248247
Helpers and Functions
249248
=====================
250249

251-
- Creation of void HTML elements like ``<input>`` via ``form_helper``, ``html_helper`` or common functions can be configured to exclude or not the solidus character (``/``) before the right angle bracket (``>``) by setting the ``$html5`` property in **app/Config/DocTypes.php** to ``true``.
252250
- Now you can autoload helpers by **app/Config/Autoload.php**.
253251
- Added new Form helper function :php:func:`validation_errors()`, :php:func:`validation_list_errors()` and :php:func:`validation_show_error()` to display Validation Errors.
254252
- You can set the locale to :php:func:`route_to()` if you pass a locale value as the last parameter.
255253
- Added :php:func:`request()` and :php:func:`response()` functions.
256254
- Added :php:func:`decamelize()` function to convert camelCase to snake_case.
257255
- Added :php:func:`is_windows()` global function to detect Windows platforms.
258256

257+
HTML5 Compatibility
258+
===================
259+
260+
Creation of void HTML elements like ``<input>`` can be configured to exclude or not the solidus character
261+
(``/``) before the right angle bracket (``>``) by setting the ``$html5`` property in
262+
**app/Config/DocTypes.php**. If you set it to ``true``, HTML5 compatible tags without ``/`` like ``<br>``
263+
will be output.
264+
265+
The following items are affected:
266+
267+
- Typography class: Creation of ``br`` tag
268+
- View Parser: The ``nl2br`` filter
269+
- Form helper
270+
- HTML helper
271+
- Common Functions
272+
259273
Error Handling
260274
==============
261275

0 commit comments

Comments
 (0)