Skip to content

Commit 984ea2c

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: [Console] Fix `Helper::removeDecoration` hyperlink bug Guard scripts from being run in non-CLI contexts a readonly property must not be reported as being writable
2 parents 3f97f6c + 4f40012 commit 984ea2c

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Helper/Helper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ public static function removeDecoration(OutputFormatterInterface $formatter, ?st
171171
$string = $formatter->format($string ?? '');
172172
// remove already formatted characters
173173
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
174+
// remove terminal hyperlinks
175+
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? '');
174176
$formatter->setDecorated($isDecorated);
175177

176178
return $string;

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
}

Tests/Helper/TableTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,31 @@ public function testWithColspanAndMaxWith()
15951595
| | | nsectetur |
15961596
+-----------------+-----------------+-----------------+
15971597
1598+
TABLE;
1599+
1600+
$this->assertSame($expected, $this->getOutputContent($output));
1601+
}
1602+
1603+
public function testWithHyperlinkAndMaxWidth()
1604+
{
1605+
$table = new Table($output = $this->getOutputStream(true));
1606+
$table
1607+
->setRows([
1608+
['<href=Lorem>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</>'],
1609+
])
1610+
;
1611+
$table->setColumnMaxWidth(0, 20);
1612+
$table->render();
1613+
1614+
$expected =
1615+
<<<TABLE
1616+
+----------------------+
1617+
| \033]8;;Lorem\033\\Lorem ipsum dolor si\033]8;;\033\\ |
1618+
| \033]8;;Lorem\033\\t amet, consectetur \033]8;;\033\\ |
1619+
| \033]8;;Lorem\033\\adipiscing elit, sed\033]8;;\033\\ |
1620+
| \033]8;;Lorem\033\\do eiusmod tempor\033]8;;\033\\ |
1621+
+----------------------+
1622+
15981623
TABLE;
15991624

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

0 commit comments

Comments
 (0)