Skip to content

Commit 41c6d0b

Browse files
committed
Combined: fix HTML output
Signed-off-by: Jack Cherng <[email protected]>
1 parent ced0c2b commit 41c6d0b

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/Renderer/Html/Combined.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ protected function mergeReplaceLines(string $oldLine, string $newLine): string
275275
);
276276
}
277277

278-
return \str_replace(["\r\n", "\r", "\n"], '<br>', $mergedLine);
278+
return $this->fixReplaceLineHtml($mergedLine);
279279
}
280280

281281
/**
@@ -391,4 +391,36 @@ protected function fixLinesForNoClosure(array &$lines, array $closures): void
391391
}
392392
}
393393
}
394+
395+
/**
396+
* Perform some special fixes for the "replace"-type output HTML.
397+
*
398+
* @param string $string the string
399+
*/
400+
protected function fixReplaceLineHtml(string $string): string
401+
{
402+
static $rules;
403+
404+
if (!isset($rules)) {
405+
$delL = \preg_quote(RendererConstant::HTML_CLOSURES_DEL[0], '/');
406+
$delR = \preg_quote(RendererConstant::HTML_CLOSURES_DEL[1], '/');
407+
$insL = \preg_quote(RendererConstant::HTML_CLOSURES_INS[0], '/');
408+
$insR = \preg_quote(RendererConstant::HTML_CLOSURES_INS[1], '/');
409+
410+
$rules = [
411+
// move leading "\n" to be out of closures
412+
"/{$delL}([\r\n]++)/uS" => '$1' . RendererConstant::HTML_CLOSURES_DEL[0],
413+
"/{$insL}([\r\n]++)/uS" => '$1' . RendererConstant::HTML_CLOSURES_INS[0],
414+
// move trailing "\n" to be out of closures
415+
"/([\r\n]++){$delR}/uS" => RendererConstant::HTML_CLOSURES_DEL[1] . '$1',
416+
"/([\r\n]++){$insR}/uS" => RendererConstant::HTML_CLOSURES_INS[1] . '$1',
417+
];
418+
}
419+
420+
foreach ($rules as $pattern => $replace) {
421+
$string = \preg_replace($pattern, $replace, $string);
422+
}
423+
424+
return \str_replace(["\r\n", "\r", "\n"], '<br>', $string);
425+
}
394426
}

0 commit comments

Comments
 (0)