Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 809cf74

Browse filesBrowse files
committed
PHP 8.3 highlight_file function output changes
1 parent fcb754a commit 809cf74
Copy full SHA for 809cf74

File tree

2 files changed

+38
-14
lines changed
Filter options

2 files changed

+38
-14
lines changed

‎src/Symfony/Bridge/Twig/Extension/CodeExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/CodeExtension.php
+19-7Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,25 @@ public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?stri
123123
// highlight_file could throw warnings
124124
// see https://bugs.php.net/25725
125125
$code = @highlight_file($file, true);
126-
// remove main code/span tags
127-
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
128-
// split multiline spans
129-
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
130-
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
131-
}, $code);
132-
$content = explode('<br />', $code);
126+
if (\PHP_VERSION_ID >= 80300) {
127+
// remove main pre/code tags
128+
$code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
129+
// split multiline code tags
130+
$code = preg_replace_callback('#<code ([^>]++)>((?:[^<]*+\\n)++[^<]*+)</code>#', function ($m) {
131+
return "<code $m[1]>".str_replace("\n", "</code>\n<code $m[1]>", $m[2]).'</code>';
132+
}, $code);
133+
// Convert spaces to html entities to preserve indentation when rendered
134+
$code = str_replace(' ', '&nbsp;', $code);
135+
$content = explode("\n", $code);
136+
} else {
137+
// remove main code/span tags
138+
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
139+
// split multiline spans
140+
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
141+
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
142+
}, $code);
143+
$content = explode('<br />', $code);
144+
}
133145

134146
$lines = [];
135147
if (0 > $srcContext) {

‎src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php
+19-7Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,25 @@ private function fileExcerpt(string $file, int $line, int $srcContext = 3): stri
269269
// highlight_file could throw warnings
270270
// see https://bugs.php.net/25725
271271
$code = @highlight_file($file, true);
272-
// remove main code/span tags
273-
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
274-
// split multiline spans
275-
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
276-
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
277-
}, $code);
278-
$content = explode('<br />', $code);
272+
if (\PHP_VERSION_ID >= 80300) {
273+
// remove main pre/code tags
274+
$code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
275+
// split multiline code tags
276+
$code = preg_replace_callback('#<code ([^>]++)>((?:[^<]*+\\n)++[^<]*+)</code>#', function ($m) {
277+
return "<code $m[1]>".str_replace("\n", "</code>\n<code $m[1]>", $m[2]).'</code>';
278+
}, $code);
279+
// Convert spaces to html entities to preserve indentation when rendered
280+
$code = str_replace(' ', '&nbsp;', $code);
281+
$content = explode("\n", $code);
282+
} else {
283+
// remove main code/span tags
284+
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
285+
// split multiline spans
286+
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
287+
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
288+
}, $code);
289+
$content = explode('<br />', $code);
290+
}
279291

280292
$lines = [];
281293
if (0 > $srcContext) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.