Skip to content

Commit a821017

Browse files
committed
AbstractHtml::expandTabs() add argument: $onlyLeadingTabs
Signed-off-by: Jack Cherng <[email protected]>
1 parent bd00670 commit a821017

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/Renderer/Html/AbstractHtml.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,29 @@ protected function formatStringFromLines(string $string): string
240240
/**
241241
* Replace tabs in a string with a number of spaces.
242242
*
243-
* @param string $string the containing tabs to convert
244-
* @param int $tabSize one tab = how many spaces, a negative does nothing
243+
* @param string $string the containing tabs to convert
244+
* @param int $tabSize one tab = how many spaces, a negative does nothing
245+
* @param bool $onlyLeadingTabs only expand leading tabs
245246
*
246247
* @return string the string with the tabs converted to spaces
247248
*/
248-
protected function expandTabs(string $string, int $tabSize = 4): string
249+
protected function expandTabs(string $string, int $tabSize = 4, bool $onlyLeadingTabs = false): string
249250
{
250-
return $tabSize >= 0
251-
? \str_replace("\t", \str_repeat(' ', $tabSize), $string)
252-
: $string;
251+
if ($tabSize < 0) {
252+
return $string;
253+
}
254+
255+
if ($onlyLeadingTabs) {
256+
return \preg_replace_callback(
257+
"/^[ \t]{1,}/mS", // tabs and spaces may be mixed
258+
function (array $matches): string {
259+
return \str_replace("\t", \str_repeat(' ', $tabSize), $matches[0]);
260+
},
261+
$string
262+
);
263+
}
264+
265+
return \str_replace("\t", \str_repeat(' ', $tabSize), $string);
253266
}
254267

255268
/**

0 commit comments

Comments
 (0)