Skip to content

Commit f907a00

Browse files
committed
Align highlight_string|file with HTML standard and modern browsers
Closes GH-11913
1 parent 4a77a1e commit f907a00

28 files changed

+143
-125
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ PHP 8.3 UPGRADE NOTES
221221
only paths starting with `..` were disallowed. This could easily be
222222
circumvented by prepending `./` to the path.
223223
. User exception handlers now catch exceptions during shutdown.
224+
. The resultant HTML of highlight_string and highlight_file has changed.
225+
Whitespace between outer HTML tags is removed. Newlines and spaces
226+
are no longer converted to HTML entities. The whole HTML is now wrapped in
227+
<pre> tag. The outer <span> has been merged with <code>.
224228

225229
- Calendar
226230
. easter_date() now supports years from 1970 to 2,000,000,000 on 64-bit systems,

Zend/tests/bug35655.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ EOT
1919
highlight_string($code);
2020
?>
2121
--EXPECT--
22-
<code><span style="color: #000000">
23-
<br /><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOT<br /></span><span style="color: #DD0000">some&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #007700">EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
24-
</span>
25-
</code>
22+
<pre><code style="color: #000000">
23+
<span style="color: #0000BB">&lt;?php
24+
$x </span><span style="color: #007700">= &lt;&lt;&lt;EOT
25+
</span><span style="color: #DD0000">some string
26+
</span><span style="color: #007700">EOT
27+
</span><span style="color: #0000BB">$y </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;
28+
</span><span style="color: #0000BB">?&gt;</span></code></pre>

Zend/tests/bug42767.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@ highlight.html = #000000
1111
highlight_string('<?php /*some comment..');
1212
?>
1313
--EXPECT--
14-
<code><span style="color: #000000">
15-
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #FF8000">/*some&nbsp;comment..</span>
16-
</span>
17-
</code>
14+
<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #FF8000">/*some comment..</span></code></pre>

Zend/tests/bug71086.phpt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ var_dump($highlightedString);
88

99
?>
1010
--EXPECT--
11-
string(169) "<code><span style="color: #000000">
12-
<span style="color: #0000BB">&lt;?php&nbsp;<br />&nbsp;09&nbsp;09&nbsp;09</span><span style="color: #007700">;</span>
13-
</span>
14-
</code>"
11+
string(139) "<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
12+
09 09 09</span><span style="color: #007700">;</span></code></pre>"

Zend/tests/nowdoc_013.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ EOF;
2020
highlight_string($code);
2121
?>
2222
--EXPECT--
23-
<code><span style="color: #000000">
24-
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOT'<br /></span><span style="color: #DD0000">some&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #007700">EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
25-
</span>
26-
</code>
23+
<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
24+
$x </span><span style="color: #007700">= &lt;&lt;&lt;'EOT'
25+
</span><span style="color: #DD0000">some string
26+
</span><span style="color: #007700">EOT
27+
</span><span style="color: #0000BB">$y </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;
28+
</span><span style="color: #0000BB">?&gt;</span></code></pre>

Zend/tests/nowdoc_014.phpt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ EOF;
1818
highlight_string($code);
1919
?>
2020
--EXPECT--
21-
<code><span style="color: #000000">
22-
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOT'<br />EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
23-
</span>
24-
</code>
21+
<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
22+
$x </span><span style="color: #007700">= &lt;&lt;&lt;'EOT'
23+
EOT
24+
</span><span style="color: #0000BB">$y </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;
25+
</span><span style="color: #0000BB">?&gt;</span></code></pre>

Zend/zend_highlight.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
ZEND_API void zend_html_putc(char c)
2929
{
3030
switch (c) {
31-
case '\n':
32-
ZEND_PUTS("<br />");
33-
break;
3431
case '<':
3532
ZEND_PUTS("&lt;");
3633
break;
@@ -40,11 +37,8 @@ ZEND_API void zend_html_putc(char c)
4037
case '&':
4138
ZEND_PUTS("&amp;");
4239
break;
43-
case ' ':
44-
ZEND_PUTS("&nbsp;");
45-
break;
4640
case '\t':
47-
ZEND_PUTS("&nbsp;&nbsp;&nbsp;&nbsp;");
41+
ZEND_PUTS(" ");
4842
break;
4943
default:
5044
ZEND_PUTC(c);
@@ -88,8 +82,7 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
8882
char *last_color = syntax_highlighter_ini->highlight_html;
8983
char *next_color;
9084

91-
zend_printf("<code>");
92-
zend_printf("<span style=\"color: %s\">\n", last_color);
85+
zend_printf("<pre><code style=\"color: %s\">", last_color);
9386
/* highlight stuff coming back from zendlex() */
9487
while ((token_type=lex_scan(&token, NULL))) {
9588
switch (token_type) {
@@ -162,10 +155,9 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
162155
}
163156

164157
if (last_color != syntax_highlighter_ini->highlight_html) {
165-
zend_printf("</span>\n");
158+
zend_printf("</span>");
166159
}
167-
zend_printf("</span>\n");
168-
zend_printf("</code>");
160+
zend_printf("</code></pre>");
169161

170162
/* Discard parse errors thrown during tokenization */
171163
zend_clear_exception();

ext/phar/tests/cache_list/frontcontroller15.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,4 @@ files/frontcontroller8.phar
1414
--EXPECTHEADERS--
1515
Content-type: text/html; charset=UTF-8
1616
--EXPECT--
17-
<code><span style="color: #000000">
18-
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">hio</span><span style="color: #007700">(){}</span>
19-
</span>
20-
</code>
17+
<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">function </span><span style="color: #0000BB">hio</span><span style="color: #007700">(){}</span></code></pre>

ext/phar/tests/cache_list/frontcontroller3.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,4 @@ files/frontcontroller.phar
1414
--EXPECTHEADERS--
1515
Content-type: text/html; charset=UTF-8
1616
--EXPECT--
17-
<code><span style="color: #000000">
18-
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">hio</span><span style="color: #007700">(){}</span>
19-
</span>
20-
</code>
17+
<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">function </span><span style="color: #0000BB">hio</span><span style="color: #007700">(){}</span></code></pre>

ext/phar/tests/cache_list/frontcontroller9.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,4 @@ files/frontcontroller3.phar
1414
--EXPECTHEADERS--
1515
Content-type: text/html; charset=UTF-8
1616
--EXPECT--
17-
<code><span style="color: #000000">
18-
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">hio</span><span style="color: #007700">(){}</span>
19-
</span>
20-
</code>
17+
<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">function </span><span style="color: #0000BB">hio</span><span style="color: #007700">(){}</span></code></pre>

0 commit comments

Comments
 (0)