8000 [Console] Fix `Helper::removeDecoration` hyperlink bug · enumag/symfony@9e1349c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e1349c

Browse files
greewfabpot
authored andcommitted
[Console] Fix Helper::removeDecoration hyperlink bug
1 parent 25c0a20 commit 9e1349c

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/Symfony/Component/Console/Helper/Helper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public static function removeDecoration(OutputFormatterInterface $formatter, $st
135135
$string = $formatter->format($string);
136136
// remove already formatted characters
137137
$string = preg_replace("/\033\[[^m]*m/", '', $string);
138+
// remove terminal hyperlinks
139+
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string);
138140
$formatter->setDecorated($isDecorated);
139141

140142
return $string;

src/Symfony/Component/Console/Tests/Helper/HelperTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Tests\Helper;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Formatter\OutputFormatter;
1516
use Symfony\Component\Console\Helper\Helper;
1617

1718
class HelperTest extends TestCase
@@ -42,6 +43,16 @@ public function formatTimeProvider()
4243
];
4344
}
4445

46+
public function decoratedTextProvider()
47+
{
48+
return [
49+
['abc', 'abc'],
50+
['abc<fg=default;bg=default>', 'abc'],
51+
["a\033[1;36mbc", 'abc'],
52+
["a\033]8;;http://url\033\\b\033]8;;\033\\c", 'abc'],
53+
];
54+
}
55+
4556
/**
4657
* @dataProvider formatTimeProvider
4758
*
@@ -52,4 +63,12 @@ public function testFormatTime($secs, $expectedFormat)
5263
{
5364
$this->assertEquals($expectedFormat, Helper::formatTime($secs));
5465
}
66+
67+
/**
68+
* @dataProvider decoratedTextProvider
69+
*/
70+
public function testRemoveDecoration(string $decoratedText, string $undecoratedText)
71+
{
72+
$this->assertEquals($undecoratedText, Helper::removeDecoration(new OutputFormatter(), $decoratedText));
73+
}
5574
}

src/Symfony/Component/Console/Tests/Helper/TableTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,31 @@ public function testWithColspanAndMaxWith()
13881388
| | | nsectetur |
13891389
+-----------------+-----------------+-----------------+
13901390
1391+
TABLE;
1392+
1393+
$this->assertSame($expected, $this->getOutputContent($output));
1394+
}
1395+
1396+
public function testWithHyperlinkAndMaxWidth()
1397+
{
1398+
$table = new Table($output = $this->getOutputStream(true));
1399+
$table
1400+
->setRows([
1401+
['<href=Lorem>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</>'],
1402+
])
1403+
;
1404+
$table->setColumnMaxWidth(0, 20);
1405+
$table->render();
1406+
1407+
$expected =
1408+
<<<TABLE
1409+
+----------------------+
1410+
| \033]8;;Lorem\033\\Lorem ipsum dolor si\033]8;;\033\\ |
1411+
| \033]8;;Lorem\033\\t amet, consectetur \033]8;;\033\\ |
1412+
| \033]8;;Lorem\033\\adipiscing elit, sed\033]8;;\033\\ |
1413+
| \033]8;;Lorem\033\\do eiusmod tempor\033]8;;\033\\ |
1414+
+----------------------+
1415+
13911416
TABLE;
13921417

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

0 commit comments

Comments
 (0)
0