Skip to content

Commit 7fa3b9c

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: [Console] Fix `Helper::removeDecoration` hyperlink bug Guard scripts from being run in non-CLI contexts Guard scripts from being run in non-CLI contexts a readonly property must not be reported as being writable
2 parents 17524a6 + 1f89cab commit 7fa3b9c

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
@@ -145,6 +145,8 @@ public static function removeDecoration(OutputFormatterInterface $formatter, ?st
145145
$string = $formatter->format($string ?? '');
146146
// remove already formatted characters
147147
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
148+
// remove terminal hyperlinks
149+
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? '');
148150
$formatter->setDecorated($isDecorated);
149151

150152
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
@@ -1969,4 +1969,29 @@ public function testVerticalRender(string $expectedOutput, array $headers, array
19691969

19701970
$this->assertEquals($expectedOutput, $this->getOutputContent($output));
19711971
}
1972+
1973+
public function testWithHyperlinkAndMaxWidth()
1974+
{
1975+
$table = new Table($output = $this->getOutputStream(true));
1976+
$table
1977+
->setRows([
1978+
['<href=Lorem>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</>'],
1979+
])
1980+
;
1981+
$table->setColumnMaxWidth(0, 20);
1982+
$table->render();
1983+
1984+
$expected =
1985+
<<<TABLE
1986+
+----------------------+
1987+
| \033]8;;Lorem\033\\Lorem ipsum dolor si\033]8;;\033\\ |
1988+
| \033]8;;Lorem\033\\t amet, consectetur \033]8;;\033\\ |
1989+
| \033]8;;Lorem\033\\adipiscing elit, sed\033]8;;\033\\ |
1990+
| \033]8;;Lorem\033\\do eiusmod tempor\033]8;;\033\\ |
1991+
+----------------------+
1992+
1993+
TABLE;
1994+
1995+
$this->assertSame($expected, $this->getOutputContent($output));
1996+
}
19721997
}

0 commit comments

Comments
 (0)