Skip to content

Commit ad10b00

Browse files
committed
8.3 highlight_file fixes
1 parent 2bac801 commit ad10b00

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,21 @@ public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?stri
117117
// highlight_file could throw warnings
118118
// see https://bugs.php.net/25725
119119
$code = @highlight_file($file, true);
120-
// remove main code/span tags
121-
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
122-
// split multiline spans
123-
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', fn ($m) => "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>', $code);
124-
$content = explode('<br />', $code);
120+
if (\PHP_VERSION_ID >= 80300) {
121+
// remove main pre/code tags
122+
$code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
123+
// split multiline code tags
124+
$code = preg_replace_callback('#<code ([^>]++)>((?:[^<]*+\\n)++[^<]*+)</code>#', fn($m) => "<code $m[1]>".str_replace("\n", "</code>\n<code $m[1]>", $m[2]).'</code>', $code);
125+
// Convert spaces to html entities to preserve indentation when rendered
126+
$code = str_replace(' ','&nbsp;', $code);
127+
$content = explode("\n", $code);
128+
} else {
129+
// remove main code/span tags
130+
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
131+
// split multiline spans
132+
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', fn($m) => "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>', $code);
133+
$content = explode('<br />', $code);
134+
}
125135

126136
$lines = [];
127137
if (0 > $srcContext) {

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,22 @@ private function fileExcerpt(string $file, int $line, int $srcContext = 3): stri
262262
// highlight_file could throw warnings
263263
// see https://bugs.php.net/25725
264264
$code = @highlight_file($file, true);
265-
// remove main code/span tags
266-
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
267-
// split multiline spans
268-
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', fn ($m) => "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>', $code);
269-
$content = explode('<br />', $code);
265+
266+
if (\PHP_VERSION_ID >= 80300) {
267+
// remove main pre/code tags
268+
$code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
269+
// split multiline code tags
270+
$code = preg_replace_callback('#<code ([^>]++)>((?:[^<]*+\\n)++[^<]*+)</code>#', fn($m) => "<code $m[1]>".str_replace("\n", "</code>\n<code $m[1]>", $m[2]).'</code>', $code);
271+
// Convert spaces to html entities to preserve indentation when rendered
272+
$code = str_replace(' ','&nbsp;', $code);
273+
$content = explode("\n", $code);
274+
} else {
275+
// remove main code/span tags
276+
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
277+
// split multiline spans
278+
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', fn($m) => "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>', $code);
279+
$content = explode('<br />', $code);
280+
}
270281

271282
$lines = [];
272283
if (0 > $srcContext) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy