Skip to content

Commit

Permalink
Fix: Determine previous non-whitespace character by trimming what has…
Browse files Browse the repository at this point in the history
… been printed so far
  • Loading branch information
localheinz committed Jan 5, 2018
1 parent 2268811 commit a1b697c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ public function print(string $original, bool $unEscapeUnicode, bool $unEscapeSla
*/
if ('}' === $character || ']' === $character) {
--$indentLevel;
$previousCharacter = \substr($original, $i - 1, 1);

if ('{' !== $previousCharacter && '[' !== $previousCharacter) {
$trimmed = \rtrim($printed);
$previousNonWhitespaceCharacter = \substr($trimmed, -1);

if ('{' !== $previousNonWhitespaceCharacter && '[' !== $previousNonWhitespaceCharacter) {
$printed .= PHP_EOL;

for ($j = 0; $j < $indentLevel; ++$j) {
Expand All @@ -143,7 +145,7 @@ public function print(string $original, bool $unEscapeUnicode, bool $unEscapeSla
/**
* Collapse empty {} and [].
*/
$printed = \rtrim($printed);
$printed = $trimmed;
}
}

Expand Down

0 comments on commit a1b697c

Please sign in to comment.