Skip to content

Commit

Permalink
[Console] Fix Helper::removeDecoration hyperlink bug
Browse files Browse the repository at this point in the history
  • Loading branch information
greew authored and fabpot committed Oct 4, 2022
1 parent 28b7797 commit 4f40012
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public static function removeDecoration(OutputFormatterInterface $formatter, $st
$string = $formatter->format($string);
// remove already formatted characters
$string = preg_replace("/\033\[[^m]*m/", '', $string);
// remove terminal hyperlinks
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string);
$formatter->setDecorated($isDecorated);

return $string;
Expand Down
19 changes: 19 additions & 0 deletions Tests/Helper/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\Helper;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Helper;

class HelperTest extends TestCase
Expand Down Expand Up @@ -42,6 +43,16 @@ public function formatTimeProvider()
];
}

public function decoratedTextProvider()
{
return [
['abc', 'abc'],
['abc<fg=default;bg=default>', 'abc'],
["a\033[1;36mbc", 'abc'],
["a\033]8;;http://url\033\\b\033]8;;\033\\c", 'abc'],
];
}

/**
* @dataProvider formatTimeProvider
*
Expand All @@ -52,4 +63,12 @@ public function testFormatTime($secs, $expectedFormat)
{
$this->assertEquals($expectedFormat, Helper::formatTime($secs));
}

/**
* @dataProvider decoratedTextProvider
*/
public function testRemoveDecoration(string $decoratedText, string $undecoratedText)
{
$this->assertEquals($undecoratedText, Helper::removeDecoration(new OutputFormatter(), $decoratedText));
}
}
25 changes: 25 additions & 0 deletions Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,31 @@ public function testWithColspanAndMaxWith()
| | | nsectetur |
+-----------------+-----------------+-----------------+
TABLE;

$this->assertSame($expected, $this->getOutputContent($output));
}

public function testWithHyperlinkAndMaxWidth()
{
$table = new Table($output = $this->getOutputStream(true));
$table
->setRows([
['<href=Lorem>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</>'],
])
;
$table->setColumnMaxWidth(0, 20);
$table->render();

$expected =
<<<TABLE
+----------------------+
| \033]8;;Lorem\033\\Lorem ipsum dolor si\033]8;;\033\\ |
| \033]8;;Lorem\033\\t amet, consectetur \033]8;;\033\\ |
| \033]8;;Lorem\033\\adipiscing elit, sed\033]8;;\033\\ |
| \033]8;;Lorem\033\\do eiusmod tempor\033]8;;\033\\ |
+----------------------+
TABLE;

$this->assertSame($expected, $this->getOutputContent($output));
Expand Down

0 comments on commit 4f40012

Please sign in to comment.